mirror of https://github.com/lukechilds/ow.git
Sam Verschueren
7 years ago
committed by
Sindre Sorhus
3 changed files with 84 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||
|
import {Predicate, Context} from './predicate'; |
||||
|
|
||||
|
export class WeakSetPredicate extends Predicate<WeakSet<any>> { |
||||
|
constructor(context?: Context) { |
||||
|
super('weakSet', context); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Test a WeakSet to include all the provided items. The items are tested by identity, not structure. |
||||
|
* |
||||
|
* @param items The items that should be a item in the WeakSet. |
||||
|
*/ |
||||
|
has(...items: any[]) { |
||||
|
const missingItems: any[] = []; |
||||
|
|
||||
|
return this.addValidator({ |
||||
|
message: () => `Expected WeakSet to have items \`${JSON.stringify(missingItems)}\``, |
||||
|
validator: set => { |
||||
|
for (const key of items) { |
||||
|
if (set.has(key)) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
missingItems.push(key); |
||||
|
|
||||
|
if (missingItems.length === 5) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return missingItems.length === 0; |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Test a WeakSet to include any of the provided items. The items are tested by identity, not structure. |
||||
|
* |
||||
|
* @param items The items that could be a item in the WeakSet. |
||||
|
*/ |
||||
|
hasAny(...items: any[]) { |
||||
|
return this.addValidator({ |
||||
|
message: () => `Expected WeakSet to have any item of \`${JSON.stringify(items)}\``, |
||||
|
validator: set => items.some(item => set.has(item)) |
||||
|
}); |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
import test from 'ava'; |
||||
|
import m from '..'; |
||||
|
|
||||
|
const unicorn: any = {unicorn: '🦄'}; |
||||
|
const rainbow: any = {rainbow: '🌈'}; |
||||
|
const rocket: any = {rocket: '🚀'}; |
||||
|
|
||||
|
test('weakSet', t => { |
||||
|
t.notThrows(() => m(new WeakSet(), m.weakSet)); |
||||
|
t.notThrows(() => m(new WeakSet([{unicorn: '🦄'}]), m.weakSet)); |
||||
|
t.notThrows(() => m(new WeakSet([unicorn]), m.weakSet)); |
||||
|
t.throws(() => m(12 as any, m.weakSet), 'Expected argument to be of type `weakSet` but received type `number`'); |
||||
|
}); |
||||
|
|
||||
|
test('weakSet.has', t => { |
||||
|
const keys = [{x: 1}, {x: 2}, {x: 3}, {x: 4}, {x: 5}, {x: 6}, {x: 7}, {x: 8}, {x: 9}, {x: 10}]; |
||||
|
|
||||
|
t.notThrows(() => m(new WeakSet([unicorn]), m.weakSet.has(unicorn))); |
||||
|
t.notThrows(() => m(new WeakSet([unicorn, rainbow]), m.weakSet.has(unicorn, rainbow))); |
||||
|
t.throws(() => m(new WeakSet([unicorn, rainbow]), m.weakSet.has(rocket)), 'Expected WeakSet to have items `[{"rocket":"🚀"}]`'); |
||||
|
t.throws(() => m(new WeakSet([unicorn, rocket]), m.weakSet.has(rainbow, rocket)), 'Expected WeakSet to have items `[{"rainbow":"🌈"}]`'); |
||||
|
t.throws(() => m(new WeakSet([keys[1], keys[3]]), m.weakSet.has(...keys)), 'Expected WeakSet to have items `[{"x":1},{"x":3},{"x":5},{"x":6},{"x":7}]`'); |
||||
|
}); |
||||
|
|
||||
|
test('weakSet.hasAny', t => { |
||||
|
t.notThrows(() => m(new WeakSet([unicorn]), m.weakSet.hasAny(unicorn, rainbow))); |
||||
|
t.notThrows(() => m(new WeakSet([unicorn, rainbow]), m.weakSet.hasAny(unicorn))); |
||||
|
t.throws(() => m(new WeakSet([unicorn, rainbow]), m.weakSet.hasAny(rocket)), 'Expected WeakSet to have any item of `[{"rocket":"🚀"}]`'); |
||||
|
}); |
Loading…
Reference in new issue