Skip to content

Commit

Permalink
Catch hard-coded test cases in Leap
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Jan 20, 2024
1 parent b6fd3b9 commit 1cd6abe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import analyzer.Analysis;
import analyzer.Analyzer;
import analyzer.comments.AvoidHardCodedTestCases;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LeapAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer {
private static final Set<Integer> TEST_CASES = Set.of(1960, 1996, 2000, 2400);

private final Set<Integer> intLiterals = new HashSet<>();
private Analysis analysis;

@Override
Expand All @@ -20,10 +27,31 @@ public void analyze(List<CompilationUnit> compilationUnits, Analysis analysis) {
}
}

@Override
public void visit(CompilationUnit n, Void arg) {
// Reset state for each compilation unit
this.intLiterals.clear();

super.visit(n, arg);
}

@Override
public void visit(MethodCallExpr n, Void arg) {
if (n.toString().contains("Year.isLeap")) {
analysis.addComment(new DoNotUseJavaTime());
this.analysis.addComment(new DoNotUseJavaTime());
}

super.visit(n, arg);
}

@Override
public void visit(IntegerLiteralExpr n, Void arg) {
if (n.asNumber() instanceof Integer i) {
this.intLiterals.add(i);
}

if (this.intLiterals.containsAll(TEST_CASES)) {
this.analysis.addComment(new AvoidHardCodedTestCases());
}

super.visit(n, arg);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import analyzer.Analyzer;
import analyzer.AnalyzerTest;
import analyzer.Comment;
import analyzer.comments.AvoidHardCodedTestCases;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -20,7 +21,8 @@ protected Analyzer getAnalyzer() {
private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("Optimal.java", new Comment[0]),
Arguments.of("UsesJavaTime.java", new Comment[]{new DoNotUseJavaTime()})
Arguments.of("UsesJavaTime.java", new Comment[]{new DoNotUseJavaTime()}),
Arguments.of("HardCodedTestCases.java", new Comment[]{new AvoidHardCodedTestCases()})
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Leap {
boolean isLeapYear(int year) {
return year == 1960 || year == 2000 || year == 2400 || year == 1996;
}
}

0 comments on commit 1cd6abe

Please sign in to comment.