aboutsummaryrefslogtreecommitdiff
path: root/tests/test_parseHelpers.py
blob: c1405fb1e9893c174e9ce9ce40bee77390f7e8a4 (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
"""Tests for parseHelpers module."""

import unittest

from imago.data.enums import Player
from imago.engine import parseHelpers

TEST_BOARD_SIZE = 19

class TestParseHelpers(unittest.TestCase):
    """Test parseHelpers module."""

    def testParseMove(self):
        """Test parsing of GtpMove"""

        wrongMoves = [
                ["B"],
                ["A1"],
                ["B", "A1", "W"]
                ]
        for move in wrongMoves:
            self.assertRaises(
                RuntimeError,
                parseHelpers.parseMove,
                move, TEST_BOARD_SIZE
            )

        parsedMove = parseHelpers.parseMove(["B", "t1"], TEST_BOARD_SIZE)
        targetMove = parseHelpers.GtpMove(Player.BLACK, (18, 18))
        self.assertEqual(parsedMove.color, targetMove.color)
        self.assertEqual(parsedMove.vertex, targetMove.vertex)

    def testParseColor(self):
        """Test parsing of color"""
        self.assertEqual(parseHelpers.parseColor("b"), Player.BLACK)
        self.assertEqual(parseHelpers.parseColor("B"), Player.BLACK)
        self.assertEqual(parseHelpers.parseColor("w"), Player.WHITE)
        self.assertEqual(parseHelpers.parseColor("W"), Player.WHITE)

        self.assertRaises(RuntimeError, parseHelpers.parseColor, "bw")
        self.assertRaises(RuntimeError, parseHelpers.parseColor, "wb")

    def testParseVertexWrongInputs(self):
        """Test wrong inputs for parseVertex."""
        inputs = ( "a", "1", "1a", "aa1", "a0", "u1", "a"+str(TEST_BOARD_SIZE+1) )
        for text in inputs:
            self.assertRaises(
                RuntimeError,
                parseHelpers.parseVertex,
                text, TEST_BOARD_SIZE
            )

    def testParseVertexCorrectInputs(self):
        """Test correct inputs and their resulting coordinates for parseVertex."""
        self.assertEqual(parseHelpers.parseVertex(
            "a1", TEST_BOARD_SIZE),
            (18,0))
        self.assertEqual(parseHelpers.parseVertex(
            "b1", TEST_BOARD_SIZE),
            (18,1))
        self.assertEqual(parseHelpers.parseVertex(
            "a2", TEST_BOARD_SIZE),
            (17,0))
        self.assertEqual(parseHelpers.parseVertex(
            "b2", TEST_BOARD_SIZE),
            (17,1))

        self.assertEqual(parseHelpers.parseVertex(
            "T1", TEST_BOARD_SIZE),
            (18,18))
        self.assertEqual(parseHelpers.parseVertex(
            "T19", TEST_BOARD_SIZE),
            (0,18))
        self.assertEqual(parseHelpers.parseVertex(
            "A19", TEST_BOARD_SIZE),
            (0,0))

        self.assertEqual(parseHelpers.parseVertex(
            "pass", TEST_BOARD_SIZE),
            "pass")

    def testVertexToString(self):
        """Test converting vertices to strings."""

        # Try with vertices as tuples
        self.assertEqual(parseHelpers.vertexToString((0,0), TEST_BOARD_SIZE), "A19")
        self.assertEqual(parseHelpers.vertexToString((1,0), TEST_BOARD_SIZE), "A18")
        self.assertEqual(parseHelpers.vertexToString((2,0), TEST_BOARD_SIZE), "A17")
        self.assertEqual(parseHelpers.vertexToString((0,1), TEST_BOARD_SIZE), "B19")

        # Try with vertices as arrays
        self.assertEqual(parseHelpers.vertexToString([0,2], TEST_BOARD_SIZE), "C19")
        self.assertEqual(parseHelpers.vertexToString([0,18], TEST_BOARD_SIZE), "T19")
        self.assertEqual(parseHelpers.vertexToString([18,0], TEST_BOARD_SIZE), "A1")
        self.assertEqual(parseHelpers.vertexToString([18,18], TEST_BOARD_SIZE), "T1")

        self.assertEqual(parseHelpers.vertexToString("pass", TEST_BOARD_SIZE), "pass")

        wrongVertices = [
            (-1,0),
            (0,-1),
            (-1,-1),
            (19,0),
            [0,19],
            [19,19],
            [0],
            [0,0,0]
        ]
        for vertex in wrongVertices:
            self.assertRaises(
                RuntimeError,
                parseHelpers.vertexToString,
                vertex, TEST_BOARD_SIZE
            )

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