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
1 parent
184a1eb
commit 477f053
Showing
3 changed files
with
126 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
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,76 @@ | ||
import is from '@sindresorhus/is'; | ||
import * as isEqual from 'lodash.isequal'; | ||
import {Predicate, Context} from './predicate'; | ||
import hasItems from '../utils/has-items'; | ||
|
||
export class ObjectPredicate extends Predicate<object> { | ||
constructor(context?: Context) { | ||
super('object', context); | ||
} | ||
|
||
/** | ||
* Test if an Object is a plain object. | ||
*/ | ||
get plain() { | ||
return this.addValidator({ | ||
message: () => 'Expected object to be a plain object', | ||
validator: object => is.plainObject(object) | ||
}); | ||
} | ||
|
||
/** | ||
* Test an object to be empty. | ||
*/ | ||
get empty() { | ||
return this.addValidator({ | ||
message: object => `Expected object to be empty, got \`${JSON.stringify(object)}\``, | ||
validator: object => Object.keys(object).length === 0 | ||
}); | ||
} | ||
|
||
/** | ||
* Test an object to be not empty. | ||
*/ | ||
get nonEmpty() { | ||
return this.addValidator({ | ||
message: () => 'Expected object to not be empty', | ||
validator: object => Object.keys(object).length > 0 | ||
}); | ||
} | ||
|
||
/** | ||
* Test an object to be deeply equal to the provided object. | ||
* | ||
* @param expected Expected object to match. | ||
*/ | ||
deepEqual(expected: {}) { | ||
return this.addValidator({ | ||
message: object => `Expected object to be deeply equal to \`${JSON.stringify(expected)}\`, got \`${JSON.stringify(object)}\``, | ||
validator: object => isEqual(object, expected) | ||
}); | ||
} | ||
|
||
/** | ||
* Test an object to include all the provided keys. | ||
* | ||
* @param keys The keys that should be present in the object. | ||
*/ | ||
hasKeys(...keys: string[]) { | ||
return this.addValidator({ | ||
message: (_, missingKeys) => `Expected object to have keys \`${JSON.stringify(missingKeys)}\``, | ||
validator: object => hasItems(new Set(Object.keys(object)), keys) | ||
}); | ||
} | ||
|
||
/** | ||
* Test an object to include any of the provided keys. | ||
* | ||
* @param keys The keys that could be a key in the object. | ||
*/ | ||
hasAnyKeys(...keys: string[]) { | ||
return this.addValidator({ | ||
message: () => `Expected object to have any key of \`${JSON.stringify(keys)}\``, | ||
validator: object => keys.some(key => object[key] !== undefined) | ||
}); | ||
} | ||
} |
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,42 @@ | ||
import test from 'ava'; | ||
import m from '..'; | ||
|
||
test('object', t => { | ||
t.notThrows(() => m({}, m.object)); | ||
t.notThrows(() => m(new Error('foo'), m.object)); | ||
t.throws(() => m('foo' as any, m.object), 'Expected argument to be of type `object` but received type `string`'); | ||
t.throws(() => m(1 as any, m.object), 'Expected argument to be of type `object` but received type `number`'); | ||
}); | ||
|
||
test('object.plain', t => { | ||
t.notThrows(() => m({}, m.object.plain)); | ||
t.throws(() => m(new Error('foo'), m.object.plain), 'Expected object to be a plain object'); | ||
}); | ||
|
||
test('object.empty', t => { | ||
t.notThrows(() => m({}, m.object.empty)); | ||
t.throws(() => m({unicorn: 'π¦'}, m.object.empty), 'Expected object to be empty, got `{"unicorn":"π¦"}`'); | ||
}); | ||
|
||
test('object.nonEmpty', t => { | ||
t.notThrows(() => m({unicorn: 'π¦'}, m.object.nonEmpty)); | ||
t.throws(() => m({}, m.object.nonEmpty), 'Expected object to not be empty'); | ||
}); | ||
|
||
test('object.deepEqual', t => { | ||
t.notThrows(() => m({unicorn: 'π¦'}, m.object.deepEqual({unicorn: 'π¦'}))); | ||
t.notThrows(() => m({unicorn: 'π¦', rain: {bow: 'π'}}, m.object.deepEqual({unicorn: 'π¦', rain: {bow: 'π'}}))); | ||
t.throws(() => m({unicorn: 'π¦'}, m.object.deepEqual({rainbow: 'π'})), 'Expected object to be deeply equal to `{"rainbow":"π"}`, got `{"unicorn":"π¦"}`'); | ||
}); | ||
|
||
test('object.hasKeys', t => { | ||
t.notThrows(() => m({unicorn: 'π¦'}, m.object.hasKeys('unicorn'))); | ||
t.notThrows(() => m({unicorn: 'π¦', rainbow: 'π'}, m.object.hasKeys('unicorn', 'rainbow'))); | ||
t.throws(() => m({unicorn: 'π¦'}, m.object.hasKeys('unicorn', 'rainbow')), 'Expected object to have keys `["rainbow"]`'); | ||
}); | ||
|
||
test('object.hasAnyKeys', t => { | ||
t.notThrows(() => m({unicorn: 'π¦'}, m.object.hasAnyKeys('unicorn', 'rainbow'))); | ||
t.notThrows(() => m({unicorn: 'π¦', rainbow: 'π'}, m.object.hasAnyKeys('unicorn'))); | ||
t.throws(() => m({unicorn: 'π¦'}, m.object.hasAnyKeys('foo')), 'Expected object to have any key of `["foo"]`'); | ||
}); |