-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmacro_test.py
More file actions
150 lines (130 loc) · 2.58 KB
/
macro_test.py
File metadata and controls
150 lines (130 loc) · 2.58 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
from template.test import TestCase, main
class MacroTest(TestCase):
def testMacro(self):
config = {"INCLUDE_PATH": "test/src",
"TRIM": 1,
"EVAL_PYTHON": True}
self.Expect(DATA, config, self._callsign())
DATA = r"""
-- test --
[% MACRO foo INCLUDE foo -%]
foo: [% foo %]
foo(b): [% foo(a = b) %]
-- expect --
foo: This is the foo file, a is alpha
foo(b): This is the foo file, a is bravo
-- test --
foo: [% foo %].
-- expect --
foo: .
-- test --
[% MACRO foo(a) INCLUDE foo -%]
foo: [% foo %]
foo(c): [% foo(c) %]
-- expect --
foo: This is the foo file, a is
foo(c): This is the foo file, a is charlie
-- test --
[% BLOCK mypage %]
Header
[% content %]
Footer
[% END %]
[%- MACRO content BLOCK -%]
This is a macro which encapsulates a template block.
a: [% a -%]
[% END -%]
begin
[% INCLUDE mypage %]
mid
[% INCLUDE mypage a = 'New Alpha' %]
end
-- expect --
begin
Header
This is a macro which encapsulates a template block.
a: alpha
Footer
mid
Header
This is a macro which encapsulates a template block.
a: New Alpha
Footer
end
-- test --
[% BLOCK table %]
<table>
[% rows %]
</table>
[% END -%]
[% # define some dummy data
udata = [
{ id => 'foo', name => 'Fubar' },
{ id => 'bar', name => 'Babar' }
]
-%]
[% # define a macro to print each row of user data
MACRO user_summary INCLUDE user_row FOREACH user = udata
%]
[% # here's the block for each row
BLOCK user_row %]
<tr>
<td>[% user.id %]</td>
<td>[% user.name %]</td>
</tr>
[% END -%]
[% # now we can call the main table template, and alias our macro to 'rows'
INCLUDE table
rows = user_summary
%]
-- expect --
<table>
<tr>
<td>foo</td>
<td>Fubar</td>
</tr><tr>
<td>bar</td>
<td>Babar</td>
</tr>
</table>
-- test --
[% MACRO one BLOCK -%]
one: [% title %]
[% END -%]
[% saveone = one %]
[% MACRO two BLOCK; title="2[$title]" -%]
two: [% title %] -> [% saveone %]
[% END -%]
[% two(title="The Title") %]
-- expect --
two: 2[The Title] -> one:
-- test --
[% MACRO one BLOCK -%]
one: [% title %]
[% END -%]
[% saveone = \one %]
[% MACRO two BLOCK; title="2[$title]" -%]
two: [% title %] -> [% saveone %]
[% END -%]
[% two(title="The Title") %]
-- expect --
two: 2[The Title] -> one: 2[The Title]
-- test --
-- name number macro --
[% MACRO number(n) GET n.chunk(-3).join(',') -%]
[% number(1234567) %]
-- expect --
1,234,567
-- test --
-- name python macro --
[% MACRO triple(n) PYTHON %]
from __future__ import print_function
n = stash.get('n').value()
print(n * 3)
[% END -%]
[% triple(10) %]
-- expect --
30
"""
if __name__ == '__main__':
main()