Skip to content

Commit 37371ed

Browse files
committed
Add a test for debug commands.
PiperOrigin-RevId: 209981708
1 parent 1ea62f7 commit 37371ed

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

pysc2/tests/debug_test.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/python
2+
# Copyright 2018 Google Inc. All Rights Reserved.
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+
"""Test that the debug commands work."""
16+
17+
from __future__ import absolute_import
18+
from __future__ import division
19+
from __future__ import print_function
20+
21+
from absl.testing import absltest
22+
23+
from pysc2 import maps
24+
from pysc2 import run_configs
25+
from pysc2.lib import units
26+
27+
from s2clientprotocol import common_pb2 as sc_common
28+
from s2clientprotocol import debug_pb2 as sc_debug
29+
from s2clientprotocol import sc2api_pb2 as sc_pb
30+
31+
32+
class DebugTest(absltest.TestCase):
33+
34+
def test_multi_player(self):
35+
run_config = run_configs.get()
36+
map_inst = maps.get("Simple64")
37+
38+
with run_config.start() as controller:
39+
40+
create = sc_pb.RequestCreateGame(
41+
local_map=sc_pb.LocalMap(
42+
map_path=map_inst.path, map_data=map_inst.data(run_config)))
43+
create.player_setup.add(type=sc_pb.Participant)
44+
create.player_setup.add(
45+
type=sc_pb.Computer,
46+
race=sc_common.Terran,
47+
difficulty=sc_pb.VeryEasy)
48+
join = sc_pb.RequestJoinGame(race=sc_common.Terran,
49+
options=sc_pb.InterfaceOptions(raw=True))
50+
51+
controller.create_game(create)
52+
controller.join_game(join)
53+
54+
info = controller.game_info()
55+
map_size = info.start_raw.map_size
56+
57+
controller.step(2)
58+
59+
obs = controller.observe()
60+
61+
def get_marines(obs):
62+
return {u.tag: u for u in obs.observation.raw_data.units
63+
if u.unit_type == units.Terran.Marine}
64+
65+
self.assertEmpty(get_marines(obs))
66+
67+
controller.debug(sc_debug.DebugCommand(
68+
create_unit=sc_debug.DebugCreateUnit(
69+
unit_type=units.Terran.Marine,
70+
owner=1,
71+
pos=sc_common.Point2D(x=map_size.x // 2, y=map_size.y // 2),
72+
quantity=5)))
73+
74+
controller.step(2)
75+
76+
obs = controller.observe()
77+
78+
marines = get_marines(obs)
79+
self.assertEqual(5, len(marines))
80+
81+
tags = sorted(marines.keys())
82+
83+
controller.debug([
84+
sc_debug.DebugCommand(kill_unit=sc_debug.DebugKillUnit(
85+
tag=[tags[0]])),
86+
sc_debug.DebugCommand(unit_value=sc_debug.DebugSetUnitValue(
87+
unit_value=sc_debug.DebugSetUnitValue.Life, value=5,
88+
unit_tag=tags[1])),
89+
])
90+
91+
controller.step(2)
92+
93+
obs = controller.observe()
94+
95+
marines = get_marines(obs)
96+
self.assertEqual(4, len(marines))
97+
self.assertNotIn(tags[0], marines)
98+
self.assertEqual(marines[tags[1]].health, 5)
99+
100+
101+
if __name__ == "__main__":
102+
absltest.main()

0 commit comments

Comments
 (0)