Skip to content

Commit

Permalink
Implement assertEqualsNoOrder migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed Jun 14, 2024
1 parent 2329edd commit 51cf86b
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.testng.Assert;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Spliterators;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -192,6 +193,129 @@ void after(double actual, double expected, double delta, String msg) {
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Collection, Collection)`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Collection, Collection)` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " +
"sorting the collection before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderCollection {

@BeforeTemplate void before(Collection<?> actual, Collection<?> expected) {
Assert.assertEqualsNoOrder(actual, expected);
}

@AfterTemplate void after(Collection<?> actual, Collection<?> expected) {
Assertions.assertArrayEquals(
expected.stream().sorted().toArray(),
actual.stream().sorted().toArray()
);
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Collection, Collection, String)`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Collection, Collection, String)` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " +
"sorting the collection before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderCollectionWithMessage {

@BeforeTemplate void before(Collection<?> actual, Collection<?> expected, String message) {
Assert.assertEqualsNoOrder(actual, expected, message);
}

@AfterTemplate void after(Collection<?> actual, Collection<?> expected, String message) {
Assertions.assertArrayEquals(
expected.stream().sorted().toArray(),
actual.stream().sorted().toArray(),
message
);
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Object[], Object[])`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Object[], Object[])` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " +
"sorting the arrays before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderArray {

@BeforeTemplate void before(Object[] actual, Object[] expected) {
Assert.assertEqualsNoOrder(actual, expected);
}

@AfterTemplate void after(Object[] actual, Object[] expected) {
Assertions.assertArrayEquals(
Arrays.stream(expected).sorted().toArray(),
Arrays.stream(actual).sorted().toArray()
);
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Object[], Object[], String)`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Object[], Object[], String)` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " +
"sorting the collection before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderArrayWithMessage {

@BeforeTemplate void before(Object[] actual, Object[] expected, String message) {
Assert.assertEqualsNoOrder(actual, expected, message);
}

@AfterTemplate void after(Object[] actual, Object[] expected, String message) {
Assertions.assertArrayEquals(
Arrays.stream(expected).sorted().toArray(),
Arrays.stream(actual).sorted().toArray(),
message
);
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Iterator<?>, Iterator<?>)`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Iterator<?>, Iterator<?>)` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " +
"sorting the arrays before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderIterator {

@BeforeTemplate void before(Iterator<?> actual, Iterator<?> expected) {
Assert.assertEqualsNoOrder(actual, expected);
}

@AfterTemplate void after(Iterator<?> actual, Iterator<?> expected) {
Assertions.assertArrayEquals(
StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(),
StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray()
);
}
}

@RecipeDescriptor(
name = "Migrate `Assert#assertEqualsNoOrder(Iterator<?>, Iterator<?>, String)`",
description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Iterator<?>, Iterator<?>, String)` " +
"to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " +
"sorting the arrays before applying the assertion."
)
public static class MigrateAssertEqualsNoOrderIteratorWithMessage {

@BeforeTemplate void before(Iterator<?> actual, Iterator<?> expected, String message) {
Assert.assertEqualsNoOrder(actual, expected, message);
}

@AfterTemplate void after(Iterator<?> actual, Iterator<?> expected, String message) {
Assertions.assertArrayEquals(
StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(),
StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray(),
message
);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(Object[], Object[])`",
description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]))`."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,197 @@ void testMethod() {
}
}

@Nested class MigrateAssertEqualsNoOrder {

@SuppressWarnings("SimplifyStreamApiCallChains")
@Nested class WithErrorMessage {

@Test void migratesCollectionToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import java.util.Arrays;
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertEqualsNoOrder(Arrays.asList("a", "b"), Arrays.asList("b", "a"), "Should contain the same elements");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Arrays;
class MyTest {
void testMethod() {
Assertions.assertArrayEquals(Arrays.asList("b", "a").stream().sorted().toArray(), Arrays.asList("a", "b").stream().sorted().toArray(), "Should contain the same elements");
}
}
"""
));
}

@Test void migratesArrayToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertEqualsNoOrder(new String[] {"actual"}, new String[] {"expected"}, "Should contain the same elements");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Arrays;
class MyTest {
void testMethod() {
Assertions.assertArrayEquals(Arrays.stream(new String[]{"expected"}).sorted().toArray(), Arrays.stream(new String[]{"actual"}).sorted().toArray(), "Should contain the same elements");
}
}
"""
));
}

@Test void migratesIteratorToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import java.util.Iterator;
import java.util.List;
import org.testng.Assert;
class MyTest {
void testMethod() {
Iterator<String> actual = List.of("a", "b").iterator();
Iterator<String> expected = List.of("b", "a").iterator();
Assert.assertEqualsNoOrder(actual, expected, "Should contain the same elements");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterators;
import java.util.stream.StreamSupport;
class MyTest {
void testMethod() {
Iterator<String> actual = List.of("a", "b").iterator();
Iterator<String> expected = List.of("b", "a").iterator();
Assertions.assertArrayEquals(StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray(), "Should contain the same elements");
}
}
"""
));
}
}

@SuppressWarnings("SimplifyStreamApiCallChains")
@Nested class WithoutErrorMessage {

@Test void migratesCollectionToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import java.util.Arrays;
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertEqualsNoOrder(Arrays.asList("a", "b"), Arrays.asList("b", "a"));
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Arrays;
class MyTest {
void testMethod() {
Assertions.assertArrayEquals(Arrays.asList("b", "a").stream().sorted().toArray(), Arrays.asList("a", "b").stream().sorted().toArray());
}
}
"""
));
}

@Test void migratesArrayToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertEqualsNoOrder(new String[] {"actual"}, new String[] {"expected"});
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Arrays;
class MyTest {
void testMethod() {
Assertions.assertArrayEquals(Arrays.stream(new String[]{"expected"}).sorted().toArray(), Arrays.stream(new String[]{"actual"}).sorted().toArray());
}
}
"""
));
}

@Test void migratesIteratorToAssertArrayEquals() {
// language=java
rewriteRun(java(
"""
import java.util.Iterator;
import java.util.List;
import org.testng.Assert;
class MyTest {
void testMethod() {
Iterator<String> actual = List.of("a", "b").iterator();
Iterator<String> expected = List.of("b", "a").iterator();
Assert.assertEqualsNoOrder(actual, expected);
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterators;
import java.util.stream.StreamSupport;
class MyTest {
void testMethod() {
Iterator<String> actual = List.of("a", "b").iterator();
Iterator<String> expected = List.of("b", "a").iterator();
Assertions.assertArrayEquals(StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray());
}
}
"""
));
}
}
}

@Nested class MigrateAssertNotEquals {

@Nested class WithErrorMessage {
Expand Down
Loading

0 comments on commit 51cf86b

Please sign in to comment.