Skip to content

Commit

Permalink
Add more simplifications (#474)
Browse files Browse the repository at this point in the history
* 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 <timtebeek@gmail.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 6, 2024
1 parent badff6f commit 8fd34dd
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
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;
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<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");
}
}
"""
)
);
}

}

0 comments on commit 8fd34dd

Please sign in to comment.