Skip to content

Commit 97a19b3

Browse files
authored
Merge branch 'main' into display9
2 parents 5eadc87 + 766c84e commit 97a19b3

File tree

9 files changed

+133
-4
lines changed

9 files changed

+133
-4
lines changed

.github/workflows/java.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ jobs:
3939
run: ./gradlew check --exclude-task test --continue
4040
working-directory: exercises
4141

42-
test:
42+
test-all:
4343
name: Test all exercises using java-test-runner
44+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
4445
runs-on: ubuntu-24.04
4546
steps:
4647
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
@@ -52,3 +53,20 @@ jobs:
5253
name: test-results
5354
path: exercises/**/build/results.json
5455
if: failure()
56+
57+
test-changed:
58+
name: Test changed exercises using java-test-runner
59+
if: github.event_name == 'pull_request'
60+
runs-on: ubuntu-24.04
61+
steps:
62+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
63+
with:
64+
fetch-depth: 0
65+
- name: Test changed exercises using java-test-runner
66+
run: bin/test-changed-exercise
67+
- name: Archive test results
68+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
69+
with:
70+
name: test-results
71+
path: exercises/**/build/results.json
72+
if: failure()

bin/test-changed-exercise

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# Determine the base branch of the PR
5+
BASE_BRANCH=${GITHUB_BASE_REF:-main}
6+
7+
# Fetch full history for proper diff
8+
git fetch origin "$BASE_BRANCH"
9+
10+
# Compute merge base
11+
MERGE_BASE=$(git merge-base HEAD origin/"$BASE_BRANCH")
12+
13+
# Get changed files relative to merge base
14+
changed_files=$(git diff --name-only "$MERGE_BASE" HEAD)
15+
16+
# Extract unique exercise directories
17+
changed_exercises=$(echo "$changed_files" | \
18+
grep -E '^exercises/(practice|concept)/[^/]+/.+\.(java|gradle)$' | \
19+
cut -d/ -f1-3 | sort -u)
20+
21+
if [ -z "$changed_exercises" ]; then
22+
echo "No relevant exercises changed, skipping tests."
23+
exit 0
24+
fi
25+
26+
# Print exercises
27+
echo "Changed exercises detected:"
28+
echo "$changed_exercises"
29+
echo "----------------------------------------"
30+
31+
# Run tests
32+
for dir in $changed_exercises; do
33+
slug=$(basename "$dir")
34+
35+
echo "========================================"
36+
echo "=== Running tests for $slug ==="
37+
echo "========================================"
38+
39+
if [[ $dir == exercises/practice/* ]]; then
40+
./exercises/gradlew -p exercises ":practice:$slug:test"
41+
elif [[ $dir == exercises/concept/* ]]; then
42+
./exercises/gradlew -p exercises ":concept:$slug:test"
43+
fi
44+
done

exercises/practice/rectangles/src/test/java/RectangleCounterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import org.junit.jupiter.api.BeforeEach;
22
import org.junit.jupiter.api.Disabled;
3+
import org.junit.jupiter.api.DisplayName;
34
import org.junit.jupiter.api.Test;
45

56
import static org.assertj.core.api.Assertions.assertThat;
@@ -14,6 +15,7 @@ public void setUp() {
1415
}
1516

1617
@Test
18+
@DisplayName("no rows")
1719
public void testInputWithNoRowsContainsNoRectangles() {
1820
String[] inputGrid = new String[]{};
1921

@@ -22,6 +24,7 @@ public void testInputWithNoRowsContainsNoRectangles() {
2224

2325
@Disabled("Remove to run test")
2426
@Test
27+
@DisplayName("no columns")
2528
public void testInputWithNoColumnsContainsNoRectangles() {
2629
String[] inputGrid = new String[]{""};
2730

@@ -30,6 +33,7 @@ public void testInputWithNoColumnsContainsNoRectangles() {
3033

3134
@Disabled("Remove to run test")
3235
@Test
36+
@DisplayName("no rectangles")
3337
public void testNonTrivialInputWithNoRectangles() {
3438
String[] inputGrid = new String[]{" "};
3539

@@ -38,6 +42,7 @@ public void testNonTrivialInputWithNoRectangles() {
3842

3943
@Disabled("Remove to run test")
4044
@Test
45+
@DisplayName("one rectangle")
4146
public void testInputWithOneRectangle() {
4247
String[] inputGrid = new String[]{
4348
"+-+",
@@ -50,6 +55,7 @@ public void testInputWithOneRectangle() {
5055

5156
@Disabled("Remove to run test")
5257
@Test
58+
@DisplayName("two rectangles without shared parts")
5359
public void testInputWithTwoRectanglesWithoutSharedEdges() {
5460
String[] inputGrid = new String[]{
5561
" +-+",
@@ -64,6 +70,7 @@ public void testInputWithTwoRectanglesWithoutSharedEdges() {
6470

6571
@Disabled("Remove to run test")
6672
@Test
73+
@DisplayName("five rectangles with shared parts")
6774
public void testInputWithFiveRectanglesWithSharedEdges() {
6875
String[] inputGrid = new String[]{
6976
" +-+",
@@ -78,6 +85,7 @@ public void testInputWithFiveRectanglesWithSharedEdges() {
7885

7986
@Disabled("Remove to run test")
8087
@Test
88+
@DisplayName("rectangle of height 1 is counted")
8189
public void testThatRectangleOfHeightOneIsCounted() {
8290
String[] inputGrid = new String[]{
8391
"+--+",
@@ -89,6 +97,7 @@ public void testThatRectangleOfHeightOneIsCounted() {
8997

9098
@Disabled("Remove to run test")
9199
@Test
100+
@DisplayName("rectangle of width 1 is counted")
92101
public void testThatRectangleOfWidthOneIsCounted() {
93102
String[] inputGrid = new String[]{
94103
"++",
@@ -101,6 +110,7 @@ public void testThatRectangleOfWidthOneIsCounted() {
101110

102111
@Disabled("Remove to run test")
103112
@Test
113+
@DisplayName("1x1 square is counted")
104114
public void testThatOneByOneSquareIsCounted() {
105115
String[] inputGrid = new String[]{
106116
"++",
@@ -112,6 +122,7 @@ public void testThatOneByOneSquareIsCounted() {
112122

113123
@Disabled("Remove to run test")
114124
@Test
125+
@DisplayName("only complete rectangles are counted")
115126
public void testThatIncompleteRectanglesAreNotCounted() {
116127
String[] inputGrid = new String[]{
117128
" +-+",
@@ -126,6 +137,7 @@ public void testThatIncompleteRectanglesAreNotCounted() {
126137

127138
@Disabled("Remove to run test")
128139
@Test
140+
@DisplayName("rectangles can be of different sizes")
129141
public void testThatRectanglesOfDifferentSizesAreAllCounted() {
130142
String[] inputGrid = new String[]{
131143
"+------+----+",
@@ -140,6 +152,7 @@ public void testThatRectanglesOfDifferentSizesAreAllCounted() {
140152

141153
@Disabled("Remove to run test")
142154
@Test
155+
@DisplayName("corner is required for a rectangle to be complete")
143156
public void testThatIntersectionsWithoutCornerCharacterDoNotCountAsRectangleCorners() {
144157
String[] inputGrid = new String[]{
145158
"+------+----+",
@@ -154,6 +167,7 @@ public void testThatIntersectionsWithoutCornerCharacterDoNotCountAsRectangleCorn
154167

155168
@Disabled("Remove to run test")
156169
@Test
170+
@DisplayName("large input with many rectangles")
157171
public void testLargeInputWithManyRectangles() {
158172
String[] inputGrid = new String[]{
159173
"+---+--+----+",
@@ -171,6 +185,7 @@ public void testLargeInputWithManyRectangles() {
171185

172186
@Disabled("Remove to run test")
173187
@Test
188+
@DisplayName("rectangles must have four sides")
174189
public void testRectanglesMustHaveFourSides() {
175190
String[] inputGrid = new String[]{
176191
"+-+ +-+",

exercises/practice/relative-distance/src/test/java/RelativeDistanceTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.junit.jupiter.api.Disabled;
2+
import org.junit.jupiter.api.DisplayName;
23
import org.junit.jupiter.api.Test;
34

45

@@ -12,6 +13,7 @@
1213
public class RelativeDistanceTest {
1314

1415
@Test
16+
@DisplayName("Direct parent-child relation")
1517
public void testDirectParentChildRelation() {
1618
Map<String, List<String>> familyTree = new HashMap<>() {
1719
{
@@ -26,6 +28,7 @@ public void testDirectParentChildRelation() {
2628

2729
@Disabled("Remove to run test")
2830
@Test
31+
@DisplayName("Sibling relationship")
2932
public void testSiblingRelationship() {
3033
Map<String, List<String>> familyTree = new HashMap<>() {
3134
{
@@ -39,6 +42,7 @@ public void testSiblingRelationship() {
3942

4043
@Disabled("Remove to run test")
4144
@Test
45+
@DisplayName("Two degrees of separation, grandchild")
4246
public void testTwoDegreesOfSeparationGrandchild() {
4347
Map<String, List<String>> familyTree = new HashMap<>() {
4448
{
@@ -53,6 +57,7 @@ public void testTwoDegreesOfSeparationGrandchild() {
5357

5458
@Disabled("Remove to run test")
5559
@Test
60+
@DisplayName("Unrelated individuals")
5661
public void testUnrelatedIndividuals() {
5762
Map<String, List<String>> familyTree = new HashMap<>() {
5863
{
@@ -67,6 +72,7 @@ public void testUnrelatedIndividuals() {
6772

6873
@Disabled("Remove to run test")
6974
@Test
75+
@DisplayName("Complex graph, cousins")
7076
public void testComplexGraphCousins() {
7177
Map<String, List<String>> familyTree = new HashMap<>() {
7278
{
@@ -131,6 +137,7 @@ public void testComplexGraphCousins() {
131137

132138
@Disabled("Remove to run test")
133139
@Test
140+
@DisplayName("Complex graph, no shortcut, far removed nephew")
134141
public void testComplexGraphNoShortcutFarRemovedNephew() {
135142
Map<String, List<String>> familyTree = new HashMap<>() {
136143
{
@@ -194,6 +201,10 @@ public void testComplexGraphNoShortcutFarRemovedNephew() {
194201

195202
@Disabled("Remove to run test")
196203
@Test
204+
@DisplayName(
205+
"Complex graph, some shortcuts, cross-down and cross-up, " +
206+
"cousins several times removed, with unrelated family tree"
207+
)
197208
public void testComplexGraphSomeShortcutsCrossDownAndCrossUpCousinsSeveralTimesRemovedWithUnrelatedFamilyTree() {
198209
Map<String, List<String>> familyTree = new HashMap<>() {
199210
{

exercises/practice/resistor-color-duo/src/test/java/ResistorColorDuoTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.junit.jupiter.api.BeforeEach;
2-
import org.junit.jupiter.api.Test;
32
import org.junit.jupiter.api.Disabled;
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
45

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

@@ -13,6 +14,7 @@ public void setup() {
1314
}
1415

1516
@Test
17+
@DisplayName("Brown and black")
1618
public void testBrownAndBlack() {
1719
assertThat(
1820
resistorColorDuo.value(new String[]{"brown", "black"})
@@ -21,6 +23,7 @@ public void testBrownAndBlack() {
2123

2224
@Disabled("Remove to run test")
2325
@Test
26+
@DisplayName("Blue and grey")
2427
public void testBlueAndGrey() {
2528
assertThat(
2629
resistorColorDuo.value(new String[]{ "blue", "grey" })
@@ -29,6 +32,7 @@ public void testBlueAndGrey() {
2932

3033
@Disabled("Remove to run test")
3134
@Test
35+
@DisplayName("Yellow and violet")
3236
public void testYellowAndViolet() {
3337
assertThat(
3438
resistorColorDuo.value(new String[]{ "yellow", "violet" })
@@ -37,6 +41,7 @@ public void testYellowAndViolet() {
3741

3842
@Disabled("Remove to run test")
3943
@Test
44+
@DisplayName("Orange and orange")
4045
public void testOrangeAndOrange() {
4146
assertThat(
4247
resistorColorDuo.value(new String[]{ "orange", "orange" })
@@ -45,6 +50,7 @@ public void testOrangeAndOrange() {
4550

4651
@Disabled("Remove to run test")
4752
@Test
53+
@DisplayName("White and red")
4854
public void testWhiteAndRed() {
4955
assertThat(
5056
resistorColorDuo.value(new String[]{ "white", "red" })
@@ -53,6 +59,7 @@ public void testWhiteAndRed() {
5359

5460
@Disabled("Remove to run test")
5561
@Test
62+
@DisplayName("Black and brown, one-digit")
5663
public void testBlackAndBrownOneDigit() {
5764
assertThat(
5865
resistorColorDuo.value(new String[]{ "black", "brown" })
@@ -61,6 +68,7 @@ public void testBlackAndBrownOneDigit() {
6168

6269
@Disabled("Remove to run test")
6370
@Test
71+
@DisplayName("Ignore additional colors")
6472
public void testIgnoreAdditionalColors() {
6573
assertThat(
6674
resistorColorDuo.value(new String[]{ "green", "brown", "orange" })

0 commit comments

Comments
 (0)