1
exercism fetch python queen-attack

queen_attack_test.py

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
import unittest

from queen_attack import board, can_attack


# Tests adapted from `problem-specifications//canonical-data.json` @ v2.0.0

class QueenAttackTest(unittest.TestCase):

    def test_queen_valid_position(self):
        try:
            board((1, 1), (2, 2))
        except ValueError:
            self.fail("Unexpected Exception")

    def test_queen_negative_row(self):
        with self.assertRaises(ValueError):
            board((1, 1), (-2, 2))

    def test_queen_invalid_row(self):
        with self.assertRaises(ValueError):
            board((1, 1), (8, 4))

    def test_queen_negative_column(self):
        with self.assertRaises(ValueError):
            board((1, 1), (2, -2))

    def test_queen_invalid_column(self):
        with self.assertRaises(ValueError):
            board((1, 1), (4, 8))

    def test_attack_false(self):
        self.assertIs(can_attack((2, 4), (6, 6)), False)

    def test_attack_same_row(self):
        self.assertIs(can_attack((2, 4), (2, 6)), True)

    def test_attack_same_column(self):
        self.assertIs(can_attack((4, 5), (2, 5)), True)

    def test_attack_diagonal1(self):
        self.assertIs(can_attack((2, 2), (0, 4)), True)

    def test_attack_diagonal2(self):
        self.assertIs(can_attack((2, 2), (3, 1)), True)

    def test_attack_diagonal3(self):
        self.assertIs(can_attack((2, 2), (1, 1)), True)

    def test_attack_diagonal4(self):
        self.assertIs(can_attack((2, 2), (5, 5)), True)

    # Tests beyond this point are not part of the canonical data.

    # If either board or can_attack are called with an invalid board position
    # they should raise a ValueError with a meaningful error message.
    def test_invalid_position_can_attack(self):
        with self.assertRaises(ValueError):
            can_attack((0, 0), (7, 8))

    def test_queens_same_position_board(self):
        with self.assertRaises(ValueError):
            board((2, 2), (2, 2))

    def test_queens_same_position_can_attack(self):
        with self.assertRaises(ValueError):
            can_attack((2, 2), (2, 2))

    def test_board1(self):
        ans = ['________',
               '________',
               '___W____',
               '________',
               '________',
               '______B_',
               '________',
               '________']
        self.assertEqual(board((2, 3), (5, 6)), ans)

    def test_board2(self):
        ans = ['______W_',
               '_______B',
               '________',
               '________',
               '________',
               '________',
               '________',
               '________']
        self.assertEqual(board((0, 6), (1, 7)), ans)


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