forked from noahmorrison/chevron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spec.py
executable file
·558 lines (428 loc) · 15.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/python
# -*- coding: utf-8 -*-
import collections
import unittest
import os
import json
import io
import chevron
import sys
if sys.version_info[0] == 3:
python3 = True
else: # python 2
python3 = False
SPECS_PATH = os.path.join('spec', 'specs')
if os.path.exists(SPECS_PATH):
SPECS = [path for path in os.listdir(SPECS_PATH) if path.endswith('.json')]
else:
SPECS = []
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 io.open(json_path, 'r', encoding='utf-8') 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_missing_key_partial(self):
args = {
'template': 'before, {{> with_missing_key }}, after',
'partials_dict': {
'with_missing_key': '{{#missing_key}}bloop{{/missing_key}}',
},
}
result = chevron.render(**args)
expected = 'before, , after'
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 io.open('tests/test.rendered', 'r', encoding='utf-8') as f:
expected = f.read()
if not python3:
expected = expected.encode('utf-8')
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')
# https://github.com/noahmorrison/chevron/issues/17
def test_callable_1(self):
args_passed = {}
def first(content, render):
args_passed['content'] = content
args_passed['render'] = render
return "not implemented"
args = {
'template': '{{{postcode}}} {{#first}} {{{city}}} || {{{town}}} '
'|| {{{village}}} || {{{state}}} {{/first}}',
'data': {
"postcode": "1234",
"city": "Mustache City",
"state": "Nowhere",
"first": first,
}
}
result = chevron.render(**args)
expected = '1234 not implemented'
template_content = " {{& city }} || {{& town }} || {{& village }} "\
"|| {{& state }} "
self.assertEqual(result, expected)
self.assertEqual(args_passed['content'], template_content)
def test_callable_2(self):
def first(content, render):
result = render(content)
result = [x.strip() for x in result.split(" || ") if x.strip()]
return result[0]
args = {
'template': '{{{postcode}}} {{#first}} {{{city}}} || {{{town}}} '
'|| {{{village}}} || {{{state}}} {{/first}}',
'data': {
"postcode": "1234",
"town": "Mustache Town",
"state": "Nowhere",
"first": first,
}
}
result = chevron.render(**args)
expected = '1234 Mustache Town'
self.assertEqual(result, expected)
def test_callable_3(self):
'''Test generating some data within the function
'''
def first(content, render):
result = render(content, {'city': "Injected City"})
result = [x.strip() for x in result.split(" || ") if x.strip()]
return result[0]
args = {
'template': '{{{postcode}}} {{#first}} {{{city}}} || {{{town}}} '
'|| {{{village}}} || {{{state}}} {{/first}}',
'data': {
"postcode": "1234",
"town": "Mustache Town",
"state": "Nowhere",
"first": first,
}
}
result = chevron.render(**args)
expected = '1234 Injected City'
self.assertEqual(result, expected)
def test_callable_4(self):
'''Test render of partial inside lambda
'''
def function(content, render):
return render(content)
args = {
'template': '{{#function}}{{>partial}}{{!comment}}{{/function}}',
'partials_dict': {
'partial': 'partial content',
},
'data': {
'function': function,
}
}
result = chevron.render(**args)
expected = 'partial content'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/issues/35
def test_custom_falsy(self):
class CustomData(dict):
class LowercaseBool:
_CHEVRON_return_scope_when_falsy = True
def __init__(self, value):
self.value = value
def __bool__(self):
return self.value
__nonzero__ = __bool__
def __str__(self):
if self.value:
return 'true'
return 'false'
def __getitem__(self, key):
item = dict.__getitem__(self, key)
if isinstance(item, dict):
return CustomData(item)
if isinstance(item, bool):
return self.LowercaseBool(item)
return item
args = {
'data': CustomData({
'truthy': True,
'falsy': False,
}),
'template': '{{ truthy }} {{ falsy }}',
}
result = chevron.render(**args)
expected = 'true false'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/issues/39
def test_nest_loops_with_same_key(self):
args = {
'template': 'A{{#x}}B{{#x}}{{.}}{{/x}}C{{/x}}D',
'data': {'x': ['z', 'x']}
}
result = chevron.render(**args)
expected = 'ABzxCBzxCD'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/issues/49
def test_partial_indentation(self):
args = {
'template': '\t{{> count }}',
'partials_dict': {
'count': '\tone\n\ttwo'
}
}
result = chevron.render(**args)
expected = '\t\tone\n\t\ttwo'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/issues/52
def test_indexed(self):
args = {
'template': 'count {{count.0}}, {{count.1}}, '
'{{count.100}}, {{nope.0}}',
'data': {
"count": [5, 4, 3, 2, 1],
}
}
result = chevron.render(**args)
expected = 'count 5, 4, , '
self.assertEqual(result, expected)
def test_iterator_scope_indentation(self):
args = {
'data': {
'thing': ['foo', 'bar', 'baz'],
},
'template': '{{> count }}',
'partials_dict': {
'count': ' {{> iter_scope }}',
'iter_scope': 'foobar\n{{#thing}}\n {{.}}\n{{/thing}}'
}
}
result = chevron.render(**args)
expected = ' foobar\n foo\n bar\n baz\n'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/pull/73
def test_namedtuple_data(self):
NT = collections.namedtuple('NT', ['foo', 'bar'])
args = {
'template': '{{foo}} {{bar}}',
'data': NT('hello', 'world')
}
result = chevron.render(**args)
expected = 'hello world'
self.assertEqual(result, expected)
def test_get_key_not_in_dunder_dict_returns_attribute(self):
class C:
foo = "bar"
instance = C()
self.assertTrue("foo" not in instance.__dict__)
args = {
'template': '{{foo}}',
'data': instance
}
result = chevron.render(**args)
expected = 'bar'
self.assertEqual(result, expected)
def test_disabled_partials(self):
os.chdir('tests')
resultNone = chevron.main('test.mustache', 'data.json',
partials_path=None)
resultEmpty = chevron.main('test.mustache', 'data.json',
partials_path='')
with io.open('test-partials-disabled.rendered', 'r', encoding='utf-8') as f:
expected = f.read()
if not python3:
expected = expected.encode('utf-8')
self.assertEqual(resultNone, expected)
self.assertEqual(resultEmpty, expected)
os.chdir('..')
# https://github.com/noahmorrison/chevron/pull/94
def test_keep(self):
args = {
'template': '{{ first }} {{ second }} {{ third }}',
'data': {
"first": "1st",
"third": "3rd",
},
}
result = chevron.render(**args)
expected = '1st 3rd'
self.assertEqual(result, expected)
args['keep'] = True
result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)
args['template'] = '{{first}} {{second}} {{third}}'
result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)
args['template'] = '{{ first }} {{ second }} {{ third }}'
result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)
# https://github.com/noahmorrison/chevron/pull/94
def test_keep_from_partials(self):
args = {
'template': '{{ first }} {{> with_missing_key }} {{ third }}',
'data': {
"first": "1st",
"third": "3rd",
},
'partials_dict': {
'with_missing_key': '{{missing_key}}',
},
}
result = chevron.render(**args)
expected = '1st 3rd'
self.assertEqual(result, expected)
args['keep'] = True
result = chevron.render(**args)
expected = '1st {{ missing_key }} 3rd'
self.assertEqual(result, expected)
# Run unit tests from command line
if __name__ == "__main__":
unittest.main()