-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerTest.java
173 lines (152 loc) · 6.21 KB
/
PlayerTest.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package game;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
/**
* series of unit tests for player class
* @version 1.1
*/
public class PlayerTest {
Player expectedPlayer;
Player testPlayer;
Card[] genericHand;
Card[] cardArrayl = new Card[28];
Card[] cardArrayr = new Card[28];
CardDeck cardDeckl;
CardDeck cardDeckr;
CardDeck differentDeck = new CardDeck(cardArrayl);//made just so it is different to the others
Card testCard;
Card expectedCard;
@Before
public void setUp() {
genericHand = new Card[4];
for(int i=0;i<4;i++){
genericHand[i] = new Card(i+1);}
cardDeckl = new CardDeck(cardArrayl);
cardDeckr = new CardDeck(cardArrayl);
cardDeckl.placeCard(new Card(1));
cardDeckr.placeCard(new Card(2));//done just to make both CardDecks original
}
/**
* Test to analyse behaviour of addCard() on a player whose hand has 4 cards already in it
*/
@Test
public void addCardToHandWith4Cards() {
Card[] testHand = new Card[4];
for(int i=0;i<4;i++){
testHand[i] = new Card(i);
}
testPlayer = new Player(1,testHand,cardDeckl,cardDeckr);
assertTrue(testPlayer.addCard(new Card(4)));//
}
/**
* Test to analyse behaviour of addCard() on a player whose hand has 5 cards in it
*/
@Test
public void addCardToHandWith5Cards(){
Card[] testHand = new Card[4];
for(int i=0;i<4;i++){
testHand[i] = new Card(i);
}
testPlayer = new Player(1,testHand,cardDeckl,cardDeckr); // player hand has 4 cards
testPlayer.addCard(new Card(5)); // players hand should now have 5 cards
assertFalse(testPlayer.addCard(new Card(6))); // should return false as you cant add anymore cards to a player who already has 5 cards in hand
}
/**
* Test to show winningHand() returns true on a winning hand
*/
@Test
public void testWinningHandTrue() {
Card[] winningHand = new Card[4];
for(int i=0;i<4;i++){
winningHand[i] = new Card(1);
testPlayer = new Player(1,winningHand,cardDeckl,cardDeckr);
assertTrue(testPlayer.winningHand());//asserts that if the player has a winning hand, will return true
}
}
/**
* Test to show winningHand() returns false on a non-winning hand
*/
@Test
public void testWinningHandFalse(){
testPlayer = new Player(1,genericHand,cardDeckl,cardDeckr);
assertFalse(testPlayer.winningHand());//converse for this assertion
}
/**
* Test that the method nonPreferredCard returns null and does not remove any cards from the players hand
* when there are no non-preferred cards in the players hand
*/
@Test
public void noNonPreferedCardInHand() {
Card[] winningHand = new Card[4];
for(int i=0;i<4;i++){
winningHand[i] = new Card(1);
//if the player has no non-prefered cards, will return null
testPlayer = new Player(1,winningHand,cardDeckl,cardDeckr);
expectedPlayer = new Player(1,winningHand,cardDeckl,cardDeckr);
assertNull(testPlayer.nonPreferedCard());
assertEquals(expectedPlayer,testPlayer);
}
}
/**
* Test to see whether the nonPreferredCard method works on a hand where the first non-preferred card is first in the hand
*/
@Test
public void nonPreferedCardIsFirst() {
genericHand = new Card[4];
Card[] testHand = new Card[4];
for(int i=0;i<4;i++){
genericHand[i] = new Card(i+1);
}
testHand[0] = new Card(1);
testHand[1] = new Card(3);
testHand[2] = new Card(4);
testPlayer = new Player(1,genericHand,cardDeckl,cardDeckr);
expectedPlayer = new Player(1,testHand,cardDeckl,cardDeckr);
assertEquals(new Card(2),testPlayer.nonPreferedCard());//1 is the preferred card, will return next non-preferred one,
assertEquals(expectedPlayer,testPlayer);
}
/**
* Test to see whether the nonPrefferedCard method works on a hand where the first non-preferred card is last in the hand
*/
@Test
public void nonPreferedCardIsLast() {
genericHand = new Card[4];
Card[] testHand = new Card[4];
for(int i=0;i<3;i++){
genericHand[i] = new Card(1);
testHand[i] = new Card(1);
}
genericHand[3] = new Card(2);
testPlayer = new Player(1,genericHand,cardDeckl,cardDeckr);
expectedPlayer = new Player(1,testHand,cardDeckl,cardDeckr);
expectedCard = new Card(2);
assertEquals(expectedCard,testPlayer.nonPreferedCard());//1 is the preferred card, will return next non-preferred one,
assertEquals(expectedPlayer,testPlayer);
}
/**
* Tests equals method on instances of Player in which the method should return true
*/
@Test
public void testEqualsTrue() {
//sets up instances of players to test equals() on
expectedPlayer = new Player(1,genericHand,cardDeckl,cardDeckr);
testPlayer = new Player(1,genericHand,cardDeckl,cardDeckr);
assertEquals(expectedPlayer,expectedPlayer); //player should be equal to itself
assertEquals(expectedPlayer,testPlayer); //should be equal by defined method
}
/**
* Tests equals method on instances of Player in which the method should return false
*/
@Test
public void testEqualsFalse(){
Player differentPreferencePlayer = new Player(2,genericHand,cardDeckl,cardDeckr);
Player differentHandPlayer = new Player(1,new Card[4],cardDeckl,cardDeckr);
Player differentLDeckPlayer = new Player(1,genericHand,differentDeck,cardDeckr);
Player differentRDeckPlayer = new Player(1,genericHand,cardDeckl,differentDeck);
assertNotEquals(expectedPlayer,differentPreferencePlayer);
assertNotEquals(expectedPlayer,differentHandPlayer);
assertNotEquals(expectedPlayer,differentLDeckPlayer);
assertNotEquals(expectedPlayer,differentRDeckPlayer);
}
}