Skip to content

Commit

Permalink
Additional line hitter check
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Aug 2, 2023
1 parent 0a3b421 commit 71cf500
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/github/lombrozo/testnames/Assertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface Assertion {
*
* @return State of line hitter antipattern
*/
Boolean isLineHitter();
boolean isLineHitter();

/**
* Fake assertion.
Expand All @@ -58,6 +58,8 @@ final class Fake implements Assertion {
*/
private final String message;

private final boolean hitter;

/**
* Ctor.
*/
Expand All @@ -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
Expand All @@ -79,8 +95,8 @@ public Optional<String> explanation() {
}

@Override
public Boolean isLineHitter() {
return Boolean.FALSE;
public boolean isLineHitter() {
return this.hitter;
}
}

Expand All @@ -97,7 +113,7 @@ public Optional<String> explanation() {
}

@Override
public Boolean isLineHitter() {
public boolean isLineHitter() {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Optional<String> explanation() {
}

@Override
public Boolean isLineHitter() {
public boolean isLineHitter() {
final List<String> args = this.method.getArguments()
.stream()
.map(Expression::toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand Down Expand Up @@ -99,11 +106,26 @@ public Optional<String> 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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Optional<String> explanation() {
}

@Override
public Boolean isLineHitter() {
public boolean isLineHitter() {
return new AssertionOfJUnit(this.call).isLineHitter()
|| new AssertionOfHamcrest(this.call).isLineHitter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,40 @@ void ignoresFailAssertion() {
@SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts")
@Test
void checksIfAssertTrueLineHitter() {
final List<AssertionOfJUnit> assertions = JavaTestClasses.JUNIT_ASSERT_TRUE_LINE_HITTER
.method("assertTrueHitter")
.statements()
.map(AssertionOfJUnit::new)
.filter(AssertionOfJUnit::isLineHitter)
.collect(Collectors.toList());
final List<AssertionOfJUnit> 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<AssertionOfJUnit> 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<AssertionOfJUnit> assertionsFromMethod(
final String method
) {
return JavaTestClasses.JUNIT_ASSERT_TRUE_LINE_HITTER.method(method)
.statements()
.map(AssertionOfJUnit::new)
.filter(AssertionOfJUnit::isLineHitter)
.collect(Collectors.toList());
}
}
5 changes: 5 additions & 0 deletions src/test/resources/JunitAssertTrueHitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ class JunitAssertTrueHitter {
void assertTrueHitter() {
assertTrue(true);
}

@Test
void assertFalseHitter() {
assertFalse(false);
}
}

0 comments on commit 71cf500

Please sign in to comment.