diff --git a/CleanCode.Tests/ComparisonCompactorTests.cs b/CleanCode.Tests/ComparisonCompactorTests.cs index 1257cb6..0e789f8 100644 --- a/CleanCode.Tests/ComparisonCompactorTests.cs +++ b/CleanCode.Tests/ComparisonCompactorTests.cs @@ -1,4 +1,4 @@ -using CleanCode; +//using CleanCode; using NUnit.Framework; namespace CleanCode.Tests; @@ -32,7 +32,22 @@ public void TestSame() var failure = new ComparisonCompactor(1, "ab", "ab").FormatCompactedComparison(null); Assert.That(failure, Is.EqualTo("expected: but was:")); } - + + + [Test] + public void TestSame_NoContext() + { + var failure = new ComparisonCompactor(0, "abcdef", "abcdef").FormatCompactedComparison(null); + Assert.That(failure, Is.EqualTo("expected: but was:")); + } + + [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() { @@ -123,7 +138,18 @@ public void TestComparisionErrorWithExpectedNullContext() var failure = new ComparisonCompactor(2, null, "a").FormatCompactedComparison(null); Assert.That(failure, Is.EqualTo("expected: but was:")); } + + + + [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() { diff --git a/CleanCode/ComparisonCompactor.cs b/CleanCode/ComparisonCompactor.cs index 76ecc11..7d707cb 100644 --- a/CleanCode/ComparisonCompactor.cs +++ b/CleanCode/ComparisonCompactor.cs @@ -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; @@ -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) @@ -63,6 +68,7 @@ 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) @@ -70,12 +76,12 @@ private void FindCommonPrefix() _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) =>