Skip to content

Commit

Permalink
Bugfix: If the string representation of the expected and actual value…
Browse files Browse the repository at this point in the history
…s was equal, the class names were not being compared.
  • Loading branch information
cowwoc committed Jan 3, 2025
1 parent 36a2445 commit b1f595a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private List<MessageSection> 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<MessageSection> context = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> actual = new ArrayList<>();
Collection<Integer> 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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit b1f595a

Please sign in to comment.