-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSnakeTest.java
145 lines (110 loc) · 6.22 KB
/
SnakeTest.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
package com.battlesnake.starter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SnakeTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
private Snake.Handler handler;
@BeforeEach
void setUp() {
handler = new Snake.Handler();
}
@Test
void indexTest() throws IOException {
Map<String, String> response = handler.index();
assertEquals("#888888", response.get("color"));
assertEquals("default", response.get("head"));
assertEquals("default", response.get("tail"));
}
@Test
void startTest() throws IOException {
JsonNode startRequest = OBJECT_MAPPER.readTree("{}");
Map<String, String> response = handler.end(startRequest);
assertEquals(0, response.size());
}
@Test
void moveTest() throws IOException {
JsonNode moveRequest = OBJECT_MAPPER.readTree(
"{\"game\":{\"id\":\"game-00fe20da-94ad-11ea-bb37\",\"ruleset\":{\"name\":\"standard\",\"version\":\"v.1.2.3\"},\"timeout\":500},\"turn\":14,\"board\":{\"height\":11,\"width\":11,\"food\":[{\"x\":5,\"y\":5},{\"x\":9,\"y\":0},{\"x\":2,\"y\":6}],\"hazards\":[{\"x\":3,\"y\":2}],\"snakes\":[{\"id\":\"snake-508e96ac-94ad-11ea-bb37\",\"name\":\"My Snake\",\"health\":54,\"body\":[{\"x\":0,\"y\":0},{\"x\":1,\"y\":0},{\"x\":2,\"y\":0}],\"latency\":\"111\",\"head\":{\"x\":0,\"y\":0},\"length\":3,\"shout\":\"why are we shouting??\",\"squad\":\"\"},{\"id\":\"snake-b67f4906-94ae-11ea-bb37\",\"name\":\"Another Snake\",\"health\":16,\"body\":[{\"x\":5,\"y\":4},{\"x\":5,\"y\":3},{\"x\":6,\"y\":3},{\"x\":6,\"y\":2}],\"latency\":\"222\",\"head\":{\"x\":5,\"y\":4},\"length\":4,\"shout\":\"I'm not really sure...\",\"squad\":\"\"}]},\"you\":{\"id\":\"snake-508e96ac-94ad-11ea-bb37\",\"name\":\"My Snake\",\"health\":54,\"body\":[{\"x\":0,\"y\":0},{\"x\":1,\"y\":0},{\"x\":2,\"y\":0}],\"latency\":\"111\",\"head\":{\"x\":0,\"y\":0},\"length\":3,\"shout\":\"why are we shouting??\",\"squad\":\"\"}}");
Map<String, String> response = handler.move(moveRequest);
List<String> options = new ArrayList<String>();
options.add("up");
options.add("down");
options.add("left");
options.add("right");
assertTrue(options.contains(response.get("move")));
}
@Test
void endTest() throws IOException {
JsonNode endRequest = OBJECT_MAPPER.readTree("{}");
Map<String, String> response = handler.end(endRequest);
assertEquals(0, response.size());
}
@Test
void avoidNeckAllTest() throws IOException {
JsonNode testHead = OBJECT_MAPPER.readTree("{\"x\": 5, \"y\": 5}");
JsonNode testBody = OBJECT_MAPPER
.readTree("[{\"x\": 5, \"y\": 5}, {\"x\": 5, \"y\": 5}, {\"x\": 5, \"y\": 5}]");
ArrayList<String> possibleMoves = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
handler.avoidMyNeck(testHead, testBody, possibleMoves);
assertTrue(possibleMoves.size() == 4);
assertTrue(possibleMoves.equals(expectedResult));
}
@Test
void avoidNeckLeftTest() throws IOException {
JsonNode testHead = OBJECT_MAPPER.readTree("{\"x\": 5, \"y\": 5}");
JsonNode testBody = OBJECT_MAPPER
.readTree("[{\"x\": 5, \"y\": 5}, {\"x\": 4, \"y\": 5}, {\"x\": 3, \"y\": 5}]");
ArrayList<String> possibleMoves = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("up", "down", "right"));
handler.avoidMyNeck(testHead, testBody, possibleMoves);
assertTrue(possibleMoves.size() == 3);
assertTrue(possibleMoves.equals(expectedResult));
}
@Test
void avoidNeckRightTest() throws IOException {
JsonNode testHead = OBJECT_MAPPER.readTree("{\"x\": 5, \"y\": 5}");
JsonNode testBody = OBJECT_MAPPER
.readTree("[{\"x\": 5, \"y\": 5}, {\"x\": 6, \"y\": 5}, {\"x\": 7, \"y\": 5}]");
ArrayList<String> possibleMoves = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("up", "down", "left"));
handler.avoidMyNeck(testHead, testBody, possibleMoves);
assertTrue(possibleMoves.size() == 3);
assertTrue(possibleMoves.equals(expectedResult));
}
@Test
void avoidNeckUpTest() throws IOException {
JsonNode testHead = OBJECT_MAPPER.readTree("{\"x\": 5, \"y\": 5}");
JsonNode testBody = OBJECT_MAPPER
.readTree("[{\"x\": 5, \"y\": 5}, {\"x\": 5, \"y\": 6}, {\"x\": 5, \"y\": 7}]");
ArrayList<String> possibleMoves = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("down", "left", "right"));
handler.avoidMyNeck(testHead, testBody, possibleMoves);
assertTrue(possibleMoves.size() == 3);
assertTrue(possibleMoves.equals(expectedResult));
}
@Test
void avoidNeckDownTest() throws IOException {
JsonNode testHead = OBJECT_MAPPER.readTree("{\"x\": 5, \"y\": 5}");
JsonNode testBody = OBJECT_MAPPER
.readTree("[{\"x\": 5, \"y\": 5}, {\"x\": 5, \"y\": 4}, {\"x\": 5, \"y\": 3}]");
ArrayList<String> possibleMoves = new ArrayList<>(Arrays.asList("up", "down", "left", "right"));
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("up", "left", "right"));
handler.avoidMyNeck(testHead, testBody, possibleMoves);
assertTrue(possibleMoves.size() == 3);
assertTrue(possibleMoves.equals(expectedResult));
}
}