Skip to content

Commit bfcdbf2

Browse files
Merge branch 'main' into dev/mr/addai2
2 parents 8a4116a + 18116e6 commit bfcdbf2

File tree

2 files changed

+118
-15
lines changed

2 files changed

+118
-15
lines changed

src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,14 +1483,23 @@ public static Tuple<string, string, int> CreateStringPreviews(string expected, s
14831483
int shorterStringLength = shorterString.Length;
14841484
int longerStringLength = longerString.Length;
14851485

1486-
// End marker will point to the end of the shorter string, but the end of the longer string will be replaced by ...
1487-
// make sure we don't point at the dots. To do this we need to make sure the strings are cut at the beginning, rather than preferring the maximum context shown.
1488-
bool markerPointsAtEllipsis = longerStringLength - shorterStringLength > ellipsisLength && shorterStringLength - diffIndex < ellipsisLength;
1486+
// End marker will point to the end of the shorter string, but the end of the longer string will be replaced by ... when it reaches the end of the preview.
1487+
// Make sure we don't point at the dots. To do this we need to make sure the strings are cut at the beginning, rather than preferring the maximum context shown.
1488+
//
1489+
// Marker needs to point where ellipsis would be when we shorten the longer string.
1490+
bool markerPointsAtTheEnd = shorterStringLength - diffIndex <= ellipsisLength;
1491+
// Strings need to have different lengths, for same length strings we don't add ellipsis.
1492+
bool stringsHaveDifferentLength = longerStringLength > shorterStringLength;
1493+
// Shorter string needs to be long enough to fill the preview window to the point where ellipsis shows up (last 3 chars).
1494+
bool shorterStringIsLongEnoughToFillPreviewWindow = shorterStringLength >= fullPreviewLength - ellipsisLength;
1495+
bool markerPointsAtEllipsis = markerPointsAtTheEnd && stringsHaveDifferentLength && shorterStringIsLongEnoughToFillPreviewWindow;
14891496
int ellipsisSpaceOrZero = markerPointsAtEllipsis ? ellipsisLength + 2 : 0;
14901497

1491-
// Find the end of the string that we will show, either then end of the shorter string, or the end of the preview window.
1498+
// Find the end of the string that we will show, either the end of the shorter string, or the end of the preview window.
1499+
int endOfString = Math.Min(diffIndex + contextLength, shorterStringLength);
1500+
14921501
// Then calculate the start of the preview from that. This makes sure that if diff is close end of the string we show as much as we can.
1493-
int start = Math.Min(diffIndex + contextLength, shorterStringLength) - fullPreviewLength + ellipsisSpaceOrZero;
1502+
int start = endOfString - fullPreviewLength + ellipsisSpaceOrZero;
14941503

14951504
// If the string is shorter than the preview, start cutting from 0, otherwise start cutting from the calculated start.
14961505
int cutStart = Math.Max(0, start);

test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,11 +1524,13 @@ public void CreateStringPreviews_DiffPointsToCorrectPlaceInNonShortenedString()
15241524
int length = 1;
15251525
int diffIndex = 0;
15261526
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(length, diffIndex), DigitString(length, diffIndex), diffIndex, preview));
1527-
stringPreview.Should().Be("""
1527+
StringPreviewsAreEqual(
1528+
"""
15281529
"X"
15291530
"X"
15301531
_^
1531-
""");
1532+
""",
1533+
stringPreview);
15321534
}
15331535

15341536
public void CreateStringPreviews_DiffPointsToCorrectPlaceInShortenedStringWithEndCut()
@@ -1537,11 +1539,12 @@ public void CreateStringPreviews_DiffPointsToCorrectPlaceInShortenedStringWithEn
15371539
int length = preview + 10;
15381540
int diffIndex = 0;
15391541
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(length, diffIndex), DigitString(length, diffIndex), diffIndex, preview));
1540-
stringPreview.Should().Be("""
1542+
StringPreviewsAreEqual(
1543+
"""
15411544
"X12345..."
15421545
"X12345..."
15431546
_^
1544-
""");
1547+
""", stringPreview);
15451548
}
15461549

15471550
public void CreateStringPreviews_DiffPointsToCorrectPlaceInShortenedStringWithStartCut()
@@ -1550,11 +1553,13 @@ public void CreateStringPreviews_DiffPointsToCorrectPlaceInShortenedStringWithSt
15501553
int length = 10;
15511554
int diffIndex = 9;
15521555
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(length, diffIndex), DigitString(length, diffIndex), diffIndex: diffIndex, preview));
1553-
stringPreview.Should().Be("""
1556+
StringPreviewsAreEqual(
1557+
"""
15541558
"...45678X"
15551559
"...45678X"
15561560
_________^
1557-
""");
1561+
""",
1562+
stringPreview);
15581563
}
15591564

15601565
public void CreateStringPreviews_ShowWholeStringWhenDifferenceIsAtTheEndAndJustOneStringDoesNotFit()
@@ -1563,11 +1568,13 @@ public void CreateStringPreviews_ShowWholeStringWhenDifferenceIsAtTheEndAndJustO
15631568
int length = 50;
15641569
int diffIndex = 16;
15651570
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(preview, diffIndex), DigitString(length, diffIndex), diffIndex: diffIndex, preview));
1566-
stringPreview.Should().Be("""
1571+
StringPreviewsAreEqual(
1572+
"""
15671573
"0123456789012345X7890"
15681574
"0123456789012345X7..."
15691575
_________________^
1570-
""");
1576+
""",
1577+
stringPreview);
15711578
}
15721579

