@robinpath/cli 1.74.0 → 1.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -111
- package/dist/cli.mjs +1 -1
- package/modules/_helpers.js +33 -33
- package/modules/assert.js +270 -270
- package/modules/buffer.js +245 -245
- package/modules/child.js +176 -176
- package/modules/crypto.js +352 -352
- package/modules/dns.js +146 -146
- package/modules/events.js +174 -174
- package/modules/file.js +361 -361
- package/modules/http.js +268 -268
- package/modules/index.js +76 -76
- package/modules/net.js +189 -189
- package/modules/os.js +219 -219
- package/modules/path.js +162 -162
- package/modules/process.js +214 -214
- package/modules/stream.js +322 -322
- package/modules/string_decoder.js +106 -106
- package/modules/timer.js +167 -167
- package/modules/tls.js +264 -264
- package/modules/tty.js +169 -169
- package/modules/url.js +189 -189
- package/modules/util.js +275 -275
- package/modules/zlib.js +126 -126
- package/package.json +1 -1
package/modules/assert.js
CHANGED
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native assert module for RobinPath.
|
|
3
|
-
* Testing assertions with clear error messages.
|
|
4
|
-
*/
|
|
5
|
-
import { toStr, requireArgs } from './_helpers.js';
|
|
6
|
-
|
|
7
|
-
function fail(message) {
|
|
8
|
-
const err = new Error(message);
|
|
9
|
-
err.__formattedMessage = message;
|
|
10
|
-
throw err;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const AssertFunctions = {
|
|
14
|
-
|
|
15
|
-
ok: (args) => {
|
|
16
|
-
requireArgs('assert.ok', args, 1);
|
|
17
|
-
const val = args[0];
|
|
18
|
-
const msg = args[1] ? toStr(args[1]) : `Expected truthy, got ${JSON.stringify(val)}`;
|
|
19
|
-
if (!val) fail(msg);
|
|
20
|
-
return true;
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
equal: (args) => {
|
|
24
|
-
requireArgs('assert.equal', args, 2);
|
|
25
|
-
const actual = args[0];
|
|
26
|
-
const expected = args[1];
|
|
27
|
-
const msg = args[2] ? toStr(args[2]) : `Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`;
|
|
28
|
-
if (actual != expected) fail(msg);
|
|
29
|
-
return true;
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
strictEqual: (args) => {
|
|
33
|
-
requireArgs('assert.strictEqual', args, 2);
|
|
34
|
-
const actual = args[0];
|
|
35
|
-
const expected = args[1];
|
|
36
|
-
const msg = args[2] ? toStr(args[2]) : `Expected strict ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`;
|
|
37
|
-
if (actual !== expected) fail(msg);
|
|
38
|
-
return true;
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
notEqual: (args) => {
|
|
42
|
-
requireArgs('assert.notEqual', args, 2);
|
|
43
|
-
const actual = args[0];
|
|
44
|
-
const expected = args[1];
|
|
45
|
-
const msg = args[2] ? toStr(args[2]) : `Expected not equal to ${JSON.stringify(expected)}`;
|
|
46
|
-
if (actual == expected) fail(msg);
|
|
47
|
-
return true;
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
deepEqual: (args) => {
|
|
51
|
-
requireArgs('assert.deepEqual', args, 2);
|
|
52
|
-
const actual = args[0];
|
|
53
|
-
const expected = args[1];
|
|
54
|
-
const msg = args[2] ? toStr(args[2]) : `Deep equal assertion failed`;
|
|
55
|
-
try {
|
|
56
|
-
if (JSON.stringify(actual) !== JSON.stringify(expected)) fail(msg);
|
|
57
|
-
} catch {
|
|
58
|
-
fail(msg);
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
notDeepEqual: (args) => {
|
|
64
|
-
requireArgs('assert.notDeepEqual', args, 2);
|
|
65
|
-
const msg = args[2] ? toStr(args[2]) : `Expected objects to not be deeply equal`;
|
|
66
|
-
try {
|
|
67
|
-
if (JSON.stringify(args[0]) === JSON.stringify(args[1])) fail(msg);
|
|
68
|
-
} catch {
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
truthy: (args) => {
|
|
75
|
-
requireArgs('assert.truthy', args, 1);
|
|
76
|
-
const msg = args[1] ? toStr(args[1]) : `Expected truthy, got ${JSON.stringify(args[0])}`;
|
|
77
|
-
if (!args[0]) fail(msg);
|
|
78
|
-
return true;
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
falsy: (args) => {
|
|
82
|
-
requireArgs('assert.falsy', args, 1);
|
|
83
|
-
const msg = args[1] ? toStr(args[1]) : `Expected falsy, got ${JSON.stringify(args[0])}`;
|
|
84
|
-
if (args[0]) fail(msg);
|
|
85
|
-
return true;
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
isNull: (args) => {
|
|
89
|
-
requireArgs('assert.isNull', args, 1);
|
|
90
|
-
const msg = args[1] ? toStr(args[1]) : `Expected null, got ${JSON.stringify(args[0])}`;
|
|
91
|
-
if (args[0] !== null) fail(msg);
|
|
92
|
-
return true;
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
isNotNull: (args) => {
|
|
96
|
-
requireArgs('assert.isNotNull', args, 1);
|
|
97
|
-
const msg = args[1] ? toStr(args[1]) : `Expected non-null value`;
|
|
98
|
-
if (args[0] === null) fail(msg);
|
|
99
|
-
return true;
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
isType: (args) => {
|
|
103
|
-
requireArgs('assert.isType', args, 2);
|
|
104
|
-
const val = args[0];
|
|
105
|
-
const expectedType = toStr(args[1]);
|
|
106
|
-
const actualType = val === null ? 'null' : Array.isArray(val) ? 'array' : typeof val;
|
|
107
|
-
const msg = args[2] ? toStr(args[2]) : `Expected type ${expectedType}, got ${actualType}`;
|
|
108
|
-
if (actualType !== expectedType) fail(msg);
|
|
109
|
-
return true;
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
contains: (args) => {
|
|
113
|
-
requireArgs('assert.contains', args, 2);
|
|
114
|
-
const haystack = args[0];
|
|
115
|
-
const needle = args[1];
|
|
116
|
-
const msg = args[2] ? toStr(args[2]) : `Expected to contain ${JSON.stringify(needle)}`;
|
|
117
|
-
if (typeof haystack === 'string') {
|
|
118
|
-
if (!haystack.includes(toStr(needle))) fail(msg);
|
|
119
|
-
} else if (Array.isArray(haystack)) {
|
|
120
|
-
if (!haystack.includes(needle)) fail(msg);
|
|
121
|
-
} else {
|
|
122
|
-
fail(`assert.contains: first argument must be string or array`);
|
|
123
|
-
}
|
|
124
|
-
return true;
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
notContains: (args) => {
|
|
128
|
-
requireArgs('assert.notContains', args, 2);
|
|
129
|
-
const haystack = args[0];
|
|
130
|
-
const needle = args[1];
|
|
131
|
-
const msg = args[2] ? toStr(args[2]) : `Expected to not contain ${JSON.stringify(needle)}`;
|
|
132
|
-
if (typeof haystack === 'string') {
|
|
133
|
-
if (haystack.includes(toStr(needle))) fail(msg);
|
|
134
|
-
} else if (Array.isArray(haystack)) {
|
|
135
|
-
if (haystack.includes(needle)) fail(msg);
|
|
136
|
-
}
|
|
137
|
-
return true;
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
match: (args) => {
|
|
141
|
-
requireArgs('assert.match', args, 2);
|
|
142
|
-
const str = toStr(args[0]);
|
|
143
|
-
const pattern = toStr(args[1]);
|
|
144
|
-
const msg = args[2] ? toStr(args[2]) : `Expected "${str}" to match ${pattern}`;
|
|
145
|
-
if (!new RegExp(pattern).test(str)) fail(msg);
|
|
146
|
-
return true;
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
notMatch: (args) => {
|
|
150
|
-
requireArgs('assert.notMatch', args, 2);
|
|
151
|
-
const str = toStr(args[0]);
|
|
152
|
-
const pattern = toStr(args[1]);
|
|
153
|
-
const msg = args[2] ? toStr(args[2]) : `Expected "${str}" to not match ${pattern}`;
|
|
154
|
-
if (new RegExp(pattern).test(str)) fail(msg);
|
|
155
|
-
return true;
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
greaterThan: (args) => {
|
|
159
|
-
requireArgs('assert.greaterThan', args, 2);
|
|
160
|
-
const a = Number(args[0]);
|
|
161
|
-
const b = Number(args[1]);
|
|
162
|
-
const msg = args[2] ? toStr(args[2]) : `Expected ${a} > ${b}`;
|
|
163
|
-
if (!(a > b)) fail(msg);
|
|
164
|
-
return true;
|
|
165
|
-
},
|
|
166
|
-
|
|
167
|
-
lessThan: (args) => {
|
|
168
|
-
requireArgs('assert.lessThan', args, 2);
|
|
169
|
-
const a = Number(args[0]);
|
|
170
|
-
const b = Number(args[1]);
|
|
171
|
-
const msg = args[2] ? toStr(args[2]) : `Expected ${a} < ${b}`;
|
|
172
|
-
if (!(a < b)) fail(msg);
|
|
173
|
-
return true;
|
|
174
|
-
},
|
|
175
|
-
|
|
176
|
-
between: (args) => {
|
|
177
|
-
requireArgs('assert.between', args, 3);
|
|
178
|
-
const val = Number(args[0]);
|
|
179
|
-
const min = Number(args[1]);
|
|
180
|
-
const max = Number(args[2]);
|
|
181
|
-
const msg = args[3] ? toStr(args[3]) : `Expected ${val} between ${min} and ${max}`;
|
|
182
|
-
if (val < min || val > max) fail(msg);
|
|
183
|
-
return true;
|
|
184
|
-
},
|
|
185
|
-
|
|
186
|
-
lengthOf: (args) => {
|
|
187
|
-
requireArgs('assert.lengthOf', args, 2);
|
|
188
|
-
const val = args[0];
|
|
189
|
-
const expected = Number(args[1]);
|
|
190
|
-
const actual = (typeof val === 'string' || Array.isArray(val)) ? val.length : Object.keys(val).length;
|
|
191
|
-
const msg = args[2] ? toStr(args[2]) : `Expected length ${expected}, got ${actual}`;
|
|
192
|
-
if (actual !== expected) fail(msg);
|
|
193
|
-
return true;
|
|
194
|
-
},
|
|
195
|
-
|
|
196
|
-
hasProperty: (args) => {
|
|
197
|
-
requireArgs('assert.hasProperty', args, 2);
|
|
198
|
-
const obj = args[0];
|
|
199
|
-
const prop = toStr(args[1]);
|
|
200
|
-
const msg = args[2] ? toStr(args[2]) : `Expected object to have property "${prop}"`;
|
|
201
|
-
if (typeof obj !== 'object' || obj === null || !(prop in obj)) fail(msg);
|
|
202
|
-
return true;
|
|
203
|
-
},
|
|
204
|
-
|
|
205
|
-
throws: async (args, callback) => {
|
|
206
|
-
const msg = args[0] ? toStr(args[0]) : 'Expected an error to be thrown';
|
|
207
|
-
if (!callback) fail('assert.throws requires a callback block');
|
|
208
|
-
try {
|
|
209
|
-
await callback([]);
|
|
210
|
-
fail(msg);
|
|
211
|
-
} catch {
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
doesNotThrow: async (args, callback) => {
|
|
217
|
-
const msg = args[0] ? toStr(args[0]) : 'Expected no error to be thrown';
|
|
218
|
-
if (!callback) fail('assert.doesNotThrow requires a callback block');
|
|
219
|
-
try {
|
|
220
|
-
await callback([]);
|
|
221
|
-
return true;
|
|
222
|
-
} catch (err) {
|
|
223
|
-
fail(`${msg}: ${err.message}`);
|
|
224
|
-
}
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
fail: (args) => {
|
|
228
|
-
const msg = args[0] ? toStr(args[0]) : 'Assertion failed';
|
|
229
|
-
fail(msg);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
export const AssertFunctionMetadata = {
|
|
234
|
-
ok: { description: 'Assert value is truthy', parameters: [{ name: 'value', dataType: 'any', description: 'Value to check', formInputType: 'json', required: true }, { name: 'message', dataType: 'string', description: 'Error message', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if passes', example: 'assert.ok $val' },
|
|
235
|
-
equal: { description: 'Assert loose equality (==)', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual value', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if equal', example: 'assert.equal $a $b' },
|
|
236
|
-
strictEqual: { description: 'Assert strict equality (===)', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if strict equal', example: 'assert.strictEqual $a $b' },
|
|
237
|
-
notEqual: { description: 'Assert not equal', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Not expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not equal', example: 'assert.notEqual $a $b' },
|
|
238
|
-
deepEqual: { description: 'Assert deep equality', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if deeply equal', example: 'assert.deepEqual $a $b' },
|
|
239
|
-
truthy: { description: 'Assert truthy', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true', example: 'assert.truthy $val' },
|
|
240
|
-
falsy: { description: 'Assert falsy', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true', example: 'assert.falsy $val' },
|
|
241
|
-
isNull: { description: 'Assert null', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if null', example: 'assert.isNull $val' },
|
|
242
|
-
isNotNull: { description: 'Assert not null', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not null', example: 'assert.isNotNull $val' },
|
|
243
|
-
isType: { description: 'Assert value type', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }, { name: 'type', dataType: 'string', description: 'Expected type', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if type matches', example: 'assert.isType $val "string"' },
|
|
244
|
-
contains: { description: 'Assert string/array contains value', parameters: [{ name: 'haystack', dataType: 'any', description: 'String or array', formInputType: 'json', required: true }, { name: 'needle', dataType: 'any', description: 'Value to find', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if contains', example: 'assert.contains "hello world" "world"' },
|
|
245
|
-
match: { description: 'Assert string matches regex', parameters: [{ name: 'string', dataType: 'string', description: 'String to test', formInputType: 'text', required: true }, { name: 'pattern', dataType: 'string', description: 'Regex pattern', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if matches', example: 'assert.match "hello" "^he"' },
|
|
246
|
-
greaterThan: { description: 'Assert a > b', parameters: [{ name: 'a', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'b', dataType: 'number', description: 'Comparison', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if a > b', example: 'assert.greaterThan 5 3' },
|
|
247
|
-
lessThan: { description: 'Assert a < b', parameters: [{ name: 'a', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'b', dataType: 'number', description: 'Comparison', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if a < b', example: 'assert.lessThan 3 5' },
|
|
248
|
-
between: { description: 'Assert value is between min and max', parameters: [{ name: 'value', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'min', dataType: 'number', description: 'Minimum', formInputType: 'number', required: true }, { name: 'max', dataType: 'number', description: 'Maximum', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if in range', example: 'assert.between 5 1 10' },
|
|
249
|
-
lengthOf: { description: 'Assert length of string/array/object', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }, { name: 'length', dataType: 'number', description: 'Expected length', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if length matches', example: 'assert.lengthOf [1,2,3] 3' },
|
|
250
|
-
hasProperty: { description: 'Assert object has a property', parameters: [{ name: 'object', dataType: 'object', description: 'Object', formInputType: 'json', required: true }, { name: 'property', dataType: 'string', description: 'Property name', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if has property', example: 'assert.hasProperty $obj "name"' },
|
|
251
|
-
notDeepEqual: { description: 'Assert not deeply equal', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Not expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not deeply equal', example: 'assert.notDeepEqual $a $b' },
|
|
252
|
-
notContains: { description: 'Assert string/array does not contain value', parameters: [{ name: 'haystack', dataType: 'any', description: 'String or array', formInputType: 'json', required: true }, { name: 'needle', dataType: 'any', description: 'Value to check absence', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not contains', example: 'assert.notContains "hello" "xyz"' },
|
|
253
|
-
notMatch: { description: 'Assert string does not match regex', parameters: [{ name: 'string', dataType: 'string', description: 'String to test', formInputType: 'text', required: true }, { name: 'pattern', dataType: 'string', description: 'Regex pattern', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if not matches', example: 'assert.notMatch "hello" "^xyz"' },
|
|
254
|
-
throws: { description: 'Assert callback throws an error', parameters: [{ name: 'message', dataType: 'string', description: 'Error message if no throw', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if threw', example: 'assert.throws "Should error"' },
|
|
255
|
-
doesNotThrow: { description: 'Assert callback does not throw', parameters: [{ name: 'message', dataType: 'string', description: 'Error message if throws', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if no throw', example: 'assert.doesNotThrow "Should not error"' },
|
|
256
|
-
fail: { description: 'Force assertion failure', parameters: [{ name: 'message', dataType: 'string', description: 'Failure message', formInputType: 'text', required: false }], returnType: 'null', returnDescription: 'Always throws', example: 'assert.fail "Not implemented"' }
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
export const AssertModuleMetadata = {
|
|
260
|
-
description: 'Assertions: equal, deepEqual, truthy, falsy, contains, match, greaterThan, throws, and more',
|
|
261
|
-
methods: Object.keys(AssertFunctions)
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
export default {
|
|
265
|
-
name: 'assert',
|
|
266
|
-
functions: AssertFunctions,
|
|
267
|
-
functionMetadata: AssertFunctionMetadata,
|
|
268
|
-
moduleMetadata: AssertModuleMetadata,
|
|
269
|
-
global: false
|
|
270
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Native assert module for RobinPath.
|
|
3
|
+
* Testing assertions with clear error messages.
|
|
4
|
+
*/
|
|
5
|
+
import { toStr, requireArgs } from './_helpers.js';
|
|
6
|
+
|
|
7
|
+
function fail(message) {
|
|
8
|
+
const err = new Error(message);
|
|
9
|
+
err.__formattedMessage = message;
|
|
10
|
+
throw err;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const AssertFunctions = {
|
|
14
|
+
|
|
15
|
+
ok: (args) => {
|
|
16
|
+
requireArgs('assert.ok', args, 1);
|
|
17
|
+
const val = args[0];
|
|
18
|
+
const msg = args[1] ? toStr(args[1]) : `Expected truthy, got ${JSON.stringify(val)}`;
|
|
19
|
+
if (!val) fail(msg);
|
|
20
|
+
return true;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
equal: (args) => {
|
|
24
|
+
requireArgs('assert.equal', args, 2);
|
|
25
|
+
const actual = args[0];
|
|
26
|
+
const expected = args[1];
|
|
27
|
+
const msg = args[2] ? toStr(args[2]) : `Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`;
|
|
28
|
+
if (actual != expected) fail(msg);
|
|
29
|
+
return true;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
strictEqual: (args) => {
|
|
33
|
+
requireArgs('assert.strictEqual', args, 2);
|
|
34
|
+
const actual = args[0];
|
|
35
|
+
const expected = args[1];
|
|
36
|
+
const msg = args[2] ? toStr(args[2]) : `Expected strict ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`;
|
|
37
|
+
if (actual !== expected) fail(msg);
|
|
38
|
+
return true;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
notEqual: (args) => {
|
|
42
|
+
requireArgs('assert.notEqual', args, 2);
|
|
43
|
+
const actual = args[0];
|
|
44
|
+
const expected = args[1];
|
|
45
|
+
const msg = args[2] ? toStr(args[2]) : `Expected not equal to ${JSON.stringify(expected)}`;
|
|
46
|
+
if (actual == expected) fail(msg);
|
|
47
|
+
return true;
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
deepEqual: (args) => {
|
|
51
|
+
requireArgs('assert.deepEqual', args, 2);
|
|
52
|
+
const actual = args[0];
|
|
53
|
+
const expected = args[1];
|
|
54
|
+
const msg = args[2] ? toStr(args[2]) : `Deep equal assertion failed`;
|
|
55
|
+
try {
|
|
56
|
+
if (JSON.stringify(actual) !== JSON.stringify(expected)) fail(msg);
|
|
57
|
+
} catch {
|
|
58
|
+
fail(msg);
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
notDeepEqual: (args) => {
|
|
64
|
+
requireArgs('assert.notDeepEqual', args, 2);
|
|
65
|
+
const msg = args[2] ? toStr(args[2]) : `Expected objects to not be deeply equal`;
|
|
66
|
+
try {
|
|
67
|
+
if (JSON.stringify(args[0]) === JSON.stringify(args[1])) fail(msg);
|
|
68
|
+
} catch {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
truthy: (args) => {
|
|
75
|
+
requireArgs('assert.truthy', args, 1);
|
|
76
|
+
const msg = args[1] ? toStr(args[1]) : `Expected truthy, got ${JSON.stringify(args[0])}`;
|
|
77
|
+
if (!args[0]) fail(msg);
|
|
78
|
+
return true;
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
falsy: (args) => {
|
|
82
|
+
requireArgs('assert.falsy', args, 1);
|
|
83
|
+
const msg = args[1] ? toStr(args[1]) : `Expected falsy, got ${JSON.stringify(args[0])}`;
|
|
84
|
+
if (args[0]) fail(msg);
|
|
85
|
+
return true;
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
isNull: (args) => {
|
|
89
|
+
requireArgs('assert.isNull', args, 1);
|
|
90
|
+
const msg = args[1] ? toStr(args[1]) : `Expected null, got ${JSON.stringify(args[0])}`;
|
|
91
|
+
if (args[0] !== null) fail(msg);
|
|
92
|
+
return true;
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
isNotNull: (args) => {
|
|
96
|
+
requireArgs('assert.isNotNull', args, 1);
|
|
97
|
+
const msg = args[1] ? toStr(args[1]) : `Expected non-null value`;
|
|
98
|
+
if (args[0] === null) fail(msg);
|
|
99
|
+
return true;
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
isType: (args) => {
|
|
103
|
+
requireArgs('assert.isType', args, 2);
|
|
104
|
+
const val = args[0];
|
|
105
|
+
const expectedType = toStr(args[1]);
|
|
106
|
+
const actualType = val === null ? 'null' : Array.isArray(val) ? 'array' : typeof val;
|
|
107
|
+
const msg = args[2] ? toStr(args[2]) : `Expected type ${expectedType}, got ${actualType}`;
|
|
108
|
+
if (actualType !== expectedType) fail(msg);
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
contains: (args) => {
|
|
113
|
+
requireArgs('assert.contains', args, 2);
|
|
114
|
+
const haystack = args[0];
|
|
115
|
+
const needle = args[1];
|
|
116
|
+
const msg = args[2] ? toStr(args[2]) : `Expected to contain ${JSON.stringify(needle)}`;
|
|
117
|
+
if (typeof haystack === 'string') {
|
|
118
|
+
if (!haystack.includes(toStr(needle))) fail(msg);
|
|
119
|
+
} else if (Array.isArray(haystack)) {
|
|
120
|
+
if (!haystack.includes(needle)) fail(msg);
|
|
121
|
+
} else {
|
|
122
|
+
fail(`assert.contains: first argument must be string or array`);
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
notContains: (args) => {
|
|
128
|
+
requireArgs('assert.notContains', args, 2);
|
|
129
|
+
const haystack = args[0];
|
|
130
|
+
const needle = args[1];
|
|
131
|
+
const msg = args[2] ? toStr(args[2]) : `Expected to not contain ${JSON.stringify(needle)}`;
|
|
132
|
+
if (typeof haystack === 'string') {
|
|
133
|
+
if (haystack.includes(toStr(needle))) fail(msg);
|
|
134
|
+
} else if (Array.isArray(haystack)) {
|
|
135
|
+
if (haystack.includes(needle)) fail(msg);
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
match: (args) => {
|
|
141
|
+
requireArgs('assert.match', args, 2);
|
|
142
|
+
const str = toStr(args[0]);
|
|
143
|
+
const pattern = toStr(args[1]);
|
|
144
|
+
const msg = args[2] ? toStr(args[2]) : `Expected "${str}" to match ${pattern}`;
|
|
145
|
+
if (!new RegExp(pattern).test(str)) fail(msg);
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
notMatch: (args) => {
|
|
150
|
+
requireArgs('assert.notMatch', args, 2);
|
|
151
|
+
const str = toStr(args[0]);
|
|
152
|
+
const pattern = toStr(args[1]);
|
|
153
|
+
const msg = args[2] ? toStr(args[2]) : `Expected "${str}" to not match ${pattern}`;
|
|
154
|
+
if (new RegExp(pattern).test(str)) fail(msg);
|
|
155
|
+
return true;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
greaterThan: (args) => {
|
|
159
|
+
requireArgs('assert.greaterThan', args, 2);
|
|
160
|
+
const a = Number(args[0]);
|
|
161
|
+
const b = Number(args[1]);
|
|
162
|
+
const msg = args[2] ? toStr(args[2]) : `Expected ${a} > ${b}`;
|
|
163
|
+
if (!(a > b)) fail(msg);
|
|
164
|
+
return true;
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
lessThan: (args) => {
|
|
168
|
+
requireArgs('assert.lessThan', args, 2);
|
|
169
|
+
const a = Number(args[0]);
|
|
170
|
+
const b = Number(args[1]);
|
|
171
|
+
const msg = args[2] ? toStr(args[2]) : `Expected ${a} < ${b}`;
|
|
172
|
+
if (!(a < b)) fail(msg);
|
|
173
|
+
return true;
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
between: (args) => {
|
|
177
|
+
requireArgs('assert.between', args, 3);
|
|
178
|
+
const val = Number(args[0]);
|
|
179
|
+
const min = Number(args[1]);
|
|
180
|
+
const max = Number(args[2]);
|
|
181
|
+
const msg = args[3] ? toStr(args[3]) : `Expected ${val} between ${min} and ${max}`;
|
|
182
|
+
if (val < min || val > max) fail(msg);
|
|
183
|
+
return true;
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
lengthOf: (args) => {
|
|
187
|
+
requireArgs('assert.lengthOf', args, 2);
|
|
188
|
+
const val = args[0];
|
|
189
|
+
const expected = Number(args[1]);
|
|
190
|
+
const actual = (typeof val === 'string' || Array.isArray(val)) ? val.length : Object.keys(val).length;
|
|
191
|
+
const msg = args[2] ? toStr(args[2]) : `Expected length ${expected}, got ${actual}`;
|
|
192
|
+
if (actual !== expected) fail(msg);
|
|
193
|
+
return true;
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
hasProperty: (args) => {
|
|
197
|
+
requireArgs('assert.hasProperty', args, 2);
|
|
198
|
+
const obj = args[0];
|
|
199
|
+
const prop = toStr(args[1]);
|
|
200
|
+
const msg = args[2] ? toStr(args[2]) : `Expected object to have property "${prop}"`;
|
|
201
|
+
if (typeof obj !== 'object' || obj === null || !(prop in obj)) fail(msg);
|
|
202
|
+
return true;
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
throws: async (args, callback) => {
|
|
206
|
+
const msg = args[0] ? toStr(args[0]) : 'Expected an error to be thrown';
|
|
207
|
+
if (!callback) fail('assert.throws requires a callback block');
|
|
208
|
+
try {
|
|
209
|
+
await callback([]);
|
|
210
|
+
fail(msg);
|
|
211
|
+
} catch {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
doesNotThrow: async (args, callback) => {
|
|
217
|
+
const msg = args[0] ? toStr(args[0]) : 'Expected no error to be thrown';
|
|
218
|
+
if (!callback) fail('assert.doesNotThrow requires a callback block');
|
|
219
|
+
try {
|
|
220
|
+
await callback([]);
|
|
221
|
+
return true;
|
|
222
|
+
} catch (err) {
|
|
223
|
+
fail(`${msg}: ${err.message}`);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
fail: (args) => {
|
|
228
|
+
const msg = args[0] ? toStr(args[0]) : 'Assertion failed';
|
|
229
|
+
fail(msg);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export const AssertFunctionMetadata = {
|
|
234
|
+
ok: { description: 'Assert value is truthy', parameters: [{ name: 'value', dataType: 'any', description: 'Value to check', formInputType: 'json', required: true }, { name: 'message', dataType: 'string', description: 'Error message', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if passes', example: 'assert.ok $val' },
|
|
235
|
+
equal: { description: 'Assert loose equality (==)', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual value', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if equal', example: 'assert.equal $a $b' },
|
|
236
|
+
strictEqual: { description: 'Assert strict equality (===)', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if strict equal', example: 'assert.strictEqual $a $b' },
|
|
237
|
+
notEqual: { description: 'Assert not equal', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Not expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not equal', example: 'assert.notEqual $a $b' },
|
|
238
|
+
deepEqual: { description: 'Assert deep equality', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if deeply equal', example: 'assert.deepEqual $a $b' },
|
|
239
|
+
truthy: { description: 'Assert truthy', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true', example: 'assert.truthy $val' },
|
|
240
|
+
falsy: { description: 'Assert falsy', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true', example: 'assert.falsy $val' },
|
|
241
|
+
isNull: { description: 'Assert null', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if null', example: 'assert.isNull $val' },
|
|
242
|
+
isNotNull: { description: 'Assert not null', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not null', example: 'assert.isNotNull $val' },
|
|
243
|
+
isType: { description: 'Assert value type', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }, { name: 'type', dataType: 'string', description: 'Expected type', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if type matches', example: 'assert.isType $val "string"' },
|
|
244
|
+
contains: { description: 'Assert string/array contains value', parameters: [{ name: 'haystack', dataType: 'any', description: 'String or array', formInputType: 'json', required: true }, { name: 'needle', dataType: 'any', description: 'Value to find', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if contains', example: 'assert.contains "hello world" "world"' },
|
|
245
|
+
match: { description: 'Assert string matches regex', parameters: [{ name: 'string', dataType: 'string', description: 'String to test', formInputType: 'text', required: true }, { name: 'pattern', dataType: 'string', description: 'Regex pattern', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if matches', example: 'assert.match "hello" "^he"' },
|
|
246
|
+
greaterThan: { description: 'Assert a > b', parameters: [{ name: 'a', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'b', dataType: 'number', description: 'Comparison', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if a > b', example: 'assert.greaterThan 5 3' },
|
|
247
|
+
lessThan: { description: 'Assert a < b', parameters: [{ name: 'a', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'b', dataType: 'number', description: 'Comparison', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if a < b', example: 'assert.lessThan 3 5' },
|
|
248
|
+
between: { description: 'Assert value is between min and max', parameters: [{ name: 'value', dataType: 'number', description: 'Value', formInputType: 'number', required: true }, { name: 'min', dataType: 'number', description: 'Minimum', formInputType: 'number', required: true }, { name: 'max', dataType: 'number', description: 'Maximum', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if in range', example: 'assert.between 5 1 10' },
|
|
249
|
+
lengthOf: { description: 'Assert length of string/array/object', parameters: [{ name: 'value', dataType: 'any', description: 'Value', formInputType: 'json', required: true }, { name: 'length', dataType: 'number', description: 'Expected length', formInputType: 'number', required: true }], returnType: 'boolean', returnDescription: 'true if length matches', example: 'assert.lengthOf [1,2,3] 3' },
|
|
250
|
+
hasProperty: { description: 'Assert object has a property', parameters: [{ name: 'object', dataType: 'object', description: 'Object', formInputType: 'json', required: true }, { name: 'property', dataType: 'string', description: 'Property name', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if has property', example: 'assert.hasProperty $obj "name"' },
|
|
251
|
+
notDeepEqual: { description: 'Assert not deeply equal', parameters: [{ name: 'actual', dataType: 'any', description: 'Actual', formInputType: 'json', required: true }, { name: 'expected', dataType: 'any', description: 'Not expected', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not deeply equal', example: 'assert.notDeepEqual $a $b' },
|
|
252
|
+
notContains: { description: 'Assert string/array does not contain value', parameters: [{ name: 'haystack', dataType: 'any', description: 'String or array', formInputType: 'json', required: true }, { name: 'needle', dataType: 'any', description: 'Value to check absence', formInputType: 'json', required: true }], returnType: 'boolean', returnDescription: 'true if not contains', example: 'assert.notContains "hello" "xyz"' },
|
|
253
|
+
notMatch: { description: 'Assert string does not match regex', parameters: [{ name: 'string', dataType: 'string', description: 'String to test', formInputType: 'text', required: true }, { name: 'pattern', dataType: 'string', description: 'Regex pattern', formInputType: 'text', required: true }], returnType: 'boolean', returnDescription: 'true if not matches', example: 'assert.notMatch "hello" "^xyz"' },
|
|
254
|
+
throws: { description: 'Assert callback throws an error', parameters: [{ name: 'message', dataType: 'string', description: 'Error message if no throw', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if threw', example: 'assert.throws "Should error"' },
|
|
255
|
+
doesNotThrow: { description: 'Assert callback does not throw', parameters: [{ name: 'message', dataType: 'string', description: 'Error message if throws', formInputType: 'text', required: false }], returnType: 'boolean', returnDescription: 'true if no throw', example: 'assert.doesNotThrow "Should not error"' },
|
|
256
|
+
fail: { description: 'Force assertion failure', parameters: [{ name: 'message', dataType: 'string', description: 'Failure message', formInputType: 'text', required: false }], returnType: 'null', returnDescription: 'Always throws', example: 'assert.fail "Not implemented"' }
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export const AssertModuleMetadata = {
|
|
260
|
+
description: 'Assertions: equal, deepEqual, truthy, falsy, contains, match, greaterThan, throws, and more',
|
|
261
|
+
methods: Object.keys(AssertFunctions)
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
export default {
|
|
265
|
+
name: 'assert',
|
|
266
|
+
functions: AssertFunctions,
|
|
267
|
+
functionMetadata: AssertFunctionMetadata,
|
|
268
|
+
moduleMetadata: AssertModuleMetadata,
|
|
269
|
+
global: false
|
|
270
|
+
};
|