|
10 | 10 |
|
11 | 11 | import static org.junit.Assert.assertEquals; |
12 | 12 | import static org.junit.Assert.assertFalse; |
| 13 | +import static org.junit.Assert.assertTrue; |
13 | 14 | import static org.mockito.ArgumentMatchers.any; |
14 | 15 | import static org.mockito.Mockito.doReturn; |
15 | 16 | import static org.mockito.Mockito.mock; |
16 | 17 | import static org.mockito.Mockito.spy; |
| 18 | +import static org.mockito.Mockito.verify; |
17 | 19 | import static org.mockito.Mockito.when; |
18 | 20 |
|
19 | 21 |
|
@@ -269,4 +271,113 @@ public void canUseTechUnitHealingShouldRejectEnemyTarget() { |
269 | 271 |
|
270 | 272 | assertFalse(medic.canUseTechUnit(TechType.Healing, enemyMarine, false, false, false, false)); |
271 | 273 | } |
| 274 | + |
| 275 | + @Test |
| 276 | + public void isVisibleShouldFallbackToAnyPlayerWhenSelfIsNullAndCheckValidPlayerId() { |
| 277 | + Game game = mock(Game.class); |
| 278 | + when(game.self()).thenReturn(null); |
| 279 | + |
| 280 | + ClientData.UnitData unitData = mock(ClientData.UnitData.class); |
| 281 | + Unit unit = new Unit(unitData, 0, game); |
| 282 | + |
| 283 | + assertFalse(unit.isVisible()); |
| 284 | + |
| 285 | + when(unitData.isVisible(3)).thenReturn(true); |
| 286 | + assertTrue(unit.isVisible()); |
| 287 | + assertTrue(unit.isVisible(null)); |
| 288 | + |
| 289 | + ClientData.PlayerData neutralData = mock(ClientData.PlayerData.class); |
| 290 | + when(neutralData.getName()).thenReturn("Neutral"); |
| 291 | + Player neutral = new Player(neutralData, 11, game); |
| 292 | + when(unitData.isVisible(11)).thenReturn(true); |
| 293 | + assertFalse(unit.isVisible(neutral)); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void isTargetableShouldRequireVisibilityWhenCompleteMapInfoDisabled() { |
| 298 | + Game game = mock(Game.class); |
| 299 | + Unit unit = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 300 | + doReturn(true).when(unit).exists(); |
| 301 | + doReturn(false).when(unit).isVisible(); |
| 302 | + doReturn(true).when(unit).isCompleted(); |
| 303 | + doReturn(UnitType.Terran_Marine).when(unit).getType(); |
| 304 | + |
| 305 | + when(game.isFlagEnabled(Flag.CompleteMapInformation)).thenReturn(false); |
| 306 | + assertFalse(unit.isTargetable()); |
| 307 | + |
| 308 | + when(game.isFlagEnabled(Flag.CompleteMapInformation)).thenReturn(true); |
| 309 | + assertTrue(unit.isTargetable()); |
| 310 | + } |
| 311 | + |
| 312 | + @Test |
| 313 | + public void canTargetUnitShouldCheckCommandabilityByDefaultAndDelegateToIsTargetable() { |
| 314 | + Game game = mock(Game.class); |
| 315 | + Unit source = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 316 | + Unit target = mock(Unit.class); |
| 317 | + when(target.exists()).thenReturn(true); |
| 318 | + when(target.isCompleted()).thenReturn(true); |
| 319 | + when(target.getType()).thenReturn(UnitType.Terran_Marine); |
| 320 | + when(target.isTargetable()).thenReturn(true); |
| 321 | + |
| 322 | + doReturn(false).when(source).canCommand(); |
| 323 | + assertFalse(source.canTargetUnit(target)); |
| 324 | + assertTrue(source.canTargetUnit(target, false)); |
| 325 | + |
| 326 | + doReturn(true).when(source).canCommand(); |
| 327 | + when(target.isTargetable()).thenReturn(false); |
| 328 | + assertFalse(source.canTargetUnit(target)); |
| 329 | + } |
| 330 | + |
| 331 | + @Test |
| 332 | + public void buildWithNonePositionAndTrainWithNoneUnitTypeShouldRedirectToCarrierAndReaverAmmo() { |
| 333 | + Game game = mock(Game.class); |
| 334 | + Unit carrier = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 335 | + doReturn(UnitType.Protoss_Carrier).when(carrier).getType(); |
| 336 | + doReturn(true).when(carrier).issueCommand(any(UnitCommand.class)); |
| 337 | + |
| 338 | + assertTrue(carrier.build(UnitType.None, TilePosition.None)); |
| 339 | + verify(carrier).issueCommand(UnitCommand.train(carrier, UnitType.Protoss_Interceptor)); |
| 340 | + |
| 341 | + Unit reaver = spy(new Unit(mock(ClientData.UnitData.class), 1, game)); |
| 342 | + doReturn(UnitType.Protoss_Reaver).when(reaver).getType(); |
| 343 | + doReturn(true).when(reaver).issueCommand(any(UnitCommand.class)); |
| 344 | + |
| 345 | + assertTrue(reaver.build(UnitType.None)); |
| 346 | + verify(reaver).issueCommand(UnitCommand.train(reaver, UnitType.Protoss_Scarab)); |
| 347 | + } |
| 348 | + |
| 349 | + @Test |
| 350 | + public void useTechWithNullUnitTargetShouldIssueTargetlessUseTechCommand() { |
| 351 | + Game game = mock(Game.class); |
| 352 | + Unit marine = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 353 | + doReturn(true).when(marine).issueCommand(any(UnitCommand.class)); |
| 354 | + |
| 355 | + assertTrue(marine.useTech(TechType.Stim_Packs, (Unit) null)); |
| 356 | + verify(marine).issueCommand(UnitCommand.useTech(marine, TechType.Stim_Packs)); |
| 357 | + } |
| 358 | + |
| 359 | + @Test |
| 360 | + public void canAttackGroupedPositionShouldOnlyCheckCanAttackMoveGrouped() { |
| 361 | + Game game = mock(Game.class); |
| 362 | + Unit unit = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 363 | + doReturn(false).when(unit).canAttackMoveGrouped(false, false); |
| 364 | + doReturn(true).when(unit).canAttackUnitGrouped(false, false); |
| 365 | + |
| 366 | + assertFalse(unit.canAttackGrouped(new Position(100, 100), false, true, false, false)); |
| 367 | + } |
| 368 | + |
| 369 | + @Test |
| 370 | + public void canBuildAddonWithUnitTypeShouldCheckCanBuildAddonWithoutRecursingOnUnitType() { |
| 371 | + Game game = mock(Game.class); |
| 372 | + when(game.canMake(any(UnitType.class), any(Unit.class))).thenReturn(true); |
| 373 | + when(game.canBuildHere(any(TilePosition.class), any(UnitType.class), any(Unit.class))).thenReturn(true); |
| 374 | + |
| 375 | + Unit factory = spy(new Unit(mock(ClientData.UnitData.class), 0, game)); |
| 376 | + doReturn(UnitType.Terran_Factory).when(factory).getType(); |
| 377 | + doReturn(new TilePosition(10, 10)).when(factory).getTilePosition(); |
| 378 | + // Not completed -> canBuildAddon(false) is false |
| 379 | + doReturn(false).when(factory).isCompleted(); |
| 380 | + |
| 381 | + assertFalse(factory.canBuildAddon(UnitType.Terran_Machine_Shop, true, false)); |
| 382 | + } |
272 | 383 | } |
0 commit comments