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
|
import unittest
import pystache
from examples.simple import Simple
from examples.complex_view import ComplexView
from examples.lambdas import Lambdas
class TestView(unittest.TestCase):
def test_basic(self):
view = Simple("Hi {{thing}}!", { 'thing': 'world' })
self.assertEquals(view.render(), "Hi world!")
def test_kwargs(self):
view = Simple("Hi {{thing}}!", thing='world')
self.assertEquals(view.render(), "Hi world!")
def test_template_load(self):
view = Simple(thing='world')
self.assertEquals(view.render(), "Hi world!")
def test_template_load_from_multiple_path(self):
path = Simple.template_path
Simple.template_path = ('examples/nowhere','examples',)
try:
view = Simple(thing='world')
self.assertEquals(view.render(), "Hi world!")
finally:
Simple.template_path = path
def test_template_load_from_multiple_path_fail(self):
path = Simple.template_path
Simple.template_path = ('examples/nowhere',)
try:
view = Simple(thing='world')
self.assertRaises(IOError, view.render)
finally:
Simple.template_path = path
def test_basic_method_calls(self):
view = Simple()
self.assertEquals(view.render(), "Hi pizza!")
def test_non_callable_attributes(self):
view = Simple()
view.thing = 'Chris'
self.assertEquals(view.render(), "Hi Chris!")
def test_view_instances_as_attributes(self):
other = Simple(name='chris')
other.template = '{{name}}'
view = Simple()
view.thing = other
self.assertEquals(view.render(), "Hi chris!")
def test_complex(self):
self.assertEquals(ComplexView().render(), """<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>\n \n <li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>
""")
def test_higher_order_replace(self):
view = Lambdas()
self.assertEquals(view.render(),
'bar != bar. oh, it does!')
def test_higher_order_rot13(self):
view = Lambdas()
view.template = '{{#rot13}}abcdefghijklm{{/rot13}}'
self.assertEquals(view.render(), 'nopqrstuvwxyz')
def test_higher_order_lambda(self):
view = Lambdas()
view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}'
self.assertEquals(view.render(), 'abcdefghijklmnopqrstuvwxyz')
if __name__ == '__main__':
unittest.main()
|