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

Convert assertTrue(false, String) and assertFalse(true, String) with fail(String) #558

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ dependencies {
implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion")
runtimeOnly("org.openrewrite:rewrite-java-17")

compileOnly("org.junit.jupiter:junit-jupiter-engine:latest.release")

compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")

Expand All @@ -47,6 +49,12 @@ dependencies {
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}")
implementation("org.openrewrite:rewrite-templating:${rewriteVersion}")
compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") {
exclude("com.google.auto.service", "auto-service-annotations")
}

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")

testRuntimeOnly("com.tngtech.archunit:archunit:0.23.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021 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.cleanup;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import org.openrewrite.java.template.RecipeDescriptor;

import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.junit.jupiter.api.Assertions.*;

@RecipeDescriptor(
name = "Replace JUnit `assertTrue(false, \"reason\")` and `assertFalse(true, \"reason\")` with `fail(\"reason\")`",
description = "Using fail is more direct and clear."
)
public class AssertLiteralBooleanToFail {

@BeforeTemplate
void assertFalseBefore(String message) {
assertFalse(true, message);
}

@BeforeTemplate
void assertTrueBefore(String message) {
assertTrue(false, message);
}

@AfterTemplate
// This annotation does not get taken into account
// resulting in Assertions.fail(message) being outputted
@UseImportPolicy(value = STATIC_IMPORT_ALWAYS)
void after(String message) {
fail(message);
}
}


timtebeek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2021 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.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

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 AssertFalseLiteralTrueToFailTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
.recipe(new AssertLiteralBooleanToFailRecipe());
}

@Test
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
@DocumentExample
void assertFalseToFail() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertFalse;

public class Test {
void test() {
assertFalse(true, "message");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertFalseToFailNonStatic() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.*;

public class Test {
void test() {
Assertions.assertFalse(true, "message");
}
}
""",
"""
import org.junit.jupiter.api.*;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertFalseNonLiteralNoChange() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.*;

public class Test {
void test() {
String a1 = "a";
String a2 = "a";
assertFalse(a1.equals(a2), "message");
}
}
"""
)
);
}
}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2021 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.cleanup;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

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 AssertTrueLiteralFalseToFailTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
.recipe(new AssertLiteralBooleanToFailRecipe());
}

@Test
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
@DocumentExample
void assertTrueToFail() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Test {
void test() {
assertTrue(false, "message");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertTrueToFailNonStatic() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.assertTrue(false, "message");
}
}
""",
"""
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
Assertions.fail("message");
}
}
"""
)
);
}

@Test
void assertTrueNonLiteralNoChange() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Test {
void test() {
String a = "a";
String b = "b";
assertTrue(a.equals(b), "message");
}
}
"""
)
);
}
}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved