mirror of https://github.com/lukechilds/ow.git
Sam Verschueren
7 years ago
committed by
Sindre Sorhus
4 changed files with 60 additions and 1 deletions
@ -0,0 +1,28 @@ |
|||||
|
import { Predicate, Context } from './predicate'; |
||||
|
|
||||
|
export class BooleanPredicate extends Predicate<boolean> { |
||||
|
|
||||
|
constructor(context?: Context) { |
||||
|
super('boolean', context); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Test a boolean to be true. |
||||
|
*/ |
||||
|
get true() { |
||||
|
return this.addValidator({ |
||||
|
message: value => `Expected ${value} to be true`, |
||||
|
validator: value => value === true |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Test a boolean to be false. |
||||
|
*/ |
||||
|
get false() { |
||||
|
return this.addValidator({ |
||||
|
message: value => `Expected ${value} to be false`, |
||||
|
validator: value => value === false |
||||
|
}); |
||||
|
} |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
import test from 'ava'; |
||||
|
import * as m from '..'; |
||||
|
|
||||
|
test('boolean', t => { |
||||
|
t.notThrows(() => m(true, m.boolean)); |
||||
|
t.throws(() => m('12', m.boolean), 'Expected argument to be of type `boolean` but received type `string`'); |
||||
|
}); |
||||
|
|
||||
|
test('boolean.true', t => { |
||||
|
t.notThrows(() => m(true, m.boolean.true)); |
||||
|
t.notThrows(() => m(Boolean(true), m.boolean.true)); |
||||
|
t.notThrows(() => m(Boolean(1), m.boolean.true)); |
||||
|
t.throws(() => m(false, m.boolean.true), 'Expected false to be true'); |
||||
|
t.throws(() => m(Boolean(0), m.boolean.true), 'Expected false to be true'); |
||||
|
}); |
||||
|
|
||||
|
test('boolean.false', t => { |
||||
|
t.notThrows(() => m(false, m.boolean.false)); |
||||
|
t.notThrows(() => m(Boolean(false), m.boolean.false)); |
||||
|
t.notThrows(() => m(Boolean(0), m.boolean.false)); |
||||
|
t.throws(() => m(true, m.boolean.false), 'Expected true to be false'); |
||||
|
t.throws(() => m(Boolean(1), m.boolean.false), 'Expected true to be false'); |
||||
|
}); |
Loading…
Reference in new issue