-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
161 lines (139 loc) · 4.2 KB
/
common.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// src/common.js
'use strict'
const failWithType = require('./type_error')
const toSkip = 'beforeThrow fail ifError length name ok prototype strict'.split(' ')
module.exports = (native, format) => {
let AssertionError, callback = false
const compose = parts => {
let message, i = -1
if (parts.length) {
i = 0
while (i < parts.length) {
let v = parts[i++]
if (typeof v === 'function') {
v = v(...parts.slice(i))
parts = parts.slice(0, i - 1).concat(v)
break
}
}
if (parts[0] && /(?<!%)%[cdfijoOs]/.test(parts[0])) {
message = format.apply(null, parts)
} else {
message = parts.join(' ')
}
}
return message
}
const innerThrow = (exception, args) => {
if (callback) {
try {
callback(exception, args)
} catch (err) {
exception.message += ' [EXTRA]: ' + err.message
exception.extra = err
}
}
throw exception
}
function fail (...args) {
let exception, message
if (args.length) {
if (args[0] instanceof Error) {
exception = args[0]
exception.message += compose(args.slice(1)) || ''
} else if (typeof args[0] === 'function') {
exception = new args[0](compose(args.slice(1)) || 'Failed')
} else {
message = compose(args)
}
}
if (!exception) {
exception = new AssertionError(
{ message: message || 'Failed', operator: 'fail', stackStartFn: fail })
exception.generatedMessage = !message
}
innerThrow(exception, args)
}
function ifError (actual, ...args) {
if (!(actual === null || actual === undefined)) {
let message = 'ifError got unwanted exception: ' +
((actual && typeof actual === 'object' && typeof actual.message === 'string')
? actual.message
: format('%o', actual))
if (args.length) message += compose(args)
innerThrow(new AssertionError({
actual, expected: null, operator: 'ifError', message, stackStartFn: ifError
}), args)
}
}
const innerOk = (stackStartFn, args) => {
if (!args.length) {
innerOk(stackStartFn, [undefined, 'No value argument passed to `assert.ok()`'])
} else if (!args[0]) { // Todo: remove extra condition.
innerThrow(args[0] instanceof Error
? args[0]
: new AssertionError({
actual: args[0],
expected: true,
message: compose(args.slice(1)),
operator: '==',
stackStartFn
}), args)
}
}
function ok (...args) {
innerOk(ok, args)
}
/**
* Sets new callback function if provided and returns the previous one.
*
* @param cb - undefined is ignored; other falsy value results callback set to undefined.
* @returns {function(*)|undefined} previous callback.
* @throws {AssertionError} when non-falsy non-function argument is provided.
*/
function beforeThrow (cb) {
const old = callback
if (arguments.length) {
if (cb && typeof cb !== 'function') {
failWithType('beforeThrow callback must be function or false. Received ' + cb)
}
callback = cb || undefined
}
return old
}
const enhance = (dst, src) => {
for (const key of Reflect.ownKeys(src)) {
if (!(toSkip.includes(key))) {
dst[key] = src[key]
}
}
Object.assign(dst, { fail, ifError, ok })
}
/**
* Uses the given native assertion package.
* @param {Object} assert
* @throws {TypeError} when package does not expose AssertionError.
*/
function use (assert) {
if (!assert || typeof assert.AssertionError !== 'function') {
failWithType(
'The "assert" argument must be of type object containing AssertionError. Received ' + assert)
}
for (const key of Reflect.ownKeys(ok)) {
if (!(toSkip.includes(key))) {
delete ok[key]
}
}
AssertionError = assert.AssertionError
enhance(ok, assert)
if (assert.strict) {
const strict = ok.strict = (...args) => innerOk(strict, args)
enhance(strict.strict = strict, assert.strict)
} else {
delete ok.strict
}
ok.use = use
}
use(native)
return Object.assign(ok, { beforeThrow, use })
}