Skip to content

Commit cbc53f1

Browse files
MpcsNicholasBatesNZ
authored andcommitted
Add Tests
1 parent 171cd79 commit cbc53f1

File tree

5 files changed

+352
-2
lines changed

5 files changed

+352
-2
lines changed

engine/src/main/java/org/destinationsol/game/console/commands/InvincibleCommandHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public class InvincibleCommandHandler {
3232
public String godMode(@Game SolGame game) throws CommandExecutionException {
3333
Hero hero = game.getHero();
3434
if (hero.isDead()) {
35-
throw new IllegalArgumentException("Cannot make invincible when dead");
35+
throw new CommandExecutionException("Cannot make invincible when dead");
3636
}
3737
if (hero.isTranscendent()) {
38-
throw new IllegalArgumentException("Cannot make invincible when transdencent");
38+
throw new CommandExecutionException("Cannot make invincible when transdencent");
3939
}
4040
if (!hero.isInvincible()) {
4141
hero.setInvincible(true);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.destinationsol.game.console;
17+
18+
import org.destinationsol.game.Hero;
19+
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.commands.ChangeShipCommandHandler;
21+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Answers;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
29+
import static org.junit.Assert.fail;
30+
import static org.mockito.ArgumentMatchers.any;
31+
import static org.mockito.ArgumentMatchers.eq;
32+
import static org.mockito.Mockito.never;
33+
import static org.mockito.Mockito.verify;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class ChangeShipCommandHandlerTest {
37+
38+
private ChangeShipCommandHandler commandHandler;
39+
40+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
41+
private SolGame game;
42+
43+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
44+
private Hero hero;
45+
46+
@Before
47+
public void init() {
48+
commandHandler = new ChangeShipCommandHandler();
49+
}
50+
51+
@Test
52+
public void shouldNotChangeShipWhenNonExistingShipPassed() {
53+
boolean threwException = false;
54+
try {
55+
commandHandler.changeShip(game, null);
56+
} catch (CommandExecutionException e) {
57+
threwException = true;
58+
}
59+
60+
if (!threwException) {
61+
fail();
62+
}
63+
64+
verify(hero, never()).setSolShip(any(), eq(game));
65+
}
66+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2020 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.destinationsol.game.console;
17+
18+
import org.destinationsol.game.Hero;
19+
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.commands.DieCommandHandler;
21+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Answers;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
29+
import static org.junit.Assert.fail;
30+
import static org.mockito.Mockito.never;
31+
import static org.mockito.Mockito.times;
32+
import static org.mockito.Mockito.verify;
33+
import static org.mockito.Mockito.when;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class DieCommandHandlerTest {
37+
38+
private DieCommandHandler commandHandler;
39+
40+
@Mock
41+
private SolGame game;
42+
43+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
44+
private Hero hero;
45+
46+
@Before
47+
public void init() {
48+
commandHandler = new DieCommandHandler();
49+
}
50+
51+
@Test
52+
public void shouldKillWhenAliveAndNotTranscendent() {
53+
when(hero.isTranscendent()).thenReturn(false);
54+
when(hero.isAlive()).thenReturn(true);
55+
when(game.getHero()).thenReturn(hero);
56+
try {
57+
commandHandler.die(game);
58+
} catch (CommandExecutionException e) {
59+
fail();
60+
}
61+
verify(hero, times(1)).getShip();
62+
}
63+
64+
@Test
65+
public void shouldNotKillWhenTranscendent() {
66+
when(hero.isTranscendent()).thenReturn(true);
67+
when(game.getHero()).thenReturn(hero);
68+
boolean threwException = false;
69+
try {
70+
commandHandler.die(game);
71+
} catch (CommandExecutionException e) {
72+
threwException = true;
73+
}
74+
75+
if (!threwException) {
76+
fail();
77+
}
78+
79+
verify(hero, never()).getShip();
80+
}
81+
82+
@Test
83+
public void shouldNotKillWhenAlreadyDead() {
84+
when(hero.isTranscendent()).thenReturn(false);
85+
when(hero.isAlive()).thenReturn(false);
86+
when(game.getHero()).thenReturn(hero);
87+
boolean threwException = false;
88+
try {
89+
commandHandler.die(game);
90+
} catch (CommandExecutionException e) {
91+
threwException = true;
92+
}
93+
94+
if (!threwException) {
95+
fail();
96+
}
97+
98+
verify(hero, never()).getShip();
99+
}
100+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2020 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.destinationsol.game.console;
17+
18+
import org.destinationsol.game.Hero;
19+
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.commands.InvincibleCommandHandler;
21+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Answers;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
29+
import static org.junit.Assert.fail;
30+
import static org.mockito.ArgumentMatchers.anyBoolean;
31+
import static org.mockito.Mockito.never;
32+
import static org.mockito.Mockito.times;
33+
import static org.mockito.Mockito.verify;
34+
import static org.mockito.Mockito.when;
35+
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class InvincibleCommandHandlerTest {
38+
39+
private InvincibleCommandHandler commandHandler;
40+
41+
@Mock
42+
private SolGame game;
43+
44+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
45+
private Hero hero;
46+
47+
@Before
48+
public void init() {
49+
commandHandler = new InvincibleCommandHandler();
50+
}
51+
52+
@Test
53+
public void shouldChangeInvincibilityWhenAliveAndNonTranscendent() {
54+
when(hero.isDead()).thenReturn(false);
55+
when(hero.isTranscendent()).thenReturn(false);
56+
when(game.getHero()).thenReturn(hero);
57+
try {
58+
commandHandler.godMode(game);
59+
} catch (CommandExecutionException e) {
60+
fail();
61+
}
62+
63+
verify(hero, times(1)).setInvincible(anyBoolean());
64+
}
65+
66+
@Test
67+
public void shouldNotChangeInvincibilityWhenAliveAndTranscendent() {
68+
when(hero.isDead()).thenReturn(false);
69+
when(hero.isTranscendent()).thenReturn(true);
70+
when(game.getHero()).thenReturn(hero);
71+
boolean threwException = false;
72+
try {
73+
commandHandler.godMode(game);
74+
} catch (CommandExecutionException e) {
75+
threwException = true;
76+
}
77+
78+
if (!threwException) {
79+
fail();
80+
}
81+
82+
verify(hero, never()).setInvincible(anyBoolean());
83+
}
84+
85+
@Test
86+
public void shouldNotChangeInvincibilityWhenDead() {
87+
when(hero.isDead()).thenReturn(true);
88+
when(game.getHero()).thenReturn(hero);
89+
boolean threwException = false;
90+
try {
91+
commandHandler.godMode(game);
92+
} catch (CommandExecutionException e) {
93+
threwException = true;
94+
}
95+
96+
if (!threwException) {
97+
fail();
98+
}
99+
100+
verify(hero, never()).setInvincible(anyBoolean());
101+
}
102+
103+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2020 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.destinationsol.game.console;
17+
18+
import org.destinationsol.game.Hero;
19+
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.commands.RespawnCommandHandler;
21+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.MockitoJUnitRunner;
27+
28+
import static org.junit.Assert.fail;
29+
import static org.mockito.Mockito.*;
30+
31+
32+
import static org.mockito.Mockito.when;
33+
34+
@RunWith(MockitoJUnitRunner.class)
35+
public class RespawnCommandHandlerTest {
36+
37+
private RespawnCommandHandler commandHandler;
38+
39+
@Mock
40+
private SolGame game;
41+
42+
@Mock
43+
private Hero hero;
44+
45+
@Before
46+
public void init() {
47+
commandHandler = new RespawnCommandHandler();
48+
}
49+
50+
@Test
51+
public void shouldRespawnWhenDead() {
52+
when(hero.isAlive()).thenReturn(false);
53+
when(game.getHero()).thenReturn(hero);
54+
55+
try {
56+
commandHandler.respawn(game);
57+
} catch (CommandExecutionException e) {
58+
fail();
59+
}
60+
61+
verify(game, times(1)).respawn();
62+
}
63+
64+
@Test
65+
public void shouldNotRespawnWhenAlive() {
66+
when(hero.isAlive()).thenReturn(true);
67+
when(game.getHero()).thenReturn(hero);
68+
boolean threwException = false;
69+
try {
70+
commandHandler.respawn(game);
71+
} catch (CommandExecutionException e) {
72+
threwException = true;
73+
}
74+
75+
if (!threwException) {
76+
fail();
77+
}
78+
79+
verify(game, never()).respawn();
80+
}
81+
}

0 commit comments

Comments
 (0)