-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchomp_test.py
More file actions
175 lines (154 loc) · 4.79 KB
/
chomp_test.py
File metadata and controls
175 lines (154 loc) · 4.79 KB
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
from template import Template
from template.test import TestCase, main
from template.constants import *
foo = "\n[% foo %]\n"
bar = "\n[%- bar -%]\n"
baz = "\n[%+ baz +%]\n"
ding = "!\n\n[%~ ding ~%]\n\n!"
dong = "!\n\n[%= dong =%]\n\n!"
dang = "Hello[%# blah blah blah -%]\n!"
blocks = {"foo": foo, "bar": bar, "baz": baz,
"ding": ding, "dong": dong, "dang": dang}
vars = {'foo': 3.14, 'bar': 2.718, 'baz': 1.618,
'ding': 'Hello', 'dong': 'World'}
class ChompTest(TestCase):
def AssertExpectedOutput(self, tmpl, block, vars, expected):
self.assertEqual(expected, tmpl.process(block, vars))
def testNoChomp(self):
tmpl = Template({"BLOCKS": blocks})
self.AssertExpectedOutput(tmpl, "foo", vars, "\n3.14\n")
self.AssertExpectedOutput(tmpl, "bar", vars, "2.718")
self.AssertExpectedOutput(tmpl, "baz", vars, "\n1.618\n")
self.AssertExpectedOutput(tmpl, "ding", vars, "!Hello!")
self.AssertExpectedOutput(tmpl, "dong", vars, "! World !")
self.AssertExpectedOutput(tmpl, "dang", vars, "Hello!")
def testPreChomp(self):
tmpl = Template({"PRE_CHOMP": 1, "BLOCKS": blocks})
self.AssertExpectedOutput(tmpl, "foo", vars, "3.14\n")
self.AssertExpectedOutput(tmpl, "bar", vars, "2.718")
self.AssertExpectedOutput(tmpl, "baz", vars, "\n1.618\n")
self.AssertExpectedOutput(tmpl, "ding", vars, "!Hello!")
self.AssertExpectedOutput(tmpl, "dong", vars, "! World !")
def testPostChomp(self):
tmpl = Template({'POST_CHOMP': 1, 'BLOCKS': blocks})
self.AssertExpectedOutput(tmpl, "foo", vars, "\n3.14")
self.AssertExpectedOutput(tmpl, "bar", vars, "2.718")
self.AssertExpectedOutput(tmpl, "baz", vars, "\n1.618\n")
self.AssertExpectedOutput(tmpl, "ding", vars, "!Hello!")
self.AssertExpectedOutput(tmpl, "dong", vars, "! World !")
def testChomp(self):
tt = (('tt_pre_none', Template({'PRE_CHOMP': CHOMP_NONE})),
('tt_pre_one', Template({'PRE_CHOMP': CHOMP_ONE})),
('tt_pre_all', Template({'PRE_CHOMP': CHOMP_ALL})),
('tt_pre_coll', Template({'PRE_CHOMP': CHOMP_COLLAPSE})),
('tt_post_none', Template({'POST_CHOMP': CHOMP_NONE})),
('tt_post_one', Template({'POST_CHOMP': CHOMP_ONE})),
('tt_post_all', Template({'POST_CHOMP': CHOMP_ALL})),
('tt_post_coll', Template({'POST_CHOMP': CHOMP_COLLAPSE})))
self.Expect(DATA, tt)
DATA = r"""
#------------------------------------------------------------------------
# tt_pre_none
#------------------------------------------------------------------------
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin
10
20
end
#------------------------------------------------------------------------
# tt_pre_one
#------------------------------------------------------------------------
-- test --
-- use tt_pre_one --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin1020
end
#------------------------------------------------------------------------
# tt_pre_all
#------------------------------------------------------------------------
-- test --
-- use tt_pre_all --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin1020
end
#------------------------------------------------------------------------
# tt_pre_coll
#------------------------------------------------------------------------
-- test --
-- use tt_pre_coll --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin 10 20
end
#------------------------------------------------------------------------
# tt_post_none
#------------------------------------------------------------------------
-- test --
-- use tt_post_none --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin
10
20
end
#------------------------------------------------------------------------
# tt_post_all
#------------------------------------------------------------------------
-- test --
-- use tt_post_all --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin 10 20end
#------------------------------------------------------------------------
# tt_post_one
#------------------------------------------------------------------------
-- test --
-- use tt_post_one --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin 10 20end
#------------------------------------------------------------------------
# tt_post_coll
#------------------------------------------------------------------------
-- test --
-- use tt_post_coll --
-- test --
begin[% a = 10; b = 20 %]
[% a %]
[% b %]
end
-- expect --
begin 10 20 end
"""
if __name__ == '__main__':
main()