aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/tests/test_datc_no_check.py
blob: 59e541971d6962cd6f7b935134a21b0c78af7dab (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
# ==============================================================================
# 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/>.
# ==============================================================================
""" DATC Test Cases (Using rule NO_CHECK)
    - Contains the diplomacy adjudication test cases (without order validation)
"""
from diplomacy.engine.game import Game
from diplomacy.tests.test_datc import TestDATC as RootDATC

# -----------------
# DATC TEST CASES (Without order validation)
# -----------------
class TestDATCNoCheck(RootDATC):
    """ DATC test cases"""

    @staticmethod
    def create_game():
        """ Creates a game object"""
        game = Game()
        game.add_rule('NO_CHECK')
        return game

    @staticmethod
    def check_results(game, unit, value, phase='M'):
        """ Checks adjudication results """
        # pylint: disable=too-many-return-statements
        if not game:
            return False

        result = game.result_history.last_value()

        # Checking if the results contain duplicate values
        unit_result = result.get(unit, [])
        if len(unit_result) != len(set(unit_result)):
            raise RuntimeError('Duplicate values detected in %s' % unit_result)

        # Done self.processing a retreats phase
        if phase == 'R':
            if value == 'void' and 'void' in unit_result:
                return True
            if value == '':
                success = unit not in game.popped and unit_result == []
                if not success:
                    print('Results: %s - Expected: []' % result.get(unit, '<Not Found>'))
                return success

            success = unit in game.popped and value in unit_result
            if not success:
                print('Results: %s - Expected: %s' % (result.get(unit, '<Not Found>'), value))
            return success

        # Done self.processing a retreats phase
        if phase == 'A':
            if value == 'void' and 'void' in unit_result:
                return True
            success = value == unit_result
            if not success:
                print('Results: %s - Expected: %s' % (result.get(unit, '<Not Found>'), value))
            return success

        order_status = game.get_order_status(unit=unit)

        # >>>>>>>>>>>>>>>>>>>>>>>>
        # For NO_CHECK, we expect to find the unit in game.orderable_units
        # But we require that the order is marked as 'void'
        # As opposed to a regular game, where an invalid order is dropped
        # <<<<<<<<<<<<<<<<<<<<<<<<
        # Invalid order
        if value == 'void':
            if 'void' in result.get(unit, []):
                return True
            return False

        # Invalid unit
        if unit not in game.command:
            print('Results: %s NOT FOUND - Expected: %s' % (unit, value))
            return False

        # Expected no errors
        if value == '':
            if order_status:
                print('Results: %s - Expected: []' % order_status)
                return False
            return True

        # Incorrect error
        if value not in game.get_order_status(unit=unit):
            print('Results: %s - Expected: %s' % (order_status, value))
            return False

        # Correct value
        return True