diff --git a/docs/changelog.md b/docs/changelog.md index d0f5b72c..77fef923 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,7 +4,10 @@ See https://github.com/cowwoc/requirements.java/commits/main for a full list. ## Version 10.7 - 2025/01/03 -* Compare the types of the expected and actual values even if their String representation spans multiple lines. +* Bugfix: If the string representation of the expected and actual values was equal, the class names were not + being compared. +* Compare the types of the expected and actual values even if their String representation spans multiple + lines. ## Version 10.6 - 2025/01/01 diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/diff/ContextGenerator.java b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/diff/ContextGenerator.java index 5950354a..fa93b0a5 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/diff/ContextGenerator.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/diff/ContextGenerator.java @@ -475,7 +475,7 @@ private List compareTypes(DiffResult lines) Object expectedValueOrNull = expectedValue.orThrow(AssertionError::new); String actualClassName = getClassName(getClass(actualValueOrNull)); - String expectedClassName = getClassName(getClass(actualValueOrNull)); + String expectedClassName = getClassName(getClass(expectedValueOrNull)); if (!actualClassName.equals(expectedClassName)) { List context = new ArrayList<>(); diff --git a/test/src/test/java/com/github/cowwoc/requirements10/test/java/internal/diff/DiffTest.java b/test/src/test/java/com/github/cowwoc/requirements10/test/java/internal/diff/DiffTest.java index 7e45f266..805a8222 100644 --- a/test/src/test/java/com/github/cowwoc/requirements10/test/java/internal/diff/DiffTest.java +++ b/test/src/test/java/com/github/cowwoc/requirements10/test/java/internal/diff/DiffTest.java @@ -19,6 +19,9 @@ import com.github.cowwoc.requirements10.test.scope.TestApplicationScope; import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; import java.util.List; import static com.github.cowwoc.requirements10.java.TerminalEncoding.NONE; @@ -488,10 +491,47 @@ public void combineShortDiffs() } /** - * If actual != expected but their string value is identical, make sure that the hashCode() is returned. + * If actual != expected but their single-line string value is identical, make sure that their class names + * are returned. */ @Test - public void stringValueIsEqualDifferentHashCode() + public void singleLineStringIsEqualWithDifferentClassNames() + { + Collection actual = new ArrayList<>(); + Collection expected = new HashSet<>(); + for (int i = 0; i < 10; ++i) + { + actual.add(i); + expected.add(i); + } + try (ApplicationScope scope = new TestApplicationScope(NONE)) + { + TestValidators validators = TestValidators.of(scope); + + validators.requireThat(actual, "actual").isEqualTo(expected); + fail("The method should have thrown an exception"); + } + catch (IllegalArgumentException e) + { + String actualMessage = e.getMessage(); + assert (actualMessage.contains(actual.toString())) : + "Was expecting output to contain actual value, but did not.\n" + + "actual:\n" + actualMessage; + assert (actualMessage.contains("actual.class")) : + "Was expecting output to contain actual.class, but did not.\n" + + "actual:\n" + actualMessage; + assert (actualMessage.contains("expected.class")) : + "Was expecting output to contain expected.class, but did not.\n" + + "actual:\n" + actualMessage; + } + } + + /** + * If actual != expected but their single-line string value is identical, make sure that the hashCode() is + * returned. + */ + @Test + public void singleLineStringIsEqualWithDifferentHashCode() { SameLineWithDifferentHashCode actual = new SameLineWithDifferentHashCode(); SameLineWithDifferentHashCode expected = new SameLineWithDifferentHashCode(); @@ -518,11 +558,11 @@ public void stringValueIsEqualDifferentHashCode() } /** - * If actual != expected but their string value and hashCode() are identical, make sure that the identity - * hashCode is returned. + * If actual != expected but their single-line string value and hashCode() are identical, make sure that the + * identity hashCode is returned. */ @Test - public void stringValueIsEqualDifferentIdentityHashCode() + public void singleLineStringIsEqualWithDifferentIdentityHashCode() { SameLineAndHashCodeWithDifferentIdentity actual = new SameLineAndHashCodeWithDifferentIdentity(); SameLineAndHashCodeWithDifferentIdentity expected = new SameLineAndHashCodeWithDifferentIdentity(); @@ -549,10 +589,11 @@ public void stringValueIsEqualDifferentIdentityHashCode() } /** - * If actual != expected but their string value is identical, make sure that the hashCode() is returned. + * If actual != expected but their multiline string value is identical, make sure that the hashCode() is + * returned. */ @Test - public void multilineStringValueIsEqualDifferentHashCode() + public void multilineStringValueIsEqualWithDifferentHashCode() { SameMultilineWithDifferentHashCode actual = new SameMultilineWithDifferentHashCode(); SameMultilineWithDifferentHashCode expected = new SameMultilineWithDifferentHashCode();