Skip to content

Commit 59036d4

Browse files
committed
Adjust matche to be for same elements and same count
1 parent a99f0fd commit 59036d4

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed
+22-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
Anagram = require './anagram'
22

33
###
4-
The toEqualUnordered matcher defined at the bottom
5-
checks if your returned collection has the
6-
right elements regardless of order.
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).
77
###
88

99
describe 'Anagram', ->
1010
it 'no matches', ->
1111
detector = new Anagram 'diaper'
1212
matches = detector.match ['hello', 'world', 'zombies', 'pants']
13-
expect(matches).toEqualUnordered []
13+
expect(matches).toContainSameValues ['a', 'b']
1414

1515
xit 'detects two anagrams', ->
1616
detector = new Anagram 'solemn'
1717
matches = detector.match ['lemons', 'cherry', 'melons']
18-
expect(matches).toEqualUnordered ['lemons', 'melons']
18+
expect(matches).toContainSameValues ['lemons', 'melons']
1919

2020
xit 'does not detect anagram subsets', ->
2121
detector = new Anagram 'good'
2222
matches = detector.match ['dog', 'goody']
23-
expect(matches).toEqualUnordered []
23+
expect(matches).toContainSameValues []
2424

2525
xit 'detects anagram', ->
2626
detector = new Anagram 'listen'
2727
matches = detector.match ['enlists', 'google', 'inlets', 'banana']
28-
expect(matches).toEqualUnordered ['inlets']
28+
expect(matches).toContainSameValues ['inlets']
2929

3030
xit 'detects three anagrams', ->
3131
detector = new Anagram 'allergy'
@@ -37,72 +37,72 @@ describe 'Anagram', ->
3737
'largely'
3838
'leading'
3939
]
40-
expect(matches).toEqualUnordered ['gallery', 'largely', 'regally']
40+
expect(matches).toContainSameValues ['gallery', 'largely', 'regally']
4141

4242
xit 'detects multiple anagrams with different case', ->
4343
detector = new Anagram 'nose'
4444
matches = detector.match ['Eons', 'ONES']
45-
expect(matches).toEqualUnordered ['Eons', 'ONES']
45+
expect(matches).toContainSameValues ['Eons', 'ONES']
4646

4747
xit 'does not detect non-anagrams with identical checksums', ->
4848
detector = new Anagram 'mass'
4949
matches = detector.match ['last']
50-
expect(matches).toEqualUnordered []
50+
expect(matches).toContainSameValues []
5151

5252
xit 'detects anagrams case-insensitively', ->
5353
detector = new Anagram 'Orchestra'
5454
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
55-
expect(matches).toEqualUnordered ['Carthorse']
55+
expect(matches).toContainSameValues ['Carthorse']
5656

5757
xit 'detects anagrams using case-insensitive subject', ->
5858
detector = new Anagram 'Orchestra'
5959
matches = detector.match ['cashregister', 'carthorse', 'radishes']
60-
expect(matches).toEqualUnordered ['carthorse']
60+
expect(matches).toContainSameValues ['carthorse']
6161

6262
xit 'detects anagrams using case-insensitive possible matches', ->
6363
detector = new Anagram 'Orchestra'
6464
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
65-
expect(matches).toEqualUnordered ['Carthorse']
65+
expect(matches).toContainSameValues ['Carthorse']
6666

6767
xit 'does not detect an anagram if the original word is repeated', ->
6868
detector = new Anagram 'go'
6969
matches = detector.match ['goGoGO']
70-
expect(matches).toEqualUnordered []
70+
expect(matches).toContainSameValues []
7171

7272
xit 'anagrams must use all letters exactly once', ->
7373
detector = new Anagram 'tapper'
7474
matches = detector.match ['patter']
75-
expect(matches).toEqualUnordered []
75+
expect(matches).toContainSameValues []
7676

7777
xit 'words are not anagrams of themselve', ->
7878
detector = new Anagram 'BANANA'
7979
matches = detector.match ['BANANA']
80-
expect(matches).toEqualUnordered []
80+
expect(matches).toContainSameValues []
8181

8282
xit 'words are not anagrams of themselves even if letter case is partially different', ->
8383
detector = new Anagram 'BANANA'
8484
matches = detector.match ['Banana']
85-
expect(matches).toEqualUnordered []
85+
expect(matches).toContainSameValues []
8686

8787
xit 'words are not anagrams of themselves even if letter case is completely different', ->
8888
detector = new Anagram 'BANANA'
8989
matches = detector.match ['banana']
90-
expect(matches).toEqualUnordered []
90+
expect(matches).toContainSameValues []
9191

9292
xit 'words other than themselves can be anagrams', ->
9393
detector = new Anagram 'LISTEN'
9494
matches = detector.match ['LISTEN', 'Silent']
95-
expect(matches).toEqualUnordered ['Silent']
95+
expect(matches).toContainSameValues ['Silent']
9696

9797
beforeEach ->
9898
@addMatchers
99-
toEqualUnordered: (expected) ->
100-
if not @actual? or not Array.isArray @actual or not @actual instanceof Set
99+
toContainSameValues: (expected) ->
100+
if not @actual? or !(Array.isArray(@actual) || @actual instanceof Set)
101101
@message = -> "Anagram::match should return an array or set but instead returned #{JSON.stringify @actual}."
102102
return false
103103

104104
matches = Array.from @actual
105105
if matches.length != expected.length or not matches.every((value) -> expected.includes value)
106-
@message = -> "Expected returned values (#{matches.join(', ')}) to be equal (unordered) to expected values (#{expected.join(', ')})."
106+
@message = -> "Expected returned values (#{matches.join(', ')}) to be equal to expected values (#{expected.join(', ')})."
107107
return false
108108
true

0 commit comments

Comments
 (0)