def test_Pattern_as_phones_indexes_Category_if_subscript_in_catixes(): pattern = Pattern([ Category(cats.Category(['a', 'b', 'c']), 1), Category(cats.Category(['d', 'e', 'f']), 2), Category(cats.Category(['g', 'h', 'i']), 1), ]) assert pattern.as_phones('', {1: 2, 2: 0}) == ['c', 'd', 'i']
def test_Pattern_as_phones_raises_ValueError_if_Category_subscript_not_in_catixes( ): pattern = Pattern([ Category(cats.Category(['a', 'b', 'c']), 1), Category(cats.Category(['d', 'e', 'f']), 2), Category(cats.Category(['g', 'h', 'i']), 1), ]) with raises(ValueError): pattern.as_phones('', {1: 2})
def test_LocalEnvironment_passes_catixes_from_left_into_right(word): lenv = LocalEnvironment( left=patterns.Pattern([patterns.Category(cats.Category(['a', 'b']), 1)]), right=patterns.Pattern([patterns.Category(cats.Category(['a', 'c']), 1)]) ) assert lenv.match(word, slice(1, 2), {}) lenv = LocalEnvironment( left=patterns.Pattern([patterns.Category(cats.Category(['a', 'b']), 1)]), right=patterns.Pattern([patterns.Category(cats.Category(['c', 'a']), 1)]) ) assert not lenv.match(word, slice(1, 2), {})
def test_Repetition_uses_catixes_from_self_match_in_pattern_match(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'a', 'b'] assert Repetition(subpattern, number=2).match_pattern(pattern, word, start=0) == (3, { 1: 0 }) word = ['a', 'a', 'c'] with raises(MatchFailed): Repetition(subpattern, number=2).match_pattern(pattern, word, start=0)
def test_Optional_uses_catixes_from_self_match_in_pattern_match(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'b'] assert Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (2, { 1: 0 }) word = ['a', 'c'] with raises(MatchFailed): Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0)
def test_Optional_adds_catixes_from_self_match_if_self_matches(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = MockPattern(length=1) word = ['a', '#'] assert Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (2, { 1: 0 }) assert Optional(subpattern, greedy=False).match_pattern(pattern, word, start=0) == (1, {}) pattern = Pattern([Grapheme('#')]) assert Optional(subpattern, greedy=False).match_pattern(pattern, word, start=0) == (2, { 1: 0 }) word = ['#'] assert Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (1, {}) assert Optional(subpattern, greedy=False).match_pattern(pattern, word, start=0) == (1, {})
def test_WildcardMixin_uses_catixes_from_self_match_in_pattern_match(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'a', 'b'] assert WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (3, { 1: 0 }) word = ['a', 'a', 'c'] with raises(MatchFailed): WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0)
def test_Category_without_subscript_matches_any_grapheme_it_contains(): word = ['a', 'b', 'c', 'd'] category = Category(cats.Category(['a', 'b']), None) assert category.match(word, start=0, catixes={1: 2}) == (1, {1: 2}) assert category.match(word, start=1, catixes={1: 2}) == (1, {1: 2}) with raises(MatchFailed): category.match(word, start=2, catixes={1: 2}) with raises(MatchFailed): category.match(word, start=3, catixes={1: 2})
def test_Repetition_adds_catixes_from_self_match(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = MockPattern(length=1) word = ['a', '#'] assert Repetition(subpattern, number=1).match_pattern(pattern, word, start=0) == (2, { 1: 0 })
def test_Repetition_adds_catixes_from_pattern_match(): subpattern = Pattern([Grapheme('a')]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'a', 'b'] assert Repetition(subpattern, number=2).match_pattern(pattern, word, start=0) == (3, { 1: 0 })
def test_SubstPredicate_get_changes_returns_match_with_replacement(word): pattern = Pattern([patterns.TargetRef(1), patterns.Category(cats.Category(['b', 'c']), 1)]) assert SubstPredicate([pattern], [], []).get_changes(word, slice(1, 2), {1: 1}, 0) == [(slice(1, 2), ['a', 'c'])] replacements = [ Pattern([patterns.Grapheme('a')]), Pattern([patterns.Grapheme('b')]), Pattern([patterns.Grapheme('c')]), ] assert SubstPredicate(replacements, [], []).get_changes(word, slice(1, 2), {}, 5) == [(slice(1, 2), ['c'])]
def test_Category_with_subscript_in_catixes_only_matches_indexed_grapheme(): word = ['a', 'b', 'c', 'd'] category = Category(cats.Category(['a', 'b', 'c', 'd']), 1) with raises(MatchFailed): category.match(word, start=0, catixes={1: 2}) with raises(MatchFailed): category.match(word, start=1, catixes={1: 2}) assert category.match(word, start=2, catixes={1: 2}) == (1, {1: 2}) with raises(MatchFailed): category.match(word, start=3, catixes={1: 2})
def test_Category_with_subscript_not_in_catixes_matches_any_grapheme_it_contains_and_adds_subscript_to_catixes( ): word = ['a', 'b', 'c', 'd'] category = Category(cats.Category(['a', 'b']), 0) assert category.match(word, start=0, catixes={1: 2}) == (1, {0: 0, 1: 2}) assert category.match(word, start=1, catixes={1: 2}) == (1, {0: 1, 1: 2}) with raises(MatchFailed): category.match(word, start=2, catixes={1: 2}) with raises(MatchFailed): category.match(word, start=3, catixes={1: 2})
def test_WildcardMixin_adds_catixes_from_self_match(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = MockPattern(length=1) word = ['a', '#'] assert WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (2, { 1: 0 })
def test_WildcardMixin_adds_catixes_from_pattern_match(): subpattern = Pattern([Grapheme('a')]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'a', 'b'] assert WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (3, { 1: 0 })
def test_Pattern_as_phones_Repetition_repeats_internal_pattern(): pattern = Pattern([Repetition(Pattern([Grapheme('a')]), 3)]) assert pattern.as_phones('') == ['a', 'a', 'a'] pattern = Pattern([Repetition(Pattern([Ditto(), Grapheme('b')]), 2)]) assert pattern.as_phones('a') == ['a', 'b', 'b', 'b'] pattern = Pattern([ Repetition(Pattern([Category(cats.Category(['a', 'b', 'c']), 1)]), 3) ]) assert pattern.as_phones('', {1: 2}) == ['c', 'c', 'c']
def test_Repetition_uses_same_catixes_for_all_iterations(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = MockPattern(length=1) word = ['a', 'a', '#'] assert Repetition(subpattern, number=2).match_pattern(pattern, word, start=0) == (3, { 1: 0 }) word = ['a', 'b', '#'] with raises(MatchFailed): Repetition(subpattern, number=2).match_pattern(pattern, word, start=0)
def test_Optional_adds_catixes_from_pattern_match(): subpattern = Pattern([Grapheme('a')]) pattern = Pattern([Category(cats.Category(['b', 'c']), 1)]) word = ['a', 'b'] assert Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (2, { 1: 0 }) word = ['b'] assert Optional(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (1, { 1: 0 })
def test_WildcardMixin_uses_same_catixes_for_multiple_iterations(): subpattern = Pattern([Category(cats.Category(['a', 'b']), 1)]) pattern = MockPattern(length=1) word = ['a', 'a', '#'] assert WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (3, { 1: 0 }) word = ['a', 'b', '#'] assert WildcardRepetition(subpattern, greedy=True).match_pattern(pattern, word, start=0) == (2, { 1: 0 })
def test_SubstPredicate_get_replacement_converts_indexed_replacement_to_list_str(word): pattern = Pattern([patterns.TargetRef(1), patterns.Category(cats.Category(['b', 'c']), 1)]) assert SubstPredicate([pattern], [], []).get_replacement(word, slice(1, 2), {1: 1}, 0) == ['a', 'c']