|
1 | 1 | """Tests for the ocr-numbers exercise |
2 | 2 |
|
3 | 3 | Implementation note: |
4 | | -Both ocr.grid and ocr.number should validate their input |
5 | | -and raise ValueErrors with meaningful error messages |
| 4 | +ocr.convert should validate its input and |
| 5 | +raise ValueErrors with meaningful error messages |
6 | 6 | if necessary. |
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import unittest |
10 | 10 |
|
11 | | -from ocr_numbers import grid, number |
| 11 | +from ocr_numbers import convert |
12 | 12 |
|
13 | 13 |
|
| 14 | +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 |
| 15 | + |
14 | 16 | class OcrTest(unittest.TestCase): |
15 | | - def test_0(self): |
16 | | - self.assertEqual(number([" _ ", |
17 | | - "| |", |
18 | | - "|_|", |
19 | | - " "]), '0') |
20 | | - |
21 | | - def test_1(self): |
22 | | - self.assertEqual(number([" ", |
23 | | - " |", |
24 | | - " |", |
25 | | - " "]), '1') |
26 | | - |
27 | | - def test_garbage(self): |
28 | | - self.assertEqual(number([" _ ", |
29 | | - " _|", |
30 | | - " |", |
31 | | - " "]), '?') |
32 | | - |
33 | | - def test_last_line_nonblank(self): |
34 | | - self.assertEqual(number([" ", |
35 | | - " |", |
36 | | - " |", |
37 | | - "| |"]), '?') |
38 | | - |
39 | | - def test_unknown_char(self): |
40 | | - self.assertEqual(number([" - ", |
41 | | - " _|", |
42 | | - " X|", |
43 | | - " "]), '?') |
44 | | - |
45 | | - def test_too_short_row(self): |
| 17 | + def test_recognizes_0(self): |
| 18 | + self.assertEqual(convert([" _ ", |
| 19 | + "| |", |
| 20 | + "|_|", |
| 21 | + " "]), '0') |
| 22 | + |
| 23 | + def test_recognizes_1(self): |
| 24 | + self.assertEqual(convert([" ", |
| 25 | + " |", |
| 26 | + " |", |
| 27 | + " "]), '1') |
| 28 | + |
| 29 | + def test_unreadable(self): |
| 30 | + self.assertEqual(convert([" ", |
| 31 | + " _", |
| 32 | + " |", |
| 33 | + " "]), '?') |
| 34 | + |
| 35 | + def test_line_number_not_multiple_of_four(self): |
46 | 36 | with self.assertRaises(ValueError): |
47 | | - number([" ", |
48 | | - " _|", |
49 | | - " |", |
50 | | - " "]) |
| 37 | + convert([" _ ", |
| 38 | + "| |", |
| 39 | + " "]) |
51 | 40 |
|
52 | | - def test_insufficient_rows(self): |
53 | | - with self.assertRaises(ValueError): |
54 | | - number([" ", |
55 | | - " _|", |
56 | | - " X|"]) |
57 | | - |
58 | | - def test_grid0(self): |
59 | | - self.assertEqual(grid('0'), [" _ ", |
60 | | - "| |", |
61 | | - "|_|", |
62 | | - " "]) |
63 | | - |
64 | | - def test_grid1(self): |
65 | | - self.assertEqual(grid('1'), [" ", |
66 | | - " |", |
67 | | - " |", |
68 | | - " "]) |
69 | | - |
70 | | - def test_0010110(self): |
71 | | - self.assertEqual( |
72 | | - number([ |
73 | | - " _ _ _ _ ", |
74 | | - "| || | || | | || |", |
75 | | - "|_||_| ||_| | ||_|", |
76 | | - " " |
77 | | - ]), '0010110') |
78 | | - |
79 | | - def test_3186547290(self): |
80 | | - digits = '3186547290' |
81 | | - self.assertEqual( |
82 | | - number([ |
83 | | - " _ _ _ _ _ _ _ _ ", |
84 | | - " _| ||_||_ |_ |_| | _||_|| |", |
85 | | - " _| ||_||_| _| | ||_ _||_|", |
86 | | - " " |
87 | | - ]), digits) |
88 | | - |
89 | | - def test_Lost(self): |
90 | | - digits = '4815162342' |
91 | | - self.assertEqual( |
92 | | - number([ |
93 | | - " _ _ _ _ _ _ ", |
94 | | - "|_||_| ||_ ||_ _| _||_| _|", |
95 | | - " ||_| | _| ||_||_ _| ||_ ", |
96 | | - " " |
97 | | - ]), digits) |
98 | | - |
99 | | - def test_garble_middle(self): |
100 | | - self.assertEqual( |
101 | | - number([ |
102 | | - " _ _ _ ", |
103 | | - " | _| ||_||_ ", |
104 | | - " ||_ _| | _|", |
105 | | - " " |
106 | | - ]), '12?45') |
107 | | - |
108 | | - def test_grid3186547290(self): |
109 | | - digits = '3186547290' |
110 | | - self.assertEqual( |
111 | | - grid(digits), [ |
112 | | - " _ _ _ _ _ _ _ _ ", |
113 | | - " _| ||_||_ |_ |_| | _||_|| |", |
114 | | - " _| ||_||_| _| | ||_ _||_|", |
115 | | - " " |
116 | | - ]) |
117 | | - |
118 | | - def test_invalid_grid(self): |
| 41 | + def test_col_number_not_multiple_of_three(self): |
119 | 42 | with self.assertRaises(ValueError): |
120 | | - grid('123a') |
| 43 | + convert([" ", |
| 44 | + " |", |
| 45 | + " |", |
| 46 | + " "]) |
| 47 | + |
| 48 | + 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") |
| 65 | + |
| 66 | + def test_recognizes_2(self): |
| 67 | + self.assertEqual(convert([" _ ", |
| 68 | + " _|", |
| 69 | + "|_ ", |
| 70 | + " "]), "2") |
| 71 | + |
| 72 | + def test_recognizes_3(self): |
| 73 | + self.assertEqual(convert([" _ ", |
| 74 | + " _|", |
| 75 | + " _|", |
| 76 | + " "]), "3") |
| 77 | + |
| 78 | + def test_recognizes_4(self): |
| 79 | + self.assertEqual(convert([" ", |
| 80 | + "|_|", |
| 81 | + " |", |
| 82 | + " "]), "4") |
| 83 | + |
| 84 | + def test_recognizes_5(self): |
| 85 | + self.assertEqual(convert([" _ ", |
| 86 | + "|_ ", |
| 87 | + " _|", |
| 88 | + " "]), "5") |
| 89 | + |
| 90 | + def test_recognizes_6(self): |
| 91 | + self.assertEqual(convert([" _ ", |
| 92 | + "|_ ", |
| 93 | + "|_|", |
| 94 | + " "]), "6") |
| 95 | + |
| 96 | + def test_recognizes_7(self): |
| 97 | + self.assertEqual(convert([" _ ", |
| 98 | + " |", |
| 99 | + " |", |
| 100 | + " "]), "7") |
| 101 | + |
| 102 | + def test_recognizes_8(self): |
| 103 | + self.assertEqual(convert([" _ ", |
| 104 | + "|_|", |
| 105 | + "|_|", |
| 106 | + " "]), "8") |
| 107 | + |
| 108 | + def test_recognizes_9(self): |
| 109 | + self.assertEqual(convert([" _ ", |
| 110 | + "|_|", |
| 111 | + " _|", |
| 112 | + " "]), "9") |
| 113 | + |
| 114 | + 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") |
121 | 139 |
|
122 | 140 |
|
123 | 141 | if __name__ == '__main__': |
|
0 commit comments