def test_noescape_setting(self): t = SimpleTemplate('<{{var}}>', noescape=True) self.assertRenders(t, '<b>', var='b') self.assertRenders(t, '<<&>>', var='<&>') t = SimpleTemplate('<{{!var}}>', noescape=True) self.assertRenders(t, '<b>', var='b') self.assertRenders(t, '<<&>>', var='<&>')
def assertRenders(self, source, result, syntax=None, *args, **vars): source = self.fix_ident(source) result = self.fix_ident(result) tpl = SimpleTemplate(source, syntax=syntax) try: tpl.co self.assertEqual(touni(result), tpl.render(*args, **vars)) except SyntaxError: self.fail('Syntax error in template:\n%s\n\nTemplate code:\n##########\n%s\n##########' % (traceback.format_exc(), tpl.code))
def test_dedentbug(self): ''' One-Line dednet blocks should not change indention ''' t = '%if x: a="if"\n%else: a="else"\n%end\n{{a}}' self.assertRenders(t, "if", x=True) self.assertRenders(t, "else", x=False) t = '%if x:\n%a="if"\n%else: a="else"\n%end\n{{a}}' self.assertRenders(t, "if", x=True) self.assertRenders(t, "else", x=False) t = SimpleTemplate('%if x: a="if"\n%else: a="else"\n%end') self.assertRaises(NameError, t.render)
def test_data(self): """ Templates: Data representation """ t = SimpleTemplate('<{{var}}>') self.assertRenders('<{{var}}>', '<True>', var=True) self.assertRenders('<{{var}}>', '<False>', var=False) self.assertRenders('<{{var}}>', '<>', var=None) self.assertRenders('<{{var}}>', '<0>', var=0) self.assertRenders('<{{var}}>', '<5>', var=5) self.assertRenders('<{{var}}>', '<b>', var=tob('b')) self.assertRenders('<{{var}}>', '<1.0>', var=1.0) self.assertRenders('<{{var}}>', '<[1, 2]>', var=[1,2])
def test_unicode_code(self): """ Templates: utf8 code in file""" with chdir(__file__): t = SimpleTemplate(name='./views/stpl_unicode.tpl', lookup=['.']) self.assertRenders(t, 'start ñç äöü end\n', var=touni('äöü'))
def test_name(self): with chdir(__file__): t = SimpleTemplate(name='stpl_simple', lookup=['./views/']) self.assertRenders(t, 'start var end\n', var='var')
def test_global_config(self): SimpleTemplate.global_config('meh', 1) t = SimpleTemplate('anything') self.assertEqual(touni('anything'), t.render())
def test_commentonly(self): """ Templates: Commentd should behave like code-lines (e.g. flush text-lines) """ t = SimpleTemplate('...\n%#test\n...') self.assertNotEqual('#test', t.code.splitlines()[0])
def assertRenders(self, tpl, to, *args, **vars): if isinstance(tpl, str): tpl = SimpleTemplate(tpl, lookup=[os.path.join(os.path.dirname(__file__), 'views')]) self.assertEqual(touni(to), tpl.render(*args, **vars))
def test_error(self): """ Templates: Exceptions""" self.assertRaises(SyntaxError, lambda: SimpleTemplate('%for badsyntax').co) self.assertRaises(IndexError, SimpleTemplate('{{i[5]}}', lookup=['.']).render, i=[0])
def test_rebase(self): """ Templates: %rebase and method passing """ with chdir(__file__): t = SimpleTemplate(name='stpl_t2main', lookup=['./views/']) result='+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n' self.assertRenders(t, result, content='1234')
def test_include_to_global(self): """ Templates: Updated globals() with include() result""" with chdir(__file__): t = SimpleTemplate(name='stpl_include_to_global', lookup=['./views/']) self.assertRenders(t, 'before\nstart var end\nstart 3 then var end\nafter\n', var='var')
def test_include(self): """ Templates: Include statements""" with chdir(__file__): t = SimpleTemplate(name='stpl_include', lookup=['./views/']) self.assertRenders(t, 'before\nstart var end\nafter\n', var='var')