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

Add custom matcher for anagram to allow a set or array #365

Merged
merged 3 commits into from
Jan 19, 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
92 changes: 56 additions & 36 deletions exercises/practice/anagram/anagram.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,88 +1,108 @@
Anagram = require './anagram'

###
The toContainSameValues matcher defined at the bottom
checks if your returned collection has the expected elements
regardless of the type of collection (array or set).
###

describe 'Anagram', ->
it 'no matches', ->
detector = new Anagram 'diaper'
matches = detector.match ['hello', 'world', 'zombies', 'pants']
expect(matches).toEqual []
expect(matches).toContainSameValues []

xit 'detects two anagrams', ->
detector = new Anagram 'solemn'
matches = detector.match ["lemons", "cherry", "melons"]
expect(matches).toEqual ["lemons", "melons"]
matches = detector.match ['lemons', 'cherry', 'melons']
expect(matches).toContainSameValues ['lemons', 'melons']

xit 'does not detect anagram subsets', ->
detector = new Anagram 'good'
matches = detector.match ["dog", "goody"]
expect(matches).toEqual []
matches = detector.match ['dog', 'goody']
expect(matches).toContainSameValues []

xit 'detects anagram', ->
detector = new Anagram 'listen'
matches = detector.match ["enlists", "google", "inlets", "banana"]
expect(matches).toEqual ["inlets"]
matches = detector.match ['enlists', 'google', 'inlets', 'banana']
expect(matches).toContainSameValues ['inlets']

xit 'detects three anagrams', ->
detector = new Anagram 'allergy'
matches = detector.match [
"gallery",
"ballerina",
"regally",
"clergy",
"largely",
"leading"
]
expect(matches).toEqual ["gallery", "regally", "largely"]
'gallery'
'ballerina'
'regally'
'clergy'
'largely'
'leading'
]
expect(matches).toContainSameValues ['gallery', 'largely', 'regally']

xit 'detects multiple anagrams with different case', ->
detector = new Anagram 'nose'
matches = detector.match ["Eons", "ONES"]
expect(matches).toEqual ["Eons", "ONES"]
matches = detector.match ['Eons', 'ONES']
expect(matches).toContainSameValues ['Eons', 'ONES']

xit 'does not detect non-anagrams with identical checksums', ->
detector = new Anagram 'mass'
matches = detector.match ['last']
expect(matches).toEqual []
expect(matches).toContainSameValues []

xit 'detects anagrams case-insensitively', ->
detector = new Anagram 'Orchestra'
matches = detector.match ["cashregister", "Carthorse", "radishes"]
expect(matches).toEqual ["Carthorse"]
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
expect(matches).toContainSameValues ['Carthorse']

xit 'detects anagrams using case-insensitive subject"', ->
xit 'detects anagrams using case-insensitive subject', ->
detector = new Anagram 'Orchestra'
matches = detector.match ["cashregister", "carthorse", "radishes"]
expect(matches).toEqual ["carthorse"]
matches = detector.match ['cashregister', 'carthorse', 'radishes']
expect(matches).toContainSameValues ['carthorse']

xit 'detects anagrams using case-insensitive possible matches', ->
detector = new Anagram 'Orchestra'
matches = detector.match ['cashregister', 'Carthorse', 'radishes']
expect(matches).toEqual ['Carthorse']
expect(matches).toContainSameValues ['Carthorse']

xit 'does not detect an anagram if the original word is repeated', ->
detector = new Anagram 'go'
matches = detector.match ["goGoGO"]
expect(matches).toEqual []
matches = detector.match ['goGoGO']
expect(matches).toContainSameValues []

xit 'anagrams must use all letters exactly once', ->
detector = new Anagram 'tapper'
matches = detector.match ["patter"]
expect(matches).toEqual []
matches = detector.match ['patter']
expect(matches).toContainSameValues []

xit 'words are not anagrams of themselve', ->
detector = new Anagram 'BANANA'
matches = detector.match ["BANANA"]
expect(matches).toEqual []
matches = detector.match ['BANANA']
expect(matches).toContainSameValues []

xit 'words are not anagrams of themselves even if letter case is partially different', ->
detector = new Anagram 'BANANA'
matches = detector.match ["Banana"]
expect(matches).toEqual []
matches = detector.match ['Banana']
expect(matches).toContainSameValues []

xit 'words are not anagrams of themselves even if letter case is completely different', ->
detector = new Anagram 'BANANA'
matches = detector.match ["banana"]
expect(matches).toEqual []
matches = detector.match ['banana']
expect(matches).toContainSameValues []

xit 'words other than themselves can be anagrams', ->
detector = new Anagram 'LISTEN'
matches = detector.match ["LISTEN", "Silent"]
expect(matches).toEqual ["Silent"]
matches = detector.match ['LISTEN', 'Silent']
expect(matches).toContainSameValues ['Silent']

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

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