Skip to content

Commit

Permalink
Fix CI silently not running exercise tests (#2339)
Browse files Browse the repository at this point in the history
* Fix compilation errors in Calculator Conundrum

This is not the best solution, probably it would be better if the CI script is able to parse the config.json file and copy all files listed in `files.editor` and `files.exemplar`, but for now duplicating the editor files into the exemplar implementation should help to unblock the CI build.

* Fix checkstyle errors in Calculator Conundrum

* Link editor files instead of copying them

* Fix exemplar implementation of Wizards and Warriors

* Fix exemplar implementation of Remote Control Competition

* Fix gradle test task

By making `test` depend on `copyTestsFilteringIgnores`, Gradle would run `compileTestJava` -> `copyTestFilteringIgnores` -> `Test`. This fails because there is nothing to compile in the test sourceset, since it has not been generated yet.

The correct task order should be `copyTestsFilteringIgnores` -> `compileTestJava` -> `Test`.

* Use gradle test in CI to run tests

* Only run checkstyle in build job, no tests

* Update exemplar code for SGF Parsing to pass unit tests

* Update exemplar code for Pythagorean Triplet to pass unit tests

* Fix checkstyle errors in Pythagorean Triplet exemplar code
  • Loading branch information
sanderploegsma authored Aug 24, 2023
1 parent 1cae702 commit e9e9c2d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 121 deletions.
42 changes: 20 additions & 22 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ on: [push, pull_request, workflow_dispatch]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3
- name: Set up JDK 1.17
uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2
with:
java-version: 17
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Compile and checkstyle with Gradle
run: cd exercises && ../gradlew check compileStarterSourceJava --parallel --continue --info
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3
- name: Set up JDK 1.17
uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2
with:
java-version: 17
distribution: "temurin"
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Compile and checkstyle with Gradle
run: cd exercises && ../gradlew check compileStarterSourceJava --exclude-task test --continue

test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3
- name: Set up JDK 1.17
uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2
with:
java-version: 17
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Journey test
run: bin/journey-test.sh
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3
- name: Set up JDK 1.17
uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2
with:
java-version: 17
distribution: "temurin"
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Test all exercises with Gradle
run: cd exercises && ../gradlew test --continue
12 changes: 8 additions & 4 deletions exercises/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ subprojects {

// When running the standard test task, make sure we prepopulate the test source set with the
// @Ignore-stripped tests.
test.dependsOn(copyTestsFilteringIgnores)
}


compileTestJava.dependsOn(copyTestsFilteringIgnores)

// Enable test logging when running test task from the console
test {
testLogging {
events "passed", "skipped", "failed"
}
}
}
}

def logCompileTaskSourcePath(Project project, String taskName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
class SimpleOperation {
public static int division(int operand1, int operand2)
{
public static int division(int operand1, int operand2) {
return operand1 / operand2;
}

public static int multiplication(int operand1, int operand2)
{
public static int multiplication(int operand1, int operand2) {
return operand1 * operand2;
}

public static int addition(int operand1, int operand2)
{
public static int addition(int operand1, int operand2) {
return operand1 + operand2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ProductionRemoteControlCar implements RemoteControlCar, Comparable<Product
private int numberOfVictories;

public int compareTo(ProductionRemoteControlCar other) {
return Integer.compare(this.getNumberOfVictories(), other.getNumberOfVictories());
return Integer.compare(other.getNumberOfVictories(), this.getNumberOfVictories());
}

public void drive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Wizard extends Fighter {

boolean isSpellPrepared = false;

@Override
public String toString() {
return "Fighter is a Wizard";
}

@Override
boolean isVulnerable() {
if (isSpellPrepared == false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,117 +1,67 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class PythagoreanTriplet {

private int a;
private int b;
private int c;
private final int a;
private final int b;
private final int c;

public PythagoreanTriplet(final int a, final int b, final int c) {
this.a = a;
this.b = b;
this.c = c;
}

public static TripletListBuilder makeTripletsList() {
return new TripletListBuilder();
}

public int calculateSum() {
return this.a + this.b + this.c;
}

public long calculateProduct() {
return this.a * this.b * this.c;
}

public boolean isPythagorean() {
return this.a * this.a + this.b * this.b == this.c * this.c;
}

@Override
public int hashCode() {
return Objects.hash(a, b, c);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PythagoreanTriplet other = (PythagoreanTriplet) obj;
if (this.a != other.a) {
return false;
}
if (this.b != other.b) {
return false;
}
if (this.c != other.c) {
return false;
}
return true;
public static PythagoreanTripletBuilder makeTripletsList() {
return new PythagoreanTripletBuilder();
}

public static class TripletListBuilder {

private static final int MAX_FACTOR_DEFAULT_VALUE = 0;
private static final int SUM_DEFAULT_VALUE = -1;

private int maxFactor = MAX_FACTOR_DEFAULT_VALUE;
private int minFactor = 1;
private int sum = SUM_DEFAULT_VALUE;
public static class PythagoreanTripletBuilder {
private final List<PythagoreanTriplet> triplets = new ArrayList<>();
private int limit = 0;
private int sum = 0;

public TripletListBuilder() {
}

public TripletListBuilder withFactorsLessThanOrEqualTo(
final int maxFactor) {
this.maxFactor = maxFactor;
public PythagoreanTripletBuilder withFactorsLessThanOrEqualTo(int limit) {
this.limit = limit;
return this;
}

public TripletListBuilder withFactorsGreaterThanOrEqualTo(
final int minFactor) {
this.minFactor = minFactor;
return this;
}

public TripletListBuilder thatSumTo(final int sum) {
public PythagoreanTripletBuilder thatSumTo(int sum) {
this.sum = sum;
return this;
}

public List<PythagoreanTriplet> build() {
List<PythagoreanTriplet> triplets = new ArrayList<>();
if (this.maxFactor == MAX_FACTOR_DEFAULT_VALUE) {
return triplets;
if (limit == 0) {
limit = sum / 2;
}
double sqrt;
int floor;
for (int n = minFactor; n < maxFactor; n++) {
for (int m = n + 1; m < maxFactor; m++) {
sqrt = Math.sqrt(n * n + m * m);
floor = (int) Math.floor(sqrt);
if (sqrt == floor) {
triplets.add(new PythagoreanTriplet(n, m, floor));
int start = (int) Math.sqrt(sum);
for (int a = start; a <= limit; a++) {
for (int b = a; b <= limit; b++) {
double c = Math.sqrt(a * a + b * b);
if (c % 1 == 0 && c <= limit && a + b + c == sum) {
triplets.add(new PythagoreanTriplet(a, b, (int) c));
}
}
}
if (sum != SUM_DEFAULT_VALUE) {
return triplets.stream()
.filter(t -> t.calculateSum() == sum)
.collect(Collectors.toList());
}

return triplets;
}
}

@Override
public int hashCode() {
return Objects.hash(a, b, c);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof PythagoreanTriplet other) {
return this.hashCode() == other.hashCode();
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ private int appendCharToBuffer(String input, int index, StringBuilder buffer) {
while (character == '\\') {
character = input.charAt(++index);
}
if (character == '\t') {
character = ' ';
}
buffer.append(character);
return index;
}
Expand Down
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ include 'concept:salary-calculator'
include 'concept:remote-control-competition'
include 'concept:football-match-reports'
include 'concept:wizards-and-warriors'
include 'concept:calculator-conundrum'

// practice exercises
include 'practice:accumulate'
Expand Down

0 comments on commit e9e9c2d

Please sign in to comment.