| 1 |
"""Tests for template engine.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from beets.util import functemplate |
| 6 |
|
| 7 |
|
| 8 |
def _normexpr(expr): |
| 9 |
"""Normalize an Expression object's parts, collapsing multiple |
| 10 |
adjacent text blocks and removing empty text blocks. Generates a |
| 11 |
sequence of parts. |
| 12 |
""" |
| 13 |
textbuf = [] |
| 14 |
for part in expr.parts: |
| 15 |
if isinstance(part, str): |
| 16 |
textbuf.append(part) |
| 17 |
else: |
| 18 |
if textbuf: |
| 19 |
text = "".join(textbuf) |
| 20 |
if text: |
| 21 |
yield text |
| 22 |
textbuf = [] |
| 23 |
yield part |
| 24 |
if textbuf: |
| 25 |
text = "".join(textbuf) |
| 26 |
if text: |
| 27 |
yield text |
| 28 |
|
| 29 |
|
| 30 |
def _normparse(text): |
| 31 |
"""Parse a template and then normalize the resulting Expression.""" |
| 32 |
return _normexpr(functemplate._parse(text)) |
| 33 |
|
| 34 |
|
| 35 |
class ParseTest(unittest.TestCase): |
| 36 |
def test_empty_string(self): |
| 37 |
assert list(_normparse("")) == [] |
| 38 |
|
| 39 |
def _assert_symbol(self, obj, ident): |
| 40 |
"""Assert that an object is a Symbol with the given identifier.""" |
| 41 |
assert isinstance(obj, functemplate.Symbol), f"not a Symbol: {obj}" |
| 42 |
assert obj.ident == ident, f"wrong identifier: {obj.ident} vs. {ident}" |
| 43 |
|
| 44 |
def _assert_call(self, obj, ident, numargs): |
| 45 |
"""Assert that an object is a Call with the given identifier and |
| 46 |
argument count. |
| 47 |
""" |
| 48 |
assert isinstance(obj, functemplate.Call), f"not a Call: {obj}" |
| 49 |
assert obj.ident == ident, f"wrong identifier: {obj.ident} vs. {ident}" |
| 50 |
assert len(obj.args) == numargs, ( |
| 51 |
f"wrong argument count in {obj.ident}: {len(obj.args)} vs. {numargs}" |
| 52 |
) |
| 53 |
|
| 54 |
def test_plain_text(self): |
| 55 |
assert list(_normparse("hello world")) == ["hello world"] |
| 56 |
|
| 57 |
def test_escaped_character_only(self): |
| 58 |
assert list(_normparse("$$")) == ["$"] |
| 59 |
|
| 60 |
def test_escaped_character_in_text(self): |
| 61 |
assert list(_normparse("a $$ b")) == ["a $ b"] |
| 62 |
|
| 63 |
def test_escaped_character_at_start(self): |
| 64 |
assert list(_normparse("$$ hello")) == ["$ hello"] |
| 65 |
|
| 66 |
def test_escaped_character_at_end(self): |
| 67 |
assert list(_normparse("hello $$")) == ["hello $"] |
| 68 |
|
| 69 |
def test_escaped_function_delim(self): |
| 70 |
assert list(_normparse("a $% b")) == ["a % b"] |
| 71 |
|
| 72 |
def test_escaped_sep(self): |
| 73 |
assert list(_normparse("a $, b")) == ["a , b"] |
| 74 |
|
| 75 |
def test_escaped_close_brace(self): |
| 76 |
assert list(_normparse("a $} b")) == ["a } b"] |
| 77 |
|
| 78 |
def test_bare_value_delim_kept_intact(self): |
| 79 |
assert list(_normparse("a $ b")) == ["a $ b"] |
| 80 |
|
| 81 |
def test_bare_function_delim_kept_intact(self): |
| 82 |
assert list(_normparse("a % b")) == ["a % b"] |
| 83 |
|
| 84 |
def test_bare_opener_kept_intact(self): |
| 85 |
assert list(_normparse("a { b")) == ["a { b"] |
| 86 |
|
| 87 |
def test_bare_closer_kept_intact(self): |
| 88 |
assert list(_normparse("a } b")) == ["a } b"] |
| 89 |
|
| 90 |
def test_bare_sep_kept_intact(self): |
| 91 |
assert list(_normparse("a , b")) == ["a , b"] |
| 92 |
|
| 93 |
def test_symbol_alone(self): |
| 94 |
parts = list(_normparse("$foo")) |
| 95 |
assert len(parts) == 1 |
| 96 |
self._assert_symbol(parts[0], "foo") |
| 97 |
|
| 98 |
def test_symbol_in_text(self): |
| 99 |
parts = list(_normparse("hello $foo world")) |
| 100 |
assert len(parts) == 3 |
| 101 |
assert parts[0] == "hello " |
| 102 |
self._assert_symbol(parts[1], "foo") |
| 103 |
assert parts[2] == " world" |
| 104 |
|
| 105 |
def test_symbol_with_braces(self): |
| 106 |
parts = list(_normparse("hello${foo}world")) |
| 107 |
assert len(parts) == 3 |
| 108 |
assert parts[0] == "hello" |
| 109 |
self._assert_symbol(parts[1], "foo") |
| 110 |
assert parts[2] == "world" |
| 111 |
|
| 112 |
def test_unclosed_braces_symbol(self): |
| 113 |
assert list(_normparse("a ${ b")) == ["a ${ b"] |
| 114 |
|
| 115 |
def test_empty_braces_symbol(self): |
| 116 |
assert list(_normparse("a ${} b")) == ["a ${} b"] |
| 117 |
|
| 118 |
def test_call_without_args_at_end(self): |
| 119 |
assert list(_normparse("foo %bar")) == ["foo %bar"] |
| 120 |
|
| 121 |
def test_call_without_args(self): |
| 122 |
assert list(_normparse("foo %bar baz")) == ["foo %bar baz"] |
| 123 |
|
| 124 |
def test_call_with_unclosed_args(self): |
| 125 |
assert list(_normparse("foo %bar{ baz")) == ["foo %bar{ baz"] |
| 126 |
|
| 127 |
def test_call_with_unclosed_multiple_args(self): |
| 128 |
assert list(_normparse("foo %bar{bar,bar baz")) == [ |
| 129 |
"foo %bar{bar,bar baz" |
| 130 |
] |
| 131 |
|
| 132 |
def test_call_empty_arg(self): |
| 133 |
parts = list(_normparse("%foo{}")) |
| 134 |
assert len(parts) == 1 |
| 135 |
self._assert_call(parts[0], "foo", 1) |
| 136 |
assert list(_normexpr(parts[0].args[0])) == [] |
| 137 |
|
| 138 |
def test_call_single_arg(self): |
| 139 |
parts = list(_normparse("%foo{bar}")) |
| 140 |
assert len(parts) == 1 |
| 141 |
self._assert_call(parts[0], "foo", 1) |
| 142 |
assert list(_normexpr(parts[0].args[0])) == ["bar"] |
| 143 |
|
| 144 |
def test_call_two_args(self): |
| 145 |
parts = list(_normparse("%foo{bar,baz}")) |
| 146 |
assert len(parts) == 1 |
| 147 |
self._assert_call(parts[0], "foo", 2) |
| 148 |
assert list(_normexpr(parts[0].args[0])) == ["bar"] |
| 149 |
assert list(_normexpr(parts[0].args[1])) == ["baz"] |
| 150 |
|
| 151 |
def test_call_with_escaped_sep(self): |
| 152 |
parts = list(_normparse("%foo{bar$,baz}")) |
| 153 |
assert len(parts) == 1 |
| 154 |
self._assert_call(parts[0], "foo", 1) |
| 155 |
assert list(_normexpr(parts[0].args[0])) == ["bar,baz"] |
| 156 |
|
| 157 |
def test_call_with_escaped_close(self): |
| 158 |
parts = list(_normparse("%foo{bar$}baz}")) |
| 159 |
assert len(parts) == 1 |
| 160 |
self._assert_call(parts[0], "foo", 1) |
| 161 |
assert list(_normexpr(parts[0].args[0])) == ["bar}baz"] |
| 162 |
|
| 163 |
def test_call_with_symbol_argument(self): |
| 164 |
parts = list(_normparse("%foo{$bar,baz}")) |
| 165 |
assert len(parts) == 1 |
| 166 |
self._assert_call(parts[0], "foo", 2) |
| 167 |
arg_parts = list(_normexpr(parts[0].args[0])) |
| 168 |
assert len(arg_parts) == 1 |
| 169 |
self._assert_symbol(arg_parts[0], "bar") |
| 170 |
assert list(_normexpr(parts[0].args[1])) == ["baz"] |
| 171 |
|
| 172 |
def test_call_with_nested_call_argument(self): |
| 173 |
parts = list(_normparse("%foo{%bar{},baz}")) |
| 174 |
assert len(parts) == 1 |
| 175 |
self._assert_call(parts[0], "foo", 2) |
| 176 |
arg_parts = list(_normexpr(parts[0].args[0])) |
| 177 |
assert len(arg_parts) == 1 |
| 178 |
self._assert_call(arg_parts[0], "bar", 1) |
| 179 |
assert list(_normexpr(parts[0].args[1])) == ["baz"] |
| 180 |
|
| 181 |
def test_nested_call_with_argument(self): |
| 182 |
parts = list(_normparse("%foo{%bar{baz}}")) |
| 183 |
assert len(parts) == 1 |
| 184 |
self._assert_call(parts[0], "foo", 1) |
| 185 |
arg_parts = list(_normexpr(parts[0].args[0])) |
| 186 |
assert len(arg_parts) == 1 |
| 187 |
self._assert_call(arg_parts[0], "bar", 1) |
| 188 |
assert list(_normexpr(arg_parts[0].args[0])) == ["baz"] |
| 189 |
|
| 190 |
def test_sep_before_call_two_args(self): |
| 191 |
parts = list(_normparse("hello, %foo{bar,baz}")) |
| 192 |
assert len(parts) == 2 |
| 193 |
assert parts[0] == "hello, " |
| 194 |
self._assert_call(parts[1], "foo", 2) |
| 195 |
assert list(_normexpr(parts[1].args[0])) == ["bar"] |
| 196 |
assert list(_normexpr(parts[1].args[1])) == ["baz"] |
| 197 |
|
| 198 |
def test_sep_with_symbols(self): |
| 199 |
parts = list(_normparse("hello,$foo,$bar")) |
| 200 |
assert len(parts) == 4 |
| 201 |
assert parts[0] == "hello," |
| 202 |
self._assert_symbol(parts[1], "foo") |
| 203 |
assert parts[2] == "," |
| 204 |
self._assert_symbol(parts[3], "bar") |
| 205 |
|
| 206 |
def test_newline_at_end(self): |
| 207 |
parts = list(_normparse("foo\n")) |
| 208 |
assert len(parts) == 1 |
| 209 |
assert parts[0] == "foo\n" |
| 210 |
|
| 211 |
|
| 212 |
class EvalTest(unittest.TestCase): |
| 213 |
def _eval(self, template): |
| 214 |
values = {"foo": "bar", "baz": "BaR"} |
| 215 |
functions = {"lower": str.lower, "len": len} |
| 216 |
return functemplate.Template(template).substitute(values, functions) |
| 217 |
|
| 218 |
def test_plain_text(self): |
| 219 |
assert self._eval("foo") == "foo" |
| 220 |
|
| 221 |
def test_subtitute_value(self): |
| 222 |
assert self._eval("$foo") == "bar" |
| 223 |
|
| 224 |
def test_subtitute_value_in_text(self): |
| 225 |
assert self._eval("hello $foo world") == "hello bar world" |
| 226 |
|
| 227 |
def test_not_subtitute_undefined_value(self): |
| 228 |
assert self._eval("$bar") == "$bar" |
| 229 |
|
| 230 |
def test_function_call(self): |
| 231 |
assert self._eval("%lower{FOO}") == "foo" |
| 232 |
|
| 233 |
def test_function_call_with_text(self): |
| 234 |
assert self._eval("A %lower{FOO} B") == "A foo B" |
| 235 |
|
| 236 |
def test_nested_function_call(self): |
| 237 |
assert self._eval("%lower{%lower{FOO}}") == "foo" |
| 238 |
|
| 239 |
def test_symbol_in_argument(self): |
| 240 |
assert self._eval("%lower{$baz}") == "bar" |
| 241 |
|
| 242 |
def test_function_call_exception(self): |
| 243 |
res = self._eval("%lower{a,b,c,d,e}") |
| 244 |
assert isinstance(res, str) |
| 245 |
|
| 246 |
def test_function_returning_integer(self): |
| 247 |
assert self._eval("%len{foo}") == "3" |
| 248 |
|
| 249 |
def test_not_subtitute_undefined_func(self): |
| 250 |
assert self._eval("%bar{}") == "%bar{}" |
| 251 |
|
| 252 |
def test_not_subtitute_func_with_no_args(self): |
| 253 |
assert self._eval("%lower") == "%lower" |
| 254 |
|
| 255 |
def test_function_call_with_empty_arg(self): |
| 256 |
assert self._eval("%len{}") == "0" |