Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update anagram.test.ts #1580

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 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'

const areSetsEqual = <T>(setA: Set<T>, setB: Set<T>): boolean =>
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')
const expected = []

expect(matches).toEqual([])
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)
})
})