Ejemplo n.º 1
0
    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')
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def test_basic(self):
        viewcode_str = dedent("""
        x = 1
        y = 2
        """)

        engine = RenderEngine(MockApp())
        viewcode, viewglobals = engine.compile(viewcode_str, 'filename')

        self.assertTrue('__builtins__' in viewglobals, 'view globals did not contain builtins')
        self.assertTrue(iscode(viewcode), 'viewcode was not a code object')
Ejemplo n.º 4
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')
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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')
Ejemplo n.º 7
0
    def test_non_existent_import_fails_during_compile(self):
        viewcode_str = dedent("""
        import froobulator
        """)

        engine = RenderEngine(MockApp())
        self.assertRaises(ImportError, engine.compile, viewcode_str, 'filename')
Ejemplo n.º 8
0
    def test_invalid_syntax(self):
        viewcode_str = dedent("""
        x = 1
        y =
        """)

        engine = RenderEngine(MockApp())
        self.assertRaises(SyntaxError, engine.compile, viewcode_str, 'filename')
Ejemplo n.º 9
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')
Ejemplo n.º 10
0
    def test_two_splits(self):
        templatefp = template_fileobj("""
        # this is python
        ----
        <strong>this is HTML</strong>
        ----
        # this is an error
        """)

        engine = RenderEngine(MockApp())
        self.assertRaises(InvalidTemplate, engine.parse, templatefp)
Ejemplo n.º 11
0
    def test_import_detection(self):
        import sys
        import keystone.http

        engine = RenderEngine(MockApp())

        viewcode_str = dedent("""
        import sys
        """)
        viewcode, viewglobals = engine.compile(viewcode_str, 'filename')
        self.assertTrue('sys' in viewglobals, 'view globals did not contain imported modules')
        self.assertTrue(viewglobals['sys'] is sys, 'view globals got a different version of sys')

        viewcode_str = dedent("""
        from sys import version_info
        """)
        viewcode, viewglobals = engine.compile(viewcode_str, 'filename')
        self.assertTrue('version_info' in viewglobals, 'view globals did not contain from foo imported modules')
        self.assertTrue(viewglobals['version_info'] is sys.version_info, 'view globals got a different version of version_info')

        viewcode_str = dedent("""
        from sys import version_info as vi
        """)
        viewcode, viewglobals = engine.compile(viewcode_str, 'filename')
        self.assertTrue('vi' in viewglobals, 'view globals did not contain from foo import as\'d modules')
        self.assertTrue(viewglobals['vi'] is sys.version_info, 'view globals got a different version of vi')

        viewcode_str = dedent("""
        from keystone.http import *
        """)
        viewcode, viewglobals = engine.compile(viewcode_str, 'filename')
        for name in keystone.http.__all__:
            self.assertTrue(name in viewglobals, 'view globals did not contain from foo import * (%s)')
Ejemplo n.º 12
0
    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)