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_file = "".join(elems) templated_file = TemplatedFile.from_string(raw_file) idx = 0 for elem in elems: if elem == "<indent>": buff.append( Indent(pos_marker=PositionMarker.from_point( idx, idx, templated_file))) continue elif elem == "<dedent>": buff.append( Dedent(pos_marker=PositionMarker.from_point( idx, idx, templated_file))) continue seg_kwargs = {} if set(elem) <= {" ", "\t"}: SegClass = WhitespaceSegment elif set(elem) <= {"\n"}: SegClass = NewlineSegment elif elem == "(": SegClass = SymbolSegment seg_kwargs = {"name": "bracket_open"} elif elem == ")": SegClass = SymbolSegment seg_kwargs = {"name": "bracket_close"} elif elem.startswith("--"): SegClass = CommentSegment seg_kwargs = {"name": "inline_comment"} elif elem.startswith('"'): SegClass = CodeSegment seg_kwargs = {"name": "double_quote"} elif elem.startswith("'"): SegClass = CodeSegment seg_kwargs = {"name": "single_quote"} else: SegClass = CodeSegment # Set a none position marker which we'll realign at the end. buff.append( SegClass(raw=elem, pos_marker=PositionMarker( slice(idx, idx + len(elem)), slice(idx, idx + len(elem)), templated_file, ), **seg_kwargs)) idx += len(elem) return tuple(buff)
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"