Skip to content

Commit

Permalink
Add additional condition for assertEqualsNoOrder (#2723)
Browse files Browse the repository at this point in the history
* Add additional condition for assertEqualsNoOrder collection and iterators to also check size
  • Loading branch information
Dymitriux committed Jan 31, 2022
1 parent 523923e commit 331bfbe
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Current
7.6.0
Fixed: assertEqualsNoOrder for Collection and Iterators size check was missing (Adam Kaczmarek)
Fixed: GITHUB-2709: Testnames not working together with suites in suite (Martin Aldrin)
Fixed: GITHUB-2704: IHookable and IConfigurable callback discrepancy (Krishnan Mahadevan)
Fixed: GITHUB-2637: Upgrade to JDK11 as the minimum JDK requirements(Krishnan Mahadevan)
Fixed: GITHUB-2637: Upgrade to JDK11 as the minimum JDK requirements (Krishnan Mahadevan)

7.5
Fixed: GITHUB-2701: Bump gradle version to 7.3.3 to support java17 build (ZhangJian He)
Expand Down
49 changes: 48 additions & 1 deletion testng-asserts/src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,22 @@ public static void assertEqualsNoOrder(Object[] actual, Object[] expected, Strin
}
}

/**
* Asserts that two collection contain the same elements in no particular order. If they do not,
* an {@code AssertionError}, with the given message, is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEqualsNoOrder(
Collection<?> actual, Collection<?> expected, String message) {
if (actual.size() != expected.size()) {
failAssertNoEqual(
"Collections do not have the same size: " + actual.size() + " != " + expected.size(),
message);
}

List<?> actualCollection = Lists.newArrayList(actual);
actualCollection.removeAll(expected);
if (!actualCollection.isEmpty()) {
Expand All @@ -1679,9 +1693,28 @@ public static void assertEqualsNoOrder(
}
}

/**
* Asserts that two iterators contain the same elements in no particular order. If they do not, an
* {@code AssertionError}, with the given message, is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEqualsNoOrder(Iterator<?> actual, Iterator<?> expected, String message) {
List<?> actualCollection = Lists.newArrayList(actual);
actualCollection.removeAll(Lists.newArrayList(expected));
List<?> expectedCollection = Lists.newArrayList(expected);

if (actualCollection.size() != expectedCollection.size()) {
failAssertNoEqual(
"Iterators do not have the same size: "
+ +actualCollection.size()
+ " != "
+ expectedCollection.size(),
message);
}

actualCollection.removeAll(expectedCollection);
if (!actualCollection.isEmpty()) {
failAssertNoEqual(
"Iterators not equal: expected: "
Expand Down Expand Up @@ -1734,10 +1767,24 @@ public static void assertEqualsNoOrder(Object[] actual, Object[] expected) {
assertEqualsNoOrder(actual, expected, null);
}

/**
* Asserts that two collection contain the same elements in no particular order. If they do not,
* an {@code AssertionError} is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEqualsNoOrder(Collection<?> actual, Collection<?> expected) {
assertEqualsNoOrder(actual, expected, null);
}

/**
* Asserts that two iterators contain the same elements in no particular order. If they do not, an
* {@code AssertionError} is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEqualsNoOrder(Iterator<?> actual, Iterator<?> expected) {
assertEqualsNoOrder(actual, expected, null);
}
Expand Down
56 changes: 56 additions & 0 deletions testng-asserts/src/test/java/test/asserttests/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@ public void checkCollectionEqualsNoOrder() {
assertEqualsNoOrder(collection1, collection2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkCollectionEqualsNoOrderCollection1SizeGreater() {

Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));

assertEqualsNoOrder(collection1, collection2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkCollectionEqualsNoOrderCollection2SizeGreater() {

Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d"));

assertEqualsNoOrder(collection1, collection2);
}

@Test(description = "GITHUB-2540")
public void checkCollectionEquals() {

Expand Down Expand Up @@ -346,6 +364,26 @@ public void checkIteratorEqualsNoOrder() {
assertEqualsNoOrder(iterator1, iterator2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkIteratorEqualsNoOrderIterator1SizeGreater() {

Iterator<String> iterator1 =
(new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "c", "b"))).iterator();

assertEqualsNoOrder(iterator1, iterator2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkIteratorEqualsNoOrderIterator2SizeGreater() {

Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 =
(new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d"))).iterator();

assertEqualsNoOrder(iterator1, iterator2);
}

@Test(description = "GITHUB-2540")
public void checkIteratorEquals() {

Expand Down Expand Up @@ -391,6 +429,24 @@ public void checkSetEqualsNoOrder() {
assertEqualsNoOrder(set1, set2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkSetEqualsNoOrderSet1SizeGreater() {

Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));

assertEqualsNoOrder(set1, set2);
}

@Test(expectedExceptions = AssertionError.class)
public void checkSetEqualsNoOrderSet2SizeGreater() {

Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b", "d"));

assertEqualsNoOrder(set1, set2);
}

@Test(description = "GITHUB-2540")
public void checkSetEquals() {

Expand Down

0 comments on commit 331bfbe

Please sign in to comment.