Exemplo n.º 1
0
    def test_viewfunc(self):
        # the viewfunc is essentially "do some stuff, then return locals()",
        # so we just want to ensure that things we expect in the output dict
        # are there
        templatefp = template_fileobj("""
        x = 1
        y = 'abc'
        ----
        <strong>this is HTML</strong>
        """)

        engine = RenderEngine(MockApp())
        template = engine.parse(templatefp)

        returned_locals = template.viewfunc({})
        self.assertEquals({'x': 1, 'y': 'abc'}, returned_locals)

        # also make sure that injected variables are returned
        returned_locals = template.viewfunc({'injected': 'anything'})
        self.assertEquals({'x': 1, 'y': 'abc', 'injected': 'anything'}, returned_locals)

        # unless we delete things
        templatefp = template_fileobj("""
        x = 1
        y = 'abc'
        del injected
        del y
        ----
        <strong>this is HTML</strong>
        """)

        template = engine.parse(templatefp)

        returned_locals = template.viewfunc({'injected': 'anything'})
        self.assertEquals({'x': 1}, returned_locals)
Exemplo n.º 2
0
    def test_no_split(self):
        templatefp = template_fileobj("""
        <strong>this is HTML</strong>
        """)

        engine = RenderEngine(MockApp())
        template = engine.parse(templatefp)
        viewfunc, body = template.viewfunc, template.body

        self.assertEquals(1, viewfunc(1), 'viewfunc should be an identity function')

        self.assertEquals(body, '<strong>this is HTML</strong>\n', 'template body is incorrect')
Exemplo n.º 3
0
    def test_split(self):
        templatefp = template_fileobj("""
        # this is python
        ----
        <strong>this is HTML</strong>
        """)

        engine = RenderEngine(MockApp())
        template = engine.parse(templatefp)
        viewfunc, body = template.viewfunc, template.body

        self.assertTrue(isfunction(viewfunc), 'viewfunc is not a function')
        self.assertEquals(len(getargspec(viewfunc)[0]), 1, 'viewfunc should take only 1 argument')
        self.assertTrue(getargspec(viewfunc)[1] is None, 'viewfunc should take no varargs')
        self.assertTrue(getargspec(viewfunc)[2] is None, 'viewfunc should take no kwargs')
        self.assertTrue(getargspec(viewfunc)[3] is None, 'viewfunc should have no defaults')

        self.assertEquals(body, '<strong>this is HTML</strong>\n', 'template body is incorrect')