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

Hamcrest Is matcher from core #498

Merged
merged 5 commits into from
Apr 14, 2024
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
7 changes: 6 additions & 1 deletion src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ tags:
- hamcrest
- assertj
recipeList:
# First remove wrapping `is(Matcher)` calls such that further recipes will match
# First change `is(..)` to `Matchers.is(..)` for consistent matching
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.hamcrest.core.Is is(..)
fullyQualifiedTargetTypeName: org.hamcrest.Matchers

# Then remove wrapping `is(Matcher)` calls such that further recipes will match
- org.openrewrite.java.testing.hamcrest.RemoveIsMatcher

# Then remove calls to `MatcherAssert.assertThat(String, is(Matcher))`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,75 @@ void isMatcher() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, is(str2));
}
}
""", """
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1).isEqualTo(str2);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, is(str2));
}
}
""",
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1).isEqualTo(str2);
}
}
"""
)
);
}

@Test
void isMatcherWithReason() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
// Foo
assertThat("Reason", str1, is(str2));
}
}
""", """
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
// Foo
assertThat(str1).as("Reason").isEqualTo(str2);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
// Foo
assertThat("Reason", str1, is(str2));
}
}
""",
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
// Foo
assertThat(str1).as("Reason").isEqualTo(str2);
}
}
"""
)
);
}


Expand All @@ -112,88 +118,96 @@ void isMatcherWithMatcher() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;

class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, is(equalTo(str2)));
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;

class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, is(equalTo(str2)));
}
}
"""
)
);
}

@Test
void isObjectArray() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String[] str1 = new String[]{"Hello world!"};
String[] str2 = new String[]{"Hello world!"};
assertThat(str1, is(str2));
}
}
""", """
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String[] str1 = new String[]{"Hello world!"};
String[] str2 = new String[]{"Hello world!"};
assertThat(str1).containsExactly(str2);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
String[] str1 = new String[]{"Hello world!"};
String[] str2 = new String[]{"Hello world!"};
assertThat(str1, is(str2));
}
}
""",
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
String[] str1 = new String[]{"Hello world!"};
String[] str2 = new String[]{"Hello world!"};
assertThat(str1).containsExactly(str2);
}
}
"""
)
);
}

@Test
void isPrimitiveArray() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
int[] str1 = new int[]{1};
int[] str2 = new int[]{1};
assertThat(str1, is(str2));
}
}
""", """
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
int[] str1 = new int[]{1};
int[] str2 = new int[]{1};
assertThat(str1).containsExactly(str2);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class ATest {
@Test
void testEquals() {
int[] str1 = new int[]{1};
int[] str2 = new int[]{1};
assertThat(str1, is(str2));
}
}
""",
"""
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ATest {
@Test
void testEquals() {
int[] str1 = new int[]{1};
int[] str2 = new int[]{1};
assertThat(str1).containsExactly(str2);
}
}
"""
)
);
}
}
Loading
Loading