-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevalpython_test.py
More file actions
168 lines (154 loc) · 3.58 KB
/
evalpython_test.py
File metadata and controls
168 lines (154 loc) · 3.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from template import Template
from template.test import TestCase, main
class EvalPythonTest(TestCase):
def testEvalPython(self):
tt_no_python = Template({"INTERPOLATE": 1,
"POST_CHOMP": 1,
"EVAL_PYTHON": 0,
"INCLUDE_PATH": "test/lib"})
tt_do_python = Template({"INTERPOLATE": 1,
"POST_CHOMP": 1,
"EVAL_PYTHON": 1,
"INCLUDE_PATH": "test/lib"})
ttprocs = (("no_python", tt_no_python), ("do_python", tt_do_python))
self.Expect(DATA, ttprocs, self._callsign())
DATA = r"""
-- test --
[% META
author = 'Andy Wardley'
title = 'Test Template $foo #6'
version = 1.23
%]
[% TRY %]
[% PYTHON %]
from __future__ import print_function
output = "author: [% template.author %]\n"
stash.set('a', 'The cat sat on the mat')
output += "more python generated output\n"
print(output, end='')
[% END %]
[% CATCH %]
Not allowed: [% error +%]
[% END %]
a: [% a +%]
a: $a
[% TRY %]
[% RAWPYTHON %]
output.write("The cat sat on the mouse mat\n")
stash.set('b', 'The cat sat where?')
[% END %]
[% CATCH %]
Still not allowed: [% error +%]
[% END %]
b: [% b +%]
b: $b
-- expect --
Not allowed: python error - EVAL_PYTHON not set
a: alpha
a: alpha
Still not allowed: python error - EVAL_PYTHON not set
b: bravo
b: bravo
-- test --
[% TRY %]
nothing
[% PYTHON %]
We don't care about correct syntax within PYTHON blocks if EVAL_PYTHON isn't set.
They're simply ignored.
[% END %]
[% CATCH %]
ERROR: [% error.type %]: [% error.info %]
[% END %]
-- expect --
nothing
ERROR: python: EVAL_PYTHON not set
-- test --
some stuff
[% TRY %]
[% INCLUDE badrawpython %]
[% CATCH %]
ERROR: [[% error.type %]] [% error.info %]
[% END %]
-- expect --
some stuff
This is some text
ERROR: [python] EVAL_PYTHON not set
-- test --
-- use do_python --
some stuff
[% TRY %]
[% INCLUDE badrawpython %]
[% CATCH +%]
ERROR: [[% error.type %]]
[% END %]
-- expect --
some stuff
# This is some text
# more stuff goes here
ERROR: [file]
-- test --
-- use do_python --
[% META author = 'Andy Wardley' %]
[% PYTHON %]
from __future__ import print_function
output = "author: [% template.author %]\n"
stash.set('a', 'The cat sat on the mat')
output += "more python generated output\n"
print(output, end='')
[% END %]
-- expect --
author: Andy Wardley
more python generated output
-- test --
-- use do_python --
[% META
author = 'Andy Wardley'
title = 'Test Template $foo #6'
version = 3.14
%]
[% PYTHON %]
from __future__ import print_function
output = "author: [% template.author %]\n"
stash.set('a', 'The cat sat on the mat')
output += "more python generated output\n"
print(output, end='')
[% END %]
a: [% a +%]
a: $a
[% RAWPYTHON %]
output.write("The cat sat on the mouse mat\n")
stash.set('b', 'The cat sat where?')
[% END %]
b: [% b +%]
b: $b
-- expect --
author: Andy Wardley
more python generated output
a: The cat sat on the mat
a: The cat sat on the mat
The cat sat on the mouse mat
b: The cat sat where?
b: The cat sat where?
-- test --
[% BLOCK foo %]This is block foo[% END %]
[% PYTHON %]
from __future__ import print_function
print(context.include('foo'), end="")
print("\nbar\n", end="")
[% END %]
The end
-- expect --
This is block foo
bar
The end
-- test --
[% TRY %]
[%- PYTHON %] raise Exception("nothing to live for\n") [% END %]
[% CATCH %]
error: [% error %]
[% END %]
-- expect --
error: None error - nothing to live for
"""
if __name__ == '__main__':
main()