Skip to content

Commit 186e189

Browse files
authored
kindergarten-garden: add test template (exercism#1891)
1 parent edbc41f commit 186e189

3 files changed

Lines changed: 130 additions & 26 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"cases": [
3+
{
4+
"description": "students are unordered",
5+
"cases": [
6+
{
7+
"description": "first student",
8+
"property": "plants",
9+
"input": {
10+
"diagram": "VCRRGVRG\nRVGCCGCV",
11+
"students": ["Samantha", "Patricia", "Xander", "Roger"],
12+
"student": "Patricia"
13+
},
14+
"expected": ["Violets", "Clover", "Radishes", "Violets"]
15+
},
16+
{
17+
"description": "last student",
18+
"property": "plants",
19+
"input": {
20+
"diagram": "VCRRGVRG\nRVGCCGCV",
21+
"students": ["Samantha", "Patricia", "Xander", "Roger"],
22+
"student": "Xander"
23+
},
24+
"expected": ["Radishes", "Grass", "Clover", "Violets"]
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{%- import "generator_macros.j2" as macros -%}
2+
{%- macro test_case(group_name, case) -%}
3+
{%- set input = case["input"] -%}
4+
def test_{{ group_name | to_snake }}_
5+
{{- case["description"] | to_snake }}(self):
6+
garden = Garden(
7+
"{{ input["diagram"].replace("\n", "\\n") }}"
8+
{%- if "students" in input -%}
9+
, students={{ input["students"] }}
10+
{%- endif %}
11+
)
12+
self.assertEqual(
13+
garden.{{- case["property"] | to_snake -}}
14+
("{{ input["student"] }}"),
15+
[{% for val in case["expected"] -%}
16+
"{{ val | camel_case }}",
17+
{% endfor %}]
18+
)
19+
{% endmacro -%}
20+
import unittest
21+
22+
from {{ exercise | to_snake }} import Garden
23+
24+
{{ macros.canonical_ref(version) }}
25+
26+
class {{ exercise | camel_case }}Test(unittest.TestCase):
27+
{% for casegroup in cases %}
28+
{%- set group_name = casegroup["description"] -%}
29+
{%- for case in casegroup["cases"] %}
30+
{%- if "input" in case %}
31+
{{- test_case(group_name, case) }}
32+
{%- elif "cases" in case %}
33+
{%- for subcase in case["cases"] %}
34+
{{- test_case(group_name, subcase) }}
35+
{% endfor %}
36+
{% endif %}
37+
{% endfor %}
38+
{% endfor %}
39+
{%- if additional_cases | length %}
40+
41+
# Additional tests for this track
42+
43+
{% for casegroup in additional_cases -%}
44+
{% for case in casegroup["cases"] -%}
45+
{{ test_case(casegroup["description"], case) }}
46+
{% endfor %}
47+
{%- endfor %}
48+
{%- endif %}
49+
50+
51+
52+
{{ macros.footer(has_error_case) }}

exercises/kindergarten-garden/kindergarten_garden_test.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,76 @@
22

33
from kindergarten_garden import Garden
44

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

7+
88
class KindergartenGardenTest(unittest.TestCase):
9-
def test_garden_with_single_student(self):
9+
def test_partial_garden_garden_with_single_student(self):
10+
garden = Garden("RC\nGG")
1011
self.assertEqual(
11-
Garden("RC\nGG").plants("Alice"),
12-
"Radishes Clover Grass Grass".split())
12+
garden.plants("Alice"), ["Radishes", "Clover", "Grass", "Grass"]
13+
)
1314

14-
def test_different_garden_with_single_student(self):
15+
def test_partial_garden_different_garden_with_single_student(self):
16+
garden = Garden("VC\nRC")
1517
self.assertEqual(
16-
Garden("VC\nRC").plants("Alice"),
17-
"Violets Clover Radishes Clover".split())
18+
garden.plants("Alice"), ["Violets", "Clover", "Radishes", "Clover"]
19+
)
1820

19-
def test_garden_with_two_students(self):
21+
def test_partial_garden_garden_with_two_students(self):
2022
garden = Garden("VVCG\nVVRC")
2123
self.assertEqual(
22-
garden.plants("Bob"), "Clover Grass Radishes Clover".split())
24+
garden.plants("Bob"), ["Clover", "Grass", "Radishes", "Clover"]
25+
)
26+
27+
def test_partial_garden_second_student_s_garden(self):
28+
garden = Garden("VVCCGG\nVVCCGG")
29+
self.assertEqual(garden.plants("Bob"), ["Clover", "Clover", "Clover", "Clover"])
2330

24-
def test_multiple_students_for_the_same_garden_with_three_students(self):
31+
def test_partial_garden_third_student_s_garden(self):
2532
garden = Garden("VVCCGG\nVVCCGG")
26-
self.assertEqual(garden.plants("Bob"), ["Clover"] * 4)
27-
self.assertEqual(garden.plants("Charlie"), ["Grass"] * 4)
33+
self.assertEqual(garden.plants("Charlie"), ["Grass", "Grass", "Grass", "Grass"])
2834

29-
def test_full_garden(self):
35+
def test_full_garden_first_student_s_garden(self):
3036
garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
3137
self.assertEqual(
32-
garden.plants("Alice"),
33-
"Violets Radishes Violets Radishes".split())
34-
self.assertEqual(
35-
garden.plants("Bob"), "Clover Grass Clover Clover".split())
38+
garden.plants("Alice"), ["Violets", "Radishes", "Violets", "Radishes"]
39+
)
40+
41+
def test_full_garden_second_student_s_garden(self):
42+
garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
43+
self.assertEqual(garden.plants("Bob"), ["Clover", "Grass", "Clover", "Clover"])
44+
45+
def test_full_garden_second_to_last_student_s_garden(self):
46+
garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
3647
self.assertEqual(
37-
garden.plants("Kincaid"), "Grass Clover Clover Grass".split())
48+
garden.plants("Kincaid"), ["Grass", "Clover", "Clover", "Grass"]
49+
)
50+
51+
def test_full_garden_last_student_s_garden(self):
52+
garden = Garden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV")
3853
self.assertEqual(
39-
garden.plants("Larry"), "Grass Violets Clover Violets".split())
54+
garden.plants("Larry"), ["Grass", "Violets", "Clover", "Violets"]
55+
)
4056

4157
# Additional tests for this track
42-
def test_disordered_test(self):
58+
59+
def test_students_are_unordered_first_student(self):
4360
garden = Garden(
44-
"VCRRGVRG\nRVGCCGCV",
45-
students="Samantha Patricia Xander Roger".split())
61+
"VCRRGVRG\nRVGCCGCV", students=["Samantha", "Patricia", "Xander", "Roger"]
62+
)
4663
self.assertEqual(
47-
garden.plants("Patricia"),
48-
"Violets Clover Radishes Violets".split())
64+
garden.plants("Patricia"), ["Violets", "Clover", "Radishes", "Violets"]
65+
)
66+
67+
def test_students_are_unordered_last_student(self):
68+
garden = Garden(
69+
"VCRRGVRG\nRVGCCGCV", students=["Samantha", "Patricia", "Xander", "Roger"]
70+
)
4971
self.assertEqual(
50-
garden.plants("Xander"), "Radishes Grass Clover Violets".split())
72+
garden.plants("Xander"), ["Radishes", "Grass", "Clover", "Violets"]
73+
)
5174

5275

53-
if __name__ == '__main__':
76+
if __name__ == "__main__":
5477
unittest.main()

0 commit comments

Comments
 (0)