| 1 |
"""Test the substitute plugin regex functionality.""" |
| 2 |
|
| 3 |
from beets.test.helper import PluginTestHelper |
| 4 |
from beetsplug.substitute import Substitute |
| 5 |
|
| 6 |
|
| 7 |
class TestSubstitutePlugin(PluginTestHelper): |
| 8 |
plugin = "substitute" |
| 9 |
preload_plugin = False |
| 10 |
|
| 11 |
def run_substitute(self, config, cases): |
| 12 |
with self.configure_plugin(config): |
| 13 |
for input_, expected in cases: |
| 14 |
assert Substitute().tmpl_substitute(input_) == expected |
| 15 |
|
| 16 |
def test_simple_substitute(self): |
| 17 |
self.run_substitute( |
| 18 |
{"a": "x", "b": "y", "c": "z"}, [("a", "x"), ("b", "y"), ("c", "z")] |
| 19 |
) |
| 20 |
|
| 21 |
def test_case_insensitivity(self): |
| 22 |
self.run_substitute({"a": "x"}, [("A", "x")]) |
| 23 |
|
| 24 |
def test_unmatched_input_preserved(self): |
| 25 |
self.run_substitute({"a": "x"}, [("c", "c")]) |
| 26 |
|
| 27 |
def test_regex_to_static(self): |
| 28 |
self.run_substitute( |
| 29 |
{".*jimi hendrix.*": "Jimi Hendrix"}, |
| 30 |
[("The Jimi Hendrix Experience", "Jimi Hendrix")], |
| 31 |
) |
| 32 |
|
| 33 |
def test_regex_capture_group(self): |
| 34 |
self.run_substitute( |
| 35 |
{"^(.*?)(,| &| and).*": r"\1"}, |
| 36 |
[ |
| 37 |
("King Creosote & Jon Hopkins", "King Creosote"), |
| 38 |
( |
| 39 |
( |
| 40 |
"Michael Hurley, The Holy Modal Rounders, Jeffrey" |
| 41 |
" Frederick & The Clamtones" |
| 42 |
), |
| 43 |
"Michael Hurley", |
| 44 |
), |
| 45 |
("James Yorkston and the Athletes", "James Yorkston"), |
| 46 |
], |
| 47 |
) |
| 48 |
|
| 49 |
def test_partial_substitution(self): |
| 50 |
self.run_substitute({r"\.": ""}, [("U.N.P.O.C.", "UNPOC")]) |
| 51 |
|
| 52 |
def test_rules_applied_in_definition_order(self): |
| 53 |
self.run_substitute( |
| 54 |
{"a": "x", "[ab]": "y", "b": "z"}, [("a", "x"), ("b", "y")] |
| 55 |
) |
| 56 |
|
| 57 |
def test_rules_applied_in_sequence(self): |
| 58 |
self.run_substitute( |
| 59 |
{"a": "b", "b": "c", "d": "a"}, [("a", "c"), ("b", "c"), ("d", "a")] |
| 60 |
) |