|
| 1 | +package com.baeldung.patterns; |
| 2 | + |
| 3 | +import com.baeldung.patterns.data.Roll; |
| 4 | +import com.baeldung.patterns.data.Turn; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import static com.baeldung.patterns.Yahtzee.*; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | + |
| 16 | +class YahtzeeTest { |
| 17 | + |
| 18 | + public static Stream<Arguments> whenThePlayerChoosesAStrategy_thenCalculateCorrectScore() { |
| 19 | + return Stream.of( |
| 20 | + Arguments.of(List.of(3, 3, 3, 4, 4), "ONE_PAIR", 8), |
| 21 | + Arguments.of(List.of(3, 3, 3, 4, 4), "THREE_OF_A_KIND", 9), |
| 22 | + Arguments.of(List.of(1, 2, 2, 4, 4), "ONE_PAIR", 8), |
| 23 | + Arguments.of(List.of(1, 2, 2, 2, 5), "THREE_OF_A_KIND", 6), |
| 24 | + Arguments.of(List.of(1, 1, 1, 1, 5), "ONE_PAIR", 2), |
| 25 | + Arguments.of(List.of(1, 1, 1, 1, 5), "THREE_OF_A_KIND", 3) |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest |
| 30 | + @MethodSource |
| 31 | + void whenThePlayerChoosesAStrategy_thenCalculateCorrectScore(List<Integer> dices, String strategyStr, Integer expectedScore) { |
| 32 | + enqueueFakeDiceValues(dices); |
| 33 | + |
| 34 | + Roll roll = roll(); |
| 35 | + Turn play = chooseStrategy(roll, strategyStr); |
| 36 | + int score = score(play); |
| 37 | + |
| 38 | + assertEquals(expectedScore, score); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void whenThePlayerRerollsAndChoosesTwoPairs_thenCalculateCorrectScore() { |
| 43 | + enqueueFakeDiceValues(1, 1, 2, 2, 3, 5, 5); |
| 44 | + |
| 45 | + Roll roll = roll(); // => { dice: [1,1,2,2,3] } |
| 46 | + roll = rerollValues(roll, 1, 1); // => { dice: [5,5,2,2,3] } |
| 47 | + Turn turn = chooseStrategy(roll, "TWO_PAIRS"); |
| 48 | + int score = score(turn); |
| 49 | + |
| 50 | + assertEquals(14, score); |
| 51 | + } |
| 52 | + |
| 53 | + private static void enqueueFakeDiceValues(List<Integer> values) { |
| 54 | + Yahtzee.diceValueGenerator = values.iterator()::next; |
| 55 | + } |
| 56 | + |
| 57 | + private static void enqueueFakeDiceValues(Integer... values) { |
| 58 | + enqueueFakeDiceValues(List.of(values)); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments