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 more simplifications #474

Merged
merged 10 commits into from
Feb 6, 2024
30 changes: 30 additions & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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
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
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
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<String> 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<String> 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<String> 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<String> o = Optional.empty();
assertThat(o).contains("foo");
assertThat(o).containsSame("foo");
}
}
"""
)
);
}

}