|
1 | | -import contextlib |
2 | | -import os |
3 | | -import re |
4 | | -import unittest |
5 | | -from unittest import mock # noqa |
6 | | - |
7 | | -from mako.cache import CacheImpl |
8 | | -from mako.cache import register_plugin |
9 | | -from mako.template import Template |
10 | | -from mako.util import update_wrapper |
11 | | - |
12 | | -template_base = os.path.join(os.path.dirname(__file__), "templates") |
13 | | -module_base = os.path.join(template_base, "modules") |
14 | | - |
15 | | - |
16 | | -class TemplateTest(unittest.TestCase): |
17 | | - def _file_template(self, filename, **kw): |
18 | | - filepath = self._file_path(filename) |
19 | | - return Template( |
20 | | - uri=filename, filename=filepath, module_directory=module_base, **kw |
21 | | - ) |
22 | | - |
23 | | - def _file_path(self, filename): |
24 | | - name, ext = os.path.splitext(filename) |
25 | | - py3k_path = os.path.join(template_base, name + "_py3k" + ext) |
26 | | - if os.path.exists(py3k_path): |
27 | | - return py3k_path |
28 | | - |
29 | | - return os.path.join(template_base, filename) |
30 | | - |
31 | | - def _do_file_test( |
32 | | - self, |
33 | | - filename, |
34 | | - expected, |
35 | | - filters=None, |
36 | | - unicode_=True, |
37 | | - template_args=None, |
38 | | - **kw, |
39 | | - ): |
40 | | - t1 = self._file_template(filename, **kw) |
41 | | - self._do_test( |
42 | | - t1, |
43 | | - expected, |
44 | | - filters=filters, |
45 | | - unicode_=unicode_, |
46 | | - template_args=template_args, |
47 | | - ) |
48 | | - |
49 | | - def _do_memory_test( |
50 | | - self, |
51 | | - source, |
52 | | - expected, |
53 | | - filters=None, |
54 | | - unicode_=True, |
55 | | - template_args=None, |
56 | | - **kw, |
57 | | - ): |
58 | | - t1 = Template(text=source, **kw) |
59 | | - self._do_test( |
60 | | - t1, |
61 | | - expected, |
62 | | - filters=filters, |
63 | | - unicode_=unicode_, |
64 | | - template_args=template_args, |
65 | | - ) |
66 | | - |
67 | | - def _do_test( |
68 | | - self, |
69 | | - template, |
70 | | - expected, |
71 | | - filters=None, |
72 | | - template_args=None, |
73 | | - unicode_=True, |
74 | | - ): |
75 | | - if template_args is None: |
76 | | - template_args = {} |
77 | | - if unicode_: |
78 | | - output = template.render_unicode(**template_args) |
79 | | - else: |
80 | | - output = template.render(**template_args) |
81 | | - |
82 | | - if filters: |
83 | | - output = filters(output) |
84 | | - eq_(output, expected) |
85 | | - |
86 | | - |
87 | | -def eq_(a, b, msg=None): |
88 | | - """Assert a == b, with repr messaging on failure.""" |
89 | | - assert a == b, msg or "%r != %r" % (a, b) |
90 | | - |
91 | | - |
92 | | -def teardown(): |
93 | | - import shutil |
94 | | - |
95 | | - shutil.rmtree(module_base, True) |
96 | | - |
97 | | - |
98 | | -@contextlib.contextmanager |
99 | | -def raises(except_cls, message=None): |
100 | | - try: |
101 | | - yield |
102 | | - success = False |
103 | | - except except_cls as e: |
104 | | - if message: |
105 | | - assert re.search(message, str(e), re.UNICODE), "%r !~ %s" % ( |
106 | | - message, |
107 | | - e, |
108 | | - ) |
109 | | - print(str(e).encode("utf-8")) |
110 | | - success = True |
111 | | - |
112 | | - # assert outside the block so it works for AssertionError too ! |
113 | | - assert success, "Callable did not raise an exception" |
114 | | - |
115 | | - |
116 | | -def assert_raises(except_cls, callable_, *args, **kw): |
117 | | - with raises(except_cls): |
118 | | - return callable_(*args, **kw) |
119 | | - |
120 | | - |
121 | | -def assert_raises_message(except_cls, msg, callable_, *args, **kwargs): |
122 | | - with raises(except_cls, msg): |
123 | | - return callable_(*args, **kwargs) |
124 | | - |
125 | | - |
126 | | -def skip_if(predicate, reason=None): |
127 | | - """Skip a test if predicate is true.""" |
128 | | - reason = reason or predicate.__name__ |
129 | | - |
130 | | - def decorate(fn): |
131 | | - fn_name = fn.__name__ |
132 | | - |
133 | | - def maybe(*args, **kw): |
134 | | - if predicate(): |
135 | | - msg = "'%s' skipped: %s" % (fn_name, reason) |
136 | | - raise unittest.SkipTest(msg) |
137 | | - else: |
138 | | - return fn(*args, **kw) |
139 | | - |
140 | | - return update_wrapper(maybe, fn) |
141 | | - |
142 | | - return decorate |
143 | | - |
144 | | - |
145 | | -def requires_pygments_14(fn): |
146 | | - try: |
147 | | - import pygments |
148 | | - |
149 | | - version = pygments.__version__ |
150 | | - except: |
151 | | - version = "0" |
152 | | - return skip_if( |
153 | | - lambda: version < "1.4", "Requires pygments 1.4 or greater" |
154 | | - )(fn) |
155 | | - |
156 | | - |
157 | | -def requires_no_pygments_exceptions(fn): |
158 | | - def go(*arg, **kw): |
159 | | - from mako import exceptions |
160 | | - |
161 | | - exceptions._install_fallback() |
162 | | - try: |
163 | | - return fn(*arg, **kw) |
164 | | - finally: |
165 | | - exceptions._install_highlighting() |
166 | | - |
167 | | - return update_wrapper(go, fn) |
168 | | - |
169 | | - |
170 | | -class PlainCacheImpl(CacheImpl): |
171 | | - """Simple memory cache impl so that tests which |
172 | | - use caching can run without beaker.""" |
173 | | - |
174 | | - def __init__(self, cache): |
175 | | - self.cache = cache |
176 | | - self.data = {} |
177 | | - |
178 | | - def get_or_create(self, key, creation_function, **kw): |
179 | | - if key in self.data: |
180 | | - return self.data[key] |
181 | | - else: |
182 | | - self.data[key] = data = creation_function(**kw) |
183 | | - return data |
184 | | - |
185 | | - def put(self, key, value, **kw): |
186 | | - self.data[key] = value |
187 | | - |
188 | | - def get(self, key, **kw): |
189 | | - return self.data[key] |
190 | | - |
191 | | - def invalidate(self, key, **kw): |
192 | | - del self.data[key] |
193 | | - |
194 | | - |
195 | | -register_plugin("plain", __name__, "PlainCacheImpl") |
0 commit comments