-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import t from 'assert' | ||
import a from 'assertron' | ||
import { createSatisfier, hasAll } from './index.js' | ||
|
||
test('non array returns false', () => { | ||
a.false(createSatisfier(hasAll(1)).test(undefined)) | ||
a.false(createSatisfier(hasAll(1)).test(null)) | ||
a.false(createSatisfier(hasAll(1)).test(1)) | ||
a.false(createSatisfier(hasAll(1)).test(true)) | ||
a.false(createSatisfier(hasAll(1)).test('a')) | ||
a.false(createSatisfier(hasAll({ a: 1 })).test(true)) | ||
a.false(createSatisfier(hasAll({ a: 1 })).test(1)) | ||
a.false(createSatisfier(hasAll({ a: 1 })).test('{ a: 1 }')) | ||
a.false(createSatisfier(hasAll({ a: 1 })).test({ a: 1 })) | ||
}) | ||
|
||
test('array with no match returns false', () => { | ||
a.false(createSatisfier(hasAll(1)).test([2, 2, 3])) | ||
a.false(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }])) | ||
}) | ||
|
||
test('exact match array returns true', () => { | ||
t(createSatisfier(hasAll(1)).test([1])) | ||
t(createSatisfier(hasAll(false, false)).test([false, false])) | ||
t(createSatisfier(hasAll('a', 'b')).test(['a', 'b'])) | ||
t(createSatisfier(hasAll({ a: 1 })).test([{ a: 1 }])) | ||
t(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }, { a: 1 }])) | ||
}) | ||
|
||
test('array with more than one match returns true', () => { | ||
t(createSatisfier(hasAll({ a: 1 })).test([{ a: 1 }, { a: 1 }])) | ||
t(createSatisfier(hasAll({ a: 1 })).test([{ b: 1 }, { a: 1 }, { a: 1 }, 'x'])) | ||
}) | ||
|
||
it('is true if all entries are in the target', () => { | ||
t(createSatisfier(hasAll(1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(3)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 2)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 3)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2, 3)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(3, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(3, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 2, 3)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 3, 2)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2, 1, 3)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2, 3, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(3, 1, 2)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(3, 2, 1)).test([1, 2, 3])) | ||
}) | ||
|
||
it('is true even if there are duplicates in expectation', () => { | ||
t(createSatisfier(hasAll(1, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 1, 2)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(1, 2, 1)).test([1, 2, 3])) | ||
t(createSatisfier(hasAll(2, 1, 1)).test([1, 2, 3])) | ||
}) | ||
|
||
test('tersify()', () => { | ||
t.strictEqual(hasAll({ a: 1 }, { b: 2 }).tersify(), 'hasAll({ a: 1 }, { b: 2 })') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { tersible, tersify } from 'tersify' | ||
import { some } from './some.js' | ||
|
||
/** | ||
* Check if an array has all of the expected entries, regardless of order. | ||
*/ | ||
export function hasAll(...expectations: any[]) { | ||
return tersible((arr: any) => { | ||
if (!Array.isArray(arr)) return false | ||
return expectations.every(e => some(e)(arr)) | ||
}, () => `hasAll(${expectations.map(e => tersify(e)).join(', ')})`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters