[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
Provenance
- Commit
93e14c2- Tree
f1ef283- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b9c8ae1
6 files changed+194 added−9 removed
modified:docs/src/loaders.txt+8 added-3 removed
51 unmodified lines52 53 .. sourcecode:: python54 55 import codecs56- from os.path import join, getmtime56+ from os.path import join, getmtime, exists57 from jinja.loaders import BaseLoaderCachedLoaderMixin58 from jinja.exceptions import TemplateNotFound59 60 class CachedLoader(CachedLoaderMixin, BaseLoader):17 unmodified lines78 finally:79 f.close()80 81 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 086 87 You don't have to provide the `check_source_changed` method. If it doesn't85-exist the option `auto_reload` won't have an effect.88+exist the option `auto_reload` won't have an effect. Also note that the89+`check_source_changed` method must not raise an exception if the template90+does not exist but return ``0``.modified:jinja/loaders.py+52 added-6 removed
23 unmodified lines24 except ImportError:25 resource_exists = resource_string = resource_filename = None26 27 28-__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader']28+__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader', 'ChoiceLoader']29 30 31 def get_template_filename(searchpath, name):32 """227 unmodified lines260 else:261 raise TemplateNotFound(name)262 263 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 0268 269 270 class PackageLoader(CachedLoaderMixin, BaseLoader):271 """53 unmodified lines325 contents = resource_string(self.package_name, name)326 return contents.decode(environment.template_charset)327 328 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 0334 335 336 class DictLoader(BaseLoader):337 """3 unmodified lines341 342 from jinja import Environment, DictLoader343 e = Environment(loader=DictLoader(dict(344 layout='...',340- index='{% extends layout %}...'345+ index='{% extends 'layout' %}...'346 )))347 """348 349 def __init__(self, templates):2 unmodified lines352 def get_source(self, environment, name, parent):353 if name in self.templates:354 return self.templates[name]355 raise TemplateNotFound(name)356+357+358+class ChoiceLoader(object):359+ """360+ A loader that tries multiple loaders in the order they are given to361+ the `ChoiceLoader`:362+363+ .. sourcecode:: python364+365+ from jinja import ChoiceLoader, FileSystemLoader366+ loader1 = FileSystemLoader("templates1")367+ loader2 = FileSystemLoader("templates2")368+ loader = ChoiceLoader([loader1, loader2])369+ """370+371+ def __init__(self, loaders):372+ self.loaders = list(loaders)373+374+ 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)381+382+ 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)389+390+ 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
1BARadded:tests/loaderres/__init__.py
added:tests/test_loaders.py+72 added
1+# -*- coding: utf-8 -*-2+"""3+ unit test for the loaders4+ ~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ :copyright: 2007 by Armin Ronacher.7+ :license: BSD, see LICENSE for more details.8+"""9+10+from jinja import Environment, loaders11+from jinja.exceptions import TemplateNotFound12+13+14+dict_loader = loaders.DictLoader({15+ 'justdict.html': 'FOO'16+})17+18+package_loader = loaders.PackageLoader('loaderres', 'templates')19+20+filesystem_loader = loaders.FileSystemLoader('loaderres/templates')21+22+choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])23+24+25+def 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')35+36+37+def 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')47+48+49+def 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')59+60+61+def 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+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ :copyright: 2007 by Armin Ronacher.7+ :license: BSD, see LICENSE for more details.8+"""9+10+KEYWORDS = '''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+'''32+33+UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''34+35+RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''36+37+38+def test_keywords(env):39+ env.from_string(KEYWORDS)40+41+42+def test_unpacking(env):43+ tmpl = env.from_string(UNPACKING)44+ assert tmpl.render() == '1|2|3'45+46+47+def test_raw(env):48+ tmpl = env.from_string(RAW)49+ assert tmpl.render() == '{{ FOO }} and {% BAR %}'50+51+52+def 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