Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit bde584f

Browse files
committed
- Fix Position.ToString() and Range.ToString() implementation
- Use `Range.ToString()` in tests - Add test case for `\r\n` line break (test fails).
1 parent 3ebb288 commit bde584f

6 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/Analysis/Engine/Impl/Definitions/Position.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public struct Position {
4040
public static bool operator >(Position p1, Position p2) => p1.line > p2.line || p1.line == p2.line && p1.character > p2.character;
4141
public static bool operator <(Position p1, Position p2) => p1.line < p2.line || p1.line == p2.line && p1.character < p2.character;
4242

43-
public override string ToString() => ((SourceLocation)this).ToString();
43+
public override string ToString() => $"({line}, {character})";
4444
}
4545
}

src/Analysis/Engine/Impl/Definitions/Range.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public struct Range {
2424
public static implicit operator SourceSpan(Range r) => new SourceSpan(r.start, r.end);
2525
public static implicit operator Range(SourceSpan span) => new Range { start = span.Start, end = span.End };
2626

27-
public override string ToString() => ((SourceSpan)this).ToString();
27+
public override string ToString() => $"{start} - {end}";
2828
}
2929
}

src/Analysis/Engine/Test/FluentAssertions/AssertionsUtilities.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ public static string GetName(object value) {
186186
return scope.Name;
187187
case IAnalysisValue analysisValue:
188188
return $"value {analysisValue.Name}";
189-
case Range range:
190-
return $"({range.start.line}, {range.start.character}) - ({range.end.line}, {range.end.character})";
191-
case Position position:
192-
return $"({position.line}, {position.character})";
193189
case string str:
194190
return str;
195191
default:

src/Analysis/Engine/Test/FluentAssertions/ReferenceCollectionAssertions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public AndConstraint<ReferenceCollectionAssertions> OnlyHaveReferences(IEnumerab
8787
}
8888

8989
private static string Format((Uri uri, (int, int, int, int) range, ReferenceKind? kind) reference)
90-
=> $"({TestData.GetTestRelativePath(reference.uri)}, {GetName(reference.range)}, {reference.kind})";
90+
=> $"({TestData.GetTestRelativePath(reference.uri)}, {reference.range.ToString()}, {reference.kind})";
9191

9292
[CustomAssertion]
9393
public AndConstraint<ReferenceCollectionAssertions> HaveReferenceAt(Uri documentUri, int startLine, int startCharacter, int endLine, int endCharacter, ReferenceKind? referenceKind = null, string because = "", params object[] reasonArgs) {
@@ -119,18 +119,18 @@ private string FindReference(Uri documentUri, string moduleName, Range range, Re
119119

120120
foreach (var candidate in candidates.Where(c => RangeEquals(c.range, range))) {
121121
return referenceKind.HasValue && candidate._kind != referenceKind
122-
? $"Expected {GetSubjectName()} to have reference of type '{referenceKind}'{{reason}}, but reference in module '{moduleName}' at {GetName(range)} has type '{candidate._kind}'"
122+
? $"Expected {GetSubjectName()} to have reference of type '{referenceKind}'{{reason}}, but reference in module '{moduleName}' at {range.ToString()} has type '{candidate._kind}'"
123123
: string.Empty;
124124
}
125125

126-
var errorMessage = $"Expected {GetSubjectName()} to have reference at {GetName(range)}{{reason}}, but module '{moduleName}' has no references at that range.";
126+
var errorMessage = $"Expected {GetSubjectName()} to have reference at {range.ToString()}{{reason}}, but module '{moduleName}' has no references at that range.";
127127
if (!referenceKind.HasValue) {
128128
return errorMessage;
129129
}
130130

131131
var matchingTypes = candidates.Where(av => av._kind == referenceKind).ToArray();
132132
var matchingTypesString = matchingTypes.Length > 0
133-
? $"References that match type '{referenceKind}' have spans {string.Join(" ,", matchingTypes.Select(av => GetName(av.range)))}"
133+
? $"References that match type '{referenceKind}' have spans {string.Join(" ,", matchingTypes.Select(av => av.range.ToString()))}"
134134
: $"There are no references with type '{referenceKind}' either";
135135

136136
return $"{errorMessage} {matchingTypesString}";

src/Analysis/Engine/Test/FluentAssertions/TextEditCollectionAssertions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public AndConstraint<TextEditCollectionAssertions> OnlyHaveTextEdits(IEnumerable
5252
.ToArray();
5353

5454
if (excess.Length > 0) {
55-
var excessString = string.Join(", ", excess.Select(((string text, (int, int, int, int) range) te) => $"({te.text}, {GetName(te.range)})"));
55+
var excessString = string.Join(", ", excess.Select(((string text, (int, int, int, int) range) te) => $"({te.text}, {te.range.ToString()})"));
5656
var errorMessage = expected.Length > 1
5757
? $"Expected {GetSubjectName()} to have only {expected.Length} textEdits{{reason}}, but it also has textEdits: {excessString}."
5858
: expected.Length > 0
@@ -76,7 +76,7 @@ public AndConstraint<TextEditCollectionAssertions> HaveTextEditAt(string expecte
7676
if (errorMessage != string.Empty) {
7777
var assertion = Execute.Assertion.BecauseOf(because, reasonArgs);
7878
assertion.AddNonReportable("expectedText", expectedText);
79-
assertion.AddNonReportable("expectedRange", GetName(expectedRange));
79+
assertion.AddNonReportable("expectedRange", range);
8080
assertion.AddNonReportable("currentTexts", GetQuotedNames(Subject.Select(te => te.newText)));
8181
assertion.FailWith(errorMessage);
8282
}
@@ -94,14 +94,14 @@ private string GetHaveTextEditErrorMessage(string expectedText, Range expectedRa
9494

9595
var candidatesWithRange = candidates.Where(c => RangeEquals(c.range, expectedRange)).ToArray();
9696
if (candidatesWithRange.Length > 1) {
97-
return $"Expected {{context:subject}} to have only one text edit with newText '{{expectedText}}' a nd range {{expectedRange}}{{reason}}, but there are {candidatesWithRange.Length}";
97+
return $"Expected {{context:subject}} to have only one text edit with newText '{{expectedText}}' and range {{expectedRange}}{{reason}}, but there are {candidatesWithRange.Length}";
9898
}
9999

100100
if (candidatesWithRange.Length == 0) {
101101
return "Expected {context:subject} to have text edit with newText \'{expectedText}\' in range {expectedRange}{reason}, but "
102-
+ (candidatesWithRange.Length == 1
103-
? $"it has range {GetName(candidatesWithRange[0].range)}"
104-
: $"they are in ranges {string.Join(", ", candidatesWithRange.Select(te => GetName(te.range)))}");
102+
+ (candidates.Length == 1
103+
? $"it has range {candidates[0].range.ToString()}"
104+
: $"they are in ranges {string.Join(", ", candidates.Select(te => te.range.ToString()))}");
105105
}
106106

107107
return string.Empty;

src/Analysis/Engine/Test/LineFormatterTests.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,16 @@ public void LineContinuation() {
348348
AssertSingleLineFormat("a+b+ \\\n", "a + b + \\");
349349
}
350350

351-
[DataRow("foo.a() \\\n .b() \\\n .c()", "foo.a() \\", 0, 0)]
352-
[DataRow("foo.a() \\\n .b() \\\n .c()", ".b() \\", 1, 3)]
353-
[DataRow("foo.a() \\\n .b() \\\n .c()", ".c()", 2, 3)]
351+
[DataRow("foo.a() \\\n .b() \\\n .c()", "foo.a() \\", 0, 0, 9)]
352+
[DataRow("foo.a() \\\r\n .b() \\\r\n .c()", "foo.a() \\", 0, 0, 9)]
353+
[DataRow("foo.a() \\\n .b() \\\n .c()", ".b() \\", 1, 3, 9)]
354+
[DataRow("foo.a() \\\r\n .b() \\\r\n .c()", ".b() \\", 1, 3, 9)]
355+
[DataRow("foo.a() \\\n .b() \\\n .c()", ".c()", 2, 3, 7)]
356+
[DataRow("foo.a() \\\r\n .b() \\\r\n .c()", ".c()", 2, 3, 7)]
354357
[DataTestMethod, Priority(0)]
355-
public void MultilineChainedCall(string code, string expected, int line, int editStart) {
356-
AssertSingleLineFormat(code, expected, line: line, editStart: editStart);
358+
public void MultilineChainedCall(string code, string expected, int line, int characterStart, int characterEnd) {
359+
var edits = new LineFormatter(new StringReader(code), PythonLanguageVersion.V36).FormatLine(line + 1);
360+
edits.Should().OnlyHaveTextEdit(expected, (line, characterStart, line, characterEnd));
357361
}
358362

359363

0 commit comments

Comments
 (0)