Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Next to the exercises, the Java track also consists of the following tooling:
- [exercism/java-test-runner] - The Exercism [test runner][docs-test-runners] for the Java track that automatically verifies if a submitted solution passes all of the exercise's tests.
- [exercism/java-representer] - The Exercism [representer][docs-representers] for the Java track that creates normalized representations of submitted solutions.
- [exercism/java-analyzer] - The Exercism [analyzer][docs-analyzers] for the Java track that automatically provides comments on submitted solutions.
- [test-generator](test-generator) - Tool to generate JUnit 5 practice exercise test suites from canonical data.

## Contributing Guide

Expand Down
22 changes: 22 additions & 0 deletions bin/generate-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -eo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Default to --help if no arguments provided
if [ $# -eq 0 ]; then
ARGS="--help"
else
ARGS="$*"
fi

INSTALLED_APP="$REPO_ROOT/test-generator/build/install/test-generator/bin/test-generator"

# If not installed yet, build the distribution once
if [ ! -f "$INSTALLED_APP" ]; then
"$REPO_ROOT/test-generator/gradlew" -p "$REPO_ROOT/test-generator" installDist --quiet
fi

# Run pre-compiled binary directly (instant startup, zero Gradle daemon overhead!)
"$INSTALLED_APP" $ARGS
20 changes: 20 additions & 0 deletions exercises/practice/acronym/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class {{testClassName}} {
{{#each testCases}}

{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
@DisplayName("{{{description}}}")
public void {{methodName}}() {
assertThat(new Acronym({{{formattedInputs.phrase}}}).get())
.isEqualTo({{{expectedValue}}});
}
{{/each}}
}
31 changes: 31 additions & 0 deletions exercises/practice/affine-cipher/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class AffineCipherTest {

private AffineCipher affineCipher = new AffineCipher();
{{#each testCases}}

{{#if disabled}}
@Disabled("Remove to run test")
{{/if}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{#if expectedError}}
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> affineCipher.{{rawCase.property}}(
"{{{escapeJava rawCase.input.phrase}}}", {{rawCase.input.key.a}}, {{rawCase.input.key.b}}))
.withMessage("Error: keyA and alphabet size must be coprime.");
{{else}}
assertThat(affineCipher.{{rawCase.property}}(
"{{{escapeJava rawCase.input.phrase}}}", {{rawCase.input.key.a}}, {{rawCase.input.key.b}}))
.isEqualTo({{{expectedValue}}});
{{/if}}
}
{{/each}}
}
29 changes: 29 additions & 0 deletions exercises/practice/all-your-base/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class BaseConverterTest {
{{#each testCases}}

{{#if disabled}}
@Disabled("Remove to run test")
{{/if}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{#if expectedError}}
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter({{{formattedInputs.inputBase}}}, {{{asIntArray rawCase.input.digits}}})
.convertToBase({{{formattedInputs.outputBase}}}))
.withMessage("{{{allYourBaseErrorMessage this}}}");
{{else}}
assertThat(new BaseConverter({{{formattedInputs.inputBase}}}, {{{asIntArray rawCase.input.digits}}})
.convertToBase({{{formattedInputs.outputBase}}}))
.containsExactly({{{formatElements rawCase.expected}}});
{{/if}}
}
{{/each}}
}
30 changes: 30 additions & 0 deletions exercises/practice/allergies/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class AllergiesTest {
{{#each testCases}}

{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{#if (eq property "allergicTo")~}}
assertThat(new Allergies({{{formattedInputs.score}}}).isAllergicTo({{toAllergen inputNodes.item}}))
.{{#if (eq expectedValue "true")}}isTrue(){{else}}isFalse(){{/if}};
{{~else~}}
{{#if (eq rawCase.expected.size 0)~}}
assertThat(new Allergies({{{formattedInputs.score}}}).getList())
.isEmpty();
{{~else~}}
assertThat(new Allergies({{{formattedInputs.score}}}).getList())
.containsExactly({{{asAllergens rawCase.expected}}});
{{~/if}}
{{~/if}}
}
{{/each}}
}
29 changes: 29 additions & 0 deletions exercises/practice/alphametics/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class AlphameticsTest {
{{#each testCases}}

{{#if disabled}}
@Disabled("Remove to run test")
{{/if}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() throws UnsolvablePuzzleException {
{{#if isNullExpected}}
assertThatExceptionOfType(UnsolvablePuzzleException.class)
.isThrownBy(() -> new Alphametics({{{alphameticsPuzzle rawCase.input.puzzle}}}).solve());
{{else}}
assertThat(new Alphametics({{{alphameticsPuzzle rawCase.input.puzzle}}}).solve())
.isEqualTo({{{formatAlphameticsExpected rawCase.expected}}});
{{/if}}
}
{{/each}}
}
22 changes: 22 additions & 0 deletions exercises/practice/anagram/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class AnagramTest {
{{#each testCases}}

{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
assertThat(new Anagram({{{formattedInputs.subject}}}).match({{{formattedInputs.candidates}}}))
.containsExactlyInAnyOrderElementsOf({{{expectedValue}}});
}
{{/each}}
}
53 changes: 53 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.List;

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

public class BafflingBirthdaysTest {

private BafflingBirthdays birthdays = new BafflingBirthdays();

{{#each testCases}}
{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{#if (eq rawCase.property "sharedBirthday")}}
List<LocalDate> birthdates = {{{asLocalDateList rawCase.input.birthdates}}};
{{#if (eq rawCase.expected true)}}
assertThat(birthdays.sharedBirthday(birthdates)).isTrue();
{{else}}
assertThat(birthdays.sharedBirthday(birthdates)).isFalse();
{{/if}}
{{else if (eq rawCase.property "randomBirthdates")}}
{{#if rawCase.expected.years}}
assertThat(birthdays.randomBirthdates(100)).noneMatch(LocalDate::isLeapYear);
{{else if rawCase.expected.months}}
assertThat(birthdays.randomBirthdates(500).stream().map(LocalDate::getMonth).distinct())
.hasSizeGreaterThanOrEqualTo(7);
{{else if rawCase.expected.days}}
assertThat(birthdays.randomBirthdates(500).stream().map(LocalDate::getDayOfMonth).distinct())
.hasSizeGreaterThanOrEqualTo(11);
{{else}}
int groupSize = 50;
assertThat(birthdays.randomBirthdates(groupSize).size()).isEqualTo(groupSize);
{{/if}}
{{else if (eq rawCase.property "estimatedProbabilityOfSharedBirthday")}}
{{#if (eq rawCase.expected 0.0)}}
assertThat(birthdays.estimatedProbabilityOfSharedBirthday({{rawCase.input.groupSize}}))
.isEqualTo(0.0);
{{else}}
assertThat(birthdays.estimatedProbabilityOfSharedBirthday({{rawCase.input.groupSize}}))
.isCloseTo({{rawCase.expected}}, offset(1.0));
{{/if}}
{{/if}}
}
{{/each}}
}
23 changes: 23 additions & 0 deletions exercises/practice/bank-account/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Random;

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

public class BankAccountTest {

{{#each testCases}}
{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() throws Exception {
{{{renderBankAccountCase rawCase}}}
}
{{/each}}
}
21 changes: 21 additions & 0 deletions exercises/practice/binary-search-tree/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class BinarySearchTreeTest {

{{#each testCases}}
{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{{renderBinarySearchTreeCase rawCase}}}
}
{{/each}}
}
31 changes: 31 additions & 0 deletions exercises/practice/binary-search/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class BinarySearchTest {
{{#each testCases}}

{{#if disabled}}
@Disabled("Remove to run test")
{{/if}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() throws ValueNotFoundException {
{{#if expectedError}}
assertThatExceptionOfType(ValueNotFoundException.class)
.isThrownBy(() -> new BinarySearch({{{formattedInputs.array}}})
.indexOf({{{formattedInputs.value}}}))
.withMessage("Value not in array");
{{else}}
assertThat(new BinarySearch({{{formattedInputs.array}}})
.indexOf({{{formattedInputs.value}}}))
.isEqualTo({{{expectedValue}}});
{{/if}}
}
{{/each}}
}
20 changes: 20 additions & 0 deletions exercises/practice/bob/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

public class BobTest {
{{#each testCases}}

{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
assertThat(new Bob().hey({{{formattedInputs.heyBob}}}))
.isEqualTo({{{expectedValue}}});
}
{{/each}}
}
33 changes: 33 additions & 0 deletions exercises/practice/book-store/.meta/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class {{testClassName}} {

// A 0.01 tolerance covers minor rounding variations in floating point calculations
private static final double EQUALITY_TOLERANCE = 0.01;

private BookStore bookStore;

@BeforeEach
public void setUp() {
bookStore = new BookStore();
}
{{#each testCases}}

{{#if disabled~}}
@Disabled("Remove to run test")
{{/if~}}
@Test
{{{displayNameAnnotation}}}
public void {{methodName}}() {
{{{renderBookStoreCase this}}}
}
{{/each}}
}
Loading