def generate_test_segments_func(elems): """Roughly generate test segments. This function isn't totally robust, but good enough for testing. Use with caution. """ buff = [] raw_buff = "" for elem in elems: if set(elem) <= {" ", "\t"}: cls = RawSegment.make(" ", name="whitespace", type="whitespace") elif set(elem) <= {"\n"}: cls = RawSegment.make("\n", name="newline", type="newline") elif elem == "(": cls = RawSegment.make("(", name="bracket_open", _is_code=True) elif elem == ")": cls = RawSegment.make(")", name="bracket_close", _is_code=True) 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 test__parser__common_marker(): """Test construction and comparison of markers.""" # 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)