blob: e52458c7966bb3aa3e1796ab6fd88404795f22c2 [file] [log] [blame]
Armin Ronacher93e14c22007-03-20 16:17:48 +01001# -*- coding: utf-8 -*-
2"""
3 unit test for the loaders
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 :copyright: 2007 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8"""
9
10from jinja import Environment, loaders
11from jinja.exceptions import TemplateNotFound
12
13
14dict_loader = loaders.DictLoader({
15 'justdict.html': 'FOO'
16})
17
18package_loader = loaders.PackageLoader('loaderres', 'templates')
19
20filesystem_loader = loaders.FileSystemLoader('loaderres/templates')
21
Armin Ronacher9baa5ba2007-03-21 18:05:32 +010022function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get)
23
Armin Ronacher93e14c22007-03-20 16:17:48 +010024choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])
25
26
27def test_dict_loader():
28 env = Environment(loader=dict_loader)
29 tmpl = env.get_template('justdict.html')
30 assert tmpl.render().strip() == 'FOO'
31 try:
32 env.get_template('missing.html')
33 except TemplateNotFound:
34 pass
35 else:
36 raise AssertionError('expected template exception')
37
38
39def test_package_loader():
40 env = Environment(loader=package_loader)
41 tmpl = env.get_template('test.html')
42 assert tmpl.render().strip() == 'BAR'
43 try:
44 env.get_template('missing.html')
45 except TemplateNotFound:
46 pass
47 else:
48 raise AssertionError('expected template exception')
49
50
51def test_filesystem_loader():
52 env = Environment(loader=filesystem_loader)
53 tmpl = env.get_template('test.html')
54 assert tmpl.render().strip() == 'BAR'
55 try:
56 env.get_template('missing.html')
57 except TemplateNotFound:
58 pass
59 else:
60 raise AssertionError('expected template exception')
61
62
63def test_choice_loader():
64 env = Environment(loader=choice_loader)
65 tmpl = env.get_template('justdict.html')
66 assert tmpl.render().strip() == 'FOO'
67 tmpl = env.get_template('test.html')
68 assert tmpl.render().strip() == 'BAR'
69 try:
70 env.get_template('missing.html')
71 except TemplateNotFound:
72 pass
73 else:
74 raise AssertionError('expected template exception')
Armin Ronacher9baa5ba2007-03-21 18:05:32 +010075
76
77def test_function_loader():
78 env = Environment(loader=function_loader)
79 tmpl = env.get_template('justfunction.html')
80 assert tmpl.render().strip() == 'FOO'
81 try:
82 env.get_template('missing.html')
83 except TemplateNotFound:
84 pass
85 else:
86 raise AssertionError('expected template exception')