예제 #1
0
def test__parser__lexer_multimatcher(caplog):
    """Test the RepeatedMultiMatcher."""
    matcher = RepeatedMultiMatcher(
        SingletonMatcher("dot", ".",
                         RawSegment.make(".", name="dot", is_code=True)),
        RegexMatcher("test", r"#[^#]*#", RawSegment.make("test", name="test")),
    )
    start_pos = FilePositionMarker()
    with caplog.at_level(logging.DEBUG):
        res = matcher.match("..#..#..#", start_pos)
        assert res.new_string == "#"  # Should match right up to the final element
        assert res.new_pos == start_pos.advance_by("..#..#..")
        assert len(res.segments) == 5
        assert res.segments[2].raw == "#..#"
예제 #2
0
def assert_matches(instring, matcher, matchstring):
    """Assert that a matcher does or doesn't work on a string.

    The optional `matchstring` argument, which can optionally
    be None, allows to either test positive matching of a
    particular string or negative matching (that it explicitly)
    doesn't match.
    """
    start_pos = FilePositionMarker()
    res = matcher.match(instring, start_pos)
    # Check we've got the right type
    assert isinstance(res, LexMatch)
    if matchstring is None:
        assert res.new_string == instring
        assert res.new_pos == start_pos
        assert res.segments == ()  # tuple
    else:
        new_pos = start_pos.advance_by(matchstring)
        assert res.new_string == instring[len(matchstring):]
        assert res.new_pos == new_pos
        assert len(res.segments) == 1
        assert res.segments[0].raw == matchstring