|
1 |
| -"""Tests for the ocr-numbers exercise |
2 |
| -
|
3 |
| -Implementation note: |
4 |
| -ocr.convert should validate its input and |
5 |
| -raise ValueErrors with meaningful error messages |
6 |
| -if necessary. |
7 |
| -""" |
8 |
| - |
9 | 1 | import unittest
|
10 | 2 |
|
11 | 3 | from ocr_numbers import convert
|
12 | 4 |
|
13 |
| - |
14 | 5 | # Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
|
15 | 6 |
|
| 7 | + |
16 | 8 | class OcrNumbersTest(unittest.TestCase):
|
17 | 9 | def test_recognizes_0(self):
|
18 |
| - self.assertEqual(convert([" _ ", |
19 |
| - "| |", |
20 |
| - "|_|", |
21 |
| - " "]), '0') |
| 10 | + self.assertEqual(convert([" _ ", "| |", "|_|", " "]), "0") |
22 | 11 |
|
23 | 12 | def test_recognizes_1(self):
|
24 |
| - self.assertEqual(convert([" ", |
25 |
| - " |", |
26 |
| - " |", |
27 |
| - " "]), '1') |
28 |
| - |
29 |
| - def test_unreadable_but_correctly_sized(self): |
30 |
| - self.assertEqual(convert([" ", |
31 |
| - " _", |
32 |
| - " |", |
33 |
| - " "]), '?') |
34 |
| - |
35 |
| - def test_line_number_not_multiple_of_four(self): |
| 13 | + self.assertEqual(convert([" ", " |", " |", " "]), "1") |
| 14 | + |
| 15 | + def test_unreadable_but_correctly_sized_inputs_return(self): |
| 16 | + self.assertEqual(convert([" ", " _", " |", " "]), "?") |
| 17 | + |
| 18 | + def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error( |
| 19 | + self |
| 20 | + ): |
36 | 21 | with self.assertRaisesWithMessage(ValueError):
|
37 |
| - convert([" _ ", |
38 |
| - "| |", |
39 |
| - " "]) |
| 22 | + convert([" _ ", "| |", " "]) |
40 | 23 |
|
41 |
| - def test_col_number_not_multiple_of_three(self): |
| 24 | + def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error( |
| 25 | + self |
| 26 | + ): |
42 | 27 | with self.assertRaisesWithMessage(ValueError):
|
43 |
| - convert([" ", |
44 |
| - " |", |
45 |
| - " |", |
46 |
| - " "]) |
| 28 | + convert([" ", " |", " |", " "]) |
47 | 29 |
|
48 | 30 | def test_recognizes_110101100(self):
|
49 |
| - input_grid = [ |
50 |
| - " _ _ _ _ ", |
51 |
| - " | || | || | | || || |", |
52 |
| - " | ||_| ||_| | ||_||_|", |
53 |
| - " " |
54 |
| - ] |
55 |
| - self.assertEqual(convert(input_grid), "110101100") |
56 |
| - |
57 |
| - def test_garbled_numbers_in_string(self): |
58 |
| - input_grid = [ |
59 |
| - " _ _ _ ", |
60 |
| - " | || | || | || || |", |
61 |
| - " | | _| ||_| | ||_||_|", |
62 |
| - " " |
63 |
| - ] |
64 |
| - self.assertEqual(convert(input_grid), "11?10?1?0") |
| 31 | + self.assertEqual( |
| 32 | + convert( |
| 33 | + [ |
| 34 | + " _ _ _ _ ", |
| 35 | + " | || | || | | || || |", |
| 36 | + " | ||_| ||_| | ||_||_|", |
| 37 | + " ", |
| 38 | + ] |
| 39 | + ), |
| 40 | + "110101100", |
| 41 | + ) |
| 42 | + |
| 43 | + def test_garbled_numbers_in_a_string_are_replaced_with(self): |
| 44 | + self.assertEqual( |
| 45 | + convert( |
| 46 | + [ |
| 47 | + " _ _ _ ", |
| 48 | + " | || | || | || || |", |
| 49 | + " | | _| ||_| | ||_||_|", |
| 50 | + " ", |
| 51 | + ] |
| 52 | + ), |
| 53 | + "11?10?1?0", |
| 54 | + ) |
65 | 55 |
|
66 | 56 | def test_recognizes_2(self):
|
67 |
| - self.assertEqual(convert([" _ ", |
68 |
| - " _|", |
69 |
| - "|_ ", |
70 |
| - " "]), "2") |
| 57 | + self.assertEqual(convert([" _ ", " _|", "|_ ", " "]), "2") |
71 | 58 |
|
72 | 59 | def test_recognizes_3(self):
|
73 |
| - self.assertEqual(convert([" _ ", |
74 |
| - " _|", |
75 |
| - " _|", |
76 |
| - " "]), "3") |
| 60 | + self.assertEqual(convert([" _ ", " _|", " _|", " "]), "3") |
77 | 61 |
|
78 | 62 | def test_recognizes_4(self):
|
79 |
| - self.assertEqual(convert([" ", |
80 |
| - "|_|", |
81 |
| - " |", |
82 |
| - " "]), "4") |
| 63 | + self.assertEqual(convert([" ", "|_|", " |", " "]), "4") |
83 | 64 |
|
84 | 65 | def test_recognizes_5(self):
|
85 |
| - self.assertEqual(convert([" _ ", |
86 |
| - "|_ ", |
87 |
| - " _|", |
88 |
| - " "]), "5") |
| 66 | + self.assertEqual(convert([" _ ", "|_ ", " _|", " "]), "5") |
89 | 67 |
|
90 | 68 | def test_recognizes_6(self):
|
91 |
| - self.assertEqual(convert([" _ ", |
92 |
| - "|_ ", |
93 |
| - "|_|", |
94 |
| - " "]), "6") |
| 69 | + self.assertEqual(convert([" _ ", "|_ ", "|_|", " "]), "6") |
95 | 70 |
|
96 | 71 | def test_recognizes_7(self):
|
97 |
| - self.assertEqual(convert([" _ ", |
98 |
| - " |", |
99 |
| - " |", |
100 |
| - " "]), "7") |
| 72 | + self.assertEqual(convert([" _ ", " |", " |", " "]), "7") |
101 | 73 |
|
102 | 74 | def test_recognizes_8(self):
|
103 |
| - self.assertEqual(convert([" _ ", |
104 |
| - "|_|", |
105 |
| - "|_|", |
106 |
| - " "]), "8") |
| 75 | + self.assertEqual(convert([" _ ", "|_|", "|_|", " "]), "8") |
107 | 76 |
|
108 | 77 | def test_recognizes_9(self):
|
109 |
| - self.assertEqual(convert([" _ ", |
110 |
| - "|_|", |
111 |
| - " _|", |
112 |
| - " "]), "9") |
| 78 | + self.assertEqual(convert([" _ ", "|_|", " _|", " "]), "9") |
113 | 79 |
|
114 | 80 | def test_recognizes_string_of_decimal_numbers(self):
|
115 |
| - input_grid = [ |
116 |
| - " _ _ _ _ _ _ _ _ ", |
117 |
| - " | _| _||_||_ |_ ||_||_|| |", |
118 |
| - " ||_ _| | _||_| ||_| _||_|", |
119 |
| - " " |
120 |
| - ] |
121 |
| - self.assertEqual(convert(input_grid), "1234567890") |
122 |
| - |
123 |
| - def test_recognizes_numbers_separated_by_empty_lines(self): |
124 |
| - input_grid = [ |
125 |
| - " _ _ ", |
126 |
| - " | _| _|", |
127 |
| - " ||_ _|", |
128 |
| - " ", |
129 |
| - " _ _ ", |
130 |
| - "|_||_ |_ ", |
131 |
| - " | _||_|", |
132 |
| - " ", |
133 |
| - " _ _ _ ", |
134 |
| - " ||_||_|", |
135 |
| - " ||_| _|", |
136 |
| - " " |
137 |
| - ] |
138 |
| - self.assertEqual(convert(input_grid), "123,456,789") |
| 81 | + self.assertEqual( |
| 82 | + convert( |
| 83 | + [ |
| 84 | + " _ _ _ _ _ _ _ _ ", |
| 85 | + " | _| _||_||_ |_ ||_||_|| |", |
| 86 | + " ||_ _| | _||_| ||_| _||_|", |
| 87 | + " ", |
| 88 | + ] |
| 89 | + ), |
| 90 | + "1234567890", |
| 91 | + ) |
| 92 | + |
| 93 | + def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas( |
| 94 | + self |
| 95 | + ): |
| 96 | + self.assertEqual( |
| 97 | + convert( |
| 98 | + [ |
| 99 | + " _ _ ", |
| 100 | + " | _| _|", |
| 101 | + " ||_ _|", |
| 102 | + " ", |
| 103 | + " _ _ ", |
| 104 | + "|_||_ |_ ", |
| 105 | + " | _||_|", |
| 106 | + " ", |
| 107 | + " _ _ _ ", |
| 108 | + " ||_||_|", |
| 109 | + " ||_| _|", |
| 110 | + " ", |
| 111 | + ] |
| 112 | + ), |
| 113 | + "123,456,789", |
| 114 | + ) |
139 | 115 |
|
140 | 116 | # Utility functions
|
141 | 117 | def assertRaisesWithMessage(self, exception):
|
142 | 118 | return self.assertRaisesRegex(exception, r".+")
|
143 | 119 |
|
144 | 120 |
|
145 |
| -if __name__ == '__main__': |
| 121 | +if __name__ == "__main__": |
146 | 122 | unittest.main()
|
0 commit comments