-
Notifications
You must be signed in to change notification settings - Fork 4
/
verify.js
76 lines (62 loc) · 3.66 KB
/
verify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {
isString,
isBoolean,
isNumber,
isInteger,
isRegExp,
isObjectAlike,
isBasicObject, isObjectKey, isObjectContain,
isBasicArray, isArrayLength,
isBasicFunction,
isPromiseAlike,
isOneOf,
isFunctionThrow,
isFunctionThrowAsync,
isStrictEqual,
isStringifyEqual
} from './check'
import { describe } from './format'
const throwError = (title, message, detail) => { throw new Error(`[verify|${title}]${message ? ` ${message};` : ''} ${detail || ''}`) }
const createVerify = (title, checkFunc) => (value, message) => checkFunc(value) || throwError(title, message, `get: ${describe(value)}`)
const string = createVerify('String', isString)
const boolean = createVerify('Boolean', isBoolean)
const number = createVerify('Number', isNumber)
const integer = createVerify('Integer', isInteger)
const regexp = createVerify('RegExp', isRegExp)
const objectAlike = createVerify('ObjectAlike', isObjectAlike)
const basicObject = createVerify('BasicObject', isBasicObject)
const objectKey = (value, key, message) => isObjectKey(value, key) || throwError('ObjectKey', message, `expect to have key: ${key}`)
const objectContain = (value, target, message) => isObjectContain(value, target) || throwError('ObjectContain', message, `expect to contain: ${describe(target)}`)
const basicArray = createVerify('Array', isBasicArray)
const arrayLength = (value, length, message) => isArrayLength(value, length) || throwError('ArrayLength', message, `expect length: ${length}, get: ${isBasicArray(value) ? value.length : describe(value)}`)
const basicFunction = createVerify('Function', isBasicFunction)
const promiseAlike = createVerify('PromiseAlike', isPromiseAlike)
const oneOf = (value, validList, message) => isOneOf(value, validList) || throwError('OneOf', message, `expect one of: [${validList}], get: ${describe(value)}`)
const doThrow = (func, message) => isFunctionThrow(func) || throwError('DoThrow', message)
const doNotThrow = (func, message) => isFunctionThrow(func) && throwError('DoNotThrow', message)
const doThrowAsync = async (func, message) => (await isFunctionThrowAsync(func)) || throwError('DoThrowAsync', message)
const doNotThrowAsync = async (func, message) => (await isFunctionThrowAsync(func)) && throwError('DoNotThrowAsync', message)
// const doThrowAsync = (func, message) => isFunctionThrowAsync(func).then((isThrow) => !isThrow && throwError('DoThrowAsync', message)) // NOTE: reference async-less implementation
// const doNotThrowAsync = (func, message) => isFunctionThrowAsync(func).then((isThrow) => isThrow && throwError('DoNotThrowAsync', message)) // NOTE: reference async-less implementation
const describeEqual = (actual, expect) => `\nactual: ${describe(actual)}\nexpect: ${describe(expect)}`
const strictEqual = (actual, expect, message) => isStrictEqual(actual, expect) || throwError('StrictEqual', message, describeEqual(actual, expect))
const notStrictEqual = (actual, expect, message) => isStrictEqual(actual, expect) && throwError('NotStrictEqual', message, describeEqual(actual, expect))
const stringifyEqual = (actual, expect, message = 'should stringify equal') => isStringifyEqual(actual, expect) || throwError('StringifyEqual', message, describeEqual(actual, expect))
const notStringifyEqual = (actual, expect, message = 'should not stringify equal') => isStringifyEqual(actual, expect) && throwError('NotStringifyEqual', message, describeEqual(actual, expect))
export {
string,
boolean,
number,
integer,
regexp,
objectAlike,
basicObject, objectKey, objectContain,
basicArray, arrayLength,
basicFunction,
promiseAlike,
oneOf,
doThrow, doNotThrow,
doThrowAsync, doNotThrowAsync,
strictEqual, notStrictEqual,
stringifyEqual, notStringifyEqual
}