Skip to content

Commit f14a5da

Browse files
Update anagram.test.ts (#1580)
* Update anagram.test.ts * [CI] Format code * Fixing copy/paste error * more dumb mistakes --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent c8f6a02 commit f14a5da

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed
Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import { describe, it, expect, xit } from '@jest/globals'
22
import { Anagram } from './anagram.ts'
33

4+
const areSetsEqual = <T>(setA: Set<T>, setB: Set<T>): boolean =>
5+
setA.size === setB.size && [...setA].every((val) => setB.has(val))
6+
47
describe('Anagram', () => {
58
it('no matches', () => {
69
const subject = new Anagram('diaper')
710
const matches = subject.matches('hello', 'world', 'zombies', 'pants')
11+
const expected = []
812

9-
expect(matches).toEqual([])
13+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
1014
})
1115

1216
xit('detects two anagrams', () => {
1317
const subject = new Anagram('solemn')
1418
const matches = subject.matches('lemons', 'cherry', 'melons')
19+
const expected = ['lemons', 'melons']
1520

16-
expect(matches).toEqual(['lemons', 'melons'])
21+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
1722
})
1823

1924
xit('does not detect anagram subsets', () => {
2025
const subject = new Anagram('good')
2126
const matches = subject.matches('dog', 'goody')
27+
const expected = []
2228

23-
expect(matches).toEqual([])
29+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
2430
})
2531

2632
xit('detects anagram', () => {
2733
const subject = new Anagram('listen')
2834
const matches = subject.matches('enlists', 'google', 'inlets', 'banana')
35+
const expected = ['inlets']
2936

30-
expect(matches).toEqual(['inlets'])
37+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
3138
})
3239

3340
xit('detects three anagrams', () => {
@@ -40,98 +47,112 @@ describe('Anagram', () => {
4047
'largely',
4148
'leading'
4249
)
50+
const expected = ['gallery', 'regally', 'largely']
4351

44-
expect(matches).toEqual(['gallery', 'regally', 'largely'])
52+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
4553
})
4654

4755
xit('detects multiple anagrams with different case', () => {
4856
const subject = new Anagram('nose')
4957
const matches = subject.matches('Eons', 'ONES')
58+
const expected = ['Eons', 'ONES']
5059

51-
expect(matches).toEqual(['Eons', 'ONES'])
60+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
5261
})
5362

5463
xit('does not detect non-anagrams with identical checksum', () => {
5564
const subject = new Anagram('mass')
5665
const matches = subject.matches('last')
66+
const expected = []
5767

58-
expect(matches).toEqual([])
68+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
5969
})
6070

6171
xit('detects anagrams case-insensitively', () => {
6272
const subject = new Anagram('Orchestra')
6373
const matches = subject.matches('cashregister', 'Carthorse', 'radishes')
74+
const expected = ['Carthorse']
6475

65-
expect(matches).toEqual(['Carthorse'])
76+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
6677
})
6778

6879
xit('detects anagrams using case-insensitive subject', () => {
6980
const subject = new Anagram('Orchestra')
7081
const matches = subject.matches('cashregister', 'carthorse', 'radishes')
82+
const expected = ['carthorse']
7183

72-
expect(matches).toEqual(['carthorse'])
84+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
7385
})
7486

7587
xit('detects anagrams using case-insensitive possible matches', () => {
7688
const subject = new Anagram('orchestra')
7789
const matches = subject.matches('cashregister', 'Carthorse', 'radishes')
90+
const expected = ['Carthorse']
7891

79-
expect(matches).toEqual(['Carthorse'])
92+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
8093
})
8194

8295
xit('does not detect an anagram if the original word is repeated', () => {
8396
const subject = new Anagram('go')
8497
const matches = subject.matches('go Go GO')
98+
const expected = []
8599

86-
expect(matches).toEqual([])
100+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
87101
})
88102

89103
xit('anagrams must use all letters exactly once', () => {
90104
const subject = new Anagram('tapper')
91105
const matches = subject.matches('patter')
106+
const expected = []
92107

93-
expect(matches).toEqual([])
108+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
94109
})
95110

96111
xit('words are not anagrams of themselves', () => {
97112
const subject = new Anagram('BANANA')
98113
const matches = subject.matches('BANANA')
114+
const expected = []
99115

100-
expect(matches).toEqual([])
116+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
101117
})
102118

103119
xit('words are not anagrams of themselves even if letter case is partially different', () => {
104120
const subject = new Anagram('BANANA')
105121
const matches = subject.matches('Banana')
122+
const expected = []
106123

107-
expect(matches).toEqual([])
124+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
108125
})
109126

110127
xit('words are not anagrams of themselves even if letter case is completely different', () => {
111128
const subject = new Anagram('BANANA')
112129
const matches = subject.matches('Banana')
130+
const expected = []
113131

114-
expect(matches).toEqual([])
132+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
115133
})
116134

117135
xit('words other than themselves can be anagrams', () => {
118136
const subject = new Anagram('LISTEN')
119137
const matches = subject.matches('LISTEN', 'Silent')
138+
const expected = ['Silent']
120139

121-
expect(matches).toEqual(['Silent'])
140+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
122141
})
123142

124143
xit('matches() accepts string arguments', () => {
125144
const subject = new Anagram('ant')
126145
const matches = subject.matches('stand', 'tan', 'at')
146+
const expected = ['tan']
127147

128-
expect(matches).toEqual(['tan'])
148+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
129149
})
130150

131151
xit('matches() accepts single string argument', () => {
132152
const subject = new Anagram('ant')
133153
const matches = subject.matches('tan')
154+
const expected = ['tan']
134155

135-
expect(matches).toEqual(['tan'])
156+
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
136157
})
137158
})

0 commit comments

Comments
 (0)