Skip to content

Commit d99090c

Browse files
[zipper] add template (#2177)
* [zipper] add template Adds a test template for `zipper`, removes the need for #2160, which was going down a much more complicated path. Updates example.py for a minor change in the canonical data for Zipper.up. * Update exercises/zipper/.meta/template.j2 Co-Authored-By: Corey McCandless <[email protected]> * Update exercises/zipper/.meta/template.j2 Co-Authored-By: Corey McCandless <[email protected]> * Removing extra blank lines Co-authored-by: Corey McCandless <[email protected]>
1 parent f636b25 commit d99090c

File tree

3 files changed

+339
-53
lines changed

3 files changed

+339
-53
lines changed

exercises/zipper/.meta/template.j2

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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() }}

exercises/zipper/example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def set_right(self, tree):
3333
return self
3434

3535
def up(self):
36+
if not self.ancestors:
37+
return None
3638
return Zipper(self.ancestors[-1], self.ancestors[:-1])
3739

3840
def to_tree(self):

0 commit comments

Comments
 (0)