Repository navigation
Expand file tree
/
Copy pathTests.java
More file actions
203 lines (175 loc) · 5.26 KB
/
Copy pathTests.java
File metadata and controls
203 lines (175 loc) · 5.26 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JLabel;
import org.junit.Test;
public class Tests {
JLabel testScore = new JLabel();
@Test
public void snakeMovingNormally() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
for (int i = 0; i < 5; i++)
testGame.tick();
assertEquals(180, testGame.snake.get(0).x);
assertEquals(160, testGame.snake.get(1).x);
assertEquals(140, testGame.snake.get(2).x);
}
@Test
public void snakeHitsWall() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
for (int i = 0; i < 15; i++)
testGame.tick();
assertFalse(testGame.playing);
}
@Test
public void snakeHitsTail() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
for (int i = 0; i < 5; i++) { // Makes the snake long enough to hit itself
testGame.snake.add(new Piece());
testGame.tick();
}
// Loop the snake around into its tail
testGame.right = false;
testGame.up = true;
testGame.lastRight = false;
testGame.tick();
testGame.up = false;
testGame.left = true;
testGame.tick();
testGame.left = false;
testGame.down = true;
testGame.tick();
assertFalse(testGame.playing);
}
@Test
public void snakeEatsPiece() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food = new Piece(100,160);
testGame.tick();
assertEquals(4, testGame.snake.size());
assertEquals(1, testGame.score);
}
@Test
public void snakeEatsSuperFood() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food.x = 0; // Just in case food randomly spawns right in front of snake
testGame.powerUp = new SuperFood(100,160);
testGame.powerUpActive = true;
testGame.tick();
assertEquals(11, testGame.snake.size());
assertEquals(5, testGame.score);
}
@Test
public void snakeEatsPoisonFood() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food.x = 0;
for (int i = 0; i < 10; i++) { // Makes the snake long enough to not go to zero
testGame.score = 5;
testGame.snake.add(new Piece());
}
testGame.powerUp = new PoisonFood(100,160);
testGame.powerUpActive = true;
testGame.tick();
assertEquals(5, testGame.snake.size());
assertEquals(2, testGame.score);
}
@Test
public void snakeEatsPoisonFoodAtStart() { //Length needs to stay above 1 and score above 0
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food.x = 0;
testGame.powerUp = new PoisonFood(100,160);
testGame.powerUpActive = true;
testGame.tick();
assertEquals(2, testGame.snake.size());
assertEquals(0, testGame.score);
}
@Test
public void snakeEatsSlowFood() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food.x = 0;
testGame.powerUp = new SlowFood(100,160);
testGame.powerUpActive = true;
testGame.tick();
testGame.tick();
testGame.tick();
assertEquals(120, testGame.snake.get(0).x); // Moved 1/2 as far as normal
assertEquals(1, testGame.score);
}
@Test
public void snakeEatsFastFood() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.food.x = 0;
testGame.powerUp = new FastFood(100,160);
testGame.powerUpActive = true;
testGame.tick();
testGame.tick();
testGame.tick();
assertEquals(160, testGame.snake.get(0).x); // Moves faster than normal
assertEquals(6, testGame.score);
}
/* Tests writing the highscore. The test passes, but it writes to the highscore file each time
* it is run, so it is temporarily commented out as to not interfere with the current leaderboard.
@Test
public void writeReadHighscore() {
GameBoard testGame = new GameBoard(testScore);
testGame.start();
testGame.score = 999;
for (int i = 0; i < 15; i++)
testGame.tick();
assertFalse(testGame.playing);
try {
BufferedReader br = new BufferedReader(new FileReader("leaderboard.txt"));
String line = br.readLine().substring(0,3);
br.close();
assertEquals("999", line);
} catch (IOException e) {
assertTrue(false);
}
}
*/
@Test
public void writeHighscoreFakeFile() {
try {
BufferedReader br = new BufferedReader(new FileReader("fakeFile.txt"));
String line = br.readLine().substring(0,3);
br.close();
assertEquals("999", line);
} catch (IOException e) {
assertTrue(true);
}
}
@Test
public void soundFileFormatWorks() {
try {
File sound = new File("eatingSound.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(sound));
clip.start();
assertTrue(true);
} catch (Exception e) {
assertTrue(false);
}
}
@Test
public void dontSetNick() {
assertEquals("Nickname ", Game.getNick());
}
@Test
public void setNickTooLong() {
Game.nick.setText("abcdefghijklmnop");
assertEquals("abcdefghij", Game.getNick());
}
}