Skip to content

Commit 8fb0ae9

Browse files
GKotfiscmccandless
andcommitted
rational-numbers add template (exercism#2080)
Co-Authored-By: Corey McCandless <cmccandless@users.noreply.github.com>
1 parent 708a635 commit 8fb0ae9

2 files changed

Lines changed: 157 additions & 49 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
3+
{%- set operators = {
4+
"add": "+",
5+
"sub": "-",
6+
"mul": "*",
7+
"div": "/",
8+
"exprational": "**",
9+
"expreal": "**"
10+
} -%}
11+
12+
{% macro test_case_arithmetic(case) -%}
13+
{%- set r1 = "Rational({}, {})".format(case["input"]["r1"][0], case["input"]["r1"][1]) -%}
14+
{%- set r2 = "Rational({}, {})".format(case["input"]["r2"][0], case["input"]["r2"][1]) -%}
15+
{%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%}
16+
{%- set operator = operators[case["property"]] -%}
17+
18+
def test_{{ case["description"] | to_snake }}(self):
19+
self.assertEqual({{r1}} {{operator}} {{r2}}, {{expected}})
20+
{%- endmacro %}
21+
22+
{% macro test_case_absolutevalue(case) -%}
23+
{%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%}
24+
{%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%}
25+
{%- set method = case["property"] -%}
26+
def test_{{ case["description"] | to_snake }}(self):
27+
self.assertEqual({{method}}({{r}}), {{expected}})
28+
{%- endmacro %}
29+
30+
{% macro test_case_exponentiation(case) -%}
31+
{%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%}
32+
{%- set n = case["input"]["n"] -%}
33+
{%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%}
34+
{%- set operator = operators[case["property"]] -%}
35+
36+
def test_{{ case["description"] | to_snake }}(self):
37+
self.assertEqual({{r}} {{operator}} {{n}}, {{expected}})
38+
{%- endmacro %}
39+
40+
{% macro test_case_exponentiation_real(case) -%}
41+
{%- set x = case["input"]["x"] -%}
42+
{%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%}
43+
{%- set expected = case["expected"] -%}
44+
{%- set operator = operators[case["property"]] -%}
45+
46+
def test_{{ case["description"] | to_snake }}(self):
47+
self.assertAlmostEqual({{x}} {{operator}} {{r}}, {{expected}}, places=8)
48+
{%- endmacro %}
49+
50+
{% macro test_case_reduction(case) -%}
51+
{%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%}
52+
{%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%}
53+
54+
def test_{{ case["description"] | to_snake }}(self):
55+
self.assertEqual({{r}}, {{expected}})
56+
{%- endmacro %}
57+
58+
{% macro render_arithmetic_cases(mathtypescases) -%}
59+
60+
{% for mathoperationcases in mathtypescases["cases"] %}
61+
# {{ mathoperationcases["description"] }}
62+
{% for case in mathoperationcases["cases"] %}
63+
{{ test_case_arithmetic(case) }}
64+
{% endfor %}
65+
{% endfor %}
66+
67+
{%- endmacro %}
68+
69+
{% macro render_other_cases(mathtypescases) -%}
70+
71+
{% for case in mathtypescases["cases"] %}
72+
{% if mathtypescases["description"] == 'Absolute value' %}
73+
{{ test_case_absolutevalue(case) }}
74+
{% elif mathtypescases["description"] == 'Exponentiation of a rational number' %}
75+
{{ test_case_exponentiation(case) }}
76+
{% elif mathtypescases["description"] == 'Exponentiation of a real number to a rational number' %}
77+
{{ test_case_exponentiation_real(case) }}
78+
{% elif mathtypescases["description"] == 'Reduction to lowest terms' %}
79+
{{ test_case_reduction(case) }}
80+
{% endif %}
81+
{% endfor %}
82+
83+
{%- endmacro %}
84+
85+
from __future__ import division
86+
{{ macros.header(imports=["Rational"]) }}
87+
88+
class {{ exercise | camel_case }}Test(unittest.TestCase):
89+
{% for mathtypescases in cases %}
90+
# Tests of type: {{ mathtypescases["description"] }}
91+
{% if mathtypescases["description"] == 'Arithmetic' %}
92+
{{ render_arithmetic_cases(mathtypescases) }}
93+
{% else %}
94+
{{ render_other_cases(mathtypescases) }}
95+
{% endif %}
96+
{% endfor %}
97+
{{ macros.footer() }}
Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,104 @@
11
from __future__ import division
2-
32
import unittest
43

54
from rational_numbers import Rational
65

7-
86
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
97

8+
109
class RationalNumbersTest(unittest.TestCase):
1110

12-
# Test addition
13-
def test_add_two_positive(self):
11+
# Tests of type: Arithmetic
12+
13+
# Addition
14+
15+
def test_add_two_positive_rational_numbers(self):
1416
self.assertEqual(Rational(1, 2) + Rational(2, 3), Rational(7, 6))
1517

16-
def test_add_positive_and_negative(self):
18+
def test_add_a_positive_rational_number_and_a_negative_rational_number(self):
1719
self.assertEqual(Rational(1, 2) + Rational(-2, 3), Rational(-1, 6))
1820

19-
def test_add_two_negative(self):
21+
def test_add_two_negative_rational_numbers(self):
2022
self.assertEqual(Rational(-1, 2) + Rational(-2, 3), Rational(-7, 6))
2123

22-
def test_add_opposite(self):
24+
def test_add_a_rational_number_to_its_additive_inverse(self):
2325
self.assertEqual(Rational(1, 2) + Rational(-1, 2), Rational(0, 1))
2426

25-
# Test subtraction
26-
def test_subtract_two_positive(self):
27+
# Subtraction
28+
29+
def test_subtract_two_positive_rational_numbers(self):
2730
self.assertEqual(Rational(1, 2) - Rational(2, 3), Rational(-1, 6))
2831

29-
def test_subtract_positive_and_negative(self):
32+
def test_subtract_a_positive_rational_number_and_a_negative_rational_number(self):
3033
self.assertEqual(Rational(1, 2) - Rational(-2, 3), Rational(7, 6))
3134

32-
def test_subtract_two_negative(self):
35+
def test_subtract_two_negative_rational_numbers(self):
3336
self.assertEqual(Rational(-1, 2) - Rational(-2, 3), Rational(1, 6))
3437

35-
def test_subtract_from_self(self):
38+
def test_subtract_a_rational_number_from_itself(self):
3639
self.assertEqual(Rational(1, 2) - Rational(1, 2), Rational(0, 1))
3740

38-
# Test multiplication
39-
def test_multiply_two_positive(self):
41+
# Multiplication
42+
43+
def test_multiply_two_positive_rational_numbers(self):
4044
self.assertEqual(Rational(1, 2) * Rational(2, 3), Rational(1, 3))
4145

42-
def test_multiply_negative_by_positive(self):
46+
def test_multiply_a_negative_rational_number_by_a_positive_rational_number(self):
4347
self.assertEqual(Rational(-1, 2) * Rational(2, 3), Rational(-1, 3))
4448

45-
def test_multiply_two_negative(self):
49+
def test_multiply_two_negative_rational_numbers(self):
4650
self.assertEqual(Rational(-1, 2) * Rational(-2, 3), Rational(1, 3))
4751

48-
def test_multiply_reciprocal(self):
52+
def test_multiply_a_rational_number_by_its_reciprocal(self):
4953
self.assertEqual(Rational(1, 2) * Rational(2, 1), Rational(1, 1))
5054

51-
def test_multiply_by_one(self):
55+
def test_multiply_a_rational_number_by_1(self):
5256
self.assertEqual(Rational(1, 2) * Rational(1, 1), Rational(1, 2))
5357

54-
def test_multiply_by_zero(self):
58+
def test_multiply_a_rational_number_by_0(self):
5559
self.assertEqual(Rational(1, 2) * Rational(0, 1), Rational(0, 1))
5660

57-
# Test division
58-
def test_divide_two_positive(self):
61+
# Division
62+
63+
def test_divide_two_positive_rational_numbers(self):
5964
self.assertEqual(Rational(1, 2) / Rational(2, 3), Rational(3, 4))
6065

61-
def test_divide_positive_by_negative(self):
66+
def test_divide_a_positive_rational_number_by_a_negative_rational_number(self):
6267
self.assertEqual(Rational(1, 2) / Rational(-2, 3), Rational(-3, 4))
6368

64-
def test_divide_two_negative(self):
69+
def test_divide_two_negative_rational_numbers(self):
6570
self.assertEqual(Rational(-1, 2) / Rational(-2, 3), Rational(3, 4))
6671

67-
def test_divide_by_one(self):
72+
def test_divide_a_rational_number_by_1(self):
6873
self.assertEqual(Rational(1, 2) / Rational(1, 1), Rational(1, 2))
6974

70-
# Test absolute value
71-
def test_absolute_value_of_positive(self):
75+
# Tests of type: Absolute value
76+
77+
def test_absolute_value_of_a_positive_rational_number(self):
7278
self.assertEqual(abs(Rational(1, 2)), Rational(1, 2))
7379

74-
def test_absolute_value_of_positive_negative_numerator_denominator(self):
80+
def test_absolute_value_of_a_positive_rational_number_with_negative_numerator_and_denominator(
81+
self
82+
):
7583
self.assertEqual(abs(Rational(-1, -2)), Rational(1, 2))
7684

77-
def test_absolute_value_of_negative(self):
85+
def test_absolute_value_of_a_negative_rational_number(self):
7886
self.assertEqual(abs(Rational(-1, 2)), Rational(1, 2))
7987

80-
def test_absolute_value_of_negative_with_negative_denominator(self):
88+
def test_absolute_value_of_a_negative_rational_number_with_negative_denominator(
89+
self
90+
):
8191
self.assertEqual(abs(Rational(1, -2)), Rational(1, 2))
8292

8393
def test_absolute_value_of_zero(self):
8494
self.assertEqual(abs(Rational(0, 1)), Rational(0, 1))
8595

86-
# Test exponentiation of a rational number
87-
def test_raise_a_positive_rational_to_a_positive_integer_power(self):
96+
# Tests of type: Exponentiation of a rational number
97+
98+
def test_raise_a_positive_rational_number_to_a_positive_integer_power(self):
8899
self.assertEqual(Rational(1, 2) ** 3, Rational(1, 8))
89100

90-
def test_raise_a_negative_rational_to_a_positive_integer_power(self):
101+
def test_raise_a_negative_rational_number_to_a_positive_integer_power(self):
91102
self.assertEqual(Rational(-1, 2) ** 3, Rational(-1, 8))
92103

93104
def test_raise_zero_to_an_integer_power(self):
@@ -96,43 +107,43 @@ def test_raise_zero_to_an_integer_power(self):
96107
def test_raise_one_to_an_integer_power(self):
97108
self.assertEqual(Rational(1, 1) ** 4, Rational(1, 1))
98109

99-
def test_raise_a_positive_rational_to_the_power_of_zero(self):
110+
def test_raise_a_positive_rational_number_to_the_power_of_zero(self):
100111
self.assertEqual(Rational(1, 2) ** 0, Rational(1, 1))
101112

102-
def test_raise_a_negative_rational_to_the_power_of_zero(self):
113+
def test_raise_a_negative_rational_number_to_the_power_of_zero(self):
103114
self.assertEqual(Rational(-1, 2) ** 0, Rational(1, 1))
104115

105-
# Test exponentiation of a real number to a rational number
106-
def test_raise_a_real_number_to_a_positive_rational(self):
116+
# Tests of type: Exponentiation of a real number to a rational number
117+
118+
def test_raise_a_real_number_to_a_positive_rational_number(self):
107119
self.assertAlmostEqual(8 ** Rational(4, 3), 16.0, places=8)
108120

109-
def test_raise_a_real_number_to_a_negative_rational(self):
110-
self.assertAlmostEqual(
111-
9 ** Rational(-1, 2), 0.3333333333333333, places=8
112-
)
121+
def test_raise_a_real_number_to_a_negative_rational_number(self):
122+
self.assertAlmostEqual(9 ** Rational(-1, 2), 0.3333333333333333, places=8)
113123

114-
def test_raise_a_real_number_to_a_zero_rational(self):
124+
def test_raise_a_real_number_to_a_zero_rational_number(self):
115125
self.assertAlmostEqual(2 ** Rational(0, 1), 1.0, places=8)
116126

117-
# Test reduction to lowest terms
118-
def test_reduce_positive(self):
127+
# Tests of type: Reduction to lowest terms
128+
129+
def test_reduce_a_positive_rational_number_to_lowest_terms(self):
119130
self.assertEqual(Rational(2, 4), Rational(1, 2))
120131

121-
def test_reduce_negative(self):
132+
def test_reduce_a_negative_rational_number_to_lowest_terms(self):
122133
self.assertEqual(Rational(-4, 6), Rational(-2, 3))
123134

124-
def test_reduce_rational_with_negative_denominator(self):
135+
def test_reduce_a_rational_number_with_a_negative_denominator_to_lowest_terms(self):
125136
self.assertEqual(Rational(3, -9), Rational(-1, 3))
126137

127-
def test_reduce_zero(self):
138+
def test_reduce_zero_to_lowest_terms(self):
128139
self.assertEqual(Rational(0, 6), Rational(0, 1))
129140

130-
def test_reduce_integer(self):
141+
def test_reduce_an_integer_to_lowest_terms(self):
131142
self.assertEqual(Rational(-14, 7), Rational(-2, 1))
132143

133-
def test_reduce_one(self):
144+
def test_reduce_one_to_lowest_terms(self):
134145
self.assertEqual(Rational(13, 13), Rational(1, 1))
135146

136147

137-
if __name__ == '__main__':
148+
if __name__ == "__main__":
138149
unittest.main()

0 commit comments

Comments
 (0)