Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 1 | # -*- 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 | |
| 10 | from jinja import Environment, loaders |
| 11 | from jinja.exceptions import TemplateNotFound |
| 12 | |
| 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 | |
Armin Ronacher | 9baa5ba | 2007-03-21 18:05:32 +0100 | [diff] [blame] | 22 | function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get) |
| 23 | |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 24 | choice_loader = loaders.ChoiceLoader([dict_loader, package_loader]) |
| 25 | |
| 26 | |
| 27 | def 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 | |
| 39 | def 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 | |
| 51 | def 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 | |
| 63 | def 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 Ronacher | 9baa5ba | 2007-03-21 18:05:32 +0100 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def 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') |