Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertEqualsDeep(Set, Set, String) wrong comparison and add variant without message #3150

Merged
Merged
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
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
Loading