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 AssertJ dependency if using Hamcrest #648

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 8 additions & 7 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ tags:
- hamcrest
- assertj
recipeList:
# Add dependency if not already present
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.assertj
artifactId: assertj-core
version: 3.x
onlyIfUsing: org.hamcrest.*
acceptTransitive: true

# First change `is(..)` to `Matchers.is(..)` for consistent matching
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.hamcrest.core.* *(..)
Expand Down Expand Up @@ -319,10 +327,3 @@ recipeList:
notMatcher: empty
assertion: isNotEmpty

# Add dependency if not already present
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.assertj
artifactId: assertj-core
version: 3.x
onlyIfUsing: org.assertj.core.api.Assertions
acceptTransitive: true
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.testing.hamcrest;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -68,7 +69,7 @@ class Biscuit {
Biscuit(String name) {
this.name = name;
}

int getChocolateChipCount() {
return 10;
}
Expand Down Expand Up @@ -122,12 +123,12 @@ void allOfStringMatchersAndConvert() {
java(
"""
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -164,12 +165,12 @@ void convertAnyOfMatchersAfterSatisfiesAnyOfConversion() {
java(
"""
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasLength;

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -218,9 +219,9 @@ void arrayReplacements(String actual, String hamcrestMatcher, String matcherArgs
//language=java
String template = """
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -276,9 +277,9 @@ void stringReplacements(String actual, String hamcrestMatcher, String matcherArg
//language=java
String template = """
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -315,9 +316,9 @@ void objectReplacements(String actual, String hamcrestMatcher, String matcherArg
//language=java
String template = """
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -362,9 +363,9 @@ void numberReplacements(String actual, String hamcrestMatcher, String matcherArg
//language=java
String template = """
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test() {
Expand Down Expand Up @@ -404,9 +405,9 @@ void listReplacements(String actual, String hamcrestMatcher, String matcherArgs,
String template = """
import java.util.List;
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test(String item) {
Expand Down Expand Up @@ -441,9 +442,9 @@ void mapReplacements(String actual, String hamcrestMatcher, String matcherArgs,
String template = """
import java.util.Map;
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test(String key, String value) {
Expand Down Expand Up @@ -490,9 +491,9 @@ void notReplacements(String actual, String hamcrestMatcher, String matcherArgs,
//language=java
String template = """
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test() {
Expand All @@ -512,10 +513,10 @@ class Dependencies {
@Language("java")
private static final String JAVA_BEFORE = """
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class ATest {
@Test
void test() {
Expand All @@ -527,9 +528,9 @@ void test() {
@Language("java")
private static final String JAVA_AFTER = """
import org.junit.jupiter.api.Test;

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

class ATest {
@Test
void test() {
Expand All @@ -541,7 +542,6 @@ void test() {
@Test
void assertjMavenDependencyAddedWithTestScope() {
rewriteRun(
spec -> spec.expectedCyclesThatMakeChanges(2),
mavenProject("project",
srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)),
//language=xml
Expand Down Expand Up @@ -587,21 +587,23 @@ void assertjMavenDependencyAddedWithTestScope() {
);
}

@Disabled("After inexplicably null")
@Test
void assertjGradleDependencyAddedWithTestScope() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()).expectedCyclesThatMakeChanges(2),
mavenProject("project",
srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)),
buildGradle("""
//language=groovy
buildGradle(
"""
plugins {
id "java-library"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation "org.hamcrest:hamcrest:2.2"
}
Expand All @@ -610,11 +612,11 @@ void assertjGradleDependencyAddedWithTestScope() {
plugins {
id "java-library"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation "org.assertj:%s"
testImplementation "org.hamcrest:hamcrest:2.2"
Expand Down Expand Up @@ -685,14 +687,14 @@ void isMatcherFromCore() {
"""
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import org.junit.jupiter.api.Test;

class DebugTest {
class Foo {
int i = 8;
}

@Test
void ba() {
assertThat(System.out, is(System.out));
Expand All @@ -702,14 +704,14 @@ void ba() {
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class DebugTest {
class Foo {
int i = 8;
}

@Test
void ba() {
assertThat(System.out).isEqualTo(System.out);
Expand All @@ -732,9 +734,9 @@ void isEqualMatcherFromCore() {
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsSame.sameInstance;

import org.junit.jupiter.api.Test;

class DebugTest {
@Test
void ba() {
Expand All @@ -746,9 +748,9 @@ void ba() {
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class DebugTest {
@Test
void ba() {
Expand Down
Loading