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

Commit 3ebb288

Browse files
committed
Fix assertion failure on strings that contain their own formatting characters
1 parent 6a2b5a7 commit 3ebb288

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ public AndConstraint<TextEditCollectionAssertions> HaveTextEditAt(string expecte
7373
};
7474

7575
var errorMessage = GetHaveTextEditErrorMessage(expectedText, range);
76-
Execute.Assertion.ForCondition(errorMessage == string.Empty)
77-
.BecauseOf(because, reasonArgs)
78-
.FailWith(errorMessage);
76+
if (errorMessage != string.Empty) {
77+
var assertion = Execute.Assertion.BecauseOf(because, reasonArgs);
78+
assertion.AddNonReportable("expectedText", expectedText);
79+
assertion.AddNonReportable("expectedRange", GetName(expectedRange));
80+
assertion.AddNonReportable("currentTexts", GetQuotedNames(Subject.Select(te => te.newText)));
81+
assertion.FailWith(errorMessage);
82+
}
7983

8084
return new AndConstraint<TextEditCollectionAssertions>(this);
8185
}
@@ -84,17 +88,17 @@ public AndConstraint<TextEditCollectionAssertions> HaveTextEditAt(string expecte
8488
private string GetHaveTextEditErrorMessage(string expectedText, Range expectedRange) {
8589
var candidates = Subject.Where(av => string.Equals(av.newText, expectedText, StringComparison.Ordinal)).ToArray();
8690
if (candidates.Length == 0) {
87-
return $"Expected {GetSubjectName()} to have text edit with newText '{expectedText}'{{reason}}, but "
88-
+ (Subject.Any() ? "it is empty" : $"it has {GetQuotedNames(Subject.Select(te => te.newText))}");
91+
return "Expected {context:subject} to have text edit with newText \'{expectedText}\'{reason}, but "
92+
+ (Subject.Any() ? "it has {currentTexts}" : "it is empty");
8993
}
9094

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

96100
if (candidatesWithRange.Length == 0) {
97-
return $"Expected {GetSubjectName()} to have text edit with newText '{expectedText}' in range {GetName(expectedRange)} {{reason}}, but "
101+
return "Expected {context:subject} to have text edit with newText \'{expectedText}\' in range {expectedRange}{reason}, but "
98102
+ (candidatesWithRange.Length == 1
99103
? $"it has range {GetName(candidatesWithRange[0].range)}"
100104
: $"they are in ranges {string.Join(", ", candidatesWithRange.Select(te => GetName(te.range)))}");

0 commit comments

Comments
 (0)