def test_refresh_on_file_modification(self): engine = RenderEngine(MockApp(self.app_dir)) filename = os.path.join(self.app_dir, 'tmpl.ks') changer = util.MtimeChanger() with changer.change_times(file(filename, 'w')) as fp: fp.write(dedent(""" # this is python ---- <strong>this is HTML</strong> """)) template1 = engine.get_template('tmpl.ks') with changer.change_times(file(filename, 'w')) as fp: fp.write(dedent(""" # this is python ---- <strong>this is HTML</strong> """)) template2 = engine.get_template('tmpl.ks') self.assertTrue(template1 is not template2, 'template should not be the same after file changes')
def test_render_with_template_hierarchy(self): with file(os.path.join(self.app_dir, 'base.html'), 'w') as fp: fp.write(dedent(""" {% block main %} <strong>this is the base</strong> {% endblock %} <strong>this is also the base</strong> """)) with file(os.path.join(self.app_dir, 'tmpl.ks'), 'w') as fp: fp.write(dedent(""" {% extends "base.html" %} {% block main %} <strong>this is the child</strong> {% endblock %} """)) engine = RenderEngine(MockApp(self.app_dir)) t = engine.get_template('tmpl.ks') output = '\n'.join(engine.render(t, {})) self.assertEquals('\n<strong>this is the child</strong>\n\n\n<strong>this is also the base</strong>', output) with file(os.path.join(self.app_dir, 'tmpl2.ks'), 'w') as fp: fp.write(dedent(""" {% extends "_base.html" %} {% block main %} <strong>this is the child</strong> {% endblock %} """)) t = engine.get_template('tmpl2.ks') generator = engine.render(t, {}) self.assertRaises(TemplateNotFound, generator.next) del generator with file(os.path.join(self.app_dir, '_base.html'), 'w') as fp: fp.write(dedent(""" {% block main %} <strong>this is the base</strong> {% endblock %} <strong>this is the new base</strong> """)) t = engine.get_template('tmpl2.ks') output = '\n'.join(engine.render(t, {})) self.assertEquals('\n<strong>this is the child</strong>\n\n\n<strong>this is the new base</strong>', output)
def test_get_template(self): with file(os.path.join(self.app_dir, 'tmpl.ks'), 'w') as fp: fp.write(dedent(""" # this is python ---- <strong>this is HTML</strong> """)) engine = RenderEngine(MockApp(self.app_dir)) template = engine.get_template('tmpl.ks') self.assertTrue(isinstance(template, Template), 'template is not a Template') self.assertEquals(template.name, 'tmpl.ks', 'template name is wrong') self.assertTrue(template is engine.get_template('tmpl.ks'), 'templates are not identical') self.assertRaises(TemplateNotFound, engine.get_template, 'missing.ks')
def test_full_render(self): engine = RenderEngine(MockApp(self.app_dir)) filename = os.path.join(self.app_dir, 'tmpl.ks') with file(filename, 'w') as fp: fp.write(dedent(""" <strong>this is {{name}}</strong> """)) t = engine.get_template('tmpl.ks') output = '\n'.join(engine.render(t, {'name': 'HTML'})) self.assertEquals('<strong>this is HTML</strong>', output)