Rest of tests ported, just need to hook up doctests now.
[jinja2.git] / jinja2 / testsuite / __init__.py
1 # -*- coding: utf-8 -*-
2 """
3     jinja2.testsuite
4     ~~~~~~~~~~~~~~~~
5
6     All the unittests of Jinja2.  These tests can be executed by
7     either running run-tests.py using multiple Python versions at
8     the same time.
9
10     :copyright: (c) 2010 by the Jinja Team.
11     :license: BSD, see LICENSE for more details.
12 """
13 import os
14 import sys
15 import re
16 import unittest
17 from traceback import format_exception
18 from jinja2 import loaders
19
20
21 here = os.path.dirname(os.path.abspath(__file__))
22
23 dict_loader = loaders.DictLoader({
24     'justdict.html':        'FOO'
25 })
26 package_loader = loaders.PackageLoader('jinja2.testsuite.res', 'templates')
27 filesystem_loader = loaders.FileSystemLoader(here + '/res/templates')
28 function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get)
29 choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])
30 prefix_loader = loaders.PrefixLoader({
31     'a':        filesystem_loader,
32     'b':        dict_loader
33 })
34
35
36 class JinjaTestCase(unittest.TestCase):
37
38     ### use only these methods for testing.  If you need standard
39     ### unittest method, wrap them!
40
41     def assert_equal(self, a, b):
42         return self.assertEqual(a, b)
43
44     def assert_raises(self, *args, **kwargs):
45         return self.assertRaises(*args, **kwargs)
46
47     def assert_traceback_matches(self, callback, expected_tb):
48         try:
49             callback()
50         except Exception, e:
51             tb = format_exception(*sys.exc_info())
52             if re.search(expected_tb.strip(), ''.join(tb)) is None:
53                 raise self.fail('Traceback did not match:\n\n%s\nexpected:\n%s'
54                     % (''.join(tb), expected_tb))
55         else:
56             self.fail('Expected exception')
57
58
59 def suite():
60     from jinja2.testsuite import ext, filters, tests, core_tags, \
61          loader, inheritance, imports, lexnparse, security, api, \
62          regression, debug
63     suite = unittest.TestSuite()
64     suite.addTest(ext.suite())
65     suite.addTest(filters.suite())
66     suite.addTest(tests.suite())
67     suite.addTest(core_tags.suite())
68     suite.addTest(loader.suite())
69     suite.addTest(inheritance.suite())
70     suite.addTest(imports.suite())
71     suite.addTest(lexnparse.suite())
72     suite.addTest(security.suite())
73     suite.addTest(api.suite())
74     suite.addTest(regression.suite())
75     suite.addTest(debug.suite())
76     return suite