aboutsummaryrefslogtreecommitdiff
path: root/tests/test_imagoIO.py
blob: 499fdb22e5c912710ec51ec72e49f75f46190949 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Tests for the input/output component."""

import unittest

import io
import sys

from imago.data.enums import DecisionAlgorithms
from imago.engine.imagoIO import ImagoIO
from imago.engine.parseHelpers import parseVertex

class TestImagoIO(unittest.TestCase):
    """Test ImagoIO component."""

    @unittest.mock.patch('imago.engine.imagoIO.input', create=True)
    def testSimpleCommands(self, mocked_input):
        """Test simple commands."""

        self.maxDiff = None

        mocked_input.side_effect = [
                '\n',
                'name\n',
                'version\n',
                'protocol_version\n',
                'clear_board\n',
                '\n',
                'known_command\n',
                'known_command name\n',
                'known_command version\n',
                'known_command wrongcommand\n',
                '\n',
                'boardsize\n',
                'boardsize 10\n',
                '\n',
                'komi\n',
                'komi 5.5\n',
                '\n',
                'play\n',
                'play 1\n',
                'play 1 2\n',
                'play b a1\n',
                '\n',
                'undo\n',
                'undo\n',
                '\n',
                'wrongcommand\n',
                '\n',
                'quit\n'
                ]

        testout = io.StringIO()
        imagoIO = ImagoIO(
                decisionAlgorithmId=DecisionAlgorithms.MONTECARLO,
                outputStream=testout
                )

        imagoIO.start()
        value = testout.getvalue()
        self.assertEqual(
                '= Imago\n\n' +
                '= 0.0.0\n\n' +
                '= 2\n\n' +
                '= \n\n' +
                '? Wrong number of arguments\n' +
                '? Usage: known_command COMMAND_NAME\n\n' +
                '= true\n\n' +
                '= true\n\n' +
                '= false\n\n' +
                '? Wrong number of arguments\n' +
                '? Usage: boardsize <newSize>\n\n' +
                '= \n\n' +
                '? Wrong number of arguments\n' +
                '? Usage: komi <newKomi>\n\n' +
                '= \n\n' +
                '? Wrong number of arguments\n' +
                '? Usage: play <color> <vertex>\n\n' +
                '? Wrong number of arguments\n' +
                '? Usage: play <color> <vertex>\n\n' +
                '? Invalid move: Unknown color [1].\n\n' +
                '= \n\n' +
                '= \n\n' +
                '= \n\n' +
                '? unknown command\n\n' +
                '= \n\n',
                value
                )

        testout.close()


    @unittest.mock.patch('imago.engine.imagoIO.input', create=True)
    def testListsCommands(self, mocked_input):
        """Test command for listing all commands."""

        mocked_input.side_effect = [
                'list_commands\n',
                'quit\n'
                ]

        testout = io.StringIO()
        imagoIO = ImagoIO(
                decisionAlgorithmId=DecisionAlgorithms.MONTECARLO,
                outputStream=testout
                )

        commandsString = "\n".join(list(map(
            lambda cmd: "%s - %s" % (cmd.__name__, cmd.__doc__),
            imagoIO.commands_set)))

        imagoIO.start()
        value = testout.getvalue()
        self.assertEqual(
                '= ' +
                commandsString +
                '\n\n' +
                '= \n\n',
                value
                )

        testout.close()


    @unittest.mock.patch('imago.engine.imagoIO.input', create=True)
    def testFixedHandicap(self, mocked_input):
        """Test command for setting fixed handicap stones."""

        mocked_input.side_effect = [
                'fixed_handicap\n',
                'fixed_handicap 2\n',
                'quit\n'
                ]

        testout = io.StringIO()
        imagoIO = ImagoIO(
                decisionAlgorithmId=DecisionAlgorithms.MONTECARLO,
                outputStream=testout
                )

        imagoIO.start()
        value = testout.getvalue()
        self.assertEqual(
                '? Wrong number of arguments\n' +
                '? Usage: fixed_handicap <count>\n\n' +
                '= A1 A2\n\n' +
                '= \n\n',
                value
                )

        testout.close()


#    @unittest.mock.patch('imago.engine.imagoIO.input', create=True)
#    def testGenmove(self, mocked_input):
#        """Test command for generating a move."""
#
#        mocked_input.side_effect = [
#                'genmove\n',
#                'genmove w w\n',
#                'genmove 2\n',
#                'quit\n'
#                ]
#
#        testout = io.StringIO()
#        imagoIO = ImagoIO(
#                decisionAlgorithmId=DecisionAlgorithms.MONTECARLO,
#                outputStream=testout
#                )
#
#        imagoIO.start()
#        value = testout.getvalue()
#        self.assertEqual(
#                '? Wrong number of arguments\n' +
#                '? Usage: genmove <color>\n\n' +
#                '? Wrong number of arguments\n' +
#                '? Usage: genmove <color>\n\n' +
#                '? Error: Unknown color [2].\n\n',
#                value
#                )
#
#        mocked_input.side_effect = [
#                'genmove w\n',
#                'quit\n']
#
#        testout = io.StringIO()
#        imagoIO = ImagoIO(
#                decisionAlgorithmId=DecisionAlgorithms.MONTECARLO,
#                outputStream=testout
#                )
#        imagoIO.start()
#        value = testout.getvalue()
#        vertexValue = value.split(' ')[1].split('\n')[0]
#
#        # Test parsing vertex does not raise an error
#        parseVertex(vertexValue, imagoIO.gameEngine.gameState.size)
#
#        testout.close()


if __name__ == '__main__':
    unittest.main()