1
exercism fetch python ocr-numbers

ocr_numbers_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
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
"""Tests for the ocr-numbers exercise

Implementation note:
ocr.convert should validate its input and
raise ValueErrors with meaningful error messages
if necessary.
"""

import unittest

from ocr_numbers import convert


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

class OcrTest(unittest.TestCase):
    def test_recognizes_0(self):
        self.assertEqual(convert([" _ ",
                                  "| |",
                                  "|_|",
                                  "   "]), '0')

    def test_recognizes_1(self):
        self.assertEqual(convert(["   ",
                                  "  |",
                                  "  |",
                                  "   "]), '1')

    def test_unreadable(self):
        self.assertEqual(convert(["   ",
                                  "  _",
                                  "  |",
                                  "   "]), '?')

    def test_line_number_not_multiple_of_four(self):
        with self.assertRaises(ValueError):
            convert([" _ ",
                     "| |",
                     "   "])

    def test_col_number_not_multiple_of_three(self):
        with self.assertRaises(ValueError):
            convert(["    ",
                     "   |",
                     "   |",
                     "    "])

    def test_recognizes_110101100(self):
        input_grid = [
            "       _     _        _  _ ",
            "  |  || |  || |  |  || || |",
            "  |  ||_|  ||_|  |  ||_||_|",
            "                           "
        ]
        self.assertEqual(convert(input_grid), "110101100")

    def test_garbled_numbers_in_string(self):
        input_grid = [
            "       _     _           _ ",
            "  |  || |  || |     || || |",
            "  |  | _|  ||_|  |  ||_||_|",
            "                           "
        ]
        self.assertEqual(convert(input_grid), "11?10?1?0")

    def test_recognizes_2(self):
        self.assertEqual(convert([" _ ",
                                  " _|",
                                  "|_ ",
                                  "   "]), "2")

    def test_recognizes_3(self):
        self.assertEqual(convert([" _ ",
                                  " _|",
                                  " _|",
                                  "   "]), "3")

    def test_recognizes_4(self):
        self.assertEqual(convert(["   ",
                                  "|_|",
                                  "  |",
                                  "   "]), "4")

    def test_recognizes_5(self):
        self.assertEqual(convert([" _ ",
                                  "|_ ",
                                  " _|",
                                  "   "]), "5")

    def test_recognizes_6(self):
        self.assertEqual(convert([" _ ",
                                  "|_ ",
                                  "|_|",
                                  "   "]), "6")

    def test_recognizes_7(self):
        self.assertEqual(convert([" _ ",
                                  "  |",
                                  "  |",
                                  "   "]), "7")

    def test_recognizes_8(self):
        self.assertEqual(convert([" _ ",
                                  "|_|",
                                  "|_|",
                                  "   "]), "8")

    def test_recognizes_9(self):
        self.assertEqual(convert([" _ ",
                                  "|_|",
                                  " _|",
                                  "   "]), "9")

    def test_recognizes_string_of_decimal_numbers(self):
        input_grid = [
            "    _  _     _  _  _  _  _  _ ",
            "  | _| _||_||_ |_   ||_||_|| |",
            "  ||_  _|  | _||_|  ||_| _||_|",
            "                              "
        ]
        self.assertEqual(convert(input_grid), "1234567890")

    def test_recognizes_numbers_separated_by_empty_lines(self):
        input_grid = [
            "    _  _ ",
            "  | _| _|",
            "  ||_  _|",
            "         ",
            "    _  _ ",
            "|_||_ |_ ",
            "  | _||_|",
            "         ",
            " _  _  _ ",
            "  ||_||_|",
            "  ||_| _|",
            "         "
        ]
        self.assertEqual(convert(input_grid), "123,456,789")


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