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

Add additional condition for assertEqualsNoOrder #2723

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.txt
@@ -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
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
54 changes: 54 additions & 0 deletions testng-asserts/src/test/java/test/asserttests/AssertTest.java
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,24 @@ 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 +427,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