Skip to content

Commit

Permalink
Fix assertEqualsDeep wrong comparison and add method without message …
Browse files Browse the repository at this point in the history
…(refs #3140)
  • Loading branch information
Victor Uria Valle committed Jul 15, 2024
1 parent d1a96af commit 2df6a79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Current (7.11.0)
Fixed: GITHUB-3028: Execution stalls when using "use-global-thread-pool" (Krishnan Mahadevan)
Fixed: GITHUB-3122: Update JCommander to 1.83 (Antoine Dessaigne)
Fixed: GITHUB-3135: assertEquals on arrays - Failure message is missing information about the array index when an array element is unexpectedly null or non-null (Albert Choi)
Fixed: GITHUB-3140: assertEqualsDeep on Sets - Deep comparison was using the wrong expected value

7.10.2
Fixed: GITHUB-3117: ListenerComparator doesn't work (Krishnan Mahadevan)
Expand Down
6 changes: 5 additions & 1 deletion testng-asserts/src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -2067,14 +2067,18 @@ private static String getNotEqualDeepReason(Set<?> actual, Set<?> expected) {
return arrayNotEqualReason;
}
} else {
if (!areEqualImpl(value, expected)) {
if (!areEqualImpl(value, expectedValue)) {
return "Sets not equal: expected: " + expectedValue + " and actual: " + value;
}
}
}
return null;
}

public static void assertEqualsDeep(Set<?> actual, Set<?> expected) {
assertEqualsDeep(actual, expected, null);
}

public static void assertEqualsDeep(Set<?> actual, Set<?> expected, String message) {
String notEqualDeepReason = getNotEqualDeepReason(actual, expected);
if (notEqualDeepReason != null) {
Expand Down
22 changes: 22 additions & 0 deletions testng-asserts/src/test/java/org/testng/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,28 @@ public void testAssertNotEqualsWithNull() {
Assert.assertNotEquals(obj, obj);
}

@Test(description = "GITHUB-3140")
public void testAssertEqualsDeepSet() {
var expectedSet = new HashSet<>();
expectedSet.add(new Contrived(1));
expectedSet.add(new Contrived[] {new Contrived(1)});
var actualSet = new HashSet<>();
actualSet.add(new Contrived(1));
actualSet.add(new Contrived[] {new Contrived(1)});
Assert.assertEqualsDeep(actualSet, expectedSet);
}

@Test(description = "GITHUB-3140", expectedExceptions = AssertionError.class)
public void testAssertEqualsDeepSetFail() {
var expectedSet = new HashSet<>();
expectedSet.add(new Contrived(1));
expectedSet.add(new Contrived[] {new Contrived(1)});
var actualSet = new HashSet<>();
actualSet.add(new Contrived(1));
actualSet.add(new Contrived[] {new Contrived(2)});
Assert.assertEqualsDeep(actualSet, expectedSet);
}

static class Contrived {

int integer;
Expand Down

0 comments on commit 2df6a79

Please sign in to comment.