From a93ec035f0ddcde56583762b7db5eabbef120edf Mon Sep 17 00:00:00 2001 From: olivierfalahi Date: Tue, 6 Oct 2026 20:04:44 +0200 Subject: [PATCH 1/5] test(fail): add test case for handling rolls with fewer than five dice --- src/test/java/io/github/vbetsch/yams/YamsTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/io/github/vbetsch/yams/YamsTest.java b/src/test/java/io/github/vbetsch/yams/YamsTest.java index 30c58d1..db3ac87 100644 --- a/src/test/java/io/github/vbetsch/yams/YamsTest.java +++ b/src/test/java/io/github/vbetsch/yams/YamsTest.java @@ -225,4 +225,18 @@ void fourOfAKind_returnIllegalArgumentException_whenGivenLargeStraight() { "We cannot compute score with category FourOfAKind for this roll" ); } + + @Test + void shouldThrowIllegalArgumentExceptionWhenGivenLessThanFiveDices() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(1, 2, 3, 4); + + // Act & Assert + assertThrows( + IllegalArgumentException.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } } From c4d6c3f23a059dc3adcc34018f4e9202820819db Mon Sep 17 00:00:00 2001 From: olivierfalahi Date: Tue, 6 Oct 2026 20:06:11 +0200 Subject: [PATCH 2/5] test(pass): validate dice roll size before score computation --- src/main/java/io/github/vbetsch/yams/Yams.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/vbetsch/yams/Yams.java b/src/main/java/io/github/vbetsch/yams/Yams.java index e32f60c..739b73c 100644 --- a/src/main/java/io/github/vbetsch/yams/Yams.java +++ b/src/main/java/io/github/vbetsch/yams/Yams.java @@ -60,7 +60,10 @@ private int handleFourOfAKindScore(List roll) throws IllegalArgumentExc return this.calculateSumOfDices(roll); } - public int computeScore(List roll, CategoryEnum category) { + public int computeScore(List roll, CategoryEnum category) throws IllegalArgumentException { + if(roll.size() != this.DICES_AMOUNT) { + throw new IllegalArgumentException("Roll must contain exactly " + this.DICES_AMOUNT + " dices"); + } return switch (category) { case CategoryEnum.CHANCE -> this.handleChanceScore(roll); case CategoryEnum.YAMS -> this.handleYamsScore(roll); From 9a618d9880e066e926a5380935fc4e90d25af96d Mon Sep 17 00:00:00 2001 From: olivierfalahi Date: Tue, 6 Oct 2026 20:23:31 +0200 Subject: [PATCH 3/5] test(refactor): extract `InvalidRollSizeError` for detailed error messaging and replace hardcoded validations --- .../vbetsch/yams/InvalidRollSizeError.java | 7 +++++ .../java/io/github/vbetsch/yams/Yams.java | 8 +++--- .../java/io/github/vbetsch/yams/YamsTest.java | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/vbetsch/yams/InvalidRollSizeError.java diff --git a/src/main/java/io/github/vbetsch/yams/InvalidRollSizeError.java b/src/main/java/io/github/vbetsch/yams/InvalidRollSizeError.java new file mode 100644 index 0000000..cc71e7c --- /dev/null +++ b/src/main/java/io/github/vbetsch/yams/InvalidRollSizeError.java @@ -0,0 +1,7 @@ +package io.github.vbetsch.yams; + +public class InvalidRollSizeError extends IllegalArgumentException { + public InvalidRollSizeError(int actualSize) { + super("Roll must contain exactly " + Yams.DICES_AMOUNT + " dices but got " + actualSize); + } +} diff --git a/src/main/java/io/github/vbetsch/yams/Yams.java b/src/main/java/io/github/vbetsch/yams/Yams.java index 739b73c..5e14a37 100644 --- a/src/main/java/io/github/vbetsch/yams/Yams.java +++ b/src/main/java/io/github/vbetsch/yams/Yams.java @@ -4,7 +4,7 @@ import java.util.stream.IntStream; public class Yams { - private final int DICES_AMOUNT = 5; + static final int DICES_AMOUNT = 5; private int calculateSumOfDices(List roll) { return roll @@ -43,7 +43,7 @@ private boolean containsDuplicatesNth(List roll, int duplicatesNumber) .stream() .mapToInt(Integer::intValue) .distinct(); - return reducedRoll.count() == (this.DICES_AMOUNT - duplicatesNumber + 1); + return reducedRoll.count() == (Yams.DICES_AMOUNT - duplicatesNumber + 1); } private int handleThreeOfAKindScore(List roll) throws IllegalArgumentException { @@ -61,8 +61,8 @@ private int handleFourOfAKindScore(List roll) throws IllegalArgumentExc } public int computeScore(List roll, CategoryEnum category) throws IllegalArgumentException { - if(roll.size() != this.DICES_AMOUNT) { - throw new IllegalArgumentException("Roll must contain exactly " + this.DICES_AMOUNT + " dices"); + if(roll.size() != Yams.DICES_AMOUNT) { + throw new InvalidRollSizeError(roll.size()); } return switch (category) { case CategoryEnum.CHANCE -> this.handleChanceScore(roll); diff --git a/src/test/java/io/github/vbetsch/yams/YamsTest.java b/src/test/java/io/github/vbetsch/yams/YamsTest.java index db3ac87..cffde6e 100644 --- a/src/test/java/io/github/vbetsch/yams/YamsTest.java +++ b/src/test/java/io/github/vbetsch/yams/YamsTest.java @@ -239,4 +239,32 @@ void shouldThrowIllegalArgumentExceptionWhenGivenLessThanFiveDices() { "Roll must contain exactly 5 dices" ); } + + @Test + void shouldThrowIllegalArgumentExceptionWhenGivenMoreThanFiveDices() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(1, 2, 3, 4, 5, 6); + + // Act & Assert + assertThrows( + IllegalArgumentException.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } + + @Test + void shouldThrowIllegalArgumentExceptionWhenGivenNoDice() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(); + + // Act & Assert + assertThrows( + IllegalArgumentException.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } } From 0c1af360c63fb654636e9f812d83ccefec5262e9 Mon Sep 17 00:00:00 2001 From: olivierfalahi Date: Tue, 6 Oct 2026 20:36:18 +0200 Subject: [PATCH 4/5] ci: trigger workflow on pull requests --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be05b70..76d1a63 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,7 @@ name: CI on: push: + pull_request: jobs: test: From 2df4bb7f8ed0fb383777ca76cbd68ede8c5975c8 Mon Sep 17 00:00:00 2001 From: olivierfalahi Date: Tue, 6 Oct 2026 21:23:13 +0200 Subject: [PATCH 5/5] test(refactor): replace `IllegalArgumentException` with `InvalidRollSizeError` and update test cases --- src/main/java/io/github/vbetsch/yams/Yams.java | 2 +- src/test/java/io/github/vbetsch/yams/YamsTest.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/vbetsch/yams/Yams.java b/src/main/java/io/github/vbetsch/yams/Yams.java index 5e14a37..cf83cf0 100644 --- a/src/main/java/io/github/vbetsch/yams/Yams.java +++ b/src/main/java/io/github/vbetsch/yams/Yams.java @@ -60,7 +60,7 @@ private int handleFourOfAKindScore(List roll) throws IllegalArgumentExc return this.calculateSumOfDices(roll); } - public int computeScore(List roll, CategoryEnum category) throws IllegalArgumentException { + public int computeScore(List roll, CategoryEnum category) throws InvalidRollSizeError { if(roll.size() != Yams.DICES_AMOUNT) { throw new InvalidRollSizeError(roll.size()); } diff --git a/src/test/java/io/github/vbetsch/yams/YamsTest.java b/src/test/java/io/github/vbetsch/yams/YamsTest.java index cffde6e..b12d4c9 100644 --- a/src/test/java/io/github/vbetsch/yams/YamsTest.java +++ b/src/test/java/io/github/vbetsch/yams/YamsTest.java @@ -227,42 +227,42 @@ void fourOfAKind_returnIllegalArgumentException_whenGivenLargeStraight() { } @Test - void shouldThrowIllegalArgumentExceptionWhenGivenLessThanFiveDices() { + void shouldThrowInvalidRollSizeErrorWhenGivenLessThanFiveDices() { // Arrange Yams yams = new Yams(); List roll = List.of(1, 2, 3, 4); // Act & Assert assertThrows( - IllegalArgumentException.class, + InvalidRollSizeError.class, () -> yams.computeScore(roll, CategoryEnum.CHANCE), "Roll must contain exactly 5 dices" ); } @Test - void shouldThrowIllegalArgumentExceptionWhenGivenMoreThanFiveDices() { + void shouldThrowInvalidRollSizeErrorWhenGivenMoreThanFiveDices() { // Arrange Yams yams = new Yams(); List roll = List.of(1, 2, 3, 4, 5, 6); // Act & Assert assertThrows( - IllegalArgumentException.class, + InvalidRollSizeError.class, () -> yams.computeScore(roll, CategoryEnum.CHANCE), "Roll must contain exactly 5 dices" ); } @Test - void shouldThrowIllegalArgumentExceptionWhenGivenNoDice() { + void shouldThrowInvalidRollSizeErrorWhenGivenNoDice() { // Arrange Yams yams = new Yams(); List roll = List.of(); // Act & Assert assertThrows( - IllegalArgumentException.class, + InvalidRollSizeError.class, () -> yams.computeScore(roll, CategoryEnum.CHANCE), "Roll must contain exactly 5 dices" );