-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
test_template.py
276 lines (217 loc) · 9.32 KB
/
test_template.py
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
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for template engine."""
import unittest
from beets.util import functemplate
def _normexpr(expr):
"""Normalize an Expression object's parts, collapsing multiple
adjacent text blocks and removing empty text blocks. Generates a
sequence of parts.
"""
textbuf = []
for part in expr.parts:
if isinstance(part, str):
textbuf.append(part)
else:
if textbuf:
text = "".join(textbuf)
if text:
yield text
textbuf = []
yield part
if textbuf:
text = "".join(textbuf)
if text:
yield text
def _normparse(text):
"""Parse a template and then normalize the resulting Expression."""
return _normexpr(functemplate._parse(text))
class ParseTest(unittest.TestCase):
def test_empty_string(self):
assert list(_normparse("")) == []
def _assert_symbol(self, obj, ident):
"""Assert that an object is a Symbol with the given identifier."""
assert isinstance(obj, functemplate.Symbol), f"not a Symbol: {obj}"
assert obj.ident == ident, f"wrong identifier: {obj.ident} vs. {ident}"
def _assert_call(self, obj, ident, numargs):
"""Assert that an object is a Call with the given identifier and
argument count.
"""
assert isinstance(obj, functemplate.Call), f"not a Call: {obj}"
assert obj.ident == ident, f"wrong identifier: {obj.ident} vs. {ident}"
assert (
len(obj.args) == numargs
), f"wrong argument count in {obj.ident}: {len(obj.args)} vs. {numargs}"
def test_plain_text(self):
assert list(_normparse("hello world")) == ["hello world"]
def test_escaped_character_only(self):
assert list(_normparse("$$")) == ["$"]
def test_escaped_character_in_text(self):
assert list(_normparse("a $$ b")) == ["a $ b"]
def test_escaped_character_at_start(self):
assert list(_normparse("$$ hello")) == ["$ hello"]
def test_escaped_character_at_end(self):
assert list(_normparse("hello $$")) == ["hello $"]
def test_escaped_function_delim(self):
assert list(_normparse("a $% b")) == ["a % b"]
def test_escaped_sep(self):
assert list(_normparse("a $, b")) == ["a , b"]
def test_escaped_close_brace(self):
assert list(_normparse("a $} b")) == ["a } b"]
def test_bare_value_delim_kept_intact(self):
assert list(_normparse("a $ b")) == ["a $ b"]
def test_bare_function_delim_kept_intact(self):
assert list(_normparse("a % b")) == ["a % b"]
def test_bare_opener_kept_intact(self):
assert list(_normparse("a { b")) == ["a { b"]
def test_bare_closer_kept_intact(self):
assert list(_normparse("a } b")) == ["a } b"]
def test_bare_sep_kept_intact(self):
assert list(_normparse("a , b")) == ["a , b"]
def test_symbol_alone(self):
parts = list(_normparse("$foo"))
assert len(parts) == 1
self._assert_symbol(parts[0], "foo")
def test_symbol_in_text(self):
parts = list(_normparse("hello $foo world"))
assert len(parts) == 3
assert parts[0] == "hello "
self._assert_symbol(parts[1], "foo")
assert parts[2] == " world"
def test_symbol_with_braces(self):
parts = list(_normparse("hello${foo}world"))
assert len(parts) == 3
assert parts[0] == "hello"
self._assert_symbol(parts[1], "foo")
assert parts[2] == "world"
def test_unclosed_braces_symbol(self):
assert list(_normparse("a ${ b")) == ["a ${ b"]
def test_empty_braces_symbol(self):
assert list(_normparse("a ${} b")) == ["a ${} b"]
def test_call_without_args_at_end(self):
assert list(_normparse("foo %bar")) == ["foo %bar"]
def test_call_without_args(self):
assert list(_normparse("foo %bar baz")) == ["foo %bar baz"]
def test_call_with_unclosed_args(self):
assert list(_normparse("foo %bar{ baz")) == ["foo %bar{ baz"]
def test_call_with_unclosed_multiple_args(self):
assert list(_normparse("foo %bar{bar,bar baz")) == [
"foo %bar{bar,bar baz"
]
def test_call_empty_arg(self):
parts = list(_normparse("%foo{}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 1)
assert list(_normexpr(parts[0].args[0])) == []
def test_call_single_arg(self):
parts = list(_normparse("%foo{bar}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 1)
assert list(_normexpr(parts[0].args[0])) == ["bar"]
def test_call_two_args(self):
parts = list(_normparse("%foo{bar,baz}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 2)
assert list(_normexpr(parts[0].args[0])) == ["bar"]
assert list(_normexpr(parts[0].args[1])) == ["baz"]
def test_call_with_escaped_sep(self):
parts = list(_normparse("%foo{bar$,baz}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 1)
assert list(_normexpr(parts[0].args[0])) == ["bar,baz"]
def test_call_with_escaped_close(self):
parts = list(_normparse("%foo{bar$}baz}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 1)
assert list(_normexpr(parts[0].args[0])) == ["bar}baz"]
def test_call_with_symbol_argument(self):
parts = list(_normparse("%foo{$bar,baz}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 2)
arg_parts = list(_normexpr(parts[0].args[0]))
assert len(arg_parts) == 1
self._assert_symbol(arg_parts[0], "bar")
assert list(_normexpr(parts[0].args[1])) == ["baz"]
def test_call_with_nested_call_argument(self):
parts = list(_normparse("%foo{%bar{},baz}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 2)
arg_parts = list(_normexpr(parts[0].args[0]))
assert len(arg_parts) == 1
self._assert_call(arg_parts[0], "bar", 1)
assert list(_normexpr(parts[0].args[1])) == ["baz"]
def test_nested_call_with_argument(self):
parts = list(_normparse("%foo{%bar{baz}}"))
assert len(parts) == 1
self._assert_call(parts[0], "foo", 1)
arg_parts = list(_normexpr(parts[0].args[0]))
assert len(arg_parts) == 1
self._assert_call(arg_parts[0], "bar", 1)
assert list(_normexpr(arg_parts[0].args[0])) == ["baz"]
def test_sep_before_call_two_args(self):
parts = list(_normparse("hello, %foo{bar,baz}"))
assert len(parts) == 2
assert parts[0] == "hello, "
self._assert_call(parts[1], "foo", 2)
assert list(_normexpr(parts[1].args[0])) == ["bar"]
assert list(_normexpr(parts[1].args[1])) == ["baz"]
def test_sep_with_symbols(self):
parts = list(_normparse("hello,$foo,$bar"))
assert len(parts) == 4
assert parts[0] == "hello,"
self._assert_symbol(parts[1], "foo")
assert parts[2] == ","
self._assert_symbol(parts[3], "bar")
def test_newline_at_end(self):
parts = list(_normparse("foo\n"))
assert len(parts) == 1
assert parts[0] == "foo\n"
class EvalTest(unittest.TestCase):
def _eval(self, template):
values = {
"foo": "bar",
"baz": "BaR",
}
functions = {
"lower": str.lower,
"len": len,
}
return functemplate.Template(template).substitute(values, functions)
def test_plain_text(self):
assert self._eval("foo") == "foo"
def test_subtitute_value(self):
assert self._eval("$foo") == "bar"
def test_subtitute_value_in_text(self):
assert self._eval("hello $foo world") == "hello bar world"
def test_not_subtitute_undefined_value(self):
assert self._eval("$bar") == "$bar"
def test_function_call(self):
assert self._eval("%lower{FOO}") == "foo"
def test_function_call_with_text(self):
assert self._eval("A %lower{FOO} B") == "A foo B"
def test_nested_function_call(self):
assert self._eval("%lower{%lower{FOO}}") == "foo"
def test_symbol_in_argument(self):
assert self._eval("%lower{$baz}") == "bar"
def test_function_call_exception(self):
res = self._eval("%lower{a,b,c,d,e}")
assert isinstance(res, str)
def test_function_returning_integer(self):
assert self._eval("%len{foo}") == "3"
def test_not_subtitute_undefined_func(self):
assert self._eval("%bar{}") == "%bar{}"
def test_not_subtitute_func_with_no_args(self):
assert self._eval("%lower") == "%lower"
def test_function_call_with_empty_arg(self):
assert self._eval("%len{}") == "0"