From 71cf500a478f54268209a3905a65b7a9b8eb12c9 Mon Sep 17 00:00:00 2001 From: l3r8yJ Date: Wed, 2 Aug 2023 13:11:37 +0300 Subject: [PATCH] Additional line hitter check --- .../github/lombrozo/testnames/Assertion.java | 24 ++++++++++-- .../javaparser/AssertionOfHamcrest.java | 2 +- .../javaparser/AssertionOfJUnit.java | 32 +++++++++++++--- .../javaparser/AssertionOfJavaParser.java | 2 +- .../javaparser/AssertionOfJUnitTest.java | 38 +++++++++++++++---- src/test/resources/JunitAssertTrueHitter.java | 5 +++ 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/github/lombrozo/testnames/Assertion.java b/src/main/java/com/github/lombrozo/testnames/Assertion.java index 6ebcd756..ac33e0a8 100644 --- a/src/main/java/com/github/lombrozo/testnames/Assertion.java +++ b/src/main/java/com/github/lombrozo/testnames/Assertion.java @@ -44,7 +44,7 @@ public interface Assertion { * * @return State of line hitter antipattern */ - Boolean isLineHitter(); + boolean isLineHitter(); /** * Fake assertion. @@ -58,6 +58,8 @@ final class Fake implements Assertion { */ private final String message; + private final boolean hitter; + /** * Ctor. */ @@ -70,7 +72,21 @@ public Fake() { * @param msg The message. */ public Fake(final String msg) { + this(msg, false); + } + + /** + * Ctor. + * + * @param msg The message. + * @param hitter The hitter state. + */ + public Fake( + final String msg, + final boolean hitter + ) { this.message = msg; + this.hitter = hitter; } @Override @@ -79,8 +95,8 @@ public Optional explanation() { } @Override - public Boolean isLineHitter() { - return Boolean.FALSE; + public boolean isLineHitter() { + return this.hitter; } } @@ -97,7 +113,7 @@ public Optional explanation() { } @Override - public Boolean isLineHitter() { + public boolean isLineHitter() { return false; } } diff --git a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfHamcrest.java b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfHamcrest.java index b72cd01d..49605871 100644 --- a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfHamcrest.java +++ b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfHamcrest.java @@ -79,7 +79,7 @@ public Optional explanation() { } @Override - public Boolean isLineHitter() { + public boolean isLineHitter() { final List args = this.method.getArguments() .stream() .map(Expression::toString) diff --git a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnit.java b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnit.java index 9207de85..92ec3b72 100644 --- a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnit.java +++ b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnit.java @@ -29,8 +29,10 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; +import java.util.Locale; import java.util.Map; import java.util.Optional; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; @@ -46,6 +48,11 @@ final class AssertionOfJUnit implements ParsedAssertion { */ private static final String[] SPECIAL = {"assertAll", "fail"}; + /** + * The `assert` regex. + */ + private static final Pattern ASSERT = Pattern.compile("assert"); + /** * The method call. */ @@ -99,11 +106,26 @@ public Optional explanation() { } @Override - public Boolean isLineHitter() { - return "assertTrue".equalsIgnoreCase(this.call.getNameAsString()) - && this.call.getArguments() - .stream() - .anyMatch(arg -> "true".equalsIgnoreCase(arg.toString())); + public boolean isLineHitter() { + final String cut = AssertionOfJUnit.ASSERT + .matcher(this.call.getNameAsString()) + .replaceAll("") + .toLowerCase(Locale.ROOT); + return this.containsLineHitter("true", cut) + || this.containsLineHitter("false", cut); + } + + /** + * Check on simple line hitter. + * + * @param type Type of hitter + * @param cut Rest of assertion + * @return True if rest of assertion equals to type + */ + private boolean containsLineHitter(final String type, final String cut) { + return type.equalsIgnoreCase(cut) && this.call.getArguments() + .stream() + .anyMatch(arg -> cut.equals(arg.toString())); } /** diff --git a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJavaParser.java b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJavaParser.java index 15a5aceb..d42410b2 100644 --- a/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJavaParser.java +++ b/src/main/java/com/github/lombrozo/testnames/javaparser/AssertionOfJavaParser.java @@ -62,7 +62,7 @@ public Optional explanation() { } @Override - public Boolean isLineHitter() { + public boolean isLineHitter() { return new AssertionOfJUnit(this.call).isLineHitter() || new AssertionOfHamcrest(this.call).isLineHitter(); } diff --git a/src/test/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnitTest.java b/src/test/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnitTest.java index 37f2f760..d0ff97f0 100644 --- a/src/test/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnitTest.java +++ b/src/test/java/com/github/lombrozo/testnames/javaparser/AssertionOfJUnitTest.java @@ -163,16 +163,40 @@ void ignoresFailAssertion() { @SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts") @Test void checksIfAssertTrueLineHitter() { - final List assertions = JavaTestClasses.JUNIT_ASSERT_TRUE_LINE_HITTER - .method("assertTrueHitter") - .statements() - .map(AssertionOfJUnit::new) - .filter(AssertionOfJUnit::isLineHitter) - .collect(Collectors.toList()); + final List assertions = + AssertionOfJUnitTest.assertionsFromMethod("assertTrueHitter"); + MatcherAssert.assertThat( + String.format("%s\n\tHave to contain only one line hitter", assertions), + assertions, + Matchers.hasSize(1) + ); + } + + @SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts") + @Test + void checksIfAssertFalseLineHitter() { + final List assertions = + AssertionOfJUnitTest.assertionsFromMethod("assertFalseHitter"); MatcherAssert.assertThat( - String.format("%s\n\tContains only one line hitter", assertions), + String.format("%s\n\tHave to contain only one line hitter", assertions), assertions, Matchers.hasSize(1) ); } + + /** + * Return assertions from method by name. + * + * @param method Method name + * @return Assertions from method + */ + private static List assertionsFromMethod( + final String method + ) { + return JavaTestClasses.JUNIT_ASSERT_TRUE_LINE_HITTER.method(method) + .statements() + .map(AssertionOfJUnit::new) + .filter(AssertionOfJUnit::isLineHitter) + .collect(Collectors.toList()); + } } diff --git a/src/test/resources/JunitAssertTrueHitter.java b/src/test/resources/JunitAssertTrueHitter.java index 25cdcdf6..a02d6a41 100644 --- a/src/test/resources/JunitAssertTrueHitter.java +++ b/src/test/resources/JunitAssertTrueHitter.java @@ -7,4 +7,9 @@ class JunitAssertTrueHitter { void assertTrueHitter() { assertTrue(true); } + + @Test + void assertFalseHitter() { + assertFalse(false); + } } \ No newline at end of file