Skip to content

Commit 313bcbb

Browse files
tqa236yawpitch
authored andcommitted
Add test template for ocr-numbers (#2127)
* Add test template for ocr-numbers * Add hints * Update hints
1 parent fd006da commit 313bcbb

File tree

4 files changed

+118
-116
lines changed

4 files changed

+118
-116
lines changed

exercises/ocr-numbers/.meta/hints.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Hints
2+
3+
Your converter should validate its input and raise a ValueError with a meaningful message if it receives a string that isn't a valid OCR number.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.header() }}
3+
4+
class {{ exercise | camel_case }}Test(unittest.TestCase):
5+
{% for case in cases[0]["cases"] -%}
6+
def test_{{ case["description"] | to_snake }}(self):
7+
{% set candidate = case["input"]["rows"] -%}
8+
{% set output = case["expected"] -%}
9+
{% if output is mapping -%}
10+
with self.assertRaisesWithMessage(ValueError):
11+
{{ case["property"] | to_snake }}({{ candidate }})
12+
{% else -%}
13+
{% set expected = case["expected"] -%}
14+
self.assertEqual({{ case["property"] | to_snake }}({{ candidate }}), "{{ expected }}")
15+
{% endif %}
16+
17+
{% endfor %}
18+
19+
{{ macros.footer(True) }}

exercises/ocr-numbers/README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Update your program to recognize multi-character binary strings, replacing garbl
4040
Update your program to recognize all numbers 0 through 9, both individually and as part of a larger string.
4141

4242
```text
43-
_
43+
_
4444
_|
45-
|_
46-
45+
|_
46+
4747
```
4848

4949
Is converted to "2"
@@ -62,22 +62,26 @@ Is converted to "1234567890"
6262
Update your program to handle multiple numbers, one per line. When converting several lines, join the lines with commas.
6363

6464
```text
65-
_ _
65+
_ _
6666
| _| _|
6767
||_ _|
68-
69-
_ _
70-
|_||_ |_
68+
69+
_ _
70+
|_||_ |_
7171
| _||_|
72-
73-
_ _ _
72+
73+
_ _ _
7474
||_||_|
7575
||_| _|
76-
76+
7777
```
7878

7979
Is converted to "123,456,789"
8080

81+
## Hints
82+
83+
ocr.convert should validate its input and raise ValueErrors with meaningful error messages if necessary.
84+
8185

8286
## Exception messages
8387

Lines changed: 82 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,122 @@
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-
91
import unittest
102

113
from ocr_numbers import convert
124

13-
145
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
156

7+
168
class OcrNumbersTest(unittest.TestCase):
179
def test_recognizes_0(self):
18-
self.assertEqual(convert([" _ ",
19-
"| |",
20-
"|_|",
21-
" "]), '0')
10+
self.assertEqual(convert([" _ ", "| |", "|_|", " "]), "0")
2211

2312
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+
):
3621
with self.assertRaisesWithMessage(ValueError):
37-
convert([" _ ",
38-
"| |",
39-
" "])
22+
convert([" _ ", "| |", " "])
4023

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+
):
4227
with self.assertRaisesWithMessage(ValueError):
43-
convert([" ",
44-
" |",
45-
" |",
46-
" "])
28+
convert([" ", " |", " |", " "])
4729

4830
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+
)
6555

6656
def test_recognizes_2(self):
67-
self.assertEqual(convert([" _ ",
68-
" _|",
69-
"|_ ",
70-
" "]), "2")
57+
self.assertEqual(convert([" _ ", " _|", "|_ ", " "]), "2")
7158

7259
def test_recognizes_3(self):
73-
self.assertEqual(convert([" _ ",
74-
" _|",
75-
" _|",
76-
" "]), "3")
60+
self.assertEqual(convert([" _ ", " _|", " _|", " "]), "3")
7761

7862
def test_recognizes_4(self):
79-
self.assertEqual(convert([" ",
80-
"|_|",
81-
" |",
82-
" "]), "4")
63+
self.assertEqual(convert([" ", "|_|", " |", " "]), "4")
8364

8465
def test_recognizes_5(self):
85-
self.assertEqual(convert([" _ ",
86-
"|_ ",
87-
" _|",
88-
" "]), "5")
66+
self.assertEqual(convert([" _ ", "|_ ", " _|", " "]), "5")
8967

9068
def test_recognizes_6(self):
91-
self.assertEqual(convert([" _ ",
92-
"|_ ",
93-
"|_|",
94-
" "]), "6")
69+
self.assertEqual(convert([" _ ", "|_ ", "|_|", " "]), "6")
9570

9671
def test_recognizes_7(self):
97-
self.assertEqual(convert([" _ ",
98-
" |",
99-
" |",
100-
" "]), "7")
72+
self.assertEqual(convert([" _ ", " |", " |", " "]), "7")
10173

10274
def test_recognizes_8(self):
103-
self.assertEqual(convert([" _ ",
104-
"|_|",
105-
"|_|",
106-
" "]), "8")
75+
self.assertEqual(convert([" _ ", "|_|", "|_|", " "]), "8")
10776

10877
def test_recognizes_9(self):
109-
self.assertEqual(convert([" _ ",
110-
"|_|",
111-
" _|",
112-
" "]), "9")
78+
self.assertEqual(convert([" _ ", "|_|", " _|", " "]), "9")
11379

11480
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+
)
139115

140116
# Utility functions
141117
def assertRaisesWithMessage(self, exception):
142118
return self.assertRaisesRegex(exception, r".+")
143119

144120

145-
if __name__ == '__main__':
121+
if __name__ == "__main__":
146122
unittest.main()

0 commit comments

Comments
 (0)