1# -*- coding: utf-8 -*- 2import os 3 4import webapp2 5from webapp2_extras import mako 6 7import test_base 8 9current_dir = os.path.abspath(os.path.dirname(__file__)) 10template_path = os.path.join(current_dir, 'resources', 'mako_templates') 11 12 13class TestMako(test_base.BaseTestCase): 14 def test_render_template(self): 15 app = webapp2.WSGIApplication(config={ 16 'webapp2_extras.mako': { 17 'template_path': template_path, 18 }, 19 }) 20 req = webapp2.Request.blank('/') 21 app.set_globals(app=app, request=req) 22 m = mako.Mako(app) 23 24 message = 'Hello, World!' 25 res = m.render_template( 'template1.html', message=message) 26 self.assertEqual(res, message + '\n') 27 28 def test_set_mako(self): 29 app = webapp2.WSGIApplication() 30 self.assertEqual(len(app.registry), 0) 31 mako.set_mako(mako.Mako(app), app=app) 32 self.assertEqual(len(app.registry), 1) 33 j = mako.get_mako(app=app) 34 self.assertTrue(isinstance(j, mako.Mako)) 35 36 def test_get_mako(self): 37 app = webapp2.WSGIApplication() 38 self.assertEqual(len(app.registry), 0) 39 j = mako.get_mako(app=app) 40 self.assertEqual(len(app.registry), 1) 41 self.assertTrue(isinstance(j, mako.Mako)) 42 43 44if __name__ == '__main__': 45 test_base.main() 46