|
| 1 | +package fr.univ_amu.iut.exercice2; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +/** |
| 9 | + * Kata 2 - Tennis scoring. |
| 10 | + * |
| 11 | + * <p>Progression TDD conçue pour du pair programming ping-pong : chaque test ajoute exactement un |
| 12 | + * petit élément à l'implémentation. Résistez à la tentation d'écrire la machine à états complète |
| 13 | + * dès le premier "0-0". |
| 14 | + * |
| 15 | + * <ul> |
| 16 | + * <li>Tests 1 à 6 : les comptages simples avant qu'un joueur atteigne 3 points |
| 17 | + * <li>Tests 7 à 11 : les libellés "15", "30", "40" pour le premier et le second joueur |
| 18 | + * <li>Tests 12 à 14 : la gestion des égalités puis Deuce |
| 19 | + * <li>Tests 15 à 18 : Advantage (aller / retour à Deuce) |
| 20 | + * <li>Tests 19 à 22 : Win (plusieurs scénarios) |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +class JeuDeTennisTest { |
| 24 | + |
| 25 | + private JeuDeTennis jeu; |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void setUp() { |
| 29 | + jeu = new JeuDeTennis("Alice", "Bob"); |
| 30 | + } |
| 31 | + |
| 32 | + // ========= Score initial ========= |
| 33 | + |
| 34 | + @Test |
| 35 | + void le_score_initial_est_0_0() { |
| 36 | + assertThat(jeu.getScore()).isEqualTo("0-0"); |
| 37 | + } |
| 38 | + |
| 39 | + // ========= Points simples d'Alice ========= |
| 40 | + |
| 41 | + @Test |
| 42 | + void apres_un_point_d_alice_le_score_est_15_0() { |
| 43 | + jeu.marquerPoint("Alice"); |
| 44 | + assertThat(jeu.getScore()).isEqualTo("15-0"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void apres_deux_points_d_alice_le_score_est_30_0() { |
| 49 | + jeu.marquerPoint("Alice"); |
| 50 | + jeu.marquerPoint("Alice"); |
| 51 | + assertThat(jeu.getScore()).isEqualTo("30-0"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void apres_trois_points_d_alice_le_score_est_40_0() { |
| 56 | + for (int i = 0; i < 3; i++) jeu.marquerPoint("Alice"); |
| 57 | + assertThat(jeu.getScore()).isEqualTo("40-0"); |
| 58 | + } |
| 59 | + |
| 60 | + // ========= Points simples de Bob ========= |
| 61 | + |
| 62 | + @Test |
| 63 | + void apres_un_point_de_bob_le_score_est_0_15() { |
| 64 | + jeu.marquerPoint("Bob"); |
| 65 | + assertThat(jeu.getScore()).isEqualTo("0-15"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void apres_deux_points_de_bob_le_score_est_0_30() { |
| 70 | + jeu.marquerPoint("Bob"); |
| 71 | + jeu.marquerPoint("Bob"); |
| 72 | + assertThat(jeu.getScore()).isEqualTo("0-30"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void apres_trois_points_de_bob_le_score_est_0_40() { |
| 77 | + for (int i = 0; i < 3; i++) jeu.marquerPoint("Bob"); |
| 78 | + assertThat(jeu.getScore()).isEqualTo("0-40"); |
| 79 | + } |
| 80 | + |
| 81 | + // ========= Mélanges, pour exercer la matrice ========= |
| 82 | + |
| 83 | + @Test |
| 84 | + void apres_un_point_chacun_le_score_est_15_15() { |
| 85 | + jeu.marquerPoint("Alice"); |
| 86 | + jeu.marquerPoint("Bob"); |
| 87 | + assertThat(jeu.getScore()).isEqualTo("15-15"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void apres_deux_points_d_alice_et_un_point_de_bob_le_score_est_30_15() { |
| 92 | + jeu.marquerPoint("Alice"); |
| 93 | + jeu.marquerPoint("Alice"); |
| 94 | + jeu.marquerPoint("Bob"); |
| 95 | + assertThat(jeu.getScore()).isEqualTo("30-15"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void apres_trois_points_d_alice_et_un_point_de_bob_le_score_est_40_15() { |
| 100 | + for (int i = 0; i < 3; i++) jeu.marquerPoint("Alice"); |
| 101 | + jeu.marquerPoint("Bob"); |
| 102 | + assertThat(jeu.getScore()).isEqualTo("40-15"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void apres_trois_points_d_alice_et_deux_points_de_bob_le_score_est_40_30() { |
| 107 | + for (int i = 0; i < 3; i++) jeu.marquerPoint("Alice"); |
| 108 | + for (int i = 0; i < 2; i++) jeu.marquerPoint("Bob"); |
| 109 | + assertThat(jeu.getScore()).isEqualTo("40-30"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void apres_deux_points_chacun_le_score_est_30_30() { |
| 114 | + for (int i = 0; i < 2; i++) jeu.marquerPoint("Alice"); |
| 115 | + for (int i = 0; i < 2; i++) jeu.marquerPoint("Bob"); |
| 116 | + assertThat(jeu.getScore()).isEqualTo("30-30"); |
| 117 | + } |
| 118 | + |
| 119 | + // ========= Deuce ========= |
| 120 | + |
| 121 | + @Test |
| 122 | + void apres_trois_points_chacun_le_score_est_egalite() { |
| 123 | + for (int i = 0; i < 3; i++) { |
| 124 | + jeu.marquerPoint("Alice"); |
| 125 | + jeu.marquerPoint("Bob"); |
| 126 | + } |
| 127 | + assertThat(jeu.getScore()).isEqualTo("Égalité"); |
| 128 | + } |
| 129 | + |
| 130 | + // ========= Advantage ========= |
| 131 | + |
| 132 | + @Test |
| 133 | + void apres_egalite_et_un_point_d_alice_le_score_est_avantage_alice() { |
| 134 | + for (int i = 0; i < 3; i++) { |
| 135 | + jeu.marquerPoint("Alice"); |
| 136 | + jeu.marquerPoint("Bob"); |
| 137 | + } |
| 138 | + jeu.marquerPoint("Alice"); |
| 139 | + assertThat(jeu.getScore()).isEqualTo("Avantage Alice"); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void apres_egalite_et_un_point_de_bob_le_score_est_avantage_bob() { |
| 144 | + for (int i = 0; i < 3; i++) { |
| 145 | + jeu.marquerPoint("Alice"); |
| 146 | + jeu.marquerPoint("Bob"); |
| 147 | + } |
| 148 | + jeu.marquerPoint("Bob"); |
| 149 | + assertThat(jeu.getScore()).isEqualTo("Avantage Bob"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void apres_avantage_alice_et_un_point_de_bob_le_score_revient_a_egalite() { |
| 154 | + for (int i = 0; i < 3; i++) { |
| 155 | + jeu.marquerPoint("Alice"); |
| 156 | + jeu.marquerPoint("Bob"); |
| 157 | + } |
| 158 | + jeu.marquerPoint("Alice"); |
| 159 | + jeu.marquerPoint("Bob"); |
| 160 | + assertThat(jeu.getScore()).isEqualTo("Égalité"); |
| 161 | + } |
| 162 | + |
| 163 | + // ========= Win ========= |
| 164 | + |
| 165 | + @Test |
| 166 | + void apres_avantage_alice_et_un_point_d_alice_alice_a_gagne() { |
| 167 | + for (int i = 0; i < 3; i++) { |
| 168 | + jeu.marquerPoint("Alice"); |
| 169 | + jeu.marquerPoint("Bob"); |
| 170 | + } |
| 171 | + jeu.marquerPoint("Alice"); |
| 172 | + jeu.marquerPoint("Alice"); |
| 173 | + assertThat(jeu.getScore()).isEqualTo("Jeu pour Alice"); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void apres_quatre_points_d_alice_alice_a_gagne() { |
| 178 | + for (int i = 0; i < 4; i++) jeu.marquerPoint("Alice"); |
| 179 | + assertThat(jeu.getScore()).isEqualTo("Jeu pour Alice"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void apres_quatre_points_de_bob_bob_a_gagne() { |
| 184 | + for (int i = 0; i < 4; i++) jeu.marquerPoint("Bob"); |
| 185 | + assertThat(jeu.getScore()).isEqualTo("Jeu pour Bob"); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void apres_quatre_points_d_alice_et_un_point_de_bob_alice_a_gagne() { |
| 190 | + for (int i = 0; i < 4; i++) { |
| 191 | + jeu.marquerPoint("Alice"); |
| 192 | + if (i == 1) jeu.marquerPoint("Bob"); |
| 193 | + } |
| 194 | + assertThat(jeu.getScore()).isEqualTo("Jeu pour Alice"); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + void apres_quatre_points_d_alice_et_deux_points_de_bob_alice_a_gagne() { |
| 199 | + for (int i = 0; i < 4; i++) jeu.marquerPoint("Alice"); |
| 200 | + jeu.marquerPoint("Bob"); |
| 201 | + jeu.marquerPoint("Bob"); |
| 202 | + assertThat(jeu.getScore()).isEqualTo("Jeu pour Alice"); |
| 203 | + } |
| 204 | +} |
0 commit comments