+class TestGetReplacements:
+
+ @staticmethod
+ def configure_rules(*variants):
+ rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
+ trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
+ norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
+ return module.configure(rules, norm, trans)
+
+
+ def get_replacements(self, *variants):
+ config = self.configure_rules(*variants)
+
+ return sorted((k, sorted(v)) for k,v in config['replacements'])
+
+
+ @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
+ '~foo~ -> bar', 'fo~ o -> bar'])
+ def test_invalid_variant_description(self, variant):
+ with pytest.raises(UsageError):
+ self.configure_rules(variant)
+
+
+ @pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
+ def test_ignore_unnormalizable_terms(self, rule):
+ repl = self.get_replacements(rule)
+
+ assert repl == []
+
+
+ def test_add_full(self):
+ repl = self.get_replacements("foo -> bar")
+
+ assert repl == [(' foo ', [' bar', ' foo'])]
+
+
+ def test_replace_full(self):
+ repl = self.get_replacements("foo => bar")
+
+ assert repl == [(' foo ', [' bar'])]
+
+
+ def test_add_suffix_no_decompose(self):
+ repl = self.get_replacements("~berg |-> bg")
+
+ assert repl == [(' berg ', [' berg', ' bg']),
+ ('berg ', ['berg', 'bg'])]
+
+
+ def test_replace_suffix_no_decompose(self):
+ repl = self.get_replacements("~berg |=> bg")
+
+ assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
+
+
+ def test_add_suffix_decompose(self):
+ repl = self.get_replacements("~berg -> bg")
+
+ assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
+ ('berg ', [' berg', ' bg', 'berg', 'bg'])]
+
+
+ def test_replace_suffix_decompose(self):
+ repl = self.get_replacements("~berg => bg")
+
+ assert repl == [(' berg ', [' bg', 'bg']),
+ ('berg ', [' bg', 'bg'])]
+
+
+ def test_add_prefix_no_compose(self):
+ repl = self.get_replacements("hinter~ |-> hnt")
+
+ assert repl == [(' hinter', [' hinter', ' hnt']),
+ (' hinter ', [' hinter', ' hnt'])]
+
+
+ def test_replace_prefix_no_compose(self):
+ repl = self.get_replacements("hinter~ |=> hnt")
+
+ assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
+
+
+ def test_add_prefix_compose(self):
+ repl = self.get_replacements("hinter~-> h")
+
+ assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
+ (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
+
+
+ def test_replace_prefix_compose(self):
+ repl = self.get_replacements("hinter~=> h")
+
+ assert repl == [(' hinter', [' h', ' h ']),
+ (' hinter ', [' h', ' h'])]
+
+
+ def test_add_beginning_only(self):
+ repl = self.get_replacements("^Premier -> Pr")
+
+ assert repl == [('^ premier ', ['^ pr', '^ premier'])]
+
+
+ def test_replace_beginning_only(self):
+ repl = self.get_replacements("^Premier => Pr")
+
+ assert repl == [('^ premier ', ['^ pr'])]
+
+
+ def test_add_final_only(self):
+ repl = self.get_replacements("road$ -> rd")
+
+ assert repl == [(' road ^', [' rd ^', ' road ^'])]
+
+
+ def test_replace_final_only(self):
+ repl = self.get_replacements("road$ => rd")
+
+ assert repl == [(' road ^', [' rd ^'])]
+
+
+ def test_decompose_only(self):
+ repl = self.get_replacements("~foo -> foo")
+
+ assert repl == [(' foo ', [' foo', 'foo']),
+ ('foo ', [' foo', 'foo'])]
+
+
+ def test_add_suffix_decompose_end_only(self):
+ repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
+
+ assert repl == [(' berg ', [' berg', ' bg']),
+ (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
+ ('berg ', ['berg', 'bg']),
+ ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
+
+
+ def test_replace_suffix_decompose_end_only(self):
+ repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
+
+ assert repl == [(' berg ', [' bg']),
+ (' berg ^', [' bg ^', 'bg ^']),
+ ('berg ', ['bg']),
+ ('berg ^', [' bg ^', 'bg ^'])]
+
+
+ @pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
+ "~berg, ~burg -> bg",
+ "~berg,,~burg -> bg"])
+ def test_add_multiple_suffix(self, rule):
+ repl = self.get_replacements(rule)