def raw_seg_list(): """A generic list of raw segments to test against.""" return [ RawSegment("bar", FilePositionMarker()), RawSegment("foo", FilePositionMarker().advance_by("bar")), RawSegment("bar", FilePositionMarker().advance_by("barfoo")), ]
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
def test__parser__base_segments_raw_compare(): """Test comparison of raw segments.""" template = TemplatedFile.from_string("foobar") rs1 = RawSegment("foobar", PositionMarker(slice(0, 6), slice(0, 6), template)) rs2 = RawSegment("foobar", PositionMarker(slice(0, 6), slice(0, 6), template)) assert rs1 == rs2
def test__parser__base_segments_base_compare(): """Test comparison of base segments.""" rs1 = RawSegment("foobar", FilePositionMarker()) rs2 = RawSegment("foobar", FilePositionMarker()) 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__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 == "#..#"
def test__parser__base_segments_base_compare(): """Test comparison of base segments.""" template = TemplatedFile.from_string("foobar") rs1 = RawSegment("foobar", PositionMarker(slice(0, 6), slice(0, 6), template)) rs2 = RawSegment("foobar", PositionMarker(slice(0, 6), slice(0, 6), template)) 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 make_whitespace(cls, raw, pos_marker): """Make a whitespace segment.""" WhitespaceSegment = RawSegment.make(" ", name="whitespace", type="whitespace", is_whitespace=True) return WhitespaceSegment(raw=raw, pos_marker=pos_marker)
def test__cli__formatters__violation(tmpdir): """Test formatting violations. NB Position is 1 + start_pos. """ s = RawSegment( "foobarbar", PositionMarker( slice(10, 19), slice(10, 19), TemplatedFile.from_string(" \n\n foobarbar"), ), ) r = RuleGhost("A", "DESC") v = SQLLintError(segment=s, rule=r) formatter = OutputStreamFormatter( FileOutput(FluffConfig(require_dialect=False), str(tmpdir / "out.txt")), False ) f = formatter.format_violation(v) # Position is 3, 3 becase foobarbar is on the third # line (i.e. it has two newlines preceding it) and # it's at the third position in that line (i.e. there # are two characters between it and the preceding # newline). assert escape_ansi(f) == "L: 3 | P: 3 | A | DESC"
def test__parser__base_segments_stubs(): """Test stub methods that have no implementation in base class.""" template = TemplatedFile.from_string("foobar") rs1 = RawSegment("foobar", PositionMarker(slice(0, 6), slice(0, 6), template)) base_segment = BaseSegment(segments=[rs1]) with pytest.raises(NotImplementedError): base_segment.edit("foo")
def test__cli__formatters__violation(): """Test formatting violations. NB Position is 1 + start_pos. """ s = RawSegment("foobarbar", FilePositionMarker(0, 20, 11, 100)) r = RuleGhost("A", "DESC") v = SQLLintError(segment=s, rule=r) f = format_violation(v) assert escape_ansi(f) == "L: 20 | P: 11 | A | DESC"
def test__cli__formatters__violation(): """Test formatting violations. NB Position is 1 + start_pos. """ s = RawSegment( "foobarbar", PositionMarker( slice(10, 19), slice(10, 19), TemplatedFile.from_string(" \n\n foobarbar"), ), ) r = RuleGhost("A", "DESC") v = SQLLintError(segment=s, rule=r) f = format_violation(v) # Position is 3, 3 becase foobarbar is on the third # line (i.e. it has two newlines preceding it) and # it's at the third position in that line (i.e. there # are two characters between it and the preceeding # newline). assert escape_ansi(f) == "L: 3 | P: 3 | A | DESC"
def test__parser__lexer_regex(raw, reg, res, caplog): """Test the RegexMatcher.""" matcher = RegexMatcher("test", reg, RawSegment.make("test", name="test")) with caplog.at_level(logging.DEBUG): assert_matches(raw, matcher, res)
def make_newline(cls, pos_marker, raw=None): """Make a newline segment.""" # Default the newline to \n raw = raw or "\n" nls = RawSegment.make("\n", name="newline", type="newline") return nls(raw=raw, pos_marker=pos_marker)
def test__parser__base_segments_raw_compare(): """Test comparison of raw segments.""" rs1 = RawSegment("foobar", FilePositionMarker()) rs2 = RawSegment("foobar", FilePositionMarker()) assert rs1 == rs2
def test__parser__base_segments_raw_init(): """Test initialisation. Other tests just use the fixture.""" RawSegment("foobar", FilePositionMarker())
def raw_seg_list(raw_seg): """Construct a list of raw segments as a fixture.""" return [ raw_seg, RawSegment(".barfoo", raw_seg.pos_marker.advance_by(raw_seg.raw)) ]
def raw_seg(): """Construct a raw segment as a fixture.""" fp = FilePositionMarker().advance_by("abc") return RawSegment("foobar", fp)
def test__parser__lexer_singleton(raw, res): """Test the SingletonMatcher.""" matcher = SingletonMatcher("dot", ".", RawSegment.make(".", name="dot", is_code=True)) assert_matches(raw, matcher, res)
def test__parser__base_segments_raw_init(): """Test initialisation. Other tests just use the fixture.""" fp = FilePositionMarker.from_fresh() RawSegment("foobar", fp)