-
Notifications
You must be signed in to change notification settings - Fork 77
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
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3711b0c
add dependencies
timo-abele 02465c2
add refaster recipes and tests
timo-abele 2fdafa0
include in assertJ best practices recipe
timo-abele 4bae294
add dead simple case with literal to verify that the test case works
timo-abele 9981a30
Apply suggestions from code review
timtebeek 4a7fe12
Merge branch 'main' into simple-refaster-recipes
timtebeek 1c8ad7f
use primitives (Integer -> int) for two of three recipes
timo-abele bda13fe
Conform to project standards for versions
timtebeek e58ccd2
Merge branch 'main' into simple-refaster-recipes
timtebeek d2a28e2
Update src/test/java/org/openrewrite/java/testing/assertj/SimplifyAss…
timtebeek f36a3a2
Adopt `@Primitive Integer i` in static nested classes
timtebeek abd2cd6
Merge branch 'main' into simple-refaster-recipes
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.