From 8fd34dd29d43dce5ca6ac414957a4afddb2c450a Mon Sep 17 00:00:00 2001 From: timo-abele <129042237+timo-abele@users.noreply.github.com> Date: Tue, 6 Feb 2024 22:15:40 +0100 Subject: [PATCH] Add more simplifications (#474) * complete the assertions on optional presence and add tests in separate class * add tests for existing equality transformations * add two simplifications * fix typo * add string test * add map complement * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../resources/META-INF/rewrite/assertj.yml | 30 ++++ .../SimplifyChainedAssertJAssertionTest.java | 40 ++++++ ...ainedAssertJAssertionWithOptionalTest.java | 129 ++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionWithOptionalTest.java diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 5f768581c..f60e3cc07 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -81,6 +81,11 @@ recipeList: assertToReplace: isTrue dedicatedAssertion: contains requiredType: java.lang.String + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: contains + assertToReplace: isFalse + dedicatedAssertion: doesNotContain + requiredType: java.lang.String - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: chainedAssertion: startsWith assertToReplace: isTrue @@ -290,12 +295,37 @@ recipeList: assertToReplace: isEqualTo dedicatedAssertion: containsEntry requiredType: java.util.Map + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: isEmpty + assertToReplace: isTrue + dedicatedAssertion: isEmpty + requiredType: java.util.Map + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: isEmpty + assertToReplace: isFalse + dedicatedAssertion: isNotEmpty + requiredType: java.util.Map # Optional Assertions - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: chainedAssertion: isPresent assertToReplace: isTrue dedicatedAssertion: isPresent requiredType: java.util.Optional + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: isEmpty + assertToReplace: isTrue + dedicatedAssertion: isEmpty + requiredType: java.util.Optional + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: isPresent + assertToReplace: isFalse + dedicatedAssertion: isNotPresent + requiredType: java.util.Optional + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: isEmpty + assertToReplace: isFalse + dedicatedAssertion: isNotEmpty + requiredType: java.util.Optional - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: chainedAssertion: get assertToReplace: isEqualTo diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java index 8ef0d89f8..f50cd45f5 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java @@ -325,6 +325,46 @@ String getString() { ); } + @Test + void stringContains() { + rewriteRun( + spec -> spec.recipes( + new SimplifyChainedAssertJAssertion("contains", "isTrue", "contains", "java.lang.String"), + new SimplifyChainedAssertJAssertion("contains", "isFalse", "doesNotContain", "java.lang.String") + ), + //language=java + java( + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + @Test + void testMethod() { + assertThat("hello world".contains("lo wo")).isTrue(); + assertThat("hello world".contains("lll")).isFalse(); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + @Test + void testMethod() { + assertThat("hello world").contains("lo wo"); + assertThat("hello world").doesNotContain("lll"); + } + } + """ + ) + ); + } + + @Test void mapMethodDealsWithTwoArguments() { rewriteRun( diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionWithOptionalTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionWithOptionalTest.java new file mode 100644 index 000000000..8b515a371 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionWithOptionalTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 SimplifyChainedAssertJAssertionWithOptionalTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), + "junit-jupiter-api-5.9", "assertj-core-3.24")); + } + + + @DocumentExample + @Test + void simplifyPresenceAssertion() { + rewriteRun( + spec -> spec.recipes( + new SimplifyChainedAssertJAssertion("isPresent", "isTrue", "isPresent", "java.util.Optional"), + new SimplifyChainedAssertJAssertion("isEmpty", "isTrue", "isEmpty", "java.util.Optional"), + new SimplifyChainedAssertJAssertion("isPresent", "isFalse", "isNotPresent", "java.util.Optional"), + new SimplifyChainedAssertJAssertion("isEmpty", "isFalse", "isNotEmpty", "java.util.Optional") + ), + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.Optional; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + Optional o = Optional.empty(); + assertThat(o.isPresent()).isTrue(); + assertThat(o.isEmpty()).isTrue(); + assertThat(o.isPresent()).isFalse(); + assertThat(o.isEmpty()).isFalse(); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.Optional; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + Optional o = Optional.empty(); + assertThat(o).isPresent(); + assertThat(o).isEmpty(); + assertThat(o).isNotPresent(); + assertThat(o).isNotEmpty(); + } + } + """ + ) + ); + } + + @Test + void simplifiyEqualityAssertion() { + rewriteRun( + spec -> spec.recipes( + new SimplifyChainedAssertJAssertion("get", "isEqualTo", "contains", "java.util.Optional"), + new SimplifyChainedAssertJAssertion("get", "isSameAs", "containsSame", "java.util.Optional") + ), + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.Optional; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + Optional o = Optional.empty(); + assertThat(o.get()).isEqualTo("foo"); + assertThat(o.get()).isSameAs("foo"); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + import java.util.Optional; + import org.junit.jupiter.api.Test; + + class Test { + + @Test + void simpleTest() { + Optional o = Optional.empty(); + assertThat(o).contains("foo"); + assertThat(o).containsSame("foo"); + } + } + """ + ) + ); + } + +}