Skip to content

Commit 4d949b3

Browse files
tqa236yawpitch
authored andcommitted
Add test template for anagram (#2124)
* Add test template for anagram * Fix assert type * Simplify the template
1 parent 6cd7939 commit 4d949b3

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

exercises/anagram/.meta/template.j2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
3+
{{ macros.header() }}
4+
5+
class {{ exercise | camel_case }}Test(unittest.TestCase):
6+
{% for case in cases -%}
7+
def test_{{ case["description"] | to_snake }}(self):
8+
candidates = {{ case["input"]["candidates"] }}
9+
expected = {{ case["expected"] }}
10+
self.assertCountEqual(
11+
{{ case["property"] | to_snake }}("{{ case["input"]["subject"] }}", candidates), expected
12+
)
13+
14+
{% endfor %}
15+
16+
{{ macros.footer() }}

exercises/anagram/anagram_test.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,80 @@
22

33
from anagram import find_anagrams
44

5-
# Python 2/3 compatibility
6-
if not hasattr(unittest.TestCase, 'assertCountEqual'):
7-
unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual
8-
9-
105
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.5.0
116

7+
128
class AnagramTest(unittest.TestCase):
139
def test_no_matches(self):
1410
candidates = ["hello", "world", "zombies", "pants"]
15-
self.assertEqual(find_anagrams("diaper", candidates), [])
11+
expected = []
12+
self.assertCountEqual(find_anagrams("diaper", candidates), expected)
1613

1714
def test_detects_two_anagrams(self):
1815
candidates = ["stream", "pigeon", "maters"]
19-
self.assertCountEqual(
20-
find_anagrams("master", candidates), ["stream", "maters"])
16+
expected = ["stream", "maters"]
17+
self.assertCountEqual(find_anagrams("master", candidates), expected)
2118

2219
def test_does_not_detect_anagram_subsets(self):
23-
self.assertEqual(find_anagrams("good", ["dog", "goody"]), [])
20+
candidates = ["dog", "goody"]
21+
expected = []
22+
self.assertCountEqual(find_anagrams("good", candidates), expected)
2423

2524
def test_detects_anagram(self):
2625
candidates = ["enlists", "google", "inlets", "banana"]
27-
self.assertEqual(find_anagrams("listen", candidates), ["inlets"])
26+
expected = ["inlets"]
27+
self.assertCountEqual(find_anagrams("listen", candidates), expected)
2828

2929
def test_detects_three_anagrams(self):
30-
candidates = [
31-
"gallery", "ballerina", "regally", "clergy", "largely", "leading"
32-
]
33-
self.assertCountEqual(
34-
find_anagrams("allergy", candidates),
35-
["gallery", "regally", "largely"])
30+
candidates = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]
31+
expected = ["gallery", "regally", "largely"]
32+
self.assertCountEqual(find_anagrams("allergy", candidates), expected)
3633

3734
def test_detects_multiple_anagrams_with_different_case(self):
3835
candidates = ["Eons", "ONES"]
39-
self.assertEqual(find_anagrams("nose", candidates), ["Eons", "ONES"])
36+
expected = ["Eons", "ONES"]
37+
self.assertCountEqual(find_anagrams("nose", candidates), expected)
4038

4139
def test_does_not_detect_non_anagrams_with_identical_checksum(self):
42-
self.assertEqual(find_anagrams("mass", ["last"]), [])
40+
candidates = ["last"]
41+
expected = []
42+
self.assertCountEqual(find_anagrams("mass", candidates), expected)
4343

4444
def test_detects_anagrams_case_insensitively(self):
4545
candidates = ["cashregister", "Carthorse", "radishes"]
46-
self.assertEqual(
47-
find_anagrams("Orchestra", candidates), ["Carthorse"])
46+
expected = ["Carthorse"]
47+
self.assertCountEqual(find_anagrams("Orchestra", candidates), expected)
4848

4949
def test_detects_anagrams_using_case_insensitive_subject(self):
5050
candidates = ["cashregister", "carthorse", "radishes"]
51-
self.assertEqual(
52-
find_anagrams("Orchestra", candidates), ["carthorse"])
51+
expected = ["carthorse"]
52+
self.assertCountEqual(find_anagrams("Orchestra", candidates), expected)
5353

5454
def test_detects_anagrams_using_case_insensitive_possible_matches(self):
5555
candidates = ["cashregister", "Carthorse", "radishes"]
56-
self.assertEqual(
57-
find_anagrams("orchestra", candidates), ["Carthorse"])
56+
expected = ["Carthorse"]
57+
self.assertCountEqual(find_anagrams("orchestra", candidates), expected)
5858

5959
def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
60-
self.assertEqual(find_anagrams("go", ["go Go GO"]), [])
60+
candidates = ["go Go GO"]
61+
expected = []
62+
self.assertCountEqual(find_anagrams("go", candidates), expected)
6163

6264
def test_anagrams_must_use_all_letters_exactly_once(self):
63-
self.assertEqual(find_anagrams("tapper", ["patter"]), [])
65+
candidates = ["patter"]
66+
expected = []
67+
self.assertCountEqual(find_anagrams("tapper", candidates), expected)
6468

6569
def test_words_are_not_anagrams_of_themselves_case_insensitive(self):
6670
candidates = ["BANANA", "Banana", "banana"]
67-
self.assertEqual(find_anagrams("BANANA", candidates), [])
71+
expected = []
72+
self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
6873

6974
def test_words_other_than_themselves_can_be_anagrams(self):
7075
candidates = ["Listen", "Silent", "LISTEN"]
71-
self.assertEqual(find_anagrams("LISTEN", candidates), ["Silent"])
76+
expected = ["Silent"]
77+
self.assertCountEqual(find_anagrams("LISTEN", candidates), expected)
7278

7379

74-
if __name__ == '__main__':
80+
if __name__ == "__main__":
7581
unittest.main()

0 commit comments

Comments
 (0)