forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBiCardSource.java
More file actions
111 lines (94 loc) · 2.86 KB
/
TestBiCardSource.java
File metadata and controls
111 lines (94 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter5;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public class TestBiCardSource
{
/*
* This stub class takes either no card or a single card that
* can be used to verify which of the two card sources in the
* BiCardSource was the origin of the card.
*/
private static class CardSourceStub implements CardSource
{
private Optional<Card> aCard = Optional.empty();
CardSourceStub() {}
CardSourceStub(Card pCard)
{
aCard = Optional.of(pCard);
}
@Override
public Card draw()
{
assert !isEmpty();
Card card = aCard.get();
aCard = Optional.empty();
return card;
}
@Override
public boolean isEmpty()
{
return !aCard.isPresent();
}
}
private static final Card ACE = Card.get(Rank.ACE, Suit.CLUBS);
private static final Card TWO = Card.get(Rank.TWO, Suit.CLUBS);
/*
* The field initialization relies on the fact that in JUnit,
* a new instance of the test class is created for each test
* method. The solution for the first edition of the book
* initialized the field in a method annotated with @BeforeEach
* instead.
*/
private CardSource aEmpty = new CardSourceStub();
private CardSource aContainsAce = new CardSourceStub(ACE);;
private CardSource aContainsTwo = new CardSourceStub(TWO);;
@Test
public void testEmpty_True()
{
assertTrue(new BiCardSource(aEmpty, aEmpty).isEmpty());
}
@Test
public void testEmpty_False_FirstEmpty()
{
assertFalse(new BiCardSource(aEmpty, aContainsAce).isEmpty());
}
@Test
public void testEmpty_False_SecondEmpty()
{
assertFalse(new BiCardSource(aContainsAce, aEmpty).isEmpty());
}
@Test
public void testEmpty_False_NeitherEmpty()
{
assertFalse(new BiCardSource(aContainsAce, aContainsTwo).isEmpty());
}
@Test
public void testDraw_FirstEmpty()
{
assertSame(ACE, new BiCardSource(aEmpty, aContainsAce).draw());
}
@Test
public void testDraw_SecondEmpty()
{
assertSame(ACE, new BiCardSource(aContainsAce, aEmpty).draw());
}
@Test
public void testDraw_NeitherEmpty()
{
assertSame(ACE, new BiCardSource(aContainsAce, aContainsTwo).draw());
}
}