From 2df6a796b4a3f8358b8adeb6adaf94b897d15fb0 Mon Sep 17 00:00:00 2001 From: Victor Uria Valle Date: Mon, 15 Jul 2024 02:15:52 +0200 Subject: [PATCH] Fix assertEqualsDeep wrong comparison and add method without message (refs #3140) --- CHANGES.txt | 1 + .../src/main/java/org/testng/Assert.java | 6 ++++- .../src/test/java/org/testng/AssertTest.java | 22 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 36d4b7bd6..9dcce35f1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) diff --git a/testng-asserts/src/main/java/org/testng/Assert.java b/testng-asserts/src/main/java/org/testng/Assert.java index ec1d6dd0d..a5feb57f6 100644 --- a/testng-asserts/src/main/java/org/testng/Assert.java +++ b/testng-asserts/src/main/java/org/testng/Assert.java @@ -2067,7 +2067,7 @@ 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; } } @@ -2075,6 +2075,10 @@ private static String getNotEqualDeepReason(Set actual, Set expected) { 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) { diff --git a/testng-asserts/src/test/java/org/testng/AssertTest.java b/testng-asserts/src/test/java/org/testng/AssertTest.java index 582fcc558..ba97f0c67 100644 --- a/testng-asserts/src/test/java/org/testng/AssertTest.java +++ b/testng-asserts/src/test/java/org/testng/AssertTest.java @@ -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;