Exemple #1
0
    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')
Exemple #2
0
    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'
        )
Exemple #3
0
    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_encoding():
    """encoding comments"""
    params = dict(
        title=my_name()
    )
    t = Template(html, root=template_root())
    expected = expected_output()
    actual = t.render('correct', params, namespace='v')
    assert actual == expected
Exemple #5
0
def test_render_decorator():
    """test helpers.render_decorator"""
    @render_decorator('index', root=template_root(), namespace='v')
    def render_test():
        return dict(title='test decorator', message='hello, world')

    actual = render_test()
    expected = expected_output()
    assert actual == expected
def test_simple_template():
    """simple template"""
    params = dict(
        message='hello, from breve',
        title=my_name()
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
def test_loop_include():
    """looping over include() with listcomp"""
    params = dict(
        message='hello, from breve',
        title=my_name()
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #8
0
    def test_render_decorator(self):
        '''test helpers.render_decorator'''
        @render_decorator('index', root=template_root(), namespace='v')
        def render_test():
            return dict(title='test decorator', message='hello, world')

        actual = render_test()
        expected = expected_output()

        self.assertEqual(actual, expected)
def test_nested_include():
    """nested include() directives"""
    params = dict(
        message='hello, from breve',
        title=my_name()
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #10
0
    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
Exemple #11
0
    def test_render_decorator ( self ):
        '''test helpers.render_decorator'''

        @render_decorator ( 'index', root = template_root ( ), namespace = 'v' )
        def render_test ( ):
            return dict ( title='test decorator', message='hello, world' )

        actual = render_test ( )    
        expected = expected_output ( )

        self.assertEqual ( actual, expected )
Exemple #12
0
def test_assign_scope():
    """test assign directive's scope"""
    params = 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', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #13
0
def test_macros_inside_inherits():
    """test macros inside inherits(): scope issues"""
    # note: I'm not convinced this is the desired behaviour, but
    # it's *compatible* behaviour.
    params = dict(
        title=my_name(),
        message='Hello, from breve'
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #14
0
    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
Exemple #15
0
def test_register_global():
    """register_global() function"""
    params = 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', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #16
0
    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
Exemple #17
0
def test_stacks_template():
    """test stacks in template"""
    params = dict(
        title=my_name(),
        message='hello, from breve'
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
    # stack should be empty when we finish
    assert not get_stacks()
Exemple #18
0
    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
Exemple #19
0
 def test_instantiation_parameters(self):
     '''test instantiation parameters'''
     # change the defaults to something else
     args = {
         'tidy': True,
         'debug': True,
         'namespace': 'v',
         'mashup_entities': True,
         'extension': '.breve'
     }
     t = Template(html, root=template_root(), **args)
     for k, v in args.items():
         self.failUnless(getattr(t, k) == v)
Exemple #20
0
 def test_instantiation_parameters ( self ):
     '''test instantiation parameters'''
     # change the defaults to something else
     args = { 
         'tidy': True, 
         'debug': True, 
         'namespace': 'v', 
         'mashup_entities': True, 
         'extension': '.breve'
     }
     t = Template ( html, root = template_root ( ), **args )
     for k, v in args.items ( ):
         self.failUnless ( getattr ( t, k ) == v )
Exemple #21
0
    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
Exemple #22
0
    def test_render_parameters(self):
        '''test render-time parameters'''

        vars = dict(message='hello, from breve', title=my_name())
        args = {
            'tidy': True,
            'debug': True,
            'namespace': 'v',
            'extension': '.breve'
        }
        t = Template(html, root=template_root())
        t.render('index', vars, **args)
        for k, v in args.items():
            self.failUnless(getattr(t, k) == v)
Exemple #23
0
def test_instantiation_parameters():
    """test instantiation parameters"""
    # change the defaults to something else
    args = {
        'tidy': True,
        'debug': True,
        'namespace': 'v',
        'mashup_entities': True,
        'extension': '.breve'
    }
    t = Template(html, root=template_root(), **args)
    actual = [getattr(t, k, 'not_set') for k in sorted(args)]
    expected = [v for k, v in sorted(args.items())]
    assert actual == expected
Exemple #24
0
    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
Exemple #25
0
    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
Exemple #26
0
def test_custom_loader_stack():
    """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 = int(os.stat(f).st_mtime)
                    uid = f
                    return uid, timestamp
            raise OSError('No such file or directory %s' % template)

        def load(self, uid):
            return open(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)

    params = dict(
        title=my_name(),
        message='hello, world'
    )
    _test_name = my_name()
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #27
0
def test_loop_macros():
    """loop using macro"""
    params = 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', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #28
0
    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
Exemple #29
0
    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
Exemple #30
0
    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
Exemple #31
0
def test_custom_renderer_notag():
    """custom renderer returning non-Tag type"""
    def render_text(tag, data):
        tag.clear()
        return data
    register_global('render_text', render_text)

    params = dict(
        title=my_name(),
        message='hello, world'
    )
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #32
0
    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
Exemple #33
0
    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
Exemple #34
0
    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())
Exemple #35
0
    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
Exemple #36
0
def test_register_flattener():
    """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)

    params = dict(
        title=my_name(),
        today=datetime(2008, 4, 17)
    )
    _test_name = my_name()
    t = Template(html, root=template_root())
    actual = t.render('index', params, namespace='v')
    expected = expected_output()
    assert actual == expected
Exemple #37
0
    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
Exemple #38
0
    def test_let_memory_freed(self):
        '''test that let() objects are freed'''

        # is this even meaningful?

        import gc
        vars = dict(title=my_name(),
                    message="memory test",
                    biglist=['hello'] * 1000)
        collection_count = gc.get_count()

        t = Template(html, root=template_root())
        actual = t.render('index', vars, namespace='v')

        del vars
        gc.collect()
        self.assertEqual(gc.get_count(), (0, 0, 0))
Exemple #39
0
    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
Exemple #40
0
def test_render_parameters():
    """test render-time parameters"""
    params = dict(
        message='hello, from breve',
        title=my_name()
    )
    args = {
        'tidy': True,
        'debug': True,
        'namespace': 'v',
        'extension': '.breve'
    }
    t = Template(html, root=template_root())
    t.render('index', params, **args)
    actual = [getattr(t, k, 'not_set') for k in sorted(args)]
    expected = [v for k, v in sorted(args.items())]
    assert actual == expected
Exemple #41
0
    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
Exemple #42
0
 def test_render_parameters ( self ):
     '''test render-time parameters'''
     
     vars = dict ( 
         message = 'hello, from breve',
         title = my_name ( )
     )
     args = { 
         'tidy': True, 
         'debug': True, 
         'namespace': 'v', 
         'extension': '.breve'
     }
     t = Template ( html, root = template_root ( ) )
     t.render ( 'index', vars, **args )
     for k, v in args.items ( ):
         self.failUnless ( getattr ( t, k ) == v )
Exemple #43
0
    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
Exemple #44
0
    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
Exemple #45
0
    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
Exemple #46
0
    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 ( ) )
Exemple #47
0
    def test_let_memory_freed ( self ):
        '''test that let() objects are freed'''

        # is this even meaningful?

        import gc
        vars = dict (
            title = my_name ( ),
            message = "memory test",
            biglist = [ 'hello' ] * 1000
        )
        collection_count = gc.get_count ( )

        t = Template ( html, root = template_root ( ) )
        actual = t.render ( 'index', vars, namespace = 'v' )

        del vars
        gc.collect ( )
        self.assertEqual ( gc.get_count ( ), ( 0, 0, 0 ) )
Exemple #48
0
    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