aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/integration/webdiplomacy_net/tests/test_game.py
blob: 7a888d36ae8370c56c31f47482c9c5fbfaf1963b (plain)
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
# ==============================================================================
# Copyright (C) 2019 - Philip Paquette
#
#  This program is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
#  details.
#
#  You should have received a copy of the GNU Affero General Public License along
#  with this program.  If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Test order conversion. """
from diplomacy.integration.webdiplomacy_net.game import turn_to_phase, unit_dict_to_str, center_dict_to_str, \
    order_dict_to_str

# ------------------------------------
# ---- Tests for turn_to_phase ----
# ------------------------------------
def test_phase_s1901m():
    """ Tests S1901M """
    phase = turn_to_phase(0, 'Diplomacy')
    assert phase == 'S1901M'

def test_phase_s1901r():
    """ Tests S1901R """
    phase = turn_to_phase(0, 'Retreats')
    assert phase == 'S1901R'

def test_phase_f1901m():
    """ Tests F1901M """
    phase = turn_to_phase(1, 'Diplomacy')
    assert phase == 'F1901M'

def test_phase_f1901r():
    """ Tests F1901R """
    phase = turn_to_phase(1, 'Retreats')
    assert phase == 'F1901R'

def test_phase_w1901a():
    """ Tests W1901A """
    phase = turn_to_phase(1, 'Builds')
    assert phase == 'W1901A'

def test_phase_s1902m():
    """ Tests S1902M """
    phase = turn_to_phase(2, 'Diplomacy')
    assert phase == 'S1902M'


# ------------------------------------
# ---- Tests for unit_dict_to_str ----
# ------------------------------------
def test_army_france():
    """ Tests Army France """
    unit_dict = {'unitType': 'Army', 'terrID': 47, 'countryID': 2, 'retreating': 'No'}
    power_name, unit = unit_dict_to_str(unit_dict)
    assert power_name == 'FRANCE'
    assert unit == 'A PAR'

def test_dis_fleet_england():
    """ Tests Dislodged Fleet England """
    unit_dict = {'unitType': 'Fleet', 'terrID': 6, 'countryID': 1, 'retreating': 'Yes'}
    power_name, unit = unit_dict_to_str(unit_dict)
    assert power_name == 'ENGLAND'
    assert unit == '*F LON'

def test_invalid_unit():
    """ Tests invalid unit """
    unit_dict = {'unitType': 'Fleet', 'terrID': 99, 'countryID': 0, 'retreating': 'No'}
    power_name, unit = unit_dict_to_str(unit_dict)
    assert power_name == ''
    assert unit == ''


# --------------------------------------
# ---- Tests for center_dict_to_str ----
# --------------------------------------
def test_center_dict():
    """ Tests parsing centers """
    power_name, center = center_dict_to_str({'countryID': 1, 'terrID': 6}, map_id=1)
    assert power_name == 'ENGLAND'
    assert center == 'LON'

    power_name, center = center_dict_to_str({'countryID': 2, 'terrID': 47}, map_id=1)
    assert power_name == 'FRANCE'
    assert center == 'PAR'


# -------------------------------------
# ---- Tests for order_dict_to_str ----
# -------------------------------------
def test_s1901m_hold():
    """ Tests hold in S1901M """
    order_dict = {'turn': 0,
                  'phase': 'Diplomacy',
                  'countryID': 2,
                  'terrID': 6,
                  'unitType': 'Army',
                  'type': 'Hold',
                  'toTerrID': '',
                  'fromTerrID': '',
                  'viaConvoy': ''}
    power_name, order = order_dict_to_str(order_dict, phase='Diplomacy')
    assert power_name == 'FRANCE'
    assert order == 'A LON H'

def test_s1901r_disband():
    """ Tests disband in S1901R """
    order_dict = {'turn': 0,
                  'phase': 'Retreats',
                  'countryID': 1,
                  'terrID': 6,
                  'unitType': 'Fleet',
                  'type': 'Disband',
                  'toTerrID': '',
                  'fromTerrID': '',
                  'viaConvoy': ''}
    power_name, order = order_dict_to_str(order_dict, phase='Retreats')
    assert power_name == 'ENGLAND'
    assert order == 'F LON D'

def test_f1901m_move():
    """ Tests move in F1901M """
    order_dict = {'turn': 1,
                  'phase': 'Diplomacy',
                  'countryID': 2,
                  'terrID': 6,
                  'unitType': 'Army',
                  'type': 'Move',
                  'toTerrID': 47,
                  'fromTerrID': '',
                  'viaConvoy': 'Yes'}
    power_name, order = order_dict_to_str(order_dict, phase='Diplomacy')
    assert power_name == 'FRANCE'
    assert order == 'A LON - PAR VIA'

def test_f1901r_retreat():
    """ Tests retreat in F1901R """
    order_dict = {'turn': 1,
                  'phase': 'Retreats',
                  'countryID': 3,
                  'terrID': 6,
                  'unitType': 'Army',
                  'type': 'Retreat',
                  'toTerrID': 47,
                  'fromTerrID': '',
                  'viaConvoy': ''}
    power_name, order = order_dict_to_str(order_dict, phase='Retreats')
    assert power_name == 'ITALY'
    assert order == 'A LON R PAR'

def test_w1901a_build():
    """ Tests build army in W1901A """
    order_dict = {'turn': 1,
                  'phase': 'Builds',
                  'countryID': 2,
                  'terrID': 6,
                  'unitType': 'Army',
                  'type': 'Build Army',
                  'toTerrID': 6,
                  'fromTerrID': '',
                  'viaConvoy': ''}
    power_name, order = order_dict_to_str(order_dict, phase='Builds')
    assert power_name == 'FRANCE'
    assert order == 'A LON B'

def test_s1902m_hold():
    """ Tests hold in S1902M """
    order_dict = {'turn': 2,
                  'phase': 'Diplomacy',
                  'countryID': 2,
                  'terrID': 6,
                  'unitType': 'Army',
                  'type': 'Hold',
                  'toTerrID': '',
                  'fromTerrID': '',
                  'viaConvoy': ''}
    power_name, order = order_dict_to_str(order_dict, phase='Diplomacy')
    assert power_name == 'FRANCE'
    assert order == 'A LON H'