Skip to content

Commit aeec29a

Browse files
authored
Add custom matcher for anagram to allow a set or array (#365)
[no important files changed]
1 parent 5061139 commit aeec29a

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed
+56-36
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,108 @@
11
Anagram = require './anagram'
2+
3+
###
4+
The toContainSameValues matcher defined at the bottom
5+
checks if your returned collection has the expected elements
6+
regardless of the type of collection (array or set).
7+
###
8+
29
describe 'Anagram', ->
310
it 'no matches', ->
411
detector = new Anagram 'diaper'
512
matches = detector.match ['hello', 'world', 'zombies', 'pants']
6-
expect(matches).toEqual []
13+
expect(matches).toContainSameValues []
714

815
xit 'detects two anagrams', ->
916
detector = new Anagram 'solemn'
10-
matches = detector.match ["lemons", "cherry", "melons"]
11-
expect(matches).toEqual ["lemons", "melons"]
17+
matches = detector.match ['lemons', 'cherry', 'melons']
18+
expect(matches).toContainSameValues ['lemons', 'melons']
1219

1320
xit 'does not detect anagram subsets', ->
1421
detector = new Anagram 'good'
15-
matches = detector.match ["dog", "goody"]
16-
expect(matches).toEqual []
22+
matches = detector.match ['dog', 'goody']
23+
expect(matches).toContainSameValues []
1724

1825
xit 'detects anagram', ->
1926
detector = new Anagram 'listen'
20-
matches = detector.match ["enlists", "google", "inlets", "banana"]
21-
expect(matches).toEqual ["inlets"]
27+
matches = detector.match ['enlists', 'google', 'inlets', 'banana']
28+
expect(matches).toContainSameValues ['inlets']
2229

2330
xit 'detects three anagrams', ->
2431
detector = new Anagram 'allergy'
2532
matches = detector.match [
26-
"gallery",
27-
"ballerina",
28-
"regally",
29-
"clergy",
30-
"largely",
31-
"leading"
32-
]
33-
expect(matches).toEqual ["gallery", "regally", "largely"]
33+
'gallery'
34+
'ballerina'
35+
'regally'
36+
'clergy'
37+
'largely'
38+
'leading'
39+
]
40+
expect(matches).toContainSameValues ['gallery', 'largely', 'regally']
3441

3542
xit 'detects multiple anagrams with different case', ->
3643
detector = new Anagram 'nose'
37-
matches = detector.match ["Eons", "ONES"]
38-
expect(matches).toEqual ["Eons", "ONES"]
44+
matches = detector.match ['Eons', 'ONES']
45+
expect(matches).toContainSameValues ['Eons', 'ONES']
3946

4047
xit 'does not detect non-anagrams with identical checksums', ->
4148
detector = new Anagram 'mass'
4249
matches = detector.match ['last']
43-
expect(matches).toEqual []
50+
expect(matches).toContainSameValues []
4451

4552
xit 'detects anagrams case-insensitively', ->
4653
detector = new Anagram 'Orchestra'
47-
matches = detector.match ["cashregister", "Carthorse", "radishes"]
48-
expect(matches).toEqual ["Carthorse"]
54+
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
55+
expect(matches).toContainSameValues ['Carthorse']
4956

50-
xit 'detects anagrams using case-insensitive subject"', ->
57+
xit 'detects anagrams using case-insensitive subject', ->
5158
detector = new Anagram 'Orchestra'
52-
matches = detector.match ["cashregister", "carthorse", "radishes"]
53-
expect(matches).toEqual ["carthorse"]
59+
matches = detector.match ['cashregister', 'carthorse', 'radishes']
60+
expect(matches).toContainSameValues ['carthorse']
5461

5562
xit 'detects anagrams using case-insensitive possible matches', ->
5663
detector = new Anagram 'Orchestra'
5764
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
58-
expect(matches).toEqual ['Carthorse']
65+
expect(matches).toContainSameValues ['Carthorse']
5966

6067
xit 'does not detect an anagram if the original word is repeated', ->
6168
detector = new Anagram 'go'
62-
matches = detector.match ["goGoGO"]
63-
expect(matches).toEqual []
69+
matches = detector.match ['goGoGO']
70+
expect(matches).toContainSameValues []
6471

6572
xit 'anagrams must use all letters exactly once', ->
6673
detector = new Anagram 'tapper'
67-
matches = detector.match ["patter"]
68-
expect(matches).toEqual []
74+
matches = detector.match ['patter']
75+
expect(matches).toContainSameValues []
6976

7077
xit 'words are not anagrams of themselve', ->
7178
detector = new Anagram 'BANANA'
72-
matches = detector.match ["BANANA"]
73-
expect(matches).toEqual []
79+
matches = detector.match ['BANANA']
80+
expect(matches).toContainSameValues []
7481

7582
xit 'words are not anagrams of themselves even if letter case is partially different', ->
7683
detector = new Anagram 'BANANA'
77-
matches = detector.match ["Banana"]
78-
expect(matches).toEqual []
84+
matches = detector.match ['Banana']
85+
expect(matches).toContainSameValues []
7986

8087
xit 'words are not anagrams of themselves even if letter case is completely different', ->
8188
detector = new Anagram 'BANANA'
82-
matches = detector.match ["banana"]
83-
expect(matches).toEqual []
89+
matches = detector.match ['banana']
90+
expect(matches).toContainSameValues []
8491

8592
xit 'words other than themselves can be anagrams', ->
8693
detector = new Anagram 'LISTEN'
87-
matches = detector.match ["LISTEN", "Silent"]
88-
expect(matches).toEqual ["Silent"]
94+
matches = detector.match ['LISTEN', 'Silent']
95+
expect(matches).toContainSameValues ['Silent']
96+
97+
beforeEach ->
98+
@addMatchers
99+
toContainSameValues: (expected) ->
100+
if not @actual? or !(Array.isArray(@actual) || @actual instanceof Set)
101+
@message = -> "Anagram::match should return an array or set but instead returned #{JSON.stringify @actual}."
102+
return false
103+
104+
matches = Array.from @actual
105+
if matches.length != expected.length or not matches.every((value) -> expected.includes value)
106+
@message = -> "Expected returned values (#{matches.join(', ')}) to be equal to expected values (#{expected.join(', ')})."
107+
return false
108+
true

0 commit comments

Comments
 (0)