Skip to content
Myr

[svn] added more unittests for jinja

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent b9c8ae1 · Browse files · 6 files+194 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+194 added−9 removed

  • tests:+134 added−0 removed
  • other:+60 added−9 removed

6 files+194 added−9 removed

modified:docs/src/loaders.txt+8 added-3 removed
51 unmodified lines
5253.. sourcecode:: python5455 import codecs56 from os.path import join, getmtime56 from os.path import join, getmtime, exists57 from jinja.loaders import BaseLoaderCachedLoaderMixin58 from jinja.exceptions import TemplateNotFound5960 class CachedLoader(CachedLoaderMixin, BaseLoader):
17 unmodified lines
78 finally:79 f.close()8081 def check_source_changed(self, environment, name):82 return getmtime(join(self.path, name))82 fn = join(self.path, name)83 if exists(fn):84 return getmtime(fn)85 return 08687You don't have to provide the `check_source_changed` method. If it doesn't85exist the option `auto_reload` won't have an effect.88exist the option `auto_reload` won't have an effect. Also note that the89`check_source_changed` method must not raise an exception if the template90does not exist but return ``0``.
modified:jinja/loaders.py+52 added-6 removed
23 unmodified lines
24except ImportError:25 resource_exists = resource_string = resource_filename = None262728__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader']28__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader', 'ChoiceLoader']293031def get_template_filename(searchpath, name):32 """
227 unmodified lines
260 else:261 raise TemplateNotFound(name)262263 def check_source_changed(self, environment, name):264 return path.getmtime(get_template_filename(self.searchpath, name))264 filename = get_template_filename(self.searchpath, name)265 if path.exists(filename):266 return path.getmtime(filename)267 return 0268269270class PackageLoader(CachedLoaderMixin, BaseLoader):271 """
53 unmodified lines
325 contents = resource_string(self.package_name, name)326 return contents.decode(environment.template_charset)327328 def check_source_changed(self, environment, name):326 name = '/'.join([self.package_path] + [p for p in name.split('/')327 if p and p[0] != '.'])328 return path.getmtime(resource_filename(self.package_name, name))329 fn = resource_filename(self.package_name, '/'.join([self.package_path] +330 [p for p in name.split('/') if p and p[0] != '.']))331 if resource_exists(self.package_name, fn):332 return path.getmtime(fn)333 return 0334335336class DictLoader(BaseLoader):337 """
3 unmodified lines
341342 from jinja import Environment, DictLoader343 e = Environment(loader=DictLoader(dict(344 layout='...',340 index='{% extends layout %}...'345 index='{% extends 'layout' %}...'346 )))347 """348349 def __init__(self, templates):
2 unmodified lines
352 def get_source(self, environment, name, parent):353 if name in self.templates:354 return self.templates[name]355 raise TemplateNotFound(name)356357358class ChoiceLoader(object):359 """360 A loader that tries multiple loaders in the order they are given to361 the `ChoiceLoader`:362363 .. sourcecode:: python364365 from jinja import ChoiceLoader, FileSystemLoader366 loader1 = FileSystemLoader("templates1")367 loader2 = FileSystemLoader("templates2")368 loader = ChoiceLoader([loader1, loader2])369 """370371 def __init__(self, loaders):372 self.loaders = list(loaders)373374 def get_source(self, environment, name, parent):375 for loader in self.loaders:376 try:377 return loader.get_source(environment, name, parent)378 except TemplateNotFound:379 continue380 raise TemplateNotFound(name)381382 def parse(self, environment, name, parent):383 for loader in self.loaders:384 try:385 return loader.parse(environment, name, parent)386 except TemplateNotFound:387 continue388 raise TemplateNotFound(name)389390 def load(self, environment, name, translator):391 for loader in self.loaders:392 try:393 return loader.load(environment, name, translator)394 except TemplateNotFound:395 continue396 raise TemplateNotFound(name)
added:tests/loaderres/templates/test.html+1 added
added:tests/loaderres/__init__.py
added:tests/test_loaders.py+72 added
1# -*- coding: utf-8 -*-2"""3 unit test for the loaders4 ~~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""910from jinja import Environment, loaders11from jinja.exceptions import TemplateNotFound121314dict_loader = loaders.DictLoader({15 'justdict.html': 'FOO'16})1718package_loader = loaders.PackageLoader('loaderres', 'templates')1920filesystem_loader = loaders.FileSystemLoader('loaderres/templates')2122choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])232425def test_dict_loader():26 env = Environment(loader=dict_loader)27 tmpl = env.get_template('justdict.html')28 assert tmpl.render().strip() == 'FOO'29 try:30 env.get_template('missing.html')31 except TemplateNotFound:32 pass33 else:34 raise AssertionError('expected template exception')353637def test_package_loader():38 env = Environment(loader=package_loader)39 tmpl = env.get_template('test.html')40 assert tmpl.render().strip() == 'BAR'41 try:42 env.get_template('missing.html')43 except TemplateNotFound:44 pass45 else:46 raise AssertionError('expected template exception')474849def test_filesystem_loader():50 env = Environment(loader=filesystem_loader)51 tmpl = env.get_template('test.html')52 assert tmpl.render().strip() == 'BAR'53 try:54 env.get_template('missing.html')55 except TemplateNotFound:56 pass57 else:58 raise AssertionError('expected template exception')596061def test_choice_loader():62 env = Environment(loader=choice_loader)63 tmpl = env.get_template('justdict.html')64 assert tmpl.render().strip() == 'FOO'65 tmpl = env.get_template('test.html')66 assert tmpl.render().strip() == 'BAR'67 try:68 env.get_template('missing.html')69 except TemplateNotFound:70 pass71 else:72 raise AssertionError('expected template exception')
added:tests/test_various.py+61 added
1# -*- coding: utf-8 -*-2"""3 unit test for various things4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""910KEYWORDS = '''11{{ with }}12{{ as }}13{{ import }}14{{ from }}15{{ class }}16{{ def }}17{{ try }}18{{ except }}19{{ exec }}20{{ global }}21{{ assert }}22{{ break }}23{{ continue }}24{{ lambda }}25{{ return }}26{{ raise }}27{{ yield }}28{{ while }}29{{ pass }}30{{ finally }}31'''3233UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''3435RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''363738def test_keywords(env):39 env.from_string(KEYWORDS)404142def test_unpacking(env):43 tmpl = env.from_string(UNPACKING)44 assert tmpl.render() == '1|2|3'454647def test_raw(env):48 tmpl = env.from_string(RAW)49 assert tmpl.render() == '{{ FOO }} and {% BAR %}'505152def test_cache_dict():53 from jinja.utils import CacheDict54 d = CacheDict(3)55 d["a"] = 156 d["b"] = 257 d["c"] = 358 d["a"]59 d["d"] = 460 assert len(d) == 361 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d