-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
360 lines (304 loc) · 16.3 KB
/
index.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { test } from 'node:test';
import { default as assert } from 'node:assert';
/**
* Description :
* Provide some shortcut method of test/assert
*
* for example of `equal`
*
* equal = run funtion with args and execute `assert.equal(result)`
* `batch`Equal = for Multi records of equal
* `test`Equal = build a test Case with funciton and spec args
* `test``Batch`Equal = build a test Case with Multi funciton and spec args
*
*/
export { test, assert };
export type FunctionArguments = any | any[];
export type FunctionReturn = any;
export type ResultCheckFunction<T> = (result: T) => boolean;
/**
* Item Tuple : [ 0:args, 1:result ];
* args : Arguments pass by test funciton
* result : Result of Function
*/
export type RecordByResult = [FunctionArguments, FunctionReturn];
export type RecordByCheckFunction<T = any> = [FunctionArguments, ResultCheckFunction<T>];
export type DataSetWithResult = RecordByResult[];
export type DataSetWithChecker<T = any> = RecordByCheckFunction<T>[];
type TestFn = Parameters<typeof test>[0];
export const testFn = (fn: Function, testFn: TestFn) => {
test(`Test [${fn.name}]`, testFn);
}
const getArgRtn = (record: RecordByResult | RecordByCheckFunction): [any[], any] => {
const [arg, rtn] = record
if (arg instanceof Array) {
return [arg, rtn]
}
return [[arg], rtn]
}
// Equal
export const equal = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.equal(actual, expected);
}
export const equalBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
equal(fn, args, expected, self)
}
}
export const testEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => equal(fn, args, expected, self));
export const testEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => equalBatch(fn, records, self));
// deepEqual
export const deepEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.deepEqual(actual, expected);
}
export const deepEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
deepEqual(fn, args, expected, self)
}
}
export const testDeepEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => deepEqual(fn, args, expected, self));
export const testDeepEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => deepEqualBatch(fn, records, self));
// strictEqual
export const strictEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.strictEqual(actual, expected);
}
export const strictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
strictEqual(fn, args, expected, self)
}
}
export const testStrictEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => strictEqual(fn, args, expected, self));
export const testStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => strictEqualBatch(fn, records, self));
// deepStrictEqual
export const deepStrictEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.deepStrictEqual(actual, expected);
}
export const deepStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
deepStrictEqual(fn, args, expected, self)
}
}
export const testDeepStrictEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => deepStrictEqual(fn, args, expected, self));
export const testDeepStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => deepStrictEqualBatch(fn, records, self));
// NotEqual
export const notEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.notEqual(actual, expected);
}
export const notEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
notEqual(fn, args, expected, self)
}
}
export const testNotEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => notEqual(fn, args, expected, self));
export const testNotEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => notEqualBatch(fn, records, self));
// not deepEqual
export const notDeepEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.notDeepEqual(actual, expected);
}
export const notDeepEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
notDeepEqual(fn, args, expected, self)
}
}
export const testNotDeepEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => notDeepEqual(fn, args, expected, self));
export const testNotDeepEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => notDeepEqualBatch(fn, records, self));
// notStrictEqual
export const notStrictEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.notStrictEqual(actual, expected);
}
export const notStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
notStrictEqual(fn, args, expected, self)
}
}
export const testNotStrictEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => notStrictEqual(fn, args, expected, self));
export const testNotStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => notStrictEqualBatch(fn, records, self));
// notDeepStrictEqual
export const notDeepStrictEqual = async (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.notDeepStrictEqual(actual, expected);
}
export const notDeepStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => {
for (let record of records) {
const [args, expected] = getArgRtn(record);
notDeepStrictEqual(fn, args, expected, self)
}
}
export const testNotDeepStrictEqual = (fn: Function, args: FunctionArguments, expected: FunctionReturn, self: any = null) => test(`Test [${fn.name}]`, () => notDeepStrictEqual(fn, args, expected, self));
export const testNotDeepStrictEqualBatch = (fn: Function, records: DataSetWithResult, self: any = null) => test(`Test [${fn.name}]`, () => notDeepStrictEqualBatch(fn, records, self));
// true/false
export const isTrue = async (fn: Function, args: FunctionArguments, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.equal(actual, true);
}
export const isTrueBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
isTrue(fn, args, self)
}
}
export const testIsTrue = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => isTrue(fn, args, self));
export const testIsTrueBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => isTrueBatch(fn, records, self));
export const isFalse = async (fn: Function, args: FunctionArguments, self: any = null) => {
const actual = await Promise.resolve(fn.apply(self, args));
assert.equal(actual, false);
}
export const isFalseBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
isFalse(fn, args, self)
}
}
export const testIsFalse = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => isFalse(fn, args, self));
export const testIsFalseBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => isFalseBatch(fn, records, self));
// Throw
export const throws = (fn: Function, args: FunctionArguments, self: any = null) => {
assert.throws(() => fn.apply(self, args))
}
export const throwsBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
throws(fn, args, self)
}
}
export const testThrows = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => throws(fn, args, self));
export const testThrowsBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => throwsBatch(fn, records, self));
export const doesNotThrow = (fn: Function, args: FunctionArguments, self: any = null) => {
assert.doesNotThrow(() => fn.apply(self, args))
}
export const doesNotThrowBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
doesNotThrow(fn, args, self)
}
}
export const testDoesNotThrow = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => doesNotThrow(fn, args, self));
export const testDoesNotThrowBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => doesNotThrowBatch(fn, records, self));
export const rejects = (fn: Function, args: FunctionArguments, self: any = null) => {
assert.rejects(() => fn.apply(self, args))
}
export const rejectsBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
rejects(fn, args, self)
}
}
export const testRejects = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => rejects(fn, args, self));
export const testRejectsBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => rejectsBatch(fn, records, self));
export const doesNotReject = (fn: Function, args: FunctionArguments, self: any = null) => {
assert.doesNotReject(() => fn.apply(self, args))
}
export const doesNotRejectBatch = (fn: Function, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
doesNotReject(fn, args, self)
}
}
export const testDoesNotReject = (fn: Function, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => doesNotReject(fn, args, self));
export const testDoesNotRejectBatch = (fn: Function, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => doesNotRejectBatch(fn, records, self));
// Match
export const match = async (fn: Function, reg: RegExp, args: FunctionArguments, self: any = null) => {
const text = await Promise.resolve(fn.apply(self, args));
assert.match(text, reg);
}
export const matchBatch = (fn: Function, reg: RegExp, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
match(fn, reg, args, self)
}
}
export const testMatch = (fn: Function, reg: RegExp, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => match(fn, reg, args, self));
export const testMatchBatch = (fn: Function, reg: RegExp, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => matchBatch(fn, reg, records, self));
export const doesNotMatch = async (fn: Function, reg: RegExp, args: FunctionArguments, self: any = null) => {
const text = await Promise.resolve(fn.apply(self, args));
assert.doesNotMatch(text, reg);
}
export const doesNotMatchBatch = (fn: Function, reg: RegExp, records: FunctionArguments[], self: any = null) => {
for (let args of records) {
doesNotMatch(fn, reg, args, self)
}
}
export const testDoesNotMatch = (fn: Function, reg: RegExp, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => doesNotMatch(fn, reg, args, self));
export const testDoesNotMatchBatch = (fn: Function, reg: RegExp, records: FunctionArguments[], self: any = null) => test(`Test [${fn.name}]`, () => doesNotMatchBatch(fn, reg, records, self));
export const byCheckFunction = <T>(fn: (...args: any[]) => T, record: RecordByCheckFunction<T>, self: any = null) => {
// RecordByCheckFunction<T>
const [args, checker] = getArgRtn(record);;
const returnValue = fn.apply(self, args);
assert.equal(checker(returnValue), true);
}
export const byCheckFunctionBatch = <T>(fn: (...args: any[]) => T, records: DataSetWithChecker<T>, self: any = null) => {
for (let record of records) {
byCheckFunction(fn, record, self)
}
}
export const testByCheckFunction = <T>(fn: (...args: any[]) => T, args: FunctionArguments, self: any = null) => test(`Test [${fn.name}]`, () => byCheckFunction(fn, args, self));
export const testByCheckFunctionBatch = <T>(fn: (...args: any[]) => T, records: DataSetWithChecker<T>, self: any = null) => test(`Test [${fn.name}]`, () => byCheckFunctionBatch(fn, records, self));
// const getString = (data: any): string => {
// switch (typeof data) {
// case 'bigint':
// case 'boolean':
// case 'number':
// return data.toString();
// case 'string':
// return data;
// case 'function':
// return data.toString();
// case 'object':
// if (data instanceof Error) {
// // if (data.stack) {
// // return `${data.name + ' : ' + bgYellow(data.message)}
// // ${data.stack}`
// // }
// return bgYellow(data.name + ' : ' + data.message)
// }
// return JSON.stringify(data);
// case 'symbol':
// return data.toString();
// case 'undefined':
// return 'undefined'
// default:
// return '';
// }
// }
// Custom Message , Some Bug course Not effect ,todo
// class CustomError extends AssertionError {
// generatedMessage = false;
// }
// const ArgFunction = (fn: Function, args: FunctionArguments) => {
// const txt: string[] = [];
// txt.push(colorGray(fn.name));
// txt.push(colorGreen('('))
// if (args instanceof Array) {
// let txtArgs: string[] = []
// args.map(arg => {
// txtArgs.push(colorYellow(getString(arg)));
// })
// txt.push(txtArgs.join(colorGray(', ')));
// } else {
// txt.push(colorYellow(getString(args)))
// }
// txt.push(colorGreen(')'))
// return txt.join('')
// }
// const EqualMessage = (fn: Function, args: FunctionArguments, expected: any, actual: any) => {
// const message = `${colorRed('Not Equal : ')} ${fn.name}
// ${ArgFunction(fn, args)}
// ${colorGray('Expected :')} ${getString(expected)}
// ${colorGray(' Actual :')} ${getString(actual)}
// `;
// return message
// }
// const NotEqualMessage = (fn: Function, args: FunctionArguments, expected: any, actual: any): string => {
// return `${colorRed('Equal : ')} ${fn.name}
// ${ArgFunction(fn, args)}
// Expected : ${getString(expected)}
// Actual : ${getString(actual)}
// `;
// }