Skip to content

Commit 82528d0

Browse files
tsamoglouyawpitch
authored andcommitted
say: add test template (#2156)
* add test template * say: add template #fixed string parsed * say: add template #add macros.footer and remove 'and' from example.py
1 parent 7a42fb7 commit 82528d0

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

exercises/say/.meta/template.j2

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
3+
{%- macro test_call(case) %}
4+
{{ case["property"] }}(
5+
{{ case["input"]["number"] }}
6+
)
7+
{% endmacro -%}
8+
9+
{{ macros.header() }}
10+
11+
class {{ exercise | camel_case }}Test(unittest.TestCase):
12+
{% for case in cases -%}
13+
def test_{{ case["description"] | to_snake }}(self):
14+
{%- if case is error_case %}
15+
with self.assertRaisesWithMessage(ValueError):
16+
{{- test_call(case) }}
17+
{%- else %}
18+
self.assertEqual({{- test_call(case) }}, "{{ case["expected"] }}")
19+
{%- endif %}
20+
{% endfor %}
21+
22+
{{ macros.footer() }}

exercises/say/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def say(number, recursive=False):
1515
raise ValueError('number is too large: {}'.format(number))
1616

1717
if number < 20:
18-
return small[number] if not recursive else 'and ' + small[number]
18+
return small[number] if not recursive else + small[number]
1919

2020
if number < 100:
2121
if number % 10 == 0:
@@ -25,7 +25,7 @@ def say(number, recursive=False):
2525
if number < k:
2626
if number % 100 == 0:
2727
return small[number // 100] + ' hundred'
28-
return small[number // 100] + ' hundred and ' + say(number % 100)
28+
return small[number // 100] + ' hundred ' + say(number % 100)
2929

3030
if number < m:
3131
if number % k == 0:

exercises/say/say_test.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from say import say
44

5-
65
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
76

7+
88
class SayTest(unittest.TestCase):
99
def test_zero(self):
1010
self.assertEqual(say(0), "zero")
@@ -24,46 +24,44 @@ def test_twenty_two(self):
2424
def test_one_hundred(self):
2525
self.assertEqual(say(100), "one hundred")
2626

27-
# additional track specific test
2827
def test_one_hundred_twenty_three(self):
29-
self.assertEqual(say(123), "one hundred and twenty-three")
28+
self.assertEqual(say(123), "one hundred twenty-three")
3029

3130
def test_one_thousand(self):
3231
self.assertEqual(say(1000), "one thousand")
3332

3433
def test_one_thousand_two_hundred_thirty_four(self):
35-
self.assertEqual(say(1234), "one thousand two hundred and thirty-four")
34+
self.assertEqual(say(1234), "one thousand two hundred thirty-four")
3635

3736
def test_one_million(self):
3837
self.assertEqual(say(1000000), "one million")
3938

40-
def test_1002345(self):
39+
def test_one_million_two_thousand_three_hundred_forty_five(self):
4140
self.assertEqual(
42-
say(1002345),
43-
"one million two thousand three hundred and forty-five")
41+
say(1002345), "one million two thousand three hundred forty-five"
42+
)
4443

4544
def test_one_billion(self):
4645
self.assertEqual(say(1000000000), "one billion")
4746

48-
def test_987654321123(self):
47+
def test_a_big_number(self):
4948
self.assertEqual(
50-
say(987654321123), ("nine hundred and eighty-seven billion "
51-
"six hundred and fifty-four million "
52-
"three hundred and twenty-one thousand "
53-
"one hundred and twenty-three"))
49+
say(987654321123),
50+
"nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three",
51+
)
5452

55-
def test_number_too_large(self):
53+
def test_numbers_below_zero_are_out_of_range(self):
5654
with self.assertRaisesWithMessage(ValueError):
57-
say(1000000000000)
55+
say(-1)
5856

59-
def test_number_negative(self):
57+
def test_numbers_above_999_999_999_999_are_out_of_range(self):
6058
with self.assertRaisesWithMessage(ValueError):
61-
say(-1)
59+
say(1000000000000)
6260

6361
# Utility functions
6462
def assertRaisesWithMessage(self, exception):
6563
return self.assertRaisesRegex(exception, r".+")
6664

6765

68-
if __name__ == '__main__':
66+
if __name__ == "__main__":
6967
unittest.main()

0 commit comments

Comments
 (0)