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: 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 e32f60c..cf83cf0 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 { @@ -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 InvalidRollSizeError { + if(roll.size() != Yams.DICES_AMOUNT) { + throw new InvalidRollSizeError(roll.size()); + } return switch (category) { case CategoryEnum.CHANCE -> this.handleChanceScore(roll); case CategoryEnum.YAMS -> this.handleYamsScore(roll); diff --git a/src/test/java/io/github/vbetsch/yams/YamsTest.java b/src/test/java/io/github/vbetsch/yams/YamsTest.java index 30c58d1..b12d4c9 100644 --- a/src/test/java/io/github/vbetsch/yams/YamsTest.java +++ b/src/test/java/io/github/vbetsch/yams/YamsTest.java @@ -225,4 +225,46 @@ void fourOfAKind_returnIllegalArgumentException_whenGivenLargeStraight() { "We cannot compute score with category FourOfAKind for this roll" ); } + + @Test + void shouldThrowInvalidRollSizeErrorWhenGivenLessThanFiveDices() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(1, 2, 3, 4); + + // Act & Assert + assertThrows( + InvalidRollSizeError.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } + + @Test + void shouldThrowInvalidRollSizeErrorWhenGivenMoreThanFiveDices() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(1, 2, 3, 4, 5, 6); + + // Act & Assert + assertThrows( + InvalidRollSizeError.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } + + @Test + void shouldThrowInvalidRollSizeErrorWhenGivenNoDice() { + // Arrange + Yams yams = new Yams(); + List roll = List.of(); + + // Act & Assert + assertThrows( + InvalidRollSizeError.class, + () -> yams.computeScore(roll, CategoryEnum.CHANCE), + "Roll must contain exactly 5 dices" + ); + } }