|
2 | 2 |
|
3 | 3 | from anagram import find_anagrams
|
4 | 4 |
|
5 |
| -# Python 2/3 compatibility |
6 |
| -if not hasattr(unittest.TestCase, 'assertCountEqual'): |
7 |
| - unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual |
8 |
| - |
9 |
| - |
10 | 5 | # Tests adapted from `problem-specifications//canonical-data.json` @ v1.5.0
|
11 | 6 |
|
| 7 | + |
12 | 8 | class AnagramTest(unittest.TestCase):
|
13 | 9 | def test_no_matches(self):
|
14 | 10 | candidates = ["hello", "world", "zombies", "pants"]
|
15 |
| - self.assertEqual(find_anagrams("diaper", candidates), []) |
| 11 | + expected = [] |
| 12 | + self.assertCountEqual(find_anagrams("diaper", candidates), expected) |
16 | 13 |
|
17 | 14 | def test_detects_two_anagrams(self):
|
18 | 15 | 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) |
21 | 18 |
|
22 | 19 | 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) |
24 | 23 |
|
25 | 24 | def test_detects_anagram(self):
|
26 | 25 | candidates = ["enlists", "google", "inlets", "banana"]
|
27 |
| - self.assertEqual(find_anagrams("listen", candidates), ["inlets"]) |
| 26 | + expected = ["inlets"] |
| 27 | + self.assertCountEqual(find_anagrams("listen", candidates), expected) |
28 | 28 |
|
29 | 29 | 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) |
36 | 33 |
|
37 | 34 | def test_detects_multiple_anagrams_with_different_case(self):
|
38 | 35 | candidates = ["Eons", "ONES"]
|
39 |
| - self.assertEqual(find_anagrams("nose", candidates), ["Eons", "ONES"]) |
| 36 | + expected = ["Eons", "ONES"] |
| 37 | + self.assertCountEqual(find_anagrams("nose", candidates), expected) |
40 | 38 |
|
41 | 39 | 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) |
43 | 43 |
|
44 | 44 | def test_detects_anagrams_case_insensitively(self):
|
45 | 45 | candidates = ["cashregister", "Carthorse", "radishes"]
|
46 |
| - self.assertEqual( |
47 |
| - find_anagrams("Orchestra", candidates), ["Carthorse"]) |
| 46 | + expected = ["Carthorse"] |
| 47 | + self.assertCountEqual(find_anagrams("Orchestra", candidates), expected) |
48 | 48 |
|
49 | 49 | def test_detects_anagrams_using_case_insensitive_subject(self):
|
50 | 50 | candidates = ["cashregister", "carthorse", "radishes"]
|
51 |
| - self.assertEqual( |
52 |
| - find_anagrams("Orchestra", candidates), ["carthorse"]) |
| 51 | + expected = ["carthorse"] |
| 52 | + self.assertCountEqual(find_anagrams("Orchestra", candidates), expected) |
53 | 53 |
|
54 | 54 | def test_detects_anagrams_using_case_insensitive_possible_matches(self):
|
55 | 55 | candidates = ["cashregister", "Carthorse", "radishes"]
|
56 |
| - self.assertEqual( |
57 |
| - find_anagrams("orchestra", candidates), ["Carthorse"]) |
| 56 | + expected = ["Carthorse"] |
| 57 | + self.assertCountEqual(find_anagrams("orchestra", candidates), expected) |
58 | 58 |
|
59 | 59 | 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) |
61 | 63 |
|
62 | 64 | 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) |
64 | 68 |
|
65 | 69 | def test_words_are_not_anagrams_of_themselves_case_insensitive(self):
|
66 | 70 | candidates = ["BANANA", "Banana", "banana"]
|
67 |
| - self.assertEqual(find_anagrams("BANANA", candidates), []) |
| 71 | + expected = [] |
| 72 | + self.assertCountEqual(find_anagrams("BANANA", candidates), expected) |
68 | 73 |
|
69 | 74 | def test_words_other_than_themselves_can_be_anagrams(self):
|
70 | 75 | candidates = ["Listen", "Silent", "LISTEN"]
|
71 |
| - self.assertEqual(find_anagrams("LISTEN", candidates), ["Silent"]) |
| 76 | + expected = ["Silent"] |
| 77 | + self.assertCountEqual(find_anagrams("LISTEN", candidates), expected) |
72 | 78 |
|
73 | 79 |
|
74 |
| -if __name__ == '__main__': |
| 80 | +if __name__ == "__main__": |
75 | 81 | unittest.main()
|
0 commit comments