def test_init(self): m = next(regex.finditer(r'(?P<R1>match me)', 'xxx match me xxx')) r = RegexMatch(1, m) self.assertEqual(r.mstart, 4) self.assertEqual(r.mend, 12) self.assertEqual(len(r), 8) self.assertEqual(r._text, 'match me') self.assertEqual(repr(r), 'RegexMatch[4-12]{1:match me}') self.assertEqual(r.nb_str(), 'RegexMatch[]{1:match me}')
def test_init(self): m = next(regex.finditer(r"(?P<R1>match me)", "xxx match me xxx")) r = RegexMatch(1, m) self.assertEqual(r.mstart, 4) self.assertEqual(r.mend, 12) self.assertEqual(len(r), 8) self.assertEqual(r._text, "match me") self.assertEqual(repr(r), "RegexMatch[4-12]{1:match me}") self.assertEqual(r.nb_str(), "RegexMatch[]{1:match me}")
def test_partial_parse() -> None: match_a = regex.match("(?<R1>a)", "ab") match_b = next(regex.finditer("(?<R2>b)", "ab")) pp = PartialParse.from_regex_matches( (RegexMatch(1, match_a), RegexMatch(2, match_b))) assert len(pp.prod) == 2 assert len(pp.rules) == 2 assert isinstance(pp.score, float) def mock_rule(ts: datetime.datetime, a: Time) -> Time: return Time() pp2 = pp.apply_rule(datetime.datetime(day=1, month=1, year=2015), mock_rule, 'mock_rule', (0, 1)) assert pp != pp2 with pytest.raises(ValueError): PartialParse((), ())
def test_regex_match(self): m = next(regex.finditer("(?P<R1>x)", "x")) r = RegexMatch(1, m) self.assertTrue(regex_match(1)(r)) self.assertFalse(regex_match(1)(TestClassA()))