Пример #1
0
def test__parser__base_segments_raw_compare():
    """Test comparison of raw segments."""
    fp1 = FilePositionMarker.from_fresh()
    fp2 = FilePositionMarker.from_fresh()
    rs1 = RawSegment("foobar", fp1)
    rs2 = RawSegment("foobar", fp2)
    assert rs1 == rs2
Пример #2
0
def raw_seg_list():
    """A generic list of raw segments to test against."""
    return [
        RawSegment("bar", FilePositionMarker.from_fresh()),
        RawSegment("foo",
                   FilePositionMarker.from_fresh().advance_by("bar")),
        RawSegment("bar",
                   FilePositionMarker.from_fresh().advance_by("barfoo")),
    ]
Пример #3
0
def test__parser__base_segments_base_compare():
    """Test comparison of base segments."""
    fp1 = FilePositionMarker.from_fresh()
    fp2 = FilePositionMarker.from_fresh()
    rs1 = RawSegment("foobar", fp1)
    rs2 = RawSegment("foobar", fp2)

    ds1 = DummySegment([rs1])
    ds2 = DummySegment([rs2])
    dsa2 = DummyAuxSegment([rs2])

    # Check for equality
    assert ds1 == ds2
    # Check a different match on the same details are not the same
    assert ds1 != dsa2
Пример #4
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.from_fresh()
    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 == "#..#"
Пример #5
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.from_fresh()
    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
Пример #6
0
def raw_seg():
    """Construct a raw segment as a fixture."""
    fp = FilePositionMarker.from_fresh().advance_by("abc")
    return RawSegment("foobar", fp)
Пример #7
0
def test__parser__base_segments_raw_init():
    """Test initialisation. Other tests just use the fixture."""
    fp = FilePositionMarker.from_fresh()
    RawSegment("foobar", fp)