Skip to content

Commit

Permalink
Unit test coverage (#89)
Browse files Browse the repository at this point in the history
* Add unit tests for LeapAnalyzer

* Add test coverage reporting

* Add coverage badge to README
  • Loading branch information
sanderploegsma authored Jan 23, 2024
1 parent 73b09c9 commit ef2c44f
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 162 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/gradle.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
distribution: "temurin"
- name: Test with Gradle
run: ./gradlew --no-daemon --continue test
- name: Upload test coverage
uses: coverallsapp/github-action@3dfc5567390f6fa9267c0ee9c251e4c8c3f18949

smoke_tests:
name: Smoke tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# java-analyzer
# Java Analyzer [![Coverage Status](https://coveralls.io/repos/github/exercism/java-analyzer/badge.svg?branch=main)](https://coveralls.io/github/exercism/java-analyzer?branch=main)

The Java track project-auto-mentor analyzer using abstract syntax trees
The Java track project-auto-mentor analyzer using abstract syntax trees.


## Running in docker
Expand Down
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id "com.github.johnrengelman.shadow" version "8.1.1"
id "java"
id "application"
id "jacoco"
}

group = "org.exercism"
Expand Down Expand Up @@ -38,4 +39,13 @@ test {
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}

finalizedBy jacocoTestReport
}

jacocoTestReport {
dependsOn test
reports {
xml.required = true
}
}
45 changes: 45 additions & 0 deletions src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package analyzer.exercises.leap;

import analyzer.AnalyzerTest;
import analyzer.Comment;
import analyzer.comments.AvoidHardCodedTestCases;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

public class LeapAnalyzerTest extends AnalyzerTest<LeapAnalyzer> {
private static final String RESOURCE_LOCATION = "/analyzer/exercises/leap/";

public LeapAnalyzerTest() {
super(LeapAnalyzer.class);
}

@Test
public void optimalSolution() {
var analysis = analyzeResourceFile(RESOURCE_LOCATION + "OptimalSolution.java");
assertThat(analysis.getComments()).isEmpty();
}

private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("HardCodedTestCases.java", new AvoidHardCodedTestCases()),
Arguments.of("UsingGregorianCalendar.java", new NoBuiltInMethods()),
Arguments.of("UsingIfStatements.java", new AvoidConditionalLogic()),
Arguments.of("UsingJavaTime.java", new NoBuiltInMethods()),
Arguments.of("UsingTernary.java", new AvoidConditionalLogic()),
Arguments.of("UsingTooManyChecks.java", new UseMinimumNumberOfChecks())
);
}

@ParameterizedTest(name = "{0}")
@MethodSource("testCases")
public void testCommentsOnSolution(String filename, Comment expectedComment) {
var analysis = analyzeResourceFile(RESOURCE_LOCATION + filename);
assertThat(analysis.getComments()).contains(expectedComment);
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Leap {
boolean isLeapYear(int year) {
return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
}
}
7 changes: 7 additions & 0 deletions src/test/resources/analyzer/exercises/leap/UsingJavaTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.time.Year;

class Leap {
boolean isLeapYear(int year) {
return Year.isLeap(year);
}
}
29 changes: 0 additions & 29 deletions tests/leap/using-gregorian-calendar/.meta/config.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/leap/using-gregorian-calendar/expected_analysis.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/leap/using-gregorian-calendar/expected_tags.json

This file was deleted.

29 changes: 0 additions & 29 deletions tests/leap/using-if-statements/.meta/config.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/leap/using-if-statements/expected_analysis.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/leap/using-if-statements/expected_tags.json

This file was deleted.

29 changes: 0 additions & 29 deletions tests/leap/using-ternary/.meta/config.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/leap/using-ternary/expected_analysis.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/leap/using-ternary/expected_tags.json

This file was deleted.

29 changes: 0 additions & 29 deletions tests/leap/using-too-many-checks/.meta/config.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/leap/using-too-many-checks/expected_analysis.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/leap/using-too-many-checks/expected_tags.json

This file was deleted.

0 comments on commit ef2c44f

Please sign in to comment.