Exemplo n.º 1
0
 def test_alternative_with_empty_match(self):
     self._test_match(
         text='abcabca',
         match=RepeatMatch(
             mark_parens(
                 AlternativesMatch(
                     LiteralMatch('abc'),
                     InsertLiteralMatch(','),
                 ),
             ),
             0, None),
         expected_solutions=[
             {'matched_text': '(abc)(abc)(,)', 'position': 6},
             {'matched_text': '(abc)(abc)', 'position': 6},
             {'matched_text': '(abc)(,)(abc)(,)', 'position': 6},
             {'matched_text': '(abc)(,)(abc)', 'position': 6},
             {'matched_text': '(abc)(,)', 'position': 3},
             {'matched_text': '(abc)', 'position': 3},
             {'matched_text': '(,)(abc)(abc)(,)', 'position': 6},
             {'matched_text': '(,)(abc)(abc)', 'position': 6},
             {'matched_text': '(,)(abc)(,)(abc)(,)', 'position': 6},
             {'matched_text': '(,)(abc)(,)(abc)', 'position': 6},
             {'matched_text': '(,)(abc)(,)', 'position': 3},
             {'matched_text': '(,)(abc)', 'position': 3},
             {'matched_text': '(,)', 'position': 0},
             {'matched_text': '', 'position': 0},
         ])
Exemplo n.º 2
0
 def test_anchored_by_end_anchor_match(self):
     self._test_unique_match(
         text='Some text',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             EndAnchorMatch(),
         ),
         expected_solution={'matched_text': '(Some text)', 'position': 9})
Exemplo n.º 3
0
 def test_result_includes_text_not_covered_by_matches(self):
     self._test_rule(
         text='abc 123',
         rule=Rule(
             mark_parens(LiteralMatch('ab')),
         ),
         expected_text='(ab)c 123'
     )
Exemplo n.º 4
0
 def test_only_first_between_match_in_sequence_expands(self):
     self._test_unique_match(
         text='Some text',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             mark_braces(BetweenMatch()),
             mark_curlies(BetweenMatch()),
             EndAnchorMatch(),
         ),
         expected_solution={'matched_text': '(Some text)[]{}', 'position': 9})
Exemplo n.º 5
0
 def test_between_match_in_search_at_end(self):
     self._test_unique_match(
         text='abracadabra',
         match=SearchReplaceMatch(
             SequenceMatch(
                 delete_action(LiteralMatch('r')),
                 mark_parens(BetweenMatch()),
             ),
         ),
         expected_solution={'text': 'ab(acadab)(a)', 'matched_text': '', 'position': 0})
Exemplo n.º 6
0
 def test_repeated_between_match(self):
     self._test_match(
         text='Some text',
         match=SequenceMatch(
             RepeatMatch(mark_parens(BetweenMatch()), 0, None),
             EndAnchorMatch(),
         ),
         expected_solutions=[
             {'matched_text': '(Some text)()', 'position': 9},
             {'matched_text': '(Some text)', 'position': 9},
         ])
Exemplo n.º 7
0
 def test_starts_with_between_match(self):
     self._test_rule(
         text='abc 123',
         rule=Rule(
             SequenceMatch(
                 mark_braces(BetweenMatch()),
                 mark_parens(LiteralMatch('c')),
             ),
         ),
         expected_text='[ab](c) 123'
     )
Exemplo n.º 8
0
 def test_ends_with_between_match(self):
     self._test_rule(
         text='abc 123',
         rule=Rule(
             SequenceMatch(
                 mark_parens(LiteralMatch('ab')),
                 mark_braces(BetweenMatch()),
             ),
         ),
         expected_text='(ab)[c 123]'
     )
Exemplo n.º 9
0
 def test_insert_matches_are_not_anchors(self):
     self._test_unique_match(
         text='Some text',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             InsertLiteralMatch('1'),
             mark_braces(BetweenMatch()),
             InsertLiteralMatch('2'),
             mark_curlies(BetweenMatch()),
             EndAnchorMatch(),
         ),
         expected_solution={'matched_text': '(Some text)1[]2{}', 'position': 9})
Exemplo n.º 10
0
 def test_no_match(self):
     self._test_rule(
         text='abc 123',
         rule=Rule(
             SequenceMatch(
                 mark_parens(LiteralMatch('abc')),
                 delete_action(FormatMatch('ws')),
                 mark_braces(LiteralMatch('x')),
             ),
         ),
         expected_text='abc 123'
     )
Exemplo n.º 11
0
 def test_pattern_matches_are_anchors(self):
     self._test_unique_match(
         text='abcdefghi',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             LiteralMatch('d'),
             mark_braces(BetweenMatch()),
             LiteralMatch('g'),
             mark_curlies(BetweenMatch()),
             EndAnchorMatch(),
         ),
         expected_solution={'matched_text': '(abc)d[ef]g{hi}', 'position': 9})
Exemplo n.º 12
0
 def test_between_match_before_search_replace(self):
     self._test_match(
         text='abracadabra',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             SearchReplaceMatch(mark_braces(LiteralMatch('a'))),
             LiteralMatch('r'),
             BetweenMatch(),
             EndAnchorMatch(),
         ),
         expected_solutions=[
             {'matched_text': '(ab)r[a]c[a]d[a]br[a]', 'position': 19, 'text': 'abr[a]c[a]d[a]br[a]'},
             {'matched_text': '(abracadab)r[a]', 'position': 13, 'text': 'abracadabr[a]'},
         ])
Exemplo n.º 13
0
 def test_multiple_possible_anchors(self):
     self._test_match(
         text='abracadabra',
         match=SequenceMatch(
             LiteralMatch('a'),
             mark_parens(BetweenMatch()),
             LiteralMatch('a'),
         ),
         expected_solutions=[
             {'matched_text': 'a(br)a', 'position': 4},
             {'matched_text': 'a(brac)a', 'position': 6},
             {'matched_text': 'a(bracad)a', 'position': 8},
             {'matched_text': 'a(bracadabr)a', 'position': 11},
         ])
Exemplo n.º 14
0
 def test_aliases_and_text_are_passed_from_rule_to_rule(self):
     self._test_rule_set(
         text='abc 123',
         rule_set=RuleSet(
             Rule(
                 SequenceMatch(
                     mark_parens(LiteralMatch('abc')),
                     MatchWithActions(FormatMatch('d'), SaveToAliasAction('alias')),
                 ),
             ),
             Rule(
                 InsertAliasMatch('alias'),
             ),
         ),
         expected_text=' 123(abc) 123',
         expected_aliases={'alias': ' 123'}
     )
Exemplo n.º 15
0
 def test_backtracking(self):
     """
     Tests the backtracking mechanism as applied to alternative matches. The idea tested here is that even if an
     alternative succeeds by itself, BAON may still choose the next alternative if this is the only way to make
     the overall match succeed.
     """
     self._test_unique_match(
         text='abracadabra',
         match=SequenceMatch(
             mark_parens(
                 AlternativesMatch(
                     LiteralMatch('abraca'),
                     LiteralMatch('abr'),
                 ),
             ),
             LiteralMatch('acadabra'),
         ),
         expected_solution={'matched_text': '(abr)acadabra', 'position': 11})
Exemplo n.º 16
0
 def test_alternative(self):
     self._test_match(
         text='abcabcabca',
         match=RepeatMatch(
             mark_parens(
                 AlternativesMatch(
                     LiteralMatch('abc'),
                     LiteralMatch('abcabca'),
                 ),
             ),
             0, None),
         expected_solutions=[
             {'matched_text': '(abc)(abc)(abc)', 'position': 9},
             {'matched_text': '(abc)(abc)', 'position': 6},
             {'matched_text': '(abc)(abcabca)', 'position': 10},
             {'matched_text': '(abc)', 'position': 3},
             {'matched_text': '(abcabca)', 'position': 7},
             {'matched_text': '', 'position': 0},
         ])
Exemplo n.º 17
0
    def test_optional_match(self):
        match = SequenceMatch(
            mark_parens(FormatMatch('s')),
            mark_braces(RepeatMatch(FormatMatch('d'), 0, 1)),
            mark_curlies(FormatMatch('s')),
            EndAnchorMatch(),
        )

        self._test_unique_match(
            text='Time 2 Die',
            match=match,
            expected_solution={'matched_text': '(Time) [2] {Die}', 'position': 10})
        self._test_unique_match(
            text='Time Die',
            match=match,
            expected_solution={'matched_text': '(Time)[] {Die}', 'position': 8})
        self._test_unique_match(
            text='Time 2',
            match=match,
            expected_solution={'matched_text': '(Time)[] {2}', 'position': 6})
Exemplo n.º 18
0
 def test_anchor_may_not_match(self):
     self._test_match(
         text='aabbaaba',
         match=SequenceMatch(
             mark_parens(BetweenMatch()),
             RepeatMatch(LiteralMatch('b'), 0, None),
         ),
         expected_solutions=[
             {'matched_text': '()', 'position': 0, 'anchored': False},
             {'matched_text': '(a)', 'position': 1, 'anchored': False},
             {'matched_text': '(aa)bb', 'position': 4, 'anchored': True},
             {'matched_text': '(aa)b', 'position': 3, 'anchored': True},
             {'matched_text': '(aa)', 'position': 2, 'anchored': False},
             {'matched_text': '(aab)b', 'position': 4, 'anchored': True},
             {'matched_text': '(aab)', 'position': 3, 'anchored': False},
             {'matched_text': '(aabb)', 'position': 4, 'anchored': False},
             {'matched_text': '(aabba)', 'position': 5, 'anchored': False},
             {'matched_text': '(aabbaa)b', 'position': 7, 'anchored': True},
             {'matched_text': '(aabbaa)', 'position': 6, 'anchored': False},
             {'matched_text': '(aabbaab)', 'position': 7, 'anchored': False},
             {'matched_text': '(aabbaaba)', 'position': 8, 'anchored': False},
         ])