Repository navigation
Expand file tree
/
Copy pathUnitTest.java
More file actions
383 lines (317 loc) · 16.6 KB
/
Copy pathUnitTest.java
File metadata and controls
383 lines (317 loc) · 16.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package bwapi;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class UnitTest {
/**
* Test data generated using BWMirror with:
*
* <pre>
* {@code
* List<Unit> mins = mirror.getGame().getStaticMinerals().stream().filter(Unit::isVisible).collect(Collectors.toList());
* System.out.println("{" + mins.stream().map(m -> "{" + m.getX() + ", " + m.getY() + "}").collect(Collectors.joining(", ")) + "}");
* mirror.getGame().self().getUnits().forEach( u -> {
* System.out.print(u.getType() + ": " + u.getPosition());
* System.out.println(" {" + mins.stream().map(m -> ""+u.getDistance(m)).collect(Collectors.joining(", ")) + "}");
* });
* }
* </pre>
*
* Output:
* {{96, 144}, {96, 208}, {64, 240}, {96, 272}, {64, 176}, {128, 400}, {64, 304}, {96, 368}, {96, 336}}
* Terran_SCV: [288, 296] {185, 160, 181, 149, 203, 137, 181, 154, 149}
* Terran_SCV: [264, 296] {164, 139, 157, 125, 181, 115, 157, 133, 125}
* Terran_SCV: [312, 296] {208, 183, 205, 173, 225, 159, 205, 177, 173}
* Terran_SCV: [240, 296] {155, 116, 133, 101, 158, 96, 133, 110, 101}
* Terran_Command_Center: [288, 240] {109, 102, 134, 102, 134, 121, 134, 121, 109}
*
*/
@Test
public void checkDistance() {
final int[][] minPos = new int[][]{
{96, 144}, {96, 208}, {64, 240}, {96, 272}, {64, 176}, {128, 400}, {64, 304}, {96, 368}, {96, 336}
};
List<Unit> minerals = Arrays.stream(minPos)
.map(m -> createUnit(UnitType.Resource_Mineral_Field, new Position(m[0], m[1])))
.collect(Collectors.toList());
HashMap<Unit, List<Integer>> units = new HashMap<>(); // Unit with respective resulting distance array
units.put(createUnit(UnitType.Terran_SCV, new Position(288, 296)), Arrays.asList(185, 160, 181, 149, 203, 137, 181, 154, 149));
units.put(createUnit(UnitType.Terran_SCV, new Position(264, 296)), Arrays.asList(164, 139, 157, 125, 181, 115, 157, 133, 125));
units.put(createUnit(UnitType.Terran_SCV, new Position(312, 296)), Arrays.asList(208, 183, 205, 173, 225, 159, 205, 177, 173));
units.put(createUnit(UnitType.Terran_SCV, new Position(240, 296)), Arrays.asList(155, 116, 133, 101, 158, 96, 133, 110, 101));
units.put(createUnit(UnitType.Terran_Command_Center, new Position(288, 240)), Arrays.asList(109, 102, 134, 102, 134, 121, 134, 121, 109));
for (Unit unit : units.keySet()) {
List<Integer> results = units.get(unit);
assertEquals(minerals.size(), results.size());
for (int i=0; i < minerals.size(); i++) {
Unit mineral = minerals.get(i);
when(unit.getDistance(mineral)).thenCallRealMethod();
int calculated = unit.getDistance(mineral);
int expected = results.get(i);
assertEquals(calculated, expected);
}
}
}
private Unit createUnit(UnitType type, Position position) {
Unit u = mock(Unit.class);
when(u.getType()).thenReturn(type);
when(u.getPosition()).thenReturn(position);
when(u.exists()).thenReturn(true);
when(u.getLeft()).thenCallRealMethod();
when(u.getRight()).thenCallRealMethod();
when(u.getTop()).thenCallRealMethod();
when(u.getBottom()).thenCallRealMethod();
when(u.getX()).thenCallRealMethod();
when(u.getY()).thenCallRealMethod();
return u;
}
@Test
public void getUnitsInRadiusShouldExcludeSelf() {
Game game = spy(new Game());
doReturn(0).when(game).getFrameCount();
Unit selfUnit = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
Position pos = new Position(100, 100);
doReturn(pos).when(selfUnit).getPosition();
doReturn(90).when(selfUnit).getLeft();
doReturn(110).when(selfUnit).getRight();
doReturn(90).when(selfUnit).getTop();
doReturn(110).when(selfUnit).getBottom();
doReturn(true).when(selfUnit).exists();
doReturn(0).when(selfUnit).getDistance(any(Unit.class));
doReturn(0).when(selfUnit).getDistance(any(Position.class));
Unit neighbor = mock(Unit.class);
when(neighbor.getPosition()).thenReturn(new Position(105, 100));
when(neighbor.getLeft()).thenReturn(95);
when(neighbor.getRight()).thenReturn(115);
when(neighbor.getTop()).thenReturn(90);
when(neighbor.getBottom()).thenReturn(110);
when(neighbor.exists()).thenReturn(true);
when(neighbor.getDistance(any(Position.class))).thenReturn(0);
game.setAllUnits(Arrays.asList(selfUnit, neighbor));
List<Unit> result = selfUnit.getUnitsInRadius(50, u -> true);
assertEquals(Collections.singletonList(neighbor), result);
}
@Test
public void getUnitsInWeaponRangeShouldOnlyEvaluatePredicateForValidTargets() {
Game game = spy(new Game());
doReturn(0).when(game).getFrameCount();
Player selfPlayer = mock(Player.class);
Unit selfUnit = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(true).when(selfUnit).exists();
doReturn(selfPlayer).when(selfUnit).getPlayer();
doReturn(100).when(selfUnit).getLeft();
doReturn(100).when(selfUnit).getTop();
doReturn(120).when(selfUnit).getRight();
doReturn(120).when(selfUnit).getBottom();
Unit invincibleUnit = mock(Unit.class);
when(invincibleUnit.getLeft()).thenReturn(110);
when(invincibleUnit.getTop()).thenReturn(110);
when(invincibleUnit.getRight()).thenReturn(130);
when(invincibleUnit.getBottom()).thenReturn(130);
when(invincibleUnit.isInvincible()).thenReturn(true);
Unit outOfRangeUnit = mock(Unit.class);
when(outOfRangeUnit.getLeft()).thenReturn(110);
when(outOfRangeUnit.getTop()).thenReturn(110);
when(outOfRangeUnit.getRight()).thenReturn(130);
when(outOfRangeUnit.getBottom()).thenReturn(130);
when(outOfRangeUnit.isInvincible()).thenReturn(false);
doReturn(9999).when(selfUnit).getDistance(outOfRangeUnit);
game.setAllUnits(Arrays.asList(selfUnit, invincibleUnit, outOfRangeUnit));
AtomicInteger predicateCalls = new AtomicInteger(0);
selfUnit.getUnitsInWeaponRange(WeaponType.Gauss_Rifle, u -> {
predicateCalls.incrementAndGet();
return true;
});
assertEquals(0, predicateCalls.get());
}
@Test
public void canAttackUnitGroupedShouldCheckTargetUnitInvincibility() {
Unit attacker = mock(Unit.class);
Unit target = mock(Unit.class);
Player selfPlayer = mock(Player.class);
when(attacker.getPlayer()).thenReturn(selfPlayer);
when(attacker.getType()).thenReturn(UnitType.Terran_Marine);
when(attacker.isInvincible()).thenReturn(false);
when(target.isInvincible()).thenReturn(true);
when(target.isFlying()).thenReturn(false);
when(target.getType()).thenReturn(UnitType.Terran_SCV);
when(attacker.canAttackUnitGrouped(target, false, false, false, false)).thenCallRealMethod();
assertFalse(attacker.canAttackUnitGrouped(target, false, false, false, false));
}
@Test
public void canGatherShouldCheckPathToTargetPosition() {
Game game = mock(Game.class);
Player selfPlayer = mock(Player.class);
when(game.self()).thenReturn(selfPlayer);
Position workerPos = new Position(100, 100);
Position unreachableMineralPos = new Position(500, 500);
Unit worker = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(workerPos).when(worker).getPosition();
doReturn(selfPlayer).when(worker).getPlayer();
doReturn(true).when(worker).isCompleted();
doReturn(true).when(worker).hasPath(workerPos);
doReturn(false).when(worker).hasPath(unreachableMineralPos);
Unit unreachableMineral = mock(Unit.class);
when(unreachableMineral.getType()).thenReturn(UnitType.Resource_Mineral_Field);
when(unreachableMineral.getPosition()).thenReturn(unreachableMineralPos);
when(unreachableMineral.isCompleted()).thenReturn(true);
assertFalse(worker.canGather(unreachableMineral, false, false, false));
}
@Test
public void canGatherShouldCheckTargetRefineryOwner() {
Game game = mock(Game.class);
Player selfPlayer = mock(Player.class);
Player enemyPlayer = mock(Player.class);
when(game.self()).thenReturn(selfPlayer);
Position workerPos = new Position(100, 100);
Position reachableRefineryPos = new Position(150, 150);
Unit worker = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(workerPos).when(worker).getPosition();
doReturn(selfPlayer).when(worker).getPlayer();
doReturn(true).when(worker).isCompleted();
doReturn(true).when(worker).hasPath(workerPos);
doReturn(true).when(worker).hasPath(reachableRefineryPos);
Unit enemyRefinery = mock(Unit.class);
when(enemyRefinery.getType()).thenReturn(UnitType.Terran_Refinery);
when(enemyRefinery.getPosition()).thenReturn(reachableRefineryPos);
when(enemyRefinery.getPlayer()).thenReturn(enemyPlayer);
when(enemyRefinery.isCompleted()).thenReturn(true);
assertFalse(worker.canGather(enemyRefinery, false, false, false));
}
@Test
public void canGatherShouldCheckTargetUnitCompleted() {
Game game = mock(Game.class);
Player selfPlayer = mock(Player.class);
when(game.self()).thenReturn(selfPlayer);
Position workerPos = new Position(100, 100);
Position refineryPos = new Position(150, 150);
Unit worker = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(workerPos).when(worker).getPosition();
doReturn(selfPlayer).when(worker).getPlayer();
doReturn(true).when(worker).isCompleted();
doReturn(true).when(worker).hasPath(refineryPos);
Unit incompleteRefinery = mock(Unit.class);
when(incompleteRefinery.getType()).thenReturn(UnitType.Terran_Refinery);
when(incompleteRefinery.getPosition()).thenReturn(refineryPos);
when(incompleteRefinery.getPlayer()).thenReturn(selfPlayer);
when(incompleteRefinery.isCompleted()).thenReturn(false);
assertFalse(worker.canGather(incompleteRefinery, false, false, false));
}
@Test
public void canUseTechUnitHealingShouldRejectEnemyTarget() {
Player selfPlayer = mock(Player.class);
Player enemyPlayer = mock(Player.class);
when(enemyPlayer.isNeutral()).thenReturn(false);
when(selfPlayer.isEnemy(selfPlayer)).thenReturn(false);
when(selfPlayer.isEnemy(enemyPlayer)).thenReturn(true);
Unit medic = mock(Unit.class);
when(medic.getPlayer()).thenReturn(selfPlayer);
Unit enemyMarine = mock(Unit.class);
when(enemyMarine.getPlayer()).thenReturn(enemyPlayer);
when(enemyMarine.getType()).thenReturn(UnitType.Terran_Marine);
when(enemyMarine.isFlying()).thenReturn(false);
when(enemyMarine.getHitPoints()).thenReturn(20);
when(medic.canUseTechUnit(TechType.Healing, enemyMarine, false, false, false, false)).thenCallRealMethod();
assertFalse(medic.canUseTechUnit(TechType.Healing, enemyMarine, false, false, false, false));
}
@Test
public void isVisibleShouldFallbackToAnyPlayerWhenSelfIsNullAndCheckValidPlayerId() {
Game game = mock(Game.class);
when(game.self()).thenReturn(null);
ClientData.UnitData unitData = mock(ClientData.UnitData.class);
Unit unit = new Unit(unitData, 0, game);
assertFalse(unit.isVisible());
when(unitData.isVisible(3)).thenReturn(true);
assertTrue(unit.isVisible());
assertTrue(unit.isVisible(null));
ClientData.PlayerData neutralData = mock(ClientData.PlayerData.class);
when(neutralData.getName()).thenReturn("Neutral");
Player neutral = new Player(neutralData, 11, game);
when(unitData.isVisible(11)).thenReturn(true);
assertFalse(unit.isVisible(neutral));
}
@Test
public void isTargetableShouldRequireVisibilityWhenCompleteMapInfoDisabled() {
Game game = mock(Game.class);
Unit unit = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(true).when(unit).exists();
doReturn(false).when(unit).isVisible();
doReturn(true).when(unit).isCompleted();
doReturn(UnitType.Terran_Marine).when(unit).getType();
when(game.isFlagEnabled(Flag.CompleteMapInformation)).thenReturn(false);
assertFalse(unit.isTargetable());
when(game.isFlagEnabled(Flag.CompleteMapInformation)).thenReturn(true);
assertTrue(unit.isTargetable());
}
@Test
public void canTargetUnitShouldCheckCommandabilityByDefaultAndDelegateToIsTargetable() {
Game game = mock(Game.class);
Unit source = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
Unit target = mock(Unit.class);
when(target.exists()).thenReturn(true);
when(target.isCompleted()).thenReturn(true);
when(target.getType()).thenReturn(UnitType.Terran_Marine);
when(target.isTargetable()).thenReturn(true);
doReturn(false).when(source).canCommand();
assertFalse(source.canTargetUnit(target));
assertTrue(source.canTargetUnit(target, false));
doReturn(true).when(source).canCommand();
when(target.isTargetable()).thenReturn(false);
assertFalse(source.canTargetUnit(target));
}
@Test
public void buildWithNonePositionAndTrainWithNoneUnitTypeShouldRedirectToCarrierAndReaverAmmo() {
Game game = mock(Game.class);
Unit carrier = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(UnitType.Protoss_Carrier).when(carrier).getType();
doReturn(true).when(carrier).issueCommand(any(UnitCommand.class));
assertTrue(carrier.build(UnitType.None, TilePosition.None));
verify(carrier).issueCommand(UnitCommand.train(carrier, UnitType.Protoss_Interceptor));
Unit reaver = spy(new Unit(mock(ClientData.UnitData.class), 1, game));
doReturn(UnitType.Protoss_Reaver).when(reaver).getType();
doReturn(true).when(reaver).issueCommand(any(UnitCommand.class));
assertTrue(reaver.build(UnitType.None));
verify(reaver).issueCommand(UnitCommand.train(reaver, UnitType.Protoss_Scarab));
}
@Test
public void useTechWithNullUnitTargetShouldIssueTargetlessUseTechCommand() {
Game game = mock(Game.class);
Unit marine = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(true).when(marine).issueCommand(any(UnitCommand.class));
assertTrue(marine.useTech(TechType.Stim_Packs, (Unit) null));
verify(marine).issueCommand(UnitCommand.useTech(marine, TechType.Stim_Packs));
}
@Test
public void canAttackGroupedPositionShouldOnlyCheckCanAttackMoveGrouped() {
Game game = mock(Game.class);
Unit unit = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(false).when(unit).canAttackMoveGrouped(false, false);
doReturn(true).when(unit).canAttackUnitGrouped(false, false);
assertFalse(unit.canAttackGrouped(new Position(100, 100), false, true, false, false));
}
@Test
public void canBuildAddonWithUnitTypeShouldCheckCanBuildAddonWithoutRecursingOnUnitType() {
Game game = mock(Game.class);
when(game.canMake(any(UnitType.class), any(Unit.class))).thenReturn(true);
when(game.canBuildHere(any(TilePosition.class), any(UnitType.class), any(Unit.class))).thenReturn(true);
Unit factory = spy(new Unit(mock(ClientData.UnitData.class), 0, game));
doReturn(UnitType.Terran_Factory).when(factory).getType();
doReturn(new TilePosition(10, 10)).when(factory).getTilePosition();
// Not completed -> canBuildAddon(false) is false
doReturn(false).when(factory).isCompleted();
assertFalse(factory.canBuildAddon(UnitType.Terran_Machine_Shop, true, false));
}
}