15731580
public void CreateStringPreviews_MakeSureWeDontPointToEndEllipsis()
@@ -1578,11 +1585,44 @@ public void CreateStringPreviews_MakeSureWeDontPointToEndEllipsis()
15781585
int diffIndex = 24;
15791586

15801587
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(preview, diffIndex), DigitString(length, diffIndex), diffIndex: diffIndex, preview));
1581-
stringPreview.Should().Be("""
1588+
StringPreviewsAreEqual(
1589+
"""
15821590
"...8901234567890123X"
15831591
"...8901234567890123X56..."
15841592
____________________^
1585-
""");
1593+
""",
1594+
stringPreview);
1595+
}
1596+
1597+
public void CreateStringPreviews_MakeSureWeDontPointToEndEllipsis_WhenLongerStringOneCharLargerThanPreviewWindow()
1598+
{
1599+
// We will mask last 3 chars of the string, so we need to make sure that the diff index is not pointing to the end ellipsis.
1600+
int preview = 15;
1601+
int diffIndex = preview - 1;
1602+
1603+
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(preview, diffIndex), DigitString(preview + 1, diffIndex), diffIndex: diffIndex, preview));
1604+
StringPreviewsAreEqual(
1605+
"""
1606+
"...890123X"
1607+
"...890123X5"
1608+
__________^
1609+
""",
1610+
stringPreview);
1611+
}
1612+
1613+
public void CreateStringPreviews_MakeSureWeDontPointToEndEllipsis_WhenLongerStringIsBarelyLonger()
1614+
{
1615+
// We will mask last 3 chars of the string, so we need to make sure that the diff index is not pointing to the end ellipsis.
1616+
int preview = 25;
1617+
1618+
string stringPreview = FormatStringPreview(StringPreviewHelper.CreateStringPreviews("01234567890123456789012345678901234567890123X", "01234567890123456789012345678901234567890123X56", diffIndex: 44, preview));
1619+
StringPreviewsAreEqual(
1620+
"""
1621+
"...8901234567890123X"
1622+
"...8901234567890123X56"
1623+
____________________^
1624+
""",
1625+
stringPreview);
15861626
}
15871627

15881628
public void CreateStringPreviews_DiffPointsAfterLastCharacterWhenStringsAreAllTheSameCharactersUntilTheEndOfTheShorterOne()
@@ -1597,6 +1637,45 @@ public void CreateStringPreviews_DiffPointsAfterLastCharacterWhenStringsAreAllTh
15971637
""");
15981638
}
15991639

1640+
public void CreateStringPreviews_DiffNeverPointsAtEllipsis_Generated()
1641+
{
1642+
// Generate all combinations of string lengths and diff to see if in any of them we point to ellipsis.
1643+
StringBuilder s = new();
1644+
foreach (int a in Enumerable.Range(1, 20))
1645+
{
1646+
foreach (int e in Enumerable.Range(1, 20))
1647+
{
1648+
foreach (int d in Enumerable.Range(1, Math.Min(a, e)))
1649+
{
1650+
string p = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString(e, d), DigitString(a, d), diffIndex: d, 11));
1651+
1652+
string[] lines = p.Split("\n");
1653+
int diffIndicator = lines[2].IndexOf('^');
1654+
bool line0PointsOnEllipsis = lines[0].Length > diffIndicator && lines[0][diffIndicator] == '.';
1655+
bool line1PointsOnEllipsis = lines[1].Length > diffIndicator && lines[1][diffIndicator] == '.';
1656+
1657+
if (line0PointsOnEllipsis || line1PointsOnEllipsis)
1658+
{
1659+
string text = $"""
1660+
Failed for:
1661+
Expected={e}, Actual={a}, DiffIndex={d}
1662+
string result = FormatStringPreview(StringPreviewHelper.CreateStringPreviews(DigitString({e}, {d}), DigitString({a}, {d}), diffIndex: {d}, 11));
1663+
{p}
1664+
""";
1665+
1666+
s.AppendLine(text);
1667+
s.AppendLine();
1668+
}
1669+
}
1670+
}
1671+
}
1672+
1673+
if (s.Length > 0)
1674+
{
1675+
throw new InvalidOperationException($"Some combinations pointed to ellipsis:\n{s}");
1676+
}
1677+
}
1678+
16001679
private string FormatStringPreview(Tuple<string, string, int> tuple)
16011680
=> $"""
16021681
"{tuple.Item1}"
@@ -1628,4 +1707,19 @@ private static string DigitString(int length, int diffIndex)
16281707

16291708
return result.ToString();
16301709
}
1710+
1711+
private void StringPreviewsAreEqual(string expected, string actual)
1712+
{
1713+
if (expected != actual)
1714+
{
1715+
throw new InvalidOperationException(
1716+
$"""
1717+
Actual:
1718+
{actual}
1719+
1720+
Expected:
1721+
{expected}
1722+
""");
1723+
}
1724+
}
16311725
}

0 commit comments

Comments
 (0)