|
| 1 | +{%- import "generator_macros.j2" as macros with context -%} |
| 2 | + |
| 3 | +{%- macro test_case(case) %} |
| 4 | + {%- set input = case["input"] -%} |
| 5 | + {%- set expected = case["expected"] -%} |
| 6 | + def test_{{ case["description"] | to_snake }}(self): |
| 7 | + {%- if case["property"] == "sameResultFromOperations" %} |
| 8 | + {{ same_result_from_operations(input, expected) }} |
| 9 | + {%- else %} |
| 10 | + {{ expected_value(input, expected) }} |
| 11 | + {%- endif %} |
| 12 | +{%- endmacro -%} |
| 13 | + |
| 14 | +{%- macro expected_value(input, expected) -%} |
| 15 | + initial = {{ input["initialTree"] }} |
| 16 | + |
| 17 | + {% if expected["type"] == "tree" %} |
| 18 | + expected = {{ expected["value"] }} |
| 19 | + {%- endif %} |
| 20 | + |
| 21 | + zipper = Zipper.from_tree(initial) |
| 22 | + result = zipper{{ chain(input["operations"]) }} |
| 23 | + {%- if expected["type"] == "tree" %} |
| 24 | + self.assertEqual(result, expected) |
| 25 | + {%- elif expected["value"] is none %} |
| 26 | + self.assertIsNone(result) |
| 27 | + {%- else %} |
| 28 | + self.assertEqual(result, {{ expected["value"] }}) |
| 29 | + {%- endif %} |
| 30 | +{%- endmacro %} |
| 31 | + |
| 32 | +{%- macro same_result_from_operations(input, expected) -%} |
| 33 | + initial = {{ input["initialTree"] }} |
| 34 | + result = Zipper.from_tree(initial){{ chain(input["operations"]) }}.to_tree() |
| 35 | + |
| 36 | + final = {{ expected["initialTree"] }} |
| 37 | + expected = Zipper.from_tree(final){{ chain(expected["operations"]) }}.to_tree() |
| 38 | + |
| 39 | + self.assertEqual(result, expected) |
| 40 | +{%- endmacro %} |
| 41 | + |
| 42 | + |
| 43 | +{%- macro chain(operations) %} |
| 44 | + {%- for op in operations -%} |
| 45 | + .{{ op["operation"] }}({{ op["item"] }}) |
| 46 | + {%- endfor %} |
| 47 | +{%- endmacro -%} |
| 48 | + |
| 49 | +{{ macros.header (imports=["Zipper"]) }} |
| 50 | + |
| 51 | +class {{ exercise | camel_case }}Test(unittest.TestCase): |
| 52 | +{%- for case in cases %} |
| 53 | + {{ test_case(case) }} |
| 54 | +{%- endfor %} |
| 55 | + |
| 56 | +{{ macros.footer() }} |
0 commit comments