Skip to content

Commit

Permalink
Update anagram.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Cool-Katt authored Jan 15, 2025
1 parent c8f6a02 commit 912d376
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions exercises/practice/anagram/anagram.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
import { describe, it, expect, xit } from '@jest/globals'
import { Anagram } from './anagram.ts'

let areSetsEqual = (setA, setB) =>
setA.size === setB.size && [...setA].every((val) => setB.has(val));

describe('Anagram', () => {
it('no matches', () => {
const subject = new Anagram('diaper')
const matches = subject.matches('hello', 'world', 'zombies', 'pants')

expect(matches).toEqual([])
const expected = []

expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['lemons', 'melons'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['inlets'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

xit('detects three anagrams', () => {
Expand All @@ -40,98 +47,112 @@ describe('Anagram', () => {
'largely',
'leading'
)
const expected = ['gallery', 'regally', 'largely']

expect(matches).toEqual(['gallery', 'regally', 'largely'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['Eons', 'ONES'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['Carthorse'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['carthorse'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['Carthorse'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual([])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['Silent'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

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

expect(matches).toEqual(['tan'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})

xit('matches() accepts single string argument', () => {
const subject = new Anagram('ant')
const matches = subject.matches('tan')
const expected = ['tan']

expect(matches).toEqual(['tan'])
expect(areSetsEqual(new Set(expected), new Set(matches))).toEqual(true)
})
})

0 comments on commit 912d376

Please sign in to comment.