def test_let_directive_scope(self): '''test let directive's scope''' vars = dict(message='hello, from breve', title=my_name(), do_fail=False) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # do fail - try to use the variable out of scope vars['do_fail'] = True t = Template(html, root=template_root()) self.failUnlessRaises(NameError, t.render, 'index', vars, namespace='v')
def test_let_directive_scope ( self ): '''test let directive's scope''' vars = dict ( message = 'hello, from breve', title = my_name ( ), do_fail = False ) # don't fail - use variable in scope t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise # do fail - try to use the variable out of scope vars [ 'do_fail' ] = True t = Template ( html, root = template_root ( ) ) self.failUnlessRaises ( NameError, t.render, 'index', vars, namespace = 'v' )
def test_custom_renderer ( self ): '''custom renderer''' def render_row ( tag, data ): T = html tag.clear ( ) return tag [ [ T.td [ _i ] for _i in data ] ] register_global ( 'render_row', render_row ) vars = dict ( title = my_name ( ), my_data = [ range ( 5 ), range ( 5, 10 ), range ( 10, 15 ) ] ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_macro_includes(self): '''include() directive inside macro''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_simple_template(self): '''simple template''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_loop_include(self): '''looping over include() with listcomp''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_encoding(self): '''encoding comments''' vars = dict(title=my_name()) t = Template(html, root=template_root()) actual = t.render('wrong', vars, namespace='v') expected = expected_output() self.assertNotEqual(actual, expected) actual = t.render('correct', vars, namespace='v') try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_assign_scope(self): '''test assign directive's scope''' vars = dict(message='hello, from breve', title=my_name()) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macro_includes ( self ): '''include() directive inside macro''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_simple_template ( self ): '''simple template''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_register_global(self): '''register_global() function''' vars = dict(title=my_name()) register_global('global_message', 'This is a global variable') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros_inside_inherits(self): '''test macros inside inherits(): scope issues''' # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. vars = dict(title=my_name(), message='Hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_loop_include ( self ): '''looping over include() with listcomp''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_stacks_template(self): '''test stacks in template''' vars = dict(title=my_name(), message='hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # stack should be empty when we finish self.failUnless(not get_stacks())
def test_encoding ( self ): '''encoding comments''' vars = dict ( title = my_name ( ) ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'wrong', vars, namespace = 'v' ) expected = expected_output ( ) self.assertNotEqual ( actual, expected ) actual = t.render ( 'correct', vars, namespace = 'v' ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_custom_renderer_notag(self): '''custom renderer returning non-Tag type''' def render_text(tag, data): tag.clear() return data register_global('render_text', render_text) vars = dict(title=my_name(), message='hello, world') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_register_global ( self ): '''register_global() function''' vars = dict ( title = my_name ( ) ) register_global ( 'global_message', 'This is a global variable' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_assign_scope ( self ): '''test assign directive's scope''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) # don't fail - use variable in scope t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_loop_macros(self): '''loop using macro''' vars = dict(message='hello, from breve', title=my_name(), url_data=[ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_register_flattener(self): '''register_flattener() function''' def flatten_date(o): return escape(o.strftime('%Y/%m/%d')) register_flattener(datetime, flatten_date) register_global('flatten_date', flatten_date) vars = dict(title=my_name(), today=datetime(2008, 4, 17)) test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros_inside_inherits ( self ): '''test macros inside inherits(): scope issues''' # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. vars = dict ( title = my_name ( ), message = 'Hello, from breve' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_stacks_template ( self ): '''test stacks in template''' vars = dict ( title = my_name ( ), message = 'hello, from breve' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise # stack should be empty when we finish self.failUnless ( not get_stacks ( ) )
def test_custom_loader_stack ( self ): '''custom loader stack''' class PathLoader ( object ): __slots__ = [ 'paths' ] def __init__ ( self, *paths ): self.paths = paths def stat ( self, template, root ): for p in self.paths: f = os.path.join ( root, p, template ) if os.path.isfile ( f ): timestamp = long ( os.stat ( f ).st_mtime ) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load ( self, uid ): return file ( uid, 'U' ).read ( ) loader = PathLoader ( template_root ( ), os.path.join ( template_root ( ), 'path1' ), os.path.join ( template_root ( ), 'path2' ), os.path.join ( template_root ( ), 'path3' ), ) register_global ( 'path_loader', loader ) vars = dict ( title = my_name ( ), message = 'hello, world' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_custom_renderer_notag ( self ): '''custom renderer returning non-Tag type''' def render_text ( tag, data ): tag.clear ( ) return data register_global ( 'render_text', render_text ) vars = dict ( title = my_name ( ), message = 'hello, world' ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_custom_renderer(self): '''custom renderer''' def render_row(tag, data): T = html tag.clear() return tag[[T.td[_i] for _i in data]] register_global('render_row', render_row) vars = dict(title=my_name(), my_data=[range(5), range(5, 10), range(10, 15)]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_loop_macros ( self ): '''loop using macro''' vars = dict ( message = 'hello, from breve', title = my_name ( ), url_data = [ dict ( url = 'http://www.google.com', label = 'Google' ), dict ( url = 'http://www.yahoo.com', label = 'Yahoo!' ), dict ( url = 'http://www.amazon.com', label = 'Amazon' ) ] ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_register_flattener ( self ): '''register_flattener() function''' def flatten_date ( o ): return escape ( o.strftime ( '%Y/%m/%d' ) ) register_flattener ( datetime, flatten_date ) register_global ( 'flatten_date', flatten_date ) vars = dict ( title = my_name ( ), today = datetime ( 2008, 4, 17 ) ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_soup2breve(self): ''' round-trip some html ''' breve_source = ''.join( convert_file(os.path.join(test_root(), 'html/index.html'), dict(meta=meta_handler))) code_object = compile(breve_source, 'soup2breve', 'eval') _globals = dict(E=entities) _globals.update(tags) actual = flatten(eval(code_object, _globals)) expected = file(os.path.join(test_root(), 'html/index.html')).read() try: # we can't actually round trip because attributes never come out in # the same order twice =( self.assertEqual(len(actual), len(expected)) except AssertionError: diff(actual, expected) raise
def test_custom_loader_stack(self): '''custom loader stack''' class PathLoader(object): __slots__ = ['paths'] def __init__(self, *paths): self.paths = paths def stat(self, template, root): for p in self.paths: f = os.path.join(root, p, template) if os.path.isfile(f): timestamp = long(os.stat(f).st_mtime) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load(self, uid): return file(uid, 'U').read() loader = PathLoader( template_root(), os.path.join(template_root(), 'path1'), os.path.join(template_root(), 'path2'), os.path.join(template_root(), 'path3'), ) register_global('path_loader', loader) vars = dict(title=my_name(), message='hello, world') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_soup2breve ( self ): ''' round-trip some html ''' breve_source = ''.join ( convert_file ( os.path.join ( test_root ( ), 'html/index.html' ), dict ( meta=meta_handler ) ) ) code_object = compile ( breve_source, 'soup2breve', 'eval' ) _globals = dict ( E = entities ) _globals.update ( tags ) actual = flatten ( eval ( code_object, _globals ) ) expected = file ( os.path.join ( test_root ( ), 'html/index.html' ) ).read ( ) try: # we can't actually round trip because attributes never come out in # the same order twice =( self.assertEqual ( len ( actual ), len ( expected ) ) except AssertionError: diff ( actual, expected ) raise