forked from noahmorrison/chevron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_spec.py
executable file
·234 lines (173 loc) · 6.42 KB
/
test_spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unittest
import os
import json
import chevron
SPECS_PATH = os.path.join('spec', 'specs')
SPECS = [path for path in os.listdir(SPECS_PATH) if path.endswith('.json')]
STACHE = chevron.render
def _test_case_from_path(json_path):
json_path = '%s.json' % json_path
class MustacheTestCase(unittest.TestCase):
"""A simple yaml based test case"""
def _test_from_object(obj):
"""Generate a unit test from a test object"""
def test_case(self):
result = STACHE(obj['template'], obj['data'],
partials_dict=obj.get('partials', {}))
self.assertEqual(result, obj['expected'])
test_case.__doc__ = 'suite: {0} desc: {1}'.format(spec,
obj['desc'])
return test_case
with open(json_path, 'r') as f:
yaml = json.load(f)
# Generates a unit test for each test object
for i, test in enumerate(yaml['tests']):
vars()['test_' + str(i)] = _test_from_object(test)
# Return the built class
return MustacheTestCase
# Create TestCase for each json file
for spec in SPECS:
# Ignore optional tests
if spec[0] != '~':
spec = spec.split('.')[0]
globals()[spec] = _test_case_from_path(os.path.join(SPECS_PATH, spec))
class ExpandedCoverage(unittest.TestCase):
def test_unclosed_sections(self):
test1 = {
'template': '{{# section }} oops {{/ wrong_section }}'
}
test2 = {
'template': '{{# section }} end of file'
}
self.assertRaises(chevron.ChevronError, chevron.render, **test1)
self.assertRaises(chevron.ChevronError, chevron.render, **test2)
# check SyntaxError still catches ChevronError:
self.assertRaises(SyntaxError, chevron.render, **test1)
def test_bad_set_delimiter_tag(self):
args = {
'template': '{{= bad!}}'
}
self.assertRaises(SyntaxError, chevron.render, **args)
def test_unicode_basic(self):
args = {
'template': '(╯°□°)╯︵ ┻━┻'
}
result = chevron.render(**args)
expected = '(╯°□°)╯︵ ┻━┻'
self.assertEqual(result, expected)
def test_unicode_variable(self):
args = {
'template': '{{ table_flip }}',
'data': {'table_flip': '(╯°□°)╯︵ ┻━┻'}
}
result = chevron.render(**args)
expected = '(╯°□°)╯︵ ┻━┻'
self.assertEqual(result, expected)
def test_unicode_partial(self):
args = {
'template': '{{> table_flip }}',
'partials_dict': {'table_flip': '(╯°□°)╯︵ ┻━┻'}
}
result = chevron.render(**args)
expected = '(╯°□°)╯︵ ┻━┻'
self.assertEqual(result, expected)
def test_listed_data(self):
args = {
'template': '{{# . }}({{ . }}){{/ . }}',
'data': [1, 2, 3, 4, 5]
}
result = chevron.render(**args)
expected = '(1)(2)(3)(4)(5)'
self.assertEqual(result, expected)
def test_main(self):
result = chevron.main('tests/test.mustache', 'tests/data.json',
partials_path='tests')
with open('tests/test.rendered', 'r') as f:
expected = f.read()
self.assertEqual(result, expected)
def test_recursion(self):
args = {
'template': '{{# 1.2 }}{{# data }}{{.}}{{/ data }}{{/ 1.2 }}',
'data': {'1': {'2': [{'data': ["1", "2", "3"]}]}}
}
result = chevron.render(**args)
expected = '123'
self.assertEqual(result, expected)
def test_unicode_inside_list(self):
args = {
'template': '{{#list}}{{.}}{{/list}}',
'data': {'list': ['☠']}
}
result = chevron.render(**args)
expected = '☠'
self.assertEqual(result, expected)
def test_falsy(self):
args = {
'template': '{{null}}{{false}}{{list}}{{dict}}{{zero}}',
'data': {'null': None,
'false': False,
'list': [],
'dict': {},
'zero': 0
}
}
result = chevron.render(**args)
expected = 'False0'
self.assertEqual(result, expected)
def test_complex(self):
class Complex:
def __init__(self):
self.attr = 42
args = {
'template': '{{comp.attr}} {{int.attr}}',
'data': {'comp': Complex(),
'int': 1
}
}
result = chevron.render(**args)
expected = '42 '
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/issues/17
def test_inverted_coercion(self):
args = {
'template': '{{#object}}{{^child}}{{.}}{{/child}}{{/object}}',
'data': {'object': [
'foo', 'bar', {'child': True}, 'baz'
]}
}
result = chevron.render(**args)
expected = 'foobarbaz'
self.assertEqual(result, expected)
def test_closing_tag_only(self):
args = {
'template': '{{ foo } bar',
'data': {'foo': 'xx'}
}
self.assertRaises(chevron.ChevronError, chevron.render, **args)
def test_current_line_rest(self):
args = {
'template': 'first line\nsecond line\n {{ foo } bar',
'data': {'foo': 'xx'}
}
# self.assertRaisesRegex does not exist in python2.6
for _ in range(10):
try:
chevron.render(**args)
except chevron.ChevronError as error:
self.assertEqual(error.msg, 'unclosed tag at line 3')
def test_no_opening_tag(self):
args = {
'template': 'oops, no opening tag {{/ closing_tag }}',
'data': {'foo': 'xx'}
}
try:
chevron.render(**args)
except chevron.ChevronError as error:
self.assertEqual(error.msg, 'Trying to close tag "closing_tag"\n'
'Looks like it was not opened.\n'
'line 2')
# Run unit tests from command line
if __name__ == "__main__":
unittest.main()