Skip to content

Commit

Permalink
More best pratices for AssertJ (#548)
Browse files Browse the repository at this point in the history
* Added ChainedAssertions for Iterator hasNext

Signed-off-by: Marvin Froeder <velo.br@gmail.com>

* Create dedicated test for IsEqualToEmptyString

* AssertJ has a more idiomatic way of asserting that a boolean is true. This recipe will find instances of:

 -`assertThat(boolean).isEqualTo(true)` and replace them with `isTrue()`.
 -`assertThat(boolean).isEqualTo(false)` and replace them with `isFalse()`.

Signed-off-by: Marvin Froeder <velo.br@gmail.com>

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Apply formatter

* Add missing `@Test` annotations

---------

Signed-off-by: Marvin Froeder <velo.br@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Jul 8, 2024
1 parent f1fda26 commit dc5e689
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.MethodInvocation;
import org.openrewrite.java.tree.JavaType.Method;

import java.util.Collections;

/**
* AssertJ has a more idiomatic way of asserting that a boolean is true. This
* recipe will find instances of:
* <p>
* -`assertThat(boolean).isEqualTo(true)` and replace them with `isTrue()`.
* -`assertThat(boolean).isEqualTo(false)` and replace them with `isFalse()`.
*/
public class IsEqualToBoolean extends Recipe {

private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher(
"org.assertj.core.api.AbstractBooleanAssert isEqualTo(boolean)");

@Override
public String getDisplayName() {
return "Convert `assertThat(String).isEqualTo(true)` to `isTrue()` and `isEqualTo(false)` to `isFalse()`";
}

@Override
public String getDescription() {
return "Adopt idiomatic AssertJ assertion for true booleans.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(IS_EQUAL_TO), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (IS_EQUAL_TO.matches(mi)) {
String methodName;
if (J.Literal.isLiteralValue(mi.getArguments().get(0), true)) {
methodName = "isTrue";
} else {
methodName = "isFalse";
}
Method isBooleanMethod = mi.getMethodType().withName(methodName);
return mi.withName(mi.getName().withSimpleName(methodName).withType(isBooleanMethod))
.withMethodType(isBooleanMethod).withArguments(Collections.emptyList());
}
return mi;
}
});
}
}
11 changes: 11 additions & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ recipeList:
- org.openrewrite.java.testing.hamcrest.MigrateHamcrestToAssertJ
- org.openrewrite.java.testing.assertj.JUnitToAssertj
- org.openrewrite.java.testing.assertj.StaticImports
- org.openrewrite.java.testing.assertj.IsEqualToBoolean
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
- org.openrewrite.java.testing.assertj.IsEqualToEmptyString

Expand Down Expand Up @@ -337,6 +338,16 @@ recipeList:
assertToReplace: isSameAs
dedicatedAssertion: containsSame
requiredType: java.util.Optional
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion:
chainedAssertion: hasNext
assertToReplace: isTrue
dedicatedAssertion: hasNext
requiredType: java.util.Iterator
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion:
chainedAssertion: hasNext
assertToReplace: isFalse
dedicatedAssertion: isExhausted
requiredType: java.util.Iterator

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class IsEqualToBooleanTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24"))
.recipe(new IsEqualToBoolean());
}

@DocumentExample
@Test
void convertsIsEqualToTrue() {
rewriteRun(
// language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat(true).isEqualTo(true);
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat(true).isTrue();
}
}
"""
)
);
}

@Test
void convertsIsEqualToFalse() {
rewriteRun(
// language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat(false).isEqualTo(false);
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat(false).isFalse();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class IsEqualToEmptyStringTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24"))
.recipe(new IsEqualToEmptyString());
}

@DocumentExample
@Test
void convertsIsEqualToEmptyString() {
rewriteRun(
// language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat("test").isEqualTo("");
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
class Test {
void test() {
assertThat("test").isEmpty();
}
}
"""
)
);
}
}
Loading

0 comments on commit dc5e689

Please sign in to comment.