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 refaster recipes to simplify expected value #475

Closed
wants to merge 12 commits into from
Closed
16 changes: 16 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ dependencies {

// testImplementation("org.hamcrest:hamcrest:latest.release")
// testImplementation("org.assertj:assertj-core:latest.release")

// The bom version can also be set to a specific version
// https://github.com/openrewrite/rewrite-recipe-bom/releases
implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release"))

// Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes
// https://github.com/openrewrite/rewrite-templating/releases
annotationProcessor("org.openrewrite:rewrite-templating:latest.release")
implementation("org.openrewrite:rewrite-templating")
// The `@BeforeTemplate` and `@AfterTemplate` annotations are needed for refaster style recipes
compileOnly("com.google.errorprone:error_prone_core:2.19.1") {
exclude("com.google.auto.service", "auto-service-annotations")
}

implementation("org.assertj:assertj-core:3.+")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.assertj;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;

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

@RecipeDescriptor(
name = "Simplify the expectations of assertions",
description = "Simplifies expectations on various assertions."
)
public class SimplifyAssertExpectations {

@RecipeDescriptor(
name = "Simplify `assertThat(int).isEqualTo(0)`",
description = "Simplify `assertThat(int).isEqualTo(0)` to `assertThat(int).isZero()`."
)
public class SimplifyToIsZero {

@BeforeTemplate
void before(Integer i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to use primitive types in the method signatures.

Copy link
Contributor Author

@timo-abele timo-abele Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it for both "Integer" recipes, but still get "Recipe was expected to make a change but made no changes." for all three tests.

assertThat(i).isEqualTo(0);
}

@AfterTemplate
void after(Integer i) {
assertThat(i).isZero();
}
}

@RecipeDescriptor(
name = "Simplify `assertThat(o).isEqualTo(null)`",
description = "Simplify `assertThat(o).isEqualTo(null)` to `assertThat(o).isNull()`."
)
public class SimplifyToIsNull {

@BeforeTemplate
void before(Object o) {
assertThat(o).isEqualTo(null);
}

@AfterTemplate
void after(Object o) {
assertThat(o).isNull();
}
}

@RecipeDescriptor(
name = "Simplify `assertThat(i >= j).isTrue()`",
description = "Simplify `assertThat(i >= j).isTrue()` to `assertThat(i).isGreaterThanOrEqualTo(j)`."
)
public class SimplifyGreaterEqualComparison {

@BeforeTemplate
void before(Integer i, Integer j) {
assertThat(i >= j).isTrue();
}

@AfterTemplate
void after(Integer i, Integer j) {
assertThat(i).isGreaterThanOrEqualTo(j);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ recipeList:
- org.openrewrite.java.testing.assertj.JUnitToAssertj
- org.openrewrite.java.testing.assertj.StaticImports
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
- org.openrewrite.java.testing.assertj.SimplifyAssertExpectationsRecipes

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.assertj;

import org.junit.jupiter.api.Test;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class SimplifyAssertExpectationsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("assertj-core"))
.recipe(new SimplifyAssertExpectationsRecipes());
}

@DocumentExample
@Test
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
void simplifyIsZero() {
rewriteRun(
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
List<String> list = List.of();
assertThat(list.size()).isEqualTo(0);
assertThat(0).isEqualTo(0);
assertThat(list.size()).isNotEqualTo(0);
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
List<String> list = List.of();
assertThat(list.size()).isZero();
assertThat(0).isZero();
assertThat(list.size()).isNotZero();
}
}
"""
)
);
}

@Test
void simplifyIsNull() {
rewriteRun(
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
String s = null;
assertThat(s).isEqualTo(null);
assertThat(s).isNotEqualTo(null);
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
String s = null;
assertThat(s).isNull();
assertThat(s).isNull();
}
}
"""
)
);
}

@Test
void simplifyIsGreaterThanOrEqual() {
rewriteRun(
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
assertThat(2 >= 1).isTrue();
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

class Test {

@Test
void simpleTest() {
assertThat(2).isGreaterThanOrEqualTo(1);
}
}
"""
)
);
}

}
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
Loading