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
|
# coding: utf-8
"""
Unit tests of template.py.
"""
import codecs
import os
import sys
import unittest
from examples.simple import Simple
from pystache.renderer import Renderer
from pystache.loader import Loader
from .common import get_data_path
from .data.views import SayHello
class RendererInitTestCase(unittest.TestCase):
"""
Tests the Renderer.__init__() method.
"""
def test_partials__default(self):
"""
Test the default value.
"""
renderer = Renderer()
self.assertTrue(renderer.partials is None)
def test_partials(self):
"""
Test that the attribute is set correctly.
"""
renderer = Renderer(partials={'foo': 'bar'})
self.assertEquals(renderer.partials, {'foo': 'bar'})
def test_escape__default(self):
escape = Renderer().escape
self.assertEquals(escape(">"), ">")
self.assertEquals(escape('"'), """)
# Single quotes are not escaped.
self.assertEquals(escape("'"), "'")
def test_escape(self):
escape = lambda s: "**" + s
renderer = Renderer(escape=escape)
self.assertEquals(renderer.escape("bar"), "**bar")
def test_default_encoding__default(self):
"""
Check the default value.
"""
renderer = Renderer()
self.assertEquals(renderer.default_encoding, sys.getdefaultencoding())
def test_default_encoding(self):
"""
Check that the constructor sets the attribute correctly.
"""
renderer = Renderer(default_encoding="foo")
self.assertEquals(renderer.default_encoding, "foo")
def test_decode_errors__default(self):
"""
Check the default value.
"""
renderer = Renderer()
self.assertEquals(renderer.decode_errors, 'strict')
def test_decode_errors(self):
"""
Check that the constructor sets the attribute correctly.
"""
renderer = Renderer(decode_errors="foo")
self.assertEquals(renderer.decode_errors, "foo")
def test_file_encoding__default(self):
"""
Check the file_encoding default.
"""
renderer = Renderer()
self.assertEquals(renderer.file_encoding, renderer.default_encoding)
def test_file_encoding(self):
"""
Check that the file_encoding attribute is set correctly.
"""
renderer = Renderer(file_encoding='foo')
self.assertEquals(renderer.file_encoding, 'foo')
def test_file_extension__default(self):
"""
Check the file_extension default.
"""
renderer = Renderer()
self.assertEquals(renderer.file_extension, 'mustache')
def test_file_extension(self):
"""
Check that the file_encoding attribute is set correctly.
"""
renderer = Renderer(file_extension='foo')
self.assertEquals(renderer.file_extension, 'foo')
def test_search_dirs__default(self):
"""
Check the search_dirs default.
"""
renderer = Renderer()
self.assertEquals(renderer.search_dirs, [os.curdir])
def test_search_dirs__string(self):
"""
Check that the search_dirs attribute is set correctly when a string.
"""
renderer = Renderer(search_dirs='foo')
self.assertEquals(renderer.search_dirs, ['foo'])
def test_search_dirs__list(self):
"""
Check that the search_dirs attribute is set correctly when a list.
"""
renderer = Renderer(search_dirs=['foo'])
self.assertEquals(renderer.search_dirs, ['foo'])
class RendererTestCase(unittest.TestCase):
"""Test the Renderer class."""
def _renderer(self):
return Renderer()
## Test Renderer.unicode().
def test_unicode__default_encoding(self):
"""
Test that the default_encoding attribute is respected.
"""
renderer = Renderer()
s = "é"
renderer.default_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, renderer.unicode, s)
renderer.default_encoding = "utf-8"
self.assertEquals(renderer.unicode(s), u"é")
def test_unicode__decode_errors(self):
"""
Test that the decode_errors attribute is respected.
"""
renderer = Renderer()
renderer.default_encoding = "ascii"
s = "déf"
renderer.decode_errors = "ignore"
self.assertEquals(renderer.unicode(s), "df")
renderer.decode_errors = "replace"
# U+FFFD is the official Unicode replacement character.
self.assertEquals(renderer.unicode(s), u'd\ufffd\ufffdf')
## Test the _make_loader() method.
def test__make_loader__return_type(self):
"""
Test that _make_loader() returns a Loader.
"""
renderer = Renderer()
loader = renderer._make_loader()
self.assertEquals(type(loader), Loader)
def test__make_loader__attributes(self):
"""
Test that _make_locator() sets all attributes correctly..
"""
renderer = Renderer()
renderer.decode_errors = 'dec'
renderer.file_encoding = 'enc'
renderer.file_extension = 'ext'
loader = renderer._make_loader()
self.assertEquals(loader.decode_errors, 'dec')
self.assertEquals(loader.encoding, 'enc')
self.assertEquals(loader.extension, 'ext')
## Test the render() method.
def test_render__return_type(self):
"""
Check that render() returns a string of type unicode.
"""
renderer = Renderer()
rendered = renderer.render('foo')
self.assertEquals(type(rendered), unicode)
def test_render__unicode(self):
renderer = Renderer()
actual = renderer.render(u'foo')
self.assertEquals(actual, u'foo')
def test_render__str(self):
renderer = Renderer()
actual = renderer.render('foo')
self.assertEquals(actual, 'foo')
def test_render__non_ascii_character(self):
renderer = Renderer()
actual = renderer.render(u'Poincaré')
self.assertEquals(actual, u'Poincaré')
def test_render__context(self):
"""
Test render(): passing a context.
"""
renderer = Renderer()
self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
def test_render__context_and_kwargs(self):
"""
Test render(): passing a context and **kwargs.
"""
renderer = Renderer()
template = 'Hi {{person1}} and {{person2}}'
self.assertEquals(renderer.render(template, {'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
def test_render__kwargs_and_no_context(self):
"""
Test render(): passing **kwargs and no context.
"""
renderer = Renderer()
self.assertEquals(renderer.render('Hi {{person}}', person='Mom'), 'Hi Mom')
def test_render__context_and_kwargs__precedence(self):
"""
Test render(): **kwargs takes precedence over context.
"""
renderer = Renderer()
self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}, person='Dad'), 'Hi Dad')
def test_render__kwargs_does_not_modify_context(self):
"""
Test render(): passing **kwargs does not modify the passed context.
"""
context = {}
renderer = Renderer()
renderer.render('Hi {{person}}', context=context, foo="bar")
self.assertEquals(context, {})
def test_render__nonascii_template(self):
"""
Test passing a non-unicode template with non-ascii characters.
"""
renderer = Renderer()
template = "déf"
# Check that decode_errors and default_encoding are both respected.
renderer.decode_errors = 'ignore'
renderer.default_encoding = 'ascii'
self.assertEquals(renderer.render(template), "df")
renderer.default_encoding = 'utf_8'
self.assertEquals(renderer.render(template), u"déf")
def test_make_load_partial(self):
"""
Test the _make_load_partial() method.
"""
renderer = Renderer()
renderer.partials = {'foo': 'bar'}
load_partial = renderer._make_load_partial()
actual = load_partial('foo')
self.assertEquals(actual, 'bar')
self.assertEquals(type(actual), unicode, "RenderEngine requires that "
"load_partial return unicode strings.")
def test_make_load_partial__unicode(self):
"""
Test _make_load_partial(): that load_partial doesn't "double-decode" Unicode.
"""
renderer = Renderer()
renderer.partials = {'partial': 'foo'}
load_partial = renderer._make_load_partial()
self.assertEquals(load_partial("partial"), "foo")
# Now with a value that is already unicode.
renderer.partials = {'partial': u'foo'}
load_partial = renderer._make_load_partial()
# If the next line failed, we would get the following error:
# TypeError: decoding Unicode is not supported
self.assertEquals(load_partial("partial"), "foo")
def test_render_path(self):
"""
Test the render_path() method.
"""
renderer = Renderer()
path = get_data_path('say_hello.mustache')
actual = renderer.render_path(path, to='foo')
self.assertEquals(actual, "Hello, foo")
def test_render__object(self):
"""
Test rendering an object instance.
"""
renderer = Renderer()
say_hello = SayHello()
actual = renderer.render(say_hello)
self.assertEquals('Hello, World', actual)
actual = renderer.render(say_hello, to='Mars')
self.assertEquals('Hello, Mars', actual)
def test_render__view(self):
"""
Test rendering a View instance.
"""
renderer = Renderer()
view = Simple()
actual = renderer.render(view)
self.assertEquals('Hi pizza!', actual)
# By testing that Renderer.render() constructs the right RenderEngine,
# we no longer need to exercise all rendering code paths through
# the Renderer. It suffices to test rendering paths through the
# RenderEngine for the same amount of code coverage.
class Renderer_MakeRenderEngineTests(unittest.TestCase):
"""
Check the RenderEngine returned by Renderer._make_render_engine().
"""
## Test the engine's load_partial attribute.
def test__load_partial__returns_unicode(self):
"""
Check that load_partial returns unicode (and not a subclass).
"""
class MyUnicode(unicode):
pass
renderer = Renderer()
renderer.default_encoding = 'ascii'
renderer.partials = {'str': 'foo', 'subclass': MyUnicode('abc')}
engine = renderer._make_render_engine()
actual = engine.load_partial('str')
self.assertEquals(actual, "foo")
self.assertEquals(type(actual), unicode)
# Check that unicode subclasses are not preserved.
actual = engine.load_partial('subclass')
self.assertEquals(actual, "abc")
self.assertEquals(type(actual), unicode)
def test__load_partial__not_found(self):
"""
Check that load_partial provides a nice message when a template is not found.
"""
renderer = Renderer()
renderer.partials = {}
engine = renderer._make_render_engine()
load_partial = engine.load_partial
try:
load_partial("foo")
raise Exception("Shouldn't get here")
except Exception, err:
self.assertEquals(str(err), "Partial not found with name: 'foo'")
## Test the engine's literal attribute.
def test__literal__uses_renderer_unicode(self):
"""
Test that literal uses the renderer's unicode function.
"""
renderer = Renderer()
renderer.unicode = lambda s: s.upper()
engine = renderer._make_render_engine()
literal = engine.literal
self.assertEquals(literal("foo"), "FOO")
def test__literal__handles_unicode(self):
"""
Test that literal doesn't try to "double decode" unicode.
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
engine = renderer._make_render_engine()
literal = engine.literal
self.assertEquals(literal(u"foo"), "foo")
def test__literal__returns_unicode(self):
"""
Test that literal returns unicode (and not a subclass).
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
engine = renderer._make_render_engine()
literal = engine.literal
self.assertEquals(type(literal("foo")), unicode)
class MyUnicode(unicode):
pass
s = MyUnicode("abc")
self.assertEquals(type(s), MyUnicode)
self.assertTrue(isinstance(s, unicode))
self.assertEquals(type(literal(s)), unicode)
## Test the engine's escape attribute.
def test__escape__uses_renderer_escape(self):
"""
Test that escape uses the renderer's escape function.
"""
renderer = Renderer()
renderer.escape = lambda s: "**" + s
engine = renderer._make_render_engine()
escape = engine.escape
self.assertEquals(escape("foo"), "**foo")
def test__escape__uses_renderer_unicode(self):
"""
Test that escape uses the renderer's unicode function.
"""
renderer = Renderer()
renderer.unicode = lambda s: s.upper()
engine = renderer._make_render_engine()
escape = engine.escape
self.assertEquals(escape("foo"), "FOO")
def test__escape__has_access_to_original_unicode_subclass(self):
"""
Test that escape receives strings with the unicode subclass intact.
"""
renderer = Renderer()
renderer.escape = lambda s: type(s).__name__
engine = renderer._make_render_engine()
escape = engine.escape
class MyUnicode(unicode):
pass
self.assertEquals(escape("foo"), "unicode")
self.assertEquals(escape(u"foo"), "unicode")
self.assertEquals(escape(MyUnicode("foo")), "MyUnicode")
def test__escape__returns_unicode(self):
"""
Test that literal returns unicode (and not a subclass).
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
engine = renderer._make_render_engine()
escape = engine.escape
self.assertEquals(type(escape("foo")), unicode)
# Check that literal doesn't preserve unicode subclasses.
class MyUnicode(unicode):
pass
s = MyUnicode("abc")
self.assertEquals(type(s), MyUnicode)
self.assertTrue(isinstance(s, unicode))
self.assertEquals(type(escape(s)), unicode)
|