commit | author | age
|
71260a
|
1 |
import unittest |
CM |
2 |
|
|
3 |
from pyramid.compat import bytes_ |
|
4 |
|
0c29cf
|
5 |
|
71260a
|
6 |
class TestTemplate(unittest.TestCase): |
CM |
7 |
def _makeOne(self, name='whatever'): |
|
8 |
from pyramid.scaffolds.template import Template |
0c29cf
|
9 |
|
71260a
|
10 |
return Template(name) |
CM |
11 |
|
bfd4b3
|
12 |
def test_render_template_success(self): |
71260a
|
13 |
inst = self._makeOne() |
0c29cf
|
14 |
result = inst.render_template('{{a}} {{b}}', {'a': '1', 'b': '2'}) |
71260a
|
15 |
self.assertEqual(result, bytes_('1 2')) |
eeadb3
|
16 |
|
bfd4b3
|
17 |
def test_render_template_expr_failure(self): |
71260a
|
18 |
inst = self._makeOne() |
0c29cf
|
19 |
self.assertRaises( |
MM |
20 |
AttributeError, |
|
21 |
inst.render_template, |
|
22 |
'{{a.foo}}', |
|
23 |
{'a': '1', 'b': '2'}, |
|
24 |
) |
71260a
|
25 |
|
bfd4b3
|
26 |
def test_render_template_expr_success(self): |
71260a
|
27 |
inst = self._makeOne() |
0c29cf
|
28 |
result = inst.render_template('{{a.lower()}}', {'a': 'A'}) |
d20618
|
29 |
self.assertEqual(result, b'a') |
71260a
|
30 |
|
bfd4b3
|
31 |
def test_render_template_expr_success_via_pipe(self): |
71260a
|
32 |
inst = self._makeOne() |
0c29cf
|
33 |
result = inst.render_template('{{b|c|a.lower()}}', {'a': 'A'}) |
d20618
|
34 |
self.assertEqual(result, b'a') |
71260a
|
35 |
|
bfd4b3
|
36 |
def test_render_template_expr_success_via_pipe2(self): |
71260a
|
37 |
inst = self._makeOne() |
0c29cf
|
38 |
result = inst.render_template('{{b|a.lower()|c}}', {'a': 'A'}) |
d20618
|
39 |
self.assertEqual(result, b'a') |
71260a
|
40 |
|
bfd4b3
|
41 |
def test_render_template_expr_value_is_None(self): |
71260a
|
42 |
inst = self._makeOne() |
0c29cf
|
43 |
result = inst.render_template('{{a}}', {'a': None}) |
d20618
|
44 |
self.assertEqual(result, b'') |
eeadb3
|
45 |
|
de6b28
|
46 |
def test_render_template_with_escaped_double_braces(self): |
eeadb3
|
47 |
inst = self._makeOne() |
0c29cf
|
48 |
result = inst.render_template( |
25737f
|
49 |
'{{a}} {{b}} \\{\\{a\\}\\} \\{\\{c\\}\\}', {'a': '1', 'b': '2'} |
0c29cf
|
50 |
) |
eeadb3
|
51 |
self.assertEqual(result, bytes_('1 2 {{a}} {{c}}')) |
71260a
|
52 |
|
de6b28
|
53 |
def test_render_template_with_breaking_escaped_braces(self): |
TF |
54 |
inst = self._makeOne() |
0c29cf
|
55 |
result = inst.render_template( |
25737f
|
56 |
'{{a}} {{b}} \\{\\{a\\} \\{b\\}\\}', {'a': '1', 'b': '2'} |
0c29cf
|
57 |
) |
25737f
|
58 |
self.assertEqual(result, bytes_('1 2 \\{\\{a\\} \\{b\\}\\}')) |
de6b28
|
59 |
|
TF |
60 |
def test_render_template_with_escaped_single_braces(self): |
|
61 |
inst = self._makeOne() |
0c29cf
|
62 |
result = inst.render_template( |
25737f
|
63 |
'{{a}} {{b}} \\{a\\} \\{b', {'a': '1', 'b': '2'} |
0c29cf
|
64 |
) |
25737f
|
65 |
self.assertEqual(result, bytes_('1 2 \\{a\\} \\{b')) |
de6b28
|
66 |
|
71260a
|
67 |
def test_module_dir(self): |
CM |
68 |
import sys |
|
69 |
import pkg_resources |
0c29cf
|
70 |
|
71260a
|
71 |
package = sys.modules['pyramid.scaffolds.template'] |
CM |
72 |
path = pkg_resources.resource_filename(package.__name__, '') |
|
73 |
inst = self._makeOne() |
|
74 |
result = inst.module_dir() |
|
75 |
self.assertEqual(result, path) |
|
76 |
|
|
77 |
def test_template_dir__template_dir_is_None(self): |
|
78 |
inst = self._makeOne() |
|
79 |
self.assertRaises(AssertionError, inst.template_dir) |
|
80 |
|
|
81 |
def test_template_dir__template_dir_is_tuple(self): |
|
82 |
inst = self._makeOne() |
|
83 |
inst._template_dir = ('a', 'b') |
|
84 |
self.assertEqual(inst.template_dir(), ('a', 'b')) |
|
85 |
|
|
86 |
def test_template_dir__template_dir_is_not_None(self): |
|
87 |
import os |
|
88 |
import sys |
|
89 |
import pkg_resources |
0c29cf
|
90 |
|
71260a
|
91 |
package = sys.modules['pyramid.scaffolds.template'] |
CM |
92 |
path = pkg_resources.resource_filename(package.__name__, '') |
|
93 |
inst = self._makeOne() |
0c29cf
|
94 |
inst._template_dir = 'foo' |
71260a
|
95 |
result = inst.template_dir() |
CM |
96 |
self.assertEqual(result, os.path.join(path, 'foo')) |
|
97 |
|
|
98 |
def test_write_files_path_exists(self): |
|
99 |
import os |
|
100 |
import sys |
|
101 |
import pkg_resources |
0c29cf
|
102 |
|
71260a
|
103 |
package = sys.modules['pyramid.scaffolds.template'] |
CM |
104 |
path = pkg_resources.resource_filename(package.__name__, '') |
|
105 |
inst = self._makeOne() |
|
106 |
inst._template_dir = 'foo' |
|
107 |
inst.exists = lambda *arg: True |
|
108 |
copydir = DummyCopydir() |
|
109 |
inst.copydir = copydir |
|
110 |
command = DummyCommand() |
0c29cf
|
111 |
inst.write_files(command, 'output dir', {'a': 1}) |
71260a
|
112 |
self.assertEqual(copydir.template_dir, os.path.join(path, 'foo')) |
CM |
113 |
self.assertEqual(copydir.output_dir, 'output dir') |
0c29cf
|
114 |
self.assertEqual(copydir.vars, {'a': 1}) |
MM |
115 |
self.assertEqual( |
|
116 |
copydir.kw, |
|
117 |
{ |
|
118 |
'template_renderer': inst.render_template, |
|
119 |
'indent': 1, |
|
120 |
'verbosity': 1, |
|
121 |
'simulate': False, |
|
122 |
'overwrite': False, |
|
123 |
'interactive': False, |
|
124 |
}, |
|
125 |
) |
eeadb3
|
126 |
|
71260a
|
127 |
def test_write_files_path_missing(self): |
CM |
128 |
L = [] |
|
129 |
inst = self._makeOne() |
|
130 |
inst._template_dir = 'foo' |
|
131 |
inst.exists = lambda *arg: False |
|
132 |
inst.out = lambda *arg: None |
|
133 |
inst.makedirs = lambda dir: L.append(dir) |
|
134 |
copydir = DummyCopydir() |
|
135 |
inst.copydir = copydir |
|
136 |
command = DummyCommand() |
0c29cf
|
137 |
inst.write_files(command, 'output dir', {'a': 1}) |
71260a
|
138 |
self.assertEqual(L, ['output dir']) |
CM |
139 |
|
|
140 |
def test_run(self): |
|
141 |
L = [] |
|
142 |
inst = self._makeOne() |
|
143 |
inst._template_dir = 'foo' |
|
144 |
inst.exists = lambda *arg: False |
|
145 |
inst.out = lambda *arg: None |
|
146 |
inst.makedirs = lambda dir: L.append(dir) |
|
147 |
copydir = DummyCopydir() |
|
148 |
inst.copydir = copydir |
|
149 |
command = DummyCommand() |
0c29cf
|
150 |
inst.run(command, 'output dir', {'a': 1}) |
71260a
|
151 |
self.assertEqual(L, ['output dir']) |
CM |
152 |
|
bfd4b3
|
153 |
def test_check_vars(self): |
CM |
154 |
inst = self._makeOne() |
|
155 |
self.assertRaises(RuntimeError, inst.check_vars, 'one', 'two') |
0c29cf
|
156 |
|
bfd4b3
|
157 |
|
71260a
|
158 |
class DummyCopydir(object): |
CM |
159 |
def copy_dir(self, template_dir, output_dir, vars, **kw): |
|
160 |
self.template_dir = template_dir |
|
161 |
self.output_dir = output_dir |
|
162 |
self.vars = vars |
|
163 |
self.kw = kw |
|
164 |
|
0c29cf
|
165 |
|
2fcfda
|
166 |
class DummyArgs(object): |
b77e7f
|
167 |
simulate = False |
71260a
|
168 |
overwrite = False |
b77e7f
|
169 |
interactive = False |
eeadb3
|
170 |
|
0c29cf
|
171 |
|
71260a
|
172 |
class DummyCommand(object): |
2fcfda
|
173 |
args = DummyArgs() |
b77e7f
|
174 |
verbosity = 1 |