-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
rational-numbers add template #2080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cmccandless
merged 12 commits into
exercism:master
from
GKotfis:rational-numbers-add-template
Oct 25, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aad3a9a
Init template
9786f14
Import Rational type
GKotfis 939903e
Merge branch 'master' into rational-numbers-add-template
GKotfis 9c09ea7
Implement arithmetic template
1b719c1
Implement absolute value template
e717dce
Implement exponentiation template
2e39040
Implement exp real and reduce template
6313c40
Cleanup template
437f4e1
Extract nested loops to methods
c1b2302
fix exponentiation real tests assertion
766a4fc
Python 2 compatibilty - import division
fd553fe
Merge branch 'master' into rational-numbers-add-template
cmccandless File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| {%- import "generator_macros.j2" as macros with context -%} | ||
|
|
||
| {%- set operators = { | ||
| "add": "+", | ||
| "sub": "-", | ||
| "mul": "*", | ||
| "div": "/", | ||
| "exprational": "**", | ||
| "expreal": "**" | ||
| } -%} | ||
|
|
||
| {% macro test_case_arithmetic(case) -%} | ||
| {%- set r1 = "Rational({}, {})".format(case["input"]["r1"][0], case["input"]["r1"][1]) -%} | ||
| {%- set r2 = "Rational({}, {})".format(case["input"]["r2"][0], case["input"]["r2"][1]) -%} | ||
| {%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%} | ||
| {%- set operator = operators[case["property"]] -%} | ||
|
|
||
| def test_{{ case["description"] | to_snake }}(self): | ||
| self.assertEqual({{r1}} {{operator}} {{r2}}, {{expected}}) | ||
| {%- endmacro %} | ||
|
|
||
| {% macro test_case_absolutevalue(case) -%} | ||
| {%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%} | ||
| {%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%} | ||
| {%- set method = case["property"] -%} | ||
| def test_{{ case["description"] | to_snake }}(self): | ||
| self.assertEqual({{method}}({{r}}), {{expected}}) | ||
| {%- endmacro %} | ||
|
|
||
| {% macro test_case_exponentiation(case) -%} | ||
| {%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%} | ||
| {%- set n = case["input"]["n"] -%} | ||
| {%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%} | ||
| {%- set operator = operators[case["property"]] -%} | ||
|
|
||
| def test_{{ case["description"] | to_snake }}(self): | ||
| self.assertEqual({{r}} {{operator}} {{n}}, {{expected}}) | ||
| {%- endmacro %} | ||
|
|
||
| {% macro test_case_exponentiation_real(case) -%} | ||
| {%- set x = case["input"]["x"] -%} | ||
| {%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%} | ||
| {%- set expected = case["expected"] -%} | ||
| {%- set operator = operators[case["property"]] -%} | ||
|
|
||
| def test_{{ case["description"] | to_snake }}(self): | ||
| self.assertAlmostEqual({{x}} {{operator}} {{r}}, {{expected}}, places=8) | ||
| {%- endmacro %} | ||
|
|
||
| {% macro test_case_reduction(case) -%} | ||
| {%- set r = "Rational({}, {})".format(case["input"]["r"][0], case["input"]["r"][1]) -%} | ||
| {%- set expected = "Rational({}, {})".format(case["expected"][0], case["expected"][1]) -%} | ||
|
|
||
| def test_{{ case["description"] | to_snake }}(self): | ||
| self.assertEqual({{r}}, {{expected}}) | ||
| {%- endmacro %} | ||
|
|
||
| {% macro render_arithmetic_cases(mathtypescases) -%} | ||
|
|
||
| {% for mathoperationcases in mathtypescases["cases"] %} | ||
| # {{ mathoperationcases["description"] }} | ||
| {% for case in mathoperationcases["cases"] %} | ||
| {{ test_case_arithmetic(case) }} | ||
| {% endfor %} | ||
| {% endfor %} | ||
|
|
||
| {%- endmacro %} | ||
|
|
||
| {% macro render_other_cases(mathtypescases) -%} | ||
|
|
||
| {% for case in mathtypescases["cases"] %} | ||
| {% if mathtypescases["description"] == 'Absolute value' %} | ||
| {{ test_case_absolutevalue(case) }} | ||
| {% elif mathtypescases["description"] == 'Exponentiation of a rational number' %} | ||
| {{ test_case_exponentiation(case) }} | ||
| {% elif mathtypescases["description"] == 'Exponentiation of a real number to a rational number' %} | ||
| {{ test_case_exponentiation_real(case) }} | ||
| {% elif mathtypescases["description"] == 'Reduction to lowest terms' %} | ||
| {{ test_case_reduction(case) }} | ||
| {% endif %} | ||
| {% endfor %} | ||
|
|
||
| {%- endmacro %} | ||
|
|
||
| from __future__ import division | ||
| {{ macros.header(imports=["Rational"]) }} | ||
|
|
||
| class {{ exercise | camel_case }}Test(unittest.TestCase): | ||
| {% for mathtypescases in cases %} | ||
| # Tests of type: {{ mathtypescases["description"] }} | ||
| {% if mathtypescases["description"] == 'Arithmetic' %} | ||
| {{ render_arithmetic_cases(mathtypescases) }} | ||
| {% else %} | ||
| {{ render_other_cases(mathtypescases) }} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {{ macros.footer() }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.