Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions CleanCode.Tests/ComparisonCompactorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CleanCode;
//using CleanCode;
using NUnit.Framework;

namespace CleanCode.Tests;
Expand Down Expand Up @@ -32,7 +32,22 @@ public void TestSame()
var failure = new ComparisonCompactor(1, "ab", "ab").FormatCompactedComparison(null);
Assert.That(failure, Is.EqualTo("expected:<ab> but was:<ab>"));
}



[Test]
public void TestSame_NoContext()
{
var failure = new ComparisonCompactor(0, "abcdef", "abcdef").FormatCompactedComparison(null);
Assert.That(failure, Is.EqualTo("expected:<abcdef> but was:<abcdef>"));
}

[Test]
public void TestDifferent_NoContext()
{
var failure = new ComparisonCompactor(0, "abcDef", "abcdef").FormatCompactedComparison(null);
Assert.That(failure, Is.EqualTo("expected:<...[D]...> but was:<...[d]...>"));
}

[Test]
public void TestNoContextStartAndEndSame()
{
Expand Down Expand Up @@ -123,7 +138,18 @@ public void TestComparisionErrorWithExpectedNullContext()
var failure = new ComparisonCompactor(2, null, "a").FormatCompactedComparison(null);
Assert.That(failure, Is.EqualTo("expected:<null> but was:<a>"));
}



[Test, Ignore("This might make sense but isn't supported")]
public void TestBigCase()
{
var failure = new ComparisonCompactor(1, "1_2345678_0", "1234567890").FormatCompactedComparison(null);
Assert.That(failure, Is.EqualTo("expected:<1[_]2...8[_]0> but was:<1[2]3...8[9]0>"));
}

// Bad name. versionning system stuff. What is case
// T6 Exhaustively test near this bug too, sir.
[Test]
public void TestBug609972()
{
Expand Down
38 changes: 22 additions & 16 deletions CleanCode/ComparisonCompactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace CleanCode;

// Name is not the best.
// Truncator might be better
// StringDifferenceFinder
public class ComparisonCompactor
{
private const string ELLIPSIS = "...";
private const string ELLIPSIS = "..."; // Assigning ellipsis to ellipsis constant
private const char DELTA_END = ']';
private const char DELTA_START = '[';
private readonly int _contextLength;
Expand All @@ -20,25 +23,27 @@ public ComparisonCompactor(int contextLength, string? expected, string? actual)
_actual = actual;
}

// Null here is weird in usage. Maybe an overload instead?
// also difficult get what this is.
public string FormatCompactedComparison(string? message)
{
var compactExpected = _expected;
var compactActual = _actual;
if (ShouldBeCompacted())
{
FindCommonPrefixAndSuffix();
compactExpected = Compact(_expected!);
compactActual = Compact(_actual!);
}
return FormatCompact(message);

return Format(message, _expected, _actual);
}

public string FormatCompact(string? message)
{
// Temporal coupling G31
FindCommonPrefixAndSuffix();
var compactExpected = Compact(_expected!);
var compactActual = Compact(_actual!);
return Format(message, compactExpected, compactActual);
}

private bool ShouldBeCompacted() => !ShouldNotBeCompacted();
private bool ShouldBeCompacted() => _expected is not null && _actual is not null && !_expected.Equals(_actual, StringComparison.InvariantCulture);

private bool ShouldNotBeCompacted() => _expected is null || _actual is null || _expected.Equals(_actual);


private void FindCommonPrefixAndSuffix()
{
if (_expected is null || _actual is null)
Expand All @@ -63,19 +68,20 @@ private bool SuffixOverlapsPrefix()
return _actual.Length - _suffixLength <= _prefixLength || _expected.Length - _suffixLength <= _prefixLength;
}

// Name doesnt suggest side-effect N7
private void FindCommonPrefix()
{
if (_expected is null || _actual is null)
throw new InvalidOperationException();

_prefixLength = 0;
var end = Math.Min(_expected.Length, _actual.Length);
for (; _prefixLength < end; _prefixLength++)

while (_prefixLength < end && _expected[_prefixLength] == _actual[_prefixLength] )
{
if (_expected[_prefixLength] != _actual[_prefixLength])
break;
_prefixLength++;
}

}

private string Compact(string s) =>
Expand Down