Skip to content
Snippets Groups Projects
Commit 86c957e9 authored by Scott Langum Du Plessis's avatar Scott Langum Du Plessis
Browse files

Merge branch 'add-unit-tests' into 'main'

Add unit tests

See merge request !4
parents 97aa40c7 5f44deb5
Branches
No related tags found
1 merge request!4Add unit tests
...@@ -23,21 +23,19 @@ public class HandOfCards { ...@@ -23,21 +23,19 @@ public class HandOfCards {
public int getSum() { public int getSum() {
return hand.stream() return hand.stream()
.mapToInt(PlayingCard::getFace) .mapToInt(PlayingCard::getFace)
.sum(); .reduce(0, Integer::sum);
} }
public String findHearts() { public String findHearts() {
StringBuilder sb = new StringBuilder(); String hearts = hand.stream()
hand.stream()
.map(PlayingCard::getAsString) .map(PlayingCard::getAsString)
.filter(s -> s.charAt(0) == 'H') .filter(s -> s.charAt(0) == 'H')
.forEach(sb::append); .collect(Collectors.joining(" "));
if (sb.isEmpty()) { if (hearts.isEmpty()) {
return "No hearts"; return "No hearts";
} }
return sb.toString(); return hearts.trim();
} }
public boolean containsQueenOfSpades() { public boolean containsQueenOfSpades() {
......
package edu.ntnu.stud.cardgame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class DeckOfCardsTest {
DeckOfCards deck;
@BeforeEach
void setDeck() {
deck = new DeckOfCards();
}
@Nested
public class PositiveTests{
@Test
void testDeckFiller(){
assertEquals(deck.getDeck().size(), 52);
assertTrue(deck.getDeck().contains(new PlayingCard('H', 10)));
assertTrue(deck.getDeck().contains(new PlayingCard('D', 1)));
assertTrue(deck.getDeck().contains(new PlayingCard('C', 5)));
}
}
}
package edu.ntnu.stud.cardgame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class HandOfCardsTest {
HandOfCards hand;
PlayingCard card1;
PlayingCard card2;
PlayingCard card3;
PlayingCard card4;
@BeforeEach
void setHand() {
hand = new HandOfCards();
card1 = new PlayingCard('H', 12);
card2 = new PlayingCard('D', 2);
card3 = new PlayingCard('C', 7);
card4 = new PlayingCard('S', 12);
}
@Nested
class PositiveTests {
@Test
void testAddCard() {
hand.addCard(card1);
assertTrue(hand.getHand().contains(card1));
}
@Test
void testGetSum() {
hand.addCard(card1);
hand.addCard(card2);
hand.addCard(card3);
assertEquals(hand.getSum(),21);
}
@Test
void testFindHearts() {
hand.addCard(card1);
assertEquals(hand.findHearts(), card1.getAsString());
}
@Test
void testContainsQueenOfSpades(){
assertFalse(hand.containsQueenOfSpades());
hand.addCard(card4);
assertTrue(hand.containsQueenOfSpades());
}
@Test
void testContains5Flush() {
assertFalse(hand.contains5Flush());
for (int i = 0; i<5; i++){
hand.addCard(card2);
}
assertTrue(hand.contains5Flush());
}
}
}
package edu.ntnu.stud.cardgame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class PlayingCardTest {
PlayingCard card1;
PlayingCard card2;
@BeforeEach
public void setUp() {
card1 = new PlayingCard('H', 3);
}
@Nested
public class PositiveTests {
@Test
void testGetAsString() {
assertEquals("H3", card1.getAsString());
}
}
@Nested
public class NegativeTests {
@Test
void testConstructor() {
assertThrows(IllegalArgumentException.class, () -> card2 = new PlayingCard('E', 10));
assertThrows(IllegalArgumentException.class, () -> card2 = new PlayingCard('D', 90));
assertThrows(IllegalArgumentException.class, () -> card2 = new PlayingCard('H', 0));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment