From 42549363362579a942b17dd5c0eb473ddd976881 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 9 Feb 2010 16:35:08 +0100 Subject: [PATCH] Added loader tests. --HG-- branch : trunk --- jinja2/testsuite/__init__.py | 7 +- jinja2/testsuite/{tags.py => core_tags.py} | 38 +++++++- jinja2/testsuite/loader.py | 104 +++++++++++++++++++++ 3 files changed, 143 insertions(+), 6 deletions(-) rename jinja2/testsuite/{tags.py => core_tags.py} (83%) create mode 100644 jinja2/testsuite/loader.py diff --git a/jinja2/testsuite/__init__.py b/jinja2/testsuite/__init__.py index e768809..6a4b5ff 100644 --- a/jinja2/testsuite/__init__.py +++ b/jinja2/testsuite/__init__.py @@ -22,7 +22,7 @@ dict_loader = loaders.DictLoader({ 'justdict.html': 'FOO' }) package_loader = loaders.PackageLoader('jinja2.testsuite.res', 'templates') -filesystem_loader = loaders.FileSystemLoader(here + 'res/templates') +filesystem_loader = loaders.FileSystemLoader(here + '/res/templates') function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get) choice_loader = loaders.ChoiceLoader([dict_loader, package_loader]) prefix_loader = loaders.PrefixLoader({ @@ -44,9 +44,10 @@ class JinjaTestCase(unittest.TestCase): def suite(): - from jinja2.testsuite import ext, filters, tags + from jinja2.testsuite import ext, filters, core_tags, loader suite = unittest.TestSuite() suite.addTest(ext.suite()) suite.addTest(filters.suite()) - suite.addTest(tags.suite()) + suite.addTest(core_tags.suite()) + suite.addTest(loader.suite()) return suite diff --git a/jinja2/testsuite/tags.py b/jinja2/testsuite/core_tags.py similarity index 83% rename from jinja2/testsuite/tags.py rename to jinja2/testsuite/core_tags.py index 991e17a..4c9723a 100644 --- a/jinja2/testsuite/tags.py +++ b/jinja2/testsuite/core_tags.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- """ - jinja2.testsuite.tags - ~~~~~~~~~~~~~~~~~~~~~ + jinja2.testsuite.core_tags + ~~~~~~~~~~~~~~~~~~~~~~~~~~ - Test for most of the builtin tags. + Test the core tags like for and if. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. @@ -163,7 +163,39 @@ class ForLoopTestCase(JinjaTestCase): assert t.render(foo=(1,)) == '...1......2...' +class IfConditionTestCase(JinjaTestCase): + + def test_simple(self): + tmpl = env.from_string('''{% if true %}...{% endif %}''') + assert tmpl.render() == '...' + + def test_elif(self): + tmpl = env.from_string('''{% if false %}XXX{% elif true + %}...{% else %}XXX{% endif %}''') + assert tmpl.render() == '...' + + def test_else(self): + tmpl = env.from_string('{% if false %}XXX{% else %}...{% endif %}') + assert tmpl.render() == '...' + + def test_empty(self): + tmpl = env.from_string('[{% if true %}{% else %}{% endif %}]') + assert tmpl.render() == '[]' + + def test_complete(self): + tmpl = env.from_string('{% if a %}A{% elif b %}B{% elif c == d %}' + 'C{% else %}D{% endif %}') + assert tmpl.render(a=0, b=False, c=42, d=42.0) == 'C' + + def test_no_scope(self): + tmpl = env.from_string('{% if a %}{% set foo = 1 %}{% endif %}{{ foo }}') + assert tmpl.render(a=True) == '1' + tmpl = env.from_string('{% if true %}{% set foo = 1 %}{% endif %}{{ foo }}') + assert tmpl.render() == '1' + + def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(ForLoopTestCase)) + suite.addTest(unittest.makeSuite(IfConditionTestCase)) return suite diff --git a/jinja2/testsuite/loader.py b/jinja2/testsuite/loader.py new file mode 100644 index 0000000..5f61f7a --- /dev/null +++ b/jinja2/testsuite/loader.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +""" + jinja2.testsuite.loader + ~~~~~~~~~~~~~~~~~~~~~~~ + + Test the loaders. + + :copyright: (c) 2010 by the Jinja Team. + :license: BSD, see LICENSE for more details. +""" +import os +import time +import tempfile +import unittest + +from jinja2.testsuite import JinjaTestCase, dict_loader, \ + package_loader, filesystem_loader, function_loader, \ + choice_loader, prefix_loader + +from jinja2 import Environment, loaders +from jinja2.loaders import split_template_path +from jinja2.exceptions import TemplateNotFound + + +class LoaderTestCase(JinjaTestCase): + + def test_dict_loader(self): + env = Environment(loader=dict_loader) + tmpl = env.get_template('justdict.html') + assert tmpl.render().strip() == 'FOO' + self.assert_raises(TemplateNotFound, env.get_template, 'missing.html') + + def test_package_loader(self): + env = Environment(loader=package_loader) + tmpl = env.get_template('test.html') + assert tmpl.render().strip() == 'BAR' + self.assert_raises(TemplateNotFound, env.get_template, 'missing.html') + + def test_filesystem_loader(self): + env = Environment(loader=filesystem_loader) + tmpl = env.get_template('test.html') + assert tmpl.render().strip() == 'BAR' + tmpl = env.get_template('foo/test.html') + assert tmpl.render().strip() == 'FOO' + self.assert_raises(TemplateNotFound, env.get_template, 'missing.html') + + def test_choice_loader(self): + env = Environment(loader=choice_loader) + tmpl = env.get_template('justdict.html') + assert tmpl.render().strip() == 'FOO' + tmpl = env.get_template('test.html') + assert tmpl.render().strip() == 'BAR' + self.assert_raises(TemplateNotFound, env.get_template, 'missing.html') + + def test_function_loader(self): + env = Environment(loader=function_loader) + tmpl = env.get_template('justfunction.html') + assert tmpl.render().strip() == 'FOO' + self.assert_raises(TemplateNotFound, env.get_template, 'missing.html') + + def test_prefix_loader(self): + env = Environment(loader=prefix_loader) + tmpl = env.get_template('a/test.html') + assert tmpl.render().strip() == 'BAR' + tmpl = env.get_template('b/justdict.html') + assert tmpl.render().strip() == 'FOO' + self.assert_raises(TemplateNotFound, env.get_template, 'missing') + + def test_caching(self): + changed = False + class TestLoader(loaders.BaseLoader): + def get_source(self, environment, template): + return u'foo', None, lambda: not changed + env = Environment(loader=TestLoader(), cache_size=-1) + tmpl = env.get_template('template') + assert tmpl is env.get_template('template') + changed = True + assert tmpl is not env.get_template('template') + changed = False + + env = Environment(loader=TestLoader(), cache_size=0) + assert env.get_template('template') \ + is not env.get_template('template') + + env = Environment(loader=TestLoader(), cache_size=2) + t1 = env.get_template('one') + t2 = env.get_template('two') + assert t2 is env.get_template('two') + assert t1 is env.get_template('one') + t3 = env.get_template('three') + assert 'one' in env.cache + assert 'two' not in env.cache + assert 'three' in env.cache + + def test_split_template_path(self): + assert split_template_path('foo/bar') == ['foo', 'bar'] + assert split_template_path('./foo/bar') == ['foo', 'bar'] + self.assert_raises(TemplateNotFound, split_template_path, '../foo') + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(LoaderTestCase)) + return suite -- 2.26.2