|
1 | 1 | 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 | + |
2 | 9 | describe 'Anagram', ->
|
3 | 10 | it 'no matches', ->
|
4 | 11 | detector = new Anagram 'diaper'
|
5 | 12 | matches = detector.match ['hello', 'world', 'zombies', 'pants']
|
6 |
| - expect(matches).toEqual [] |
| 13 | + expect(matches).toContainSameValues [] |
7 | 14 |
|
8 | 15 | xit 'detects two anagrams', ->
|
9 | 16 | 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'] |
12 | 19 |
|
13 | 20 | xit 'does not detect anagram subsets', ->
|
14 | 21 | detector = new Anagram 'good'
|
15 |
| - matches = detector.match ["dog", "goody"] |
16 |
| - expect(matches).toEqual [] |
| 22 | + matches = detector.match ['dog', 'goody'] |
| 23 | + expect(matches).toContainSameValues [] |
17 | 24 |
|
18 | 25 | xit 'detects anagram', ->
|
19 | 26 | 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'] |
22 | 29 |
|
23 | 30 | xit 'detects three anagrams', ->
|
24 | 31 | detector = new Anagram 'allergy'
|
25 | 32 | 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'] |
34 | 41 |
|
35 | 42 | xit 'detects multiple anagrams with different case', ->
|
36 | 43 | 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'] |
39 | 46 |
|
40 | 47 | xit 'does not detect non-anagrams with identical checksums', ->
|
41 | 48 | detector = new Anagram 'mass'
|
42 | 49 | matches = detector.match ['last']
|
43 |
| - expect(matches).toEqual [] |
| 50 | + expect(matches).toContainSameValues [] |
44 | 51 |
|
45 | 52 | xit 'detects anagrams case-insensitively', ->
|
46 | 53 | 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'] |
49 | 56 |
|
50 |
| - xit 'detects anagrams using case-insensitive subject"', -> |
| 57 | + xit 'detects anagrams using case-insensitive subject', -> |
51 | 58 | 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'] |
54 | 61 |
|
55 | 62 | xit 'detects anagrams using case-insensitive possible matches', ->
|
56 | 63 | detector = new Anagram 'Orchestra'
|
57 | 64 | matches = detector.match ['cashregister', 'Carthorse', 'radishes']
|
58 |
| - expect(matches).toEqual ['Carthorse'] |
| 65 | + expect(matches).toContainSameValues ['Carthorse'] |
59 | 66 |
|
60 | 67 | xit 'does not detect an anagram if the original word is repeated', ->
|
61 | 68 | detector = new Anagram 'go'
|
62 |
| - matches = detector.match ["goGoGO"] |
63 |
| - expect(matches).toEqual [] |
| 69 | + matches = detector.match ['goGoGO'] |
| 70 | + expect(matches).toContainSameValues [] |
64 | 71 |
|
65 | 72 | xit 'anagrams must use all letters exactly once', ->
|
66 | 73 | detector = new Anagram 'tapper'
|
67 |
| - matches = detector.match ["patter"] |
68 |
| - expect(matches).toEqual [] |
| 74 | + matches = detector.match ['patter'] |
| 75 | + expect(matches).toContainSameValues [] |
69 | 76 |
|
70 | 77 | xit 'words are not anagrams of themselve', ->
|
71 | 78 | detector = new Anagram 'BANANA'
|
72 |
| - matches = detector.match ["BANANA"] |
73 |
| - expect(matches).toEqual [] |
| 79 | + matches = detector.match ['BANANA'] |
| 80 | + expect(matches).toContainSameValues [] |
74 | 81 |
|
75 | 82 | xit 'words are not anagrams of themselves even if letter case is partially different', ->
|
76 | 83 | detector = new Anagram 'BANANA'
|
77 |
| - matches = detector.match ["Banana"] |
78 |
| - expect(matches).toEqual [] |
| 84 | + matches = detector.match ['Banana'] |
| 85 | + expect(matches).toContainSameValues [] |
79 | 86 |
|
80 | 87 | xit 'words are not anagrams of themselves even if letter case is completely different', ->
|
81 | 88 | detector = new Anagram 'BANANA'
|
82 |
| - matches = detector.match ["banana"] |
83 |
| - expect(matches).toEqual [] |
| 89 | + matches = detector.match ['banana'] |
| 90 | + expect(matches).toContainSameValues [] |
84 | 91 |
|
85 | 92 | xit 'words other than themselves can be anagrams', ->
|
86 | 93 | 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