mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-13 17:11:49 +00:00
feat: dev-web - add deck and card tests (all are passed)
This commit is contained in:
@@ -61,4 +61,12 @@ public class Card {
|
||||
public Value getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Card{" +
|
||||
"color=" + color +
|
||||
", value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class Deck {
|
||||
* @throws IllegalArgumentException si le nombre de couleurs ou de valeurs est incorrect
|
||||
* @return un ensemble de cartes
|
||||
*/
|
||||
private static ArrayList<Card> createSetOfCard(int nbColors, int nbValues) throws IllegalArgumentException {
|
||||
public static ArrayList<Card> createSetOfCard(int nbColors, int nbValues) throws IllegalArgumentException {
|
||||
ArrayList<Card> cards = new ArrayList<>(nbColors*nbValues);
|
||||
|
||||
if (nbColors < 1 || nbColors > Card.Color.values().length) {
|
||||
@@ -89,7 +89,7 @@ public class Deck {
|
||||
* @param cards ensemble de cartes à mélanger
|
||||
* @return un ensemble de cartes mélangées
|
||||
*/
|
||||
private static void shuffleSetOfCard(ArrayList<Card> cards) {
|
||||
public static void shuffleSetOfCard(ArrayList<Card> cards) {
|
||||
Collections.shuffle(cards);
|
||||
}
|
||||
|
||||
|
||||
@@ -90,4 +90,32 @@ class CardTest {
|
||||
assertEquals(expected[i], FIXTURE[i].getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void to_string_return_right_format() {
|
||||
String[] expected = new String[] {
|
||||
"Card{color=HEART, value=ONE}",
|
||||
"Card{color=HEART, value=TWO}",
|
||||
"Card{color=HEART, value=THREE}",
|
||||
"Card{color=CLUBS, value=FOUR}",
|
||||
"Card{color=CLUBS, value=FIVE}",
|
||||
"Card{color=CLUBS, value=SIX}",
|
||||
"Card{color=DIAMONDS, value=SEVEN}",
|
||||
"Card{color=DIAMONDS, value=EIGHT}",
|
||||
"Card{color=DIAMONDS, value=NINE}",
|
||||
"Card{color=SPADES, value=TEN}",
|
||||
"Card{color=SPADES, value=JACK}",
|
||||
"Card{color=SPADES, value=QUEEN}",
|
||||
"Card{color=HEART, value=KING}",
|
||||
|
||||
"Card{color=HEART, value=ONE}",
|
||||
"Card{color=CLUBS, value=ONE}",
|
||||
"Card{color=DIAMONDS, value=ONE}",
|
||||
"Card{color=SPADES, value=ONE}",
|
||||
};
|
||||
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
assertEquals(expected[i], FIXTURE[i].toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package uppa.project.pojo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -18,38 +20,113 @@ class DeckTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_error_on_creation_of_invalid_deck() {
|
||||
void throw_error_on_creation_of_invalid_deck_with_an_invalid_color() {
|
||||
// Nombre de couleurs invalides
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(0, 13));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(5, 13));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(5, 10));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(8, 7));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(-1, 11));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(-6, 12));
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_error_on_creation_of_invalid_deck_with_an_invalid_value() {
|
||||
// Nombre de valeurs invalides
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(1, 0));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(1, 15));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(2, 14));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Deck(3, -2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_cards() {
|
||||
// TODO Implement this method
|
||||
fail();
|
||||
void create_deck_get_the_right_number_cards() {
|
||||
assertEquals(52, new Deck(4,13).getCards().size());
|
||||
assertEquals(33, new Deck(3,11).getCards().size());
|
||||
assertEquals(26, new Deck(2,13).getCards().size());
|
||||
assertEquals(1, new Deck(1,1).getCards().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialize_deck() {
|
||||
// TODO Implement this method
|
||||
fail();
|
||||
void create_deck_get_the_right_number_colors() {
|
||||
assertEquals(4, new Deck(4,13).getCards().stream().map(Card::getColor).distinct().count());
|
||||
assertEquals(3, new Deck(3,10).getCards().stream().map(Card::getColor).distinct().count());
|
||||
assertEquals(2, new Deck(2,7).getCards().stream().map(Card::getColor).distinct().count());
|
||||
assertEquals(1, new Deck(1,1).getCards().stream().map(Card::getColor).distinct().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_deck_get_the_right_number_values() {
|
||||
assertEquals(13, new Deck(4,13).getCards().stream().map(Card::getValue).distinct().count());
|
||||
assertEquals(10, new Deck(3,10).getCards().stream().map(Card::getValue).distinct().count());
|
||||
assertEquals(7, new Deck(2,7).getCards().stream().map(Card::getValue).distinct().count());
|
||||
assertEquals(1, new Deck(1,1).getCards().stream().map(Card::getValue).distinct().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shuffle_list_of_cards_set_them_in_an_aleatory_order(){
|
||||
ArrayList<Card> initialCards = new ArrayList<>();
|
||||
initialCards.add(new Card(Card.Color.HEART, Card.Value.ONE));
|
||||
initialCards.add(new Card(Card.Color.HEART, Card.Value.TWO));
|
||||
initialCards.add(new Card(Card.Color.HEART, Card.Value.THREE));
|
||||
initialCards.add(new Card(Card.Color.CLUBS, Card.Value.FOUR));
|
||||
initialCards.add(new Card(Card.Color.CLUBS, Card.Value.FIVE));
|
||||
initialCards.add(new Card(Card.Color.CLUBS, Card.Value.SIX));
|
||||
initialCards.add(new Card(Card.Color.DIAMONDS, Card.Value.SEVEN));
|
||||
initialCards.add(new Card(Card.Color.DIAMONDS, Card.Value.EIGHT));
|
||||
initialCards.add(new Card(Card.Color.DIAMONDS, Card.Value.NINE));
|
||||
initialCards.add(new Card(Card.Color.SPADES, Card.Value.TEN));
|
||||
initialCards.add(new Card(Card.Color.SPADES, Card.Value.JACK));
|
||||
initialCards.add(new Card(Card.Color.SPADES, Card.Value.QUEEN));
|
||||
ArrayList<Card> shuffledCards = new ArrayList<>(initialCards);
|
||||
ArrayList<Card> shuffledCards2 = new ArrayList<>(initialCards);
|
||||
Deck.shuffleSetOfCard(shuffledCards);
|
||||
Deck.shuffleSetOfCard(shuffledCards2);
|
||||
|
||||
assertNotEquals(initialCards, shuffledCards);
|
||||
assertNotEquals(initialCards, shuffledCards2);
|
||||
assertNotEquals(shuffledCards, shuffledCards2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialize_deck_shuffle_cards() {
|
||||
Deck mockDeck = new Deck(4, 13);
|
||||
Deck mockDeck2 = new Deck(4, 13);
|
||||
assertNotEquals(mockDeck.getCards(), mockDeck2.getCards());
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_same_size_when_shuffle() {
|
||||
// TODO Implement this method
|
||||
fail();
|
||||
Deck mockDeck = new Deck(4, 13);
|
||||
int initialSize = mockDeck.getCards().size();
|
||||
Deck.shuffleSetOfCard(mockDeck.getCards());
|
||||
assertEquals(initialSize, mockDeck.getCards().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_same_cards_when_shuffle(){
|
||||
// TODO Implement this method
|
||||
fail();
|
||||
Deck mockDeck = new Deck(4, 13);
|
||||
List<Card> initialCards = new ArrayList<>(mockDeck.getCards());
|
||||
Deck.shuffleSetOfCard(mockDeck.getCards());
|
||||
for (int i = 0; i < initialCards.size(); i++) {
|
||||
assertTrue(mockDeck.getCards().contains(initialCards.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_multiple_deck_with_same_parameters_get_same_cards() {
|
||||
Deck mockDeck = new Deck(4, 13);
|
||||
Deck mockDeck2 = new Deck(4, 13);
|
||||
ArrayList<Card> cards = mockDeck.getCards();
|
||||
ArrayList<Card> cards2 = mockDeck2.getCards();
|
||||
int counter = 0;
|
||||
for (int i = 0; i < cards.size(); i++) {
|
||||
for(int j = 0; j < cards2.size(); j++) {
|
||||
if (cards.get(i).getColor() == cards2.get(j).getColor()
|
||||
&& cards.get(i).getValue() == cards2.get(j).getValue()) {
|
||||
counter++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals(cards.size(), counter);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user