def raw_seg_list(): return [ RawSegment('bar', FilePositionMarker.from_fresh()), RawSegment('foo', FilePositionMarker.from_fresh().advance_by('bar')), RawSegment('bar', FilePositionMarker.from_fresh().advance_by('barfoo')) ]
def test__parser_2__base_segments_base_compare(): 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
def test__parser_2__common_marker(): # test making one from fresh fp1 = FilePositionMarker.from_fresh() fp2 = fp1.advance_by('abc') fp3 = fp2.advance_by('def\nghi\njlk') fp4 = fp3.advance_by('mno', idx=1) # check comparisons assert fp1 == FilePositionMarker(1, 1, 1, 0) assert fp4 > fp3 > fp2 > fp1 assert fp1 < fp2 < fp3 < fp4 # Check advance works without newline assert fp2 == FilePositionMarker(1, 1, 4, 3) assert fp3 == FilePositionMarker(1, 3, 4, 14) assert fp4 == FilePositionMarker(2, 3, 7, 17)
def test__parser_2__lexer_multimatcher(caplog): 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 == '#..#'
def assert_matches(instring, matcher, matchstring): 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
def generate_test_segments(elems): """ This function isn't totally robust, but good enough for testing. Use with caution. """ buff = [] raw_buff = '' for elem in elems: if set(elem) <= set([' ', '\t']): cls = RawSegment.make(' ', name='whitespace') elif set(elem) <= set(['\n']): cls = RawSegment.make('\n', name='newline') elif elem.startswith('--'): cls = RawSegment.make('--', name='inline_comment') elif elem.startswith('"'): cls = RawSegment.make('"', name='double_quote', _is_code=True) elif elem.startswith("'"): cls = RawSegment.make("'", name='single_quote', _is_code=True) else: cls = RawSegment.make('', _is_code=True) buff.append( cls(elem, FilePositionMarker.from_fresh().advance_by(raw_buff))) raw_buff += elem return tuple(buff) # Make sure we return a tuple
def raw_seg(): fp = FilePositionMarker.from_fresh().advance_by('abc') return RawSegment('foobar', fp)
def test__parser_2__base_segments_raw_compare(): fp1 = FilePositionMarker.from_fresh() fp2 = FilePositionMarker.from_fresh() rs1 = RawSegment('foobar', fp1) rs2 = RawSegment('foobar', fp2) assert rs1 == rs2
def test__parser_2__base_segments_raw_init(): """ Test initialisation. Other tests just use the fixture """ fp = FilePositionMarker.from_fresh() RawSegment('foobar', fp)
def test__parser_2__common_marker_format(): fp1 = FilePositionMarker(1, 2, 3, 0) # Check Formatting Style assert str(fp1) == "[0](1, 2, 3)"