示例#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},
         ])
示例#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})
示例#3
0
文件: test_Rule.py 项目: goc9000/baon
 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'
     )
示例#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})
示例#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})
示例#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},
         ])
示例#7
0
文件: test_Rule.py 项目: goc9000/baon
 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'
     )
示例#8
0
文件: test_Rule.py 项目: goc9000/baon
 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]'
     )
示例#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})
示例#10
0
文件: test_Rule.py 项目: goc9000/baon
 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'
     )
示例#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})
示例#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]'},
         ])
示例#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},
         ])
示例#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'}
     )
示例#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})
示例#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},
         ])
示例#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})
示例#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},
         ])