1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | (function () {
|
10 | 'use strict';
|
11 |
|
12 | var types = {
|
13 | NUMBER: 'number',
|
14 | UNDEFINED: 'undefined',
|
15 | STRING: 'string',
|
16 | BOOLEAN: 'boolean',
|
17 | OBJECT: 'object',
|
18 | FUNCTION: 'function',
|
19 | NULL: 'null',
|
20 | ARRAY: 'array',
|
21 | REGEXP: 'regexp',
|
22 | DATE: 'date',
|
23 | ERROR: 'error',
|
24 | ARGUMENTS: 'arguments',
|
25 | SYMBOL: 'symbol',
|
26 | ARRAY_BUFFER: 'array-buffer',
|
27 | TYPED_ARRAY: 'typed-array',
|
28 | DATA_VIEW: 'data-view',
|
29 | MAP: 'map',
|
30 | SET: 'set',
|
31 | WEAK_SET: 'weak-set',
|
32 | WEAK_MAP: 'weak-map',
|
33 | PROMISE: 'promise',
|
34 |
|
35 |
|
36 | BUFFER: 'buffer',
|
37 |
|
38 |
|
39 | HTML_ELEMENT: 'html-element',
|
40 | HTML_ELEMENT_TEXT: 'html-element-text',
|
41 | DOCUMENT: 'document',
|
42 | WINDOW: 'window',
|
43 | FILE: 'file',
|
44 | FILE_LIST: 'file-list',
|
45 | BLOB: 'blob',
|
46 |
|
47 | HOST: 'host',
|
48 |
|
49 | XHR: 'xhr',
|
50 |
|
51 |
|
52 | SIMD: 'simd'
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function Type(type, cls, sub) {
|
74 | if (!type) {
|
75 | throw new Error('Type class must be initialized at least with `type` information');
|
76 | }
|
77 | this.type = type;
|
78 | this.cls = cls;
|
79 | this.sub = sub;
|
80 | }
|
81 |
|
82 | Type.prototype = {
|
83 | toString: function(sep) {
|
84 | sep = sep || ';';
|
85 | var str = [this.type];
|
86 | if (this.cls) {
|
87 | str.push(this.cls);
|
88 | }
|
89 | if (this.sub) {
|
90 | str.push(this.sub);
|
91 | }
|
92 | return str.join(sep);
|
93 | },
|
94 |
|
95 | toTryTypes: function() {
|
96 | var _types = [];
|
97 | if (this.sub) {
|
98 | _types.push(new Type(this.type, this.cls, this.sub));
|
99 | }
|
100 | if (this.cls) {
|
101 | _types.push(new Type(this.type, this.cls));
|
102 | }
|
103 | _types.push(new Type(this.type));
|
104 |
|
105 | return _types;
|
106 | }
|
107 | };
|
108 |
|
109 | var toString = Object.prototype.toString;
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function TypeChecker() {
|
118 | this.checks = [];
|
119 | }
|
120 |
|
121 | TypeChecker.prototype = {
|
122 | add: function(func) {
|
123 | this.checks.push(func);
|
124 | return this;
|
125 | },
|
126 |
|
127 | addBeforeFirstMatch: function(obj, func) {
|
128 | var match = this.getFirstMatch(obj);
|
129 | if (match) {
|
130 | this.checks.splice(match.index, 0, func);
|
131 | } else {
|
132 | this.add(func);
|
133 | }
|
134 | },
|
135 |
|
136 | addTypeOf: function(type, res) {
|
137 | return this.add(function(obj, tpeOf) {
|
138 | if (tpeOf === type) {
|
139 | return new Type(res);
|
140 | }
|
141 | });
|
142 | },
|
143 |
|
144 | addClass: function(cls, res, sub) {
|
145 | return this.add(function(obj, tpeOf, objCls) {
|
146 | if (objCls === cls) {
|
147 | return new Type(types.OBJECT, res, sub);
|
148 | }
|
149 | });
|
150 | },
|
151 |
|
152 | getFirstMatch: function(obj) {
|
153 | var typeOf = typeof obj;
|
154 | var cls = toString.call(obj);
|
155 |
|
156 | for (var i = 0, l = this.checks.length; i < l; i++) {
|
157 | var res = this.checks[i].call(this, obj, typeOf, cls);
|
158 | if (typeof res !== 'undefined') {
|
159 | return { result: res, func: this.checks[i], index: i };
|
160 | }
|
161 | }
|
162 | },
|
163 |
|
164 | getType: function(obj) {
|
165 | var match = this.getFirstMatch(obj);
|
166 | return match && match.result;
|
167 | }
|
168 | };
|
169 |
|
170 | var main = new TypeChecker();
|
171 |
|
172 |
|
173 |
|
174 | main
|
175 | .addTypeOf(types.NUMBER, types.NUMBER)
|
176 | .addTypeOf(types.UNDEFINED, types.UNDEFINED)
|
177 | .addTypeOf(types.STRING, types.STRING)
|
178 | .addTypeOf(types.BOOLEAN, types.BOOLEAN)
|
179 | .addTypeOf(types.FUNCTION, types.FUNCTION)
|
180 | .addTypeOf(types.SYMBOL, types.SYMBOL)
|
181 | .add(function(obj) {
|
182 | if (obj === null) {
|
183 | return new Type(types.NULL);
|
184 | }
|
185 | })
|
186 | .addClass('[object String]', types.STRING)
|
187 | .addClass('[object Boolean]', types.BOOLEAN)
|
188 | .addClass('[object Number]', types.NUMBER)
|
189 | .addClass('[object Array]', types.ARRAY)
|
190 | .addClass('[object RegExp]', types.REGEXP)
|
191 | .addClass('[object Error]', types.ERROR)
|
192 | .addClass('[object Date]', types.DATE)
|
193 | .addClass('[object Arguments]', types.ARGUMENTS)
|
194 |
|
195 | .addClass('[object ArrayBuffer]', types.ARRAY_BUFFER)
|
196 | .addClass('[object Int8Array]', types.TYPED_ARRAY, 'int8')
|
197 | .addClass('[object Uint8Array]', types.TYPED_ARRAY, 'uint8')
|
198 | .addClass('[object Uint8ClampedArray]', types.TYPED_ARRAY, 'uint8clamped')
|
199 | .addClass('[object Int16Array]', types.TYPED_ARRAY, 'int16')
|
200 | .addClass('[object Uint16Array]', types.TYPED_ARRAY, 'uint16')
|
201 | .addClass('[object Int32Array]', types.TYPED_ARRAY, 'int32')
|
202 | .addClass('[object Uint32Array]', types.TYPED_ARRAY, 'uint32')
|
203 | .addClass('[object Float32Array]', types.TYPED_ARRAY, 'float32')
|
204 | .addClass('[object Float64Array]', types.TYPED_ARRAY, 'float64')
|
205 |
|
206 | .addClass('[object Bool16x8]', types.SIMD, 'bool16x8')
|
207 | .addClass('[object Bool32x4]', types.SIMD, 'bool32x4')
|
208 | .addClass('[object Bool8x16]', types.SIMD, 'bool8x16')
|
209 | .addClass('[object Float32x4]', types.SIMD, 'float32x4')
|
210 | .addClass('[object Int16x8]', types.SIMD, 'int16x8')
|
211 | .addClass('[object Int32x4]', types.SIMD, 'int32x4')
|
212 | .addClass('[object Int8x16]', types.SIMD, 'int8x16')
|
213 | .addClass('[object Uint16x8]', types.SIMD, 'uint16x8')
|
214 | .addClass('[object Uint32x4]', types.SIMD, 'uint32x4')
|
215 | .addClass('[object Uint8x16]', types.SIMD, 'uint8x16')
|
216 |
|
217 | .addClass('[object DataView]', types.DATA_VIEW)
|
218 | .addClass('[object Map]', types.MAP)
|
219 | .addClass('[object WeakMap]', types.WEAK_MAP)
|
220 | .addClass('[object Set]', types.SET)
|
221 | .addClass('[object WeakSet]', types.WEAK_SET)
|
222 | .addClass('[object Promise]', types.PROMISE)
|
223 | .addClass('[object Blob]', types.BLOB)
|
224 | .addClass('[object File]', types.FILE)
|
225 | .addClass('[object FileList]', types.FILE_LIST)
|
226 | .addClass('[object XMLHttpRequest]', types.XHR)
|
227 | .add(function(obj) {
|
228 | if ((typeof Promise === types.FUNCTION && obj instanceof Promise) ||
|
229 | (typeof obj.then === types.FUNCTION)) {
|
230 | return new Type(types.OBJECT, types.PROMISE);
|
231 | }
|
232 | })
|
233 | .add(function(obj) {
|
234 | if (typeof Buffer !== 'undefined' && obj instanceof Buffer) {
|
235 | return new Type(types.OBJECT, types.BUFFER);
|
236 | }
|
237 | })
|
238 | .add(function(obj) {
|
239 | if (typeof Node !== 'undefined' && obj instanceof Node) {
|
240 | return new Type(types.OBJECT, types.HTML_ELEMENT, obj.nodeName);
|
241 | }
|
242 | })
|
243 | .add(function(obj) {
|
244 |
|
245 | if (obj.Boolean === Boolean && obj.Number === Number && obj.String === String && obj.Date === Date) {
|
246 | return new Type(types.OBJECT, types.HOST);
|
247 | }
|
248 | })
|
249 | .add(function() {
|
250 | return new Type(types.OBJECT);
|
251 | });
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function getGlobalType(obj) {
|
261 | return main.getType(obj);
|
262 | }
|
263 |
|
264 | getGlobalType.checker = main;
|
265 | getGlobalType.TypeChecker = TypeChecker;
|
266 | getGlobalType.Type = Type;
|
267 |
|
268 | Object.keys(types).forEach(function(typeName) {
|
269 | getGlobalType[typeName] = types[typeName];
|
270 | });
|
271 |
|
272 | function format(msg) {
|
273 | var args = arguments;
|
274 | for (var i = 1, l = args.length; i < l; i++) {
|
275 | msg = msg.replace(/%s/, args[i]);
|
276 | }
|
277 | return msg;
|
278 | }
|
279 |
|
280 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
281 |
|
282 | function EqualityFail(a, b, reason, path) {
|
283 | this.a = a;
|
284 | this.b = b;
|
285 | this.reason = reason;
|
286 | this.path = path;
|
287 | }
|
288 |
|
289 | function typeToString(tp) {
|
290 | return tp.type + (tp.cls ? "(" + tp.cls + (tp.sub ? " " + tp.sub : "") + ")" : "");
|
291 | }
|
292 |
|
293 | var PLUS_0_AND_MINUS_0 = "+0 is not equal to -0";
|
294 | var DIFFERENT_TYPES = "A has type %s and B has type %s";
|
295 | var EQUALITY = "A is not equal to B";
|
296 | var EQUALITY_PROTOTYPE = "A and B have different prototypes";
|
297 | var WRAPPED_VALUE = "A wrapped value is not equal to B wrapped value";
|
298 | var FUNCTION_SOURCES = "function A is not equal to B by source code value (via .toString call)";
|
299 | var MISSING_KEY = "%s has no key %s";
|
300 | var SET_MAP_MISSING_KEY = "Set/Map missing key %s";
|
301 |
|
302 | var DEFAULT_OPTIONS = {
|
303 | checkProtoEql: true,
|
304 | checkSubType: true,
|
305 | plusZeroAndMinusZeroEqual: true,
|
306 | collectAllFails: false
|
307 | };
|
308 |
|
309 | function setBooleanDefault(property, obj, opts, defaults) {
|
310 | obj[property] = typeof opts[property] !== "boolean" ? defaults[property] : opts[property];
|
311 | }
|
312 |
|
313 | var METHOD_PREFIX = "_check_";
|
314 |
|
315 | function EQ(opts, a, b, path) {
|
316 | opts = opts || {};
|
317 |
|
318 | setBooleanDefault("checkProtoEql", this, opts, DEFAULT_OPTIONS);
|
319 | setBooleanDefault("plusZeroAndMinusZeroEqual", this, opts, DEFAULT_OPTIONS);
|
320 | setBooleanDefault("checkSubType", this, opts, DEFAULT_OPTIONS);
|
321 | setBooleanDefault("collectAllFails", this, opts, DEFAULT_OPTIONS);
|
322 |
|
323 | this.a = a;
|
324 | this.b = b;
|
325 |
|
326 | this._meet = opts._meet || [];
|
327 |
|
328 | this.fails = opts.fails || [];
|
329 |
|
330 | this.path = path || [];
|
331 | }
|
332 |
|
333 | function ShortcutError(fail) {
|
334 | this.name = "ShortcutError";
|
335 | this.message = "fail fast";
|
336 | this.fail = fail;
|
337 | }
|
338 |
|
339 | ShortcutError.prototype = Object.create(Error.prototype);
|
340 |
|
341 | EQ.checkStrictEquality = function(a, b) {
|
342 | this.collectFail(a !== b, EQUALITY);
|
343 | };
|
344 |
|
345 | EQ.add = function add(type, cls, sub, f) {
|
346 | var args = Array.prototype.slice.call(arguments);
|
347 | f = args.pop();
|
348 | EQ.prototype[METHOD_PREFIX + args.join("_")] = f;
|
349 | };
|
350 |
|
351 | EQ.prototype = {
|
352 | check: function() {
|
353 | try {
|
354 | this.check0();
|
355 | } catch (e) {
|
356 | if (e instanceof ShortcutError) {
|
357 | return [e.fail];
|
358 | }
|
359 | throw e;
|
360 | }
|
361 | return this.fails;
|
362 | },
|
363 |
|
364 | check0: function() {
|
365 | var a = this.a;
|
366 | var b = this.b;
|
367 |
|
368 |
|
369 | if (a === b) {
|
370 |
|
371 | return this.collectFail(a === 0 && 1 / a !== 1 / b && !this.plusZeroAndMinusZeroEqual, PLUS_0_AND_MINUS_0);
|
372 | }
|
373 |
|
374 | var typeA = getGlobalType(a);
|
375 | var typeB = getGlobalType(b);
|
376 |
|
377 |
|
378 | if (typeA.type !== typeB.type || typeA.cls !== typeB.cls || typeA.sub !== typeB.sub) {
|
379 | return this.collectFail(true, format(DIFFERENT_TYPES, typeToString(typeA), typeToString(typeB)));
|
380 | }
|
381 |
|
382 |
|
383 | var name1 = typeA.type,
|
384 | name2 = typeA.type;
|
385 | if (typeA.cls) {
|
386 | name1 += "_" + typeA.cls;
|
387 | name2 += "_" + typeA.cls;
|
388 | }
|
389 | if (typeA.sub) {
|
390 | name2 += "_" + typeA.sub;
|
391 | }
|
392 |
|
393 | var f =
|
394 | this[METHOD_PREFIX + name2] ||
|
395 | this[METHOD_PREFIX + name1] ||
|
396 | this[METHOD_PREFIX + typeA.type] ||
|
397 | this.defaultCheck;
|
398 |
|
399 | f.call(this, this.a, this.b);
|
400 | },
|
401 |
|
402 | collectFail: function(comparison, reason, showReason) {
|
403 | if (comparison) {
|
404 | var res = new EqualityFail(this.a, this.b, reason, this.path);
|
405 | res.showReason = !!showReason;
|
406 |
|
407 | this.fails.push(res);
|
408 |
|
409 | if (!this.collectAllFails) {
|
410 | throw new ShortcutError(res);
|
411 | }
|
412 | }
|
413 | },
|
414 |
|
415 | checkPlainObjectsEquality: function(a, b) {
|
416 |
|
417 |
|
418 |
|
419 | var meet = this._meet;
|
420 | var m = this._meet.length;
|
421 | while (m--) {
|
422 | var st = meet[m];
|
423 | if (st[0] === a && st[1] === b) {
|
424 | return;
|
425 | }
|
426 | }
|
427 |
|
428 |
|
429 | meet.push([a, b]);
|
430 |
|
431 |
|
432 | var key;
|
433 | for (key in b) {
|
434 | if (hasOwnProperty.call(b, key)) {
|
435 | if (hasOwnProperty.call(a, key)) {
|
436 | this.checkPropertyEquality(key);
|
437 | } else {
|
438 | this.collectFail(true, format(MISSING_KEY, "A", key));
|
439 | }
|
440 | }
|
441 | }
|
442 |
|
443 |
|
444 | for (key in a) {
|
445 | if (hasOwnProperty.call(a, key)) {
|
446 | this.collectFail(!hasOwnProperty.call(b, key), format(MISSING_KEY, "B", key));
|
447 | }
|
448 | }
|
449 |
|
450 | meet.pop();
|
451 |
|
452 | if (this.checkProtoEql) {
|
453 |
|
454 | this.collectFail(Object.getPrototypeOf(a) !== Object.getPrototypeOf(b), EQUALITY_PROTOTYPE, true);
|
455 | }
|
456 | },
|
457 |
|
458 | checkPropertyEquality: function(propertyName) {
|
459 | var _eq = new EQ(this, this.a[propertyName], this.b[propertyName], this.path.concat([propertyName]));
|
460 | _eq.check0();
|
461 | },
|
462 |
|
463 | defaultCheck: EQ.checkStrictEquality
|
464 | };
|
465 |
|
466 | EQ.add(getGlobalType.NUMBER, function(a, b) {
|
467 | this.collectFail((a !== a && b === b) || (b !== b && a === a) || (a !== b && a === a && b === b), EQUALITY);
|
468 | });
|
469 |
|
470 | [getGlobalType.SYMBOL, getGlobalType.BOOLEAN, getGlobalType.STRING].forEach(function(tp) {
|
471 | EQ.add(tp, EQ.checkStrictEquality);
|
472 | });
|
473 |
|
474 | EQ.add(getGlobalType.FUNCTION, function(a, b) {
|
475 |
|
476 | this.collectFail(a.toString() !== b.toString(), FUNCTION_SOURCES);
|
477 |
|
478 | this.checkPlainObjectsEquality(a, b);
|
479 | });
|
480 |
|
481 | EQ.add(getGlobalType.OBJECT, getGlobalType.REGEXP, function(a, b) {
|
482 |
|
483 | var flags = ["source", "global", "multiline", "lastIndex", "ignoreCase", "sticky", "unicode"];
|
484 | while (flags.length) {
|
485 | this.checkPropertyEquality(flags.shift());
|
486 | }
|
487 |
|
488 | this.checkPlainObjectsEquality(a, b);
|
489 | });
|
490 |
|
491 | EQ.add(getGlobalType.OBJECT, getGlobalType.DATE, function(a, b) {
|
492 |
|
493 | this.collectFail(+a !== +b, EQUALITY);
|
494 |
|
495 | this.checkPlainObjectsEquality(a, b);
|
496 | });
|
497 |
|
498 | [getGlobalType.NUMBER, getGlobalType.BOOLEAN, getGlobalType.STRING].forEach(function(tp) {
|
499 | EQ.add(getGlobalType.OBJECT, tp, function(a, b) {
|
500 |
|
501 | this.collectFail(a.valueOf() !== b.valueOf(), WRAPPED_VALUE);
|
502 |
|
503 | this.checkPlainObjectsEquality(a, b);
|
504 | });
|
505 | });
|
506 |
|
507 | EQ.add(getGlobalType.OBJECT, function(a, b) {
|
508 | this.checkPlainObjectsEquality(a, b);
|
509 | });
|
510 |
|
511 | [getGlobalType.ARRAY, getGlobalType.ARGUMENTS, getGlobalType.TYPED_ARRAY].forEach(function(tp) {
|
512 | EQ.add(getGlobalType.OBJECT, tp, function(a, b) {
|
513 | this.checkPropertyEquality("length");
|
514 |
|
515 | this.checkPlainObjectsEquality(a, b);
|
516 | });
|
517 | });
|
518 |
|
519 | EQ.add(getGlobalType.OBJECT, getGlobalType.ARRAY_BUFFER, function(a, b) {
|
520 | this.checkPropertyEquality("byteLength");
|
521 |
|
522 | this.checkPlainObjectsEquality(a, b);
|
523 | });
|
524 |
|
525 | EQ.add(getGlobalType.OBJECT, getGlobalType.ERROR, function(a, b) {
|
526 | this.checkPropertyEquality("name");
|
527 | this.checkPropertyEquality("message");
|
528 |
|
529 | this.checkPlainObjectsEquality(a, b);
|
530 | });
|
531 |
|
532 | EQ.add(getGlobalType.OBJECT, getGlobalType.BUFFER, function(a) {
|
533 | this.checkPropertyEquality("length");
|
534 |
|
535 | var l = a.length;
|
536 | while (l--) {
|
537 | this.checkPropertyEquality(l);
|
538 | }
|
539 |
|
540 |
|
541 |
|
542 | });
|
543 |
|
544 | function checkMapByKeys(a, b) {
|
545 | var iteratorA = a.keys();
|
546 |
|
547 | for (var nextA = iteratorA.next(); !nextA.done; nextA = iteratorA.next()) {
|
548 | var key = nextA.value;
|
549 | var hasKey = b.has(key);
|
550 | this.collectFail(!hasKey, format(SET_MAP_MISSING_KEY, key));
|
551 |
|
552 | if (hasKey) {
|
553 | var valueB = b.get(key);
|
554 | var valueA = a.get(key);
|
555 |
|
556 | eq(valueA, valueB, this);
|
557 | }
|
558 | }
|
559 | }
|
560 |
|
561 | function checkSetByKeys(a, b) {
|
562 | var iteratorA = a.keys();
|
563 |
|
564 | for (var nextA = iteratorA.next(); !nextA.done; nextA = iteratorA.next()) {
|
565 | var key = nextA.value;
|
566 | var hasKey = b.has(key);
|
567 | this.collectFail(!hasKey, format(SET_MAP_MISSING_KEY, key));
|
568 | }
|
569 | }
|
570 |
|
571 | EQ.add(getGlobalType.OBJECT, getGlobalType.MAP, function(a, b) {
|
572 | this._meet.push([a, b]);
|
573 |
|
574 | checkMapByKeys.call(this, a, b);
|
575 | checkMapByKeys.call(this, b, a);
|
576 |
|
577 | this._meet.pop();
|
578 |
|
579 | this.checkPlainObjectsEquality(a, b);
|
580 | });
|
581 | EQ.add(getGlobalType.OBJECT, getGlobalType.SET, function(a, b) {
|
582 | this._meet.push([a, b]);
|
583 |
|
584 | checkSetByKeys.call(this, a, b);
|
585 | checkSetByKeys.call(this, b, a);
|
586 |
|
587 | this._meet.pop();
|
588 |
|
589 | this.checkPlainObjectsEquality(a, b);
|
590 | });
|
591 |
|
592 | function eq(a, b, opts) {
|
593 | return new EQ(opts, a, b).check();
|
594 | }
|
595 |
|
596 | eq.EQ = EQ;
|
597 |
|
598 | var _hasOwnProperty = Object.prototype.hasOwnProperty;
|
599 | var _propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
|
600 |
|
601 | function hasOwnProperty$1(obj, key) {
|
602 | return _hasOwnProperty.call(obj, key);
|
603 | }
|
604 |
|
605 | function propertyIsEnumerable(obj, key) {
|
606 | return _propertyIsEnumerable.call(obj, key);
|
607 | }
|
608 |
|
609 | function merge(a, b) {
|
610 | if (a && b) {
|
611 | for (var key in b) {
|
612 | a[key] = b[key];
|
613 | }
|
614 | }
|
615 | return a;
|
616 | }
|
617 |
|
618 | function isIterator(obj) {
|
619 | if (!obj) {
|
620 | return false;
|
621 | }
|
622 |
|
623 | if (obj.__shouldIterator__) {
|
624 | return true;
|
625 | }
|
626 |
|
627 | return typeof obj.next === 'function' &&
|
628 | typeof Symbol === 'function' &&
|
629 | typeof Symbol.iterator === 'symbol' &&
|
630 | typeof obj[Symbol.iterator] === 'function' &&
|
631 | obj[Symbol.iterator]() === obj;
|
632 | }
|
633 |
|
634 |
|
635 | function isGeneratorFunction(f) {
|
636 | return typeof f === 'function' && /^function\s*\*\s*/.test(f.toString());
|
637 | }
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | function ObjectIterator(obj) {
|
643 | this._obj = obj;
|
644 | }
|
645 |
|
646 | ObjectIterator.prototype = {
|
647 | __shouldIterator__: true,
|
648 |
|
649 | next: function() {
|
650 | if (this._done) {
|
651 | throw new Error('Iterator already reached the end');
|
652 | }
|
653 |
|
654 | if (!this._keys) {
|
655 | this._keys = Object.keys(this._obj);
|
656 | this._index = 0;
|
657 | }
|
658 |
|
659 | var key = this._keys[this._index];
|
660 | this._done = this._index === this._keys.length;
|
661 | this._index += 1;
|
662 |
|
663 | return {
|
664 | value: this._done ? void 0: [key, this._obj[key]],
|
665 | done: this._done
|
666 | };
|
667 | }
|
668 | };
|
669 |
|
670 | if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
|
671 | ObjectIterator.prototype[Symbol.iterator] = function() {
|
672 | return this;
|
673 | };
|
674 | }
|
675 |
|
676 |
|
677 | function TypeAdaptorStorage() {
|
678 | this._typeAdaptors = [];
|
679 | this._iterableTypes = {};
|
680 | }
|
681 |
|
682 | TypeAdaptorStorage.prototype = {
|
683 | add: function(type, cls, sub, adaptor) {
|
684 | return this.addType(new getGlobalType.Type(type, cls, sub), adaptor);
|
685 | },
|
686 |
|
687 | addType: function(type, adaptor) {
|
688 | this._typeAdaptors[type.toString()] = adaptor;
|
689 | },
|
690 |
|
691 | getAdaptor: function(tp, funcName) {
|
692 | var tries = tp.toTryTypes();
|
693 | while (tries.length) {
|
694 | var toTry = tries.shift();
|
695 | var ad = this._typeAdaptors[toTry];
|
696 | if (ad && ad[funcName]) {
|
697 | return ad[funcName];
|
698 | }
|
699 | }
|
700 | },
|
701 |
|
702 | requireAdaptor: function(tp, funcName) {
|
703 | var a = this.getAdaptor(tp, funcName);
|
704 | if (!a) {
|
705 | throw new Error('There is no type adaptor `' + funcName + '` for ' + tp.toString());
|
706 | }
|
707 | return a;
|
708 | },
|
709 |
|
710 | addIterableType: function(tp) {
|
711 | this._iterableTypes[tp.toString()] = true;
|
712 | },
|
713 |
|
714 | isIterableType: function(tp) {
|
715 | return !!this._iterableTypes[tp.toString()];
|
716 | }
|
717 | };
|
718 |
|
719 | var defaultTypeAdaptorStorage = new TypeAdaptorStorage();
|
720 |
|
721 | var objectAdaptor = {
|
722 | forEach: function(obj, f, context) {
|
723 | for (var prop in obj) {
|
724 | if (hasOwnProperty$1(obj, prop) && propertyIsEnumerable(obj, prop)) {
|
725 | if (f.call(context, obj[prop], prop, obj) === false) {
|
726 | return;
|
727 | }
|
728 | }
|
729 | }
|
730 | },
|
731 |
|
732 | has: function(obj, prop) {
|
733 | return hasOwnProperty$1(obj, prop);
|
734 | },
|
735 |
|
736 | get: function(obj, prop) {
|
737 | return obj[prop];
|
738 | },
|
739 |
|
740 | iterator: function(obj) {
|
741 | return new ObjectIterator(obj);
|
742 | }
|
743 | };
|
744 |
|
745 |
|
746 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT), objectAdaptor);
|
747 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.FUNCTION), objectAdaptor);
|
748 |
|
749 | var mapAdaptor = {
|
750 | has: function(obj, key) {
|
751 | return obj.has(key);
|
752 | },
|
753 |
|
754 | get: function(obj, key) {
|
755 | return obj.get(key);
|
756 | },
|
757 |
|
758 | forEach: function(obj, f, context) {
|
759 | var iter = obj.entries();
|
760 | forEach(iter, function(value) {
|
761 | return f.call(context, value[1], value[0], obj);
|
762 | });
|
763 | },
|
764 |
|
765 | size: function(obj) {
|
766 | return obj.size;
|
767 | },
|
768 |
|
769 | isEmpty: function(obj) {
|
770 | return obj.size === 0;
|
771 | },
|
772 |
|
773 | iterator: function(obj) {
|
774 | return obj.entries();
|
775 | }
|
776 | };
|
777 |
|
778 | var setAdaptor = merge({}, mapAdaptor);
|
779 | setAdaptor.get = function(obj, key) {
|
780 | if (obj.has(key)) {
|
781 | return key;
|
782 | }
|
783 | };
|
784 | setAdaptor.iterator = function(obj) {
|
785 | return obj.values();
|
786 | };
|
787 |
|
788 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.MAP), mapAdaptor);
|
789 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SET), setAdaptor);
|
790 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_SET), setAdaptor);
|
791 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_MAP), mapAdaptor);
|
792 |
|
793 | defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.STRING), {
|
794 | isEmpty: function(obj) {
|
795 | return obj === '';
|
796 | },
|
797 |
|
798 | size: function(obj) {
|
799 | return obj.length;
|
800 | }
|
801 | });
|
802 |
|
803 | defaultTypeAdaptorStorage.addIterableType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARRAY));
|
804 | defaultTypeAdaptorStorage.addIterableType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARGUMENTS));
|
805 | defaultTypeAdaptorStorage.addIterableType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SET));
|
806 |
|
807 | function forEach(obj, f, context) {
|
808 | if (isGeneratorFunction(obj)) {
|
809 | return forEach(obj(), f, context);
|
810 | } else if (isIterator(obj)) {
|
811 | var value = obj.next();
|
812 | while (!value.done) {
|
813 | if (f.call(context, value.value, 'value', obj) === false) {
|
814 | return;
|
815 | }
|
816 | value = obj.next();
|
817 | }
|
818 | } else {
|
819 | var type = getGlobalType(obj);
|
820 | var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'forEach');
|
821 | func(obj, f, context);
|
822 | }
|
823 | }
|
824 |
|
825 |
|
826 | function size(obj) {
|
827 | var type = getGlobalType(obj);
|
828 | var func = defaultTypeAdaptorStorage.getAdaptor(type, 'size');
|
829 | if (func) {
|
830 | return func(obj);
|
831 | } else {
|
832 | var len = 0;
|
833 | forEach(obj, function() {
|
834 | len += 1;
|
835 | });
|
836 | return len;
|
837 | }
|
838 | }
|
839 |
|
840 | function isEmpty(obj) {
|
841 | var type = getGlobalType(obj);
|
842 | var func = defaultTypeAdaptorStorage.getAdaptor(type, 'isEmpty');
|
843 | if (func) {
|
844 | return func(obj);
|
845 | } else {
|
846 | var res = true;
|
847 | forEach(obj, function() {
|
848 | res = false;
|
849 | return false;
|
850 | });
|
851 | return res;
|
852 | }
|
853 | }
|
854 |
|
855 |
|
856 | function has(obj, key) {
|
857 | var type = getGlobalType(obj);
|
858 | var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'has');
|
859 | return func(obj, key);
|
860 | }
|
861 |
|
862 |
|
863 | function get(obj, key) {
|
864 | var type = getGlobalType(obj);
|
865 | var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'get');
|
866 | return func(obj, key);
|
867 | }
|
868 |
|
869 | function some(obj, f, context) {
|
870 | var res = false;
|
871 | forEach(obj, function(value, key) {
|
872 | if (f.call(context, value, key, obj)) {
|
873 | res = true;
|
874 | return false;
|
875 | }
|
876 | }, context);
|
877 | return res;
|
878 | }
|
879 |
|
880 | function isIterable(obj) {
|
881 | return defaultTypeAdaptorStorage.isIterableType(getGlobalType(obj));
|
882 | }
|
883 |
|
884 | function iterator(obj) {
|
885 | return defaultTypeAdaptorStorage.requireAdaptor(getGlobalType(obj), 'iterator')(obj);
|
886 | }
|
887 |
|
888 | function looksLikeANumber(n) {
|
889 | return !!n.match(/\d+/);
|
890 | }
|
891 |
|
892 | function keyCompare(a, b) {
|
893 | var aNum = looksLikeANumber(a);
|
894 | var bNum = looksLikeANumber(b);
|
895 | if (aNum && bNum) {
|
896 | return 1*a - 1*b;
|
897 | } else if (aNum && !bNum) {
|
898 | return -1;
|
899 | } else if (!aNum && bNum) {
|
900 | return 1;
|
901 | } else {
|
902 | return a.localeCompare(b);
|
903 | }
|
904 | }
|
905 |
|
906 | function genKeysFunc(f) {
|
907 | return function(value) {
|
908 | var k = f(value);
|
909 | k.sort(keyCompare);
|
910 | return k;
|
911 | };
|
912 | }
|
913 |
|
914 | function Formatter(opts) {
|
915 | opts = opts || {};
|
916 |
|
917 | this.seen = [];
|
918 |
|
919 | var keysFunc;
|
920 | if (typeof opts.keysFunc === 'function') {
|
921 | keysFunc = opts.keysFunc;
|
922 | } else if (opts.keys === false) {
|
923 | keysFunc = Object.getOwnPropertyNames;
|
924 | } else {
|
925 | keysFunc = Object.keys;
|
926 | }
|
927 |
|
928 | this.getKeys = genKeysFunc(keysFunc);
|
929 |
|
930 | this.maxLineLength = typeof opts.maxLineLength === 'number' ? opts.maxLineLength : 60;
|
931 | this.propSep = opts.propSep || ',';
|
932 |
|
933 | this.isUTCdate = !!opts.isUTCdate;
|
934 | }
|
935 |
|
936 |
|
937 |
|
938 | Formatter.prototype = {
|
939 | constructor: Formatter,
|
940 |
|
941 | format: function(value) {
|
942 | var tp = getGlobalType(value);
|
943 |
|
944 | if (this.alreadySeen(value)) {
|
945 | return '[Circular]';
|
946 | }
|
947 |
|
948 | var tries = tp.toTryTypes();
|
949 | var f = this.defaultFormat;
|
950 | while (tries.length) {
|
951 | var toTry = tries.shift();
|
952 | var name = Formatter.formatterFunctionName(toTry);
|
953 | if (this[name]) {
|
954 | f = this[name];
|
955 | break;
|
956 | }
|
957 | }
|
958 | return f.call(this, value).trim();
|
959 | },
|
960 |
|
961 | defaultFormat: function(obj) {
|
962 | return String(obj);
|
963 | },
|
964 |
|
965 | alreadySeen: function(value) {
|
966 | return this.seen.indexOf(value) >= 0;
|
967 | }
|
968 |
|
969 | };
|
970 |
|
971 | Formatter.addType = function addType(tp, f) {
|
972 | Formatter.prototype[Formatter.formatterFunctionName(tp)] = f;
|
973 | };
|
974 |
|
975 | Formatter.formatterFunctionName = function formatterFunctionName(tp) {
|
976 | return '_format_' + tp.toString('_');
|
977 | };
|
978 |
|
979 | var EOL = '\n';
|
980 |
|
981 | function indent(v, indentation) {
|
982 | return v
|
983 | .split(EOL)
|
984 | .map(function(vv) {
|
985 | return indentation + vv;
|
986 | })
|
987 | .join(EOL);
|
988 | }
|
989 |
|
990 | function pad(str, value, filler) {
|
991 | str = String(str);
|
992 | var isRight = false;
|
993 |
|
994 | if (value < 0) {
|
995 | isRight = true;
|
996 | value = -value;
|
997 | }
|
998 |
|
999 | if (str.length < value) {
|
1000 | var padding = new Array(value - str.length + 1).join(filler);
|
1001 | return isRight ? str + padding : padding + str;
|
1002 | } else {
|
1003 | return str;
|
1004 | }
|
1005 | }
|
1006 |
|
1007 | function pad0(str, value) {
|
1008 | return pad(str, value, '0');
|
1009 | }
|
1010 |
|
1011 | var functionNameRE = /^\s*function\s*(\S*)\s*\(/;
|
1012 |
|
1013 | function functionName(f) {
|
1014 | if (f.name) {
|
1015 | return f.name;
|
1016 | }
|
1017 | var matches = f.toString().match(functionNameRE);
|
1018 | if (matches === null) {
|
1019 |
|
1020 | return '';
|
1021 | }
|
1022 | var name = matches[1];
|
1023 | return name;
|
1024 | }
|
1025 |
|
1026 | function constructorName(obj) {
|
1027 | while (obj) {
|
1028 | var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
|
1029 | if (descriptor !== undefined && typeof descriptor.value === 'function') {
|
1030 | var name = functionName(descriptor.value);
|
1031 | if (name !== '') {
|
1032 | return name;
|
1033 | }
|
1034 | }
|
1035 |
|
1036 | obj = Object.getPrototypeOf(obj);
|
1037 | }
|
1038 | }
|
1039 |
|
1040 | var INDENT = ' ';
|
1041 |
|
1042 | function addSpaces(str) {
|
1043 | return indent(str, INDENT);
|
1044 | }
|
1045 |
|
1046 | function typeAdaptorForEachFormat(obj, opts) {
|
1047 | opts = opts || {};
|
1048 | var filterKey = opts.filterKey || function() { return true; };
|
1049 |
|
1050 | var formatKey = opts.formatKey || this.format;
|
1051 | var formatValue = opts.formatValue || this.format;
|
1052 |
|
1053 | var keyValueSep = typeof opts.keyValueSep !== 'undefined' ? opts.keyValueSep : ': ';
|
1054 |
|
1055 | this.seen.push(obj);
|
1056 |
|
1057 | var formatLength = 0;
|
1058 | var pairs = [];
|
1059 |
|
1060 | forEach(obj, function(value, key) {
|
1061 | if (!filterKey(key)) {
|
1062 | return;
|
1063 | }
|
1064 |
|
1065 | var formattedKey = formatKey.call(this, key);
|
1066 | var formattedValue = formatValue.call(this, value, key);
|
1067 |
|
1068 | var pair = formattedKey ? (formattedKey + keyValueSep + formattedValue) : formattedValue;
|
1069 |
|
1070 | formatLength += pair.length;
|
1071 | pairs.push(pair);
|
1072 | }, this);
|
1073 |
|
1074 | this.seen.pop();
|
1075 |
|
1076 | (opts.additionalKeys || []).forEach(function(keyValue) {
|
1077 | var pair = keyValue[0] + keyValueSep + this.format(keyValue[1]);
|
1078 | formatLength += pair.length;
|
1079 | pairs.push(pair);
|
1080 | }, this);
|
1081 |
|
1082 | var prefix = opts.prefix || constructorName(obj) || '';
|
1083 | if (prefix.length > 0) {
|
1084 | prefix += ' ';
|
1085 | }
|
1086 |
|
1087 | var lbracket, rbracket;
|
1088 | if (Array.isArray(opts.brackets)) {
|
1089 | lbracket = opts.brackets[0];
|
1090 | rbracket = opts.brackets[1];
|
1091 | } else {
|
1092 | lbracket = '{';
|
1093 | rbracket = '}';
|
1094 | }
|
1095 |
|
1096 | var rootValue = opts.value || '';
|
1097 |
|
1098 | if (pairs.length === 0) {
|
1099 | return rootValue || (prefix + lbracket + rbracket);
|
1100 | }
|
1101 |
|
1102 | if (formatLength <= this.maxLineLength) {
|
1103 | return prefix + lbracket + ' ' + (rootValue ? rootValue + ' ' : '') + pairs.join(this.propSep + ' ') + ' ' + rbracket;
|
1104 | } else {
|
1105 | return prefix + lbracket + '\n' + (rootValue ? ' ' + rootValue + '\n' : '') + pairs.map(addSpaces).join(this.propSep + '\n') + '\n' + rbracket;
|
1106 | }
|
1107 | }
|
1108 |
|
1109 | function formatPlainObjectKey(key) {
|
1110 | return typeof key === 'string' && key.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*$/) ? key : this.format(key);
|
1111 | }
|
1112 |
|
1113 | function getPropertyDescriptor(obj, key) {
|
1114 | var desc;
|
1115 | try {
|
1116 | desc = Object.getOwnPropertyDescriptor(obj, key) || { value: obj[key] };
|
1117 | } catch (e) {
|
1118 | desc = { value: e };
|
1119 | }
|
1120 | return desc;
|
1121 | }
|
1122 |
|
1123 | function formatPlainObjectValue(obj, key) {
|
1124 | var desc = getPropertyDescriptor(obj, key);
|
1125 | if (desc.get && desc.set) {
|
1126 | return '[Getter/Setter]';
|
1127 | }
|
1128 | if (desc.get) {
|
1129 | return '[Getter]';
|
1130 | }
|
1131 | if (desc.set) {
|
1132 | return '[Setter]';
|
1133 | }
|
1134 |
|
1135 | return this.format(desc.value);
|
1136 | }
|
1137 |
|
1138 | function formatPlainObject(obj, opts) {
|
1139 | opts = opts || {};
|
1140 | opts.keyValueSep = ': ';
|
1141 | opts.formatKey = opts.formatKey || formatPlainObjectKey;
|
1142 | opts.formatValue = opts.formatValue || function(value, key) {
|
1143 | return formatPlainObjectValue.call(this, obj, key);
|
1144 | };
|
1145 | return typeAdaptorForEachFormat.call(this, obj, opts);
|
1146 | }
|
1147 |
|
1148 | function formatWrapper1(value) {
|
1149 | return formatPlainObject.call(this, value, {
|
1150 | additionalKeys: [['[[PrimitiveValue]]', value.valueOf()]]
|
1151 | });
|
1152 | }
|
1153 |
|
1154 |
|
1155 | function formatWrapper2(value) {
|
1156 | var realValue = value.valueOf();
|
1157 |
|
1158 | return formatPlainObject.call(this, value, {
|
1159 | filterKey: function(key) {
|
1160 |
|
1161 | return !(key.match(/\d+/) && parseInt(key, 10) < realValue.length);
|
1162 | },
|
1163 | additionalKeys: [['[[PrimitiveValue]]', realValue]]
|
1164 | });
|
1165 | }
|
1166 |
|
1167 | function formatRegExp(value) {
|
1168 | return formatPlainObject.call(this, value, {
|
1169 | value: String(value)
|
1170 | });
|
1171 | }
|
1172 |
|
1173 | function formatFunction(value) {
|
1174 | return formatPlainObject.call(this, value, {
|
1175 | prefix: 'Function',
|
1176 | additionalKeys: [['name', functionName(value)]]
|
1177 | });
|
1178 | }
|
1179 |
|
1180 | function formatArray(value) {
|
1181 | return formatPlainObject.call(this, value, {
|
1182 | formatKey: function(key) {
|
1183 | if (!key.match(/\d+/)) {
|
1184 | return formatPlainObjectKey.call(this, key);
|
1185 | }
|
1186 | },
|
1187 | brackets: ['[', ']']
|
1188 | });
|
1189 | }
|
1190 |
|
1191 | function formatArguments(value) {
|
1192 | return formatPlainObject.call(this, value, {
|
1193 | formatKey: function(key) {
|
1194 | if (!key.match(/\d+/)) {
|
1195 | return formatPlainObjectKey.call(this, key);
|
1196 | }
|
1197 | },
|
1198 | brackets: ['[', ']'],
|
1199 | prefix: 'Arguments'
|
1200 | });
|
1201 | }
|
1202 |
|
1203 | function _formatDate(value, isUTC) {
|
1204 | var prefix = isUTC ? 'UTC' : '';
|
1205 |
|
1206 | var date = value['get' + prefix + 'FullYear']() +
|
1207 | '-' +
|
1208 | pad0(value['get' + prefix + 'Month']() + 1, 2) +
|
1209 | '-' +
|
1210 | pad0(value['get' + prefix + 'Date'](), 2);
|
1211 |
|
1212 | var time = pad0(value['get' + prefix + 'Hours'](), 2) +
|
1213 | ':' +
|
1214 | pad0(value['get' + prefix + 'Minutes'](), 2) +
|
1215 | ':' +
|
1216 | pad0(value['get' + prefix + 'Seconds'](), 2) +
|
1217 | '.' +
|
1218 | pad0(value['get' + prefix + 'Milliseconds'](), 3);
|
1219 |
|
1220 | var to = value.getTimezoneOffset();
|
1221 | var absTo = Math.abs(to);
|
1222 | var hours = Math.floor(absTo / 60);
|
1223 | var minutes = absTo - hours * 60;
|
1224 | var tzFormat = (to < 0 ? '+' : '-') + pad0(hours, 2) + pad0(minutes, 2);
|
1225 |
|
1226 | return date + ' ' + time + (isUTC ? '' : ' ' + tzFormat);
|
1227 | }
|
1228 |
|
1229 | function formatDate(value) {
|
1230 | return formatPlainObject.call(this, value, { value: _formatDate(value, this.isUTCdate) });
|
1231 | }
|
1232 |
|
1233 | function formatError(value) {
|
1234 | return formatPlainObject.call(this, value, {
|
1235 | prefix: value.name,
|
1236 | additionalKeys: [['message', value.message]]
|
1237 | });
|
1238 | }
|
1239 |
|
1240 | function generateFormatForNumberArray(lengthProp, name, padding) {
|
1241 | return function(value) {
|
1242 | var max = this.byteArrayMaxLength || 50;
|
1243 | var length = value[lengthProp];
|
1244 | var formattedValues = [];
|
1245 | var len = 0;
|
1246 | for (var i = 0; i < max && i < length; i++) {
|
1247 | var b = value[i] || 0;
|
1248 | var v = pad0(b.toString(16), padding);
|
1249 | len += v.length;
|
1250 | formattedValues.push(v);
|
1251 | }
|
1252 | var prefix = value.constructor.name || name || '';
|
1253 | if (prefix) {
|
1254 | prefix += ' ';
|
1255 | }
|
1256 |
|
1257 | if (formattedValues.length === 0) {
|
1258 | return prefix + '[]';
|
1259 | }
|
1260 |
|
1261 | if (len <= this.maxLineLength) {
|
1262 | return prefix + '[ ' + formattedValues.join(this.propSep + ' ') + ' ' + ']';
|
1263 | } else {
|
1264 | return prefix + '[\n' + formattedValues.map(addSpaces).join(this.propSep + '\n') + '\n' + ']';
|
1265 | }
|
1266 | };
|
1267 | }
|
1268 |
|
1269 | function formatMap(obj) {
|
1270 | return typeAdaptorForEachFormat.call(this, obj, {
|
1271 | keyValueSep: ' => '
|
1272 | });
|
1273 | }
|
1274 |
|
1275 | function formatSet(obj) {
|
1276 | return typeAdaptorForEachFormat.call(this, obj, {
|
1277 | keyValueSep: '',
|
1278 | formatKey: function() { return ''; }
|
1279 | });
|
1280 | }
|
1281 |
|
1282 | function genSimdVectorFormat(constructorName, length) {
|
1283 | return function(value) {
|
1284 | var Constructor = value.constructor;
|
1285 | var extractLane = Constructor.extractLane;
|
1286 |
|
1287 | var len = 0;
|
1288 | var props = [];
|
1289 |
|
1290 | for (var i = 0; i < length; i ++) {
|
1291 | var key = this.format(extractLane(value, i));
|
1292 | len += key.length;
|
1293 | props.push(key);
|
1294 | }
|
1295 |
|
1296 | if (len <= this.maxLineLength) {
|
1297 | return constructorName + ' [ ' + props.join(this.propSep + ' ') + ' ]';
|
1298 | } else {
|
1299 | return constructorName + ' [\n' + props.map(addSpaces).join(this.propSep + '\n') + '\n' + ']';
|
1300 | }
|
1301 | };
|
1302 | }
|
1303 |
|
1304 | function defaultFormat(value, opts) {
|
1305 | return new Formatter(opts).format(value);
|
1306 | }
|
1307 |
|
1308 | defaultFormat.Formatter = Formatter;
|
1309 | defaultFormat.addSpaces = addSpaces;
|
1310 | defaultFormat.pad0 = pad0;
|
1311 | defaultFormat.functionName = functionName;
|
1312 | defaultFormat.constructorName = constructorName;
|
1313 | defaultFormat.formatPlainObjectKey = formatPlainObjectKey;
|
1314 | defaultFormat.formatPlainObject = formatPlainObject;
|
1315 | defaultFormat.typeAdaptorForEachFormat = typeAdaptorForEachFormat;
|
1316 |
|
1317 | Formatter.addType(new getGlobalType.Type(getGlobalType.UNDEFINED), function() {
|
1318 | return 'undefined';
|
1319 | });
|
1320 | Formatter.addType(new getGlobalType.Type(getGlobalType.NULL), function() {
|
1321 | return 'null';
|
1322 | });
|
1323 | Formatter.addType(new getGlobalType.Type(getGlobalType.BOOLEAN), function(value) {
|
1324 | return value ? 'true': 'false';
|
1325 | });
|
1326 | Formatter.addType(new getGlobalType.Type(getGlobalType.SYMBOL), function(value) {
|
1327 | return value.toString();
|
1328 | });
|
1329 | Formatter.addType(new getGlobalType.Type(getGlobalType.NUMBER), function(value) {
|
1330 | if (value === 0 && 1 / value < 0) {
|
1331 | return '-0';
|
1332 | }
|
1333 | return String(value);
|
1334 | });
|
1335 |
|
1336 | Formatter.addType(new getGlobalType.Type(getGlobalType.STRING), function(value) {
|
1337 | return '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
1338 | .replace(/'/g, "\\'")
|
1339 | .replace(/\\"/g, '"') + '\'';
|
1340 | });
|
1341 |
|
1342 | Formatter.addType(new getGlobalType.Type(getGlobalType.FUNCTION), formatFunction);
|
1343 |
|
1344 |
|
1345 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT), formatPlainObject);
|
1346 |
|
1347 |
|
1348 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.NUMBER), formatWrapper1);
|
1349 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.BOOLEAN), formatWrapper1);
|
1350 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.STRING), formatWrapper2);
|
1351 |
|
1352 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.REGEXP), formatRegExp);
|
1353 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARRAY), formatArray);
|
1354 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARGUMENTS), formatArguments);
|
1355 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.DATE), formatDate);
|
1356 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ERROR), formatError);
|
1357 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SET), formatSet);
|
1358 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.MAP), formatMap);
|
1359 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_MAP), formatMap);
|
1360 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_SET), formatSet);
|
1361 |
|
1362 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.BUFFER), generateFormatForNumberArray('length', 'Buffer', 2));
|
1363 |
|
1364 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARRAY_BUFFER), generateFormatForNumberArray('byteLength', 'ArrayBuffer', 2));
|
1365 |
|
1366 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'int8'), generateFormatForNumberArray('length', 'Int8Array', 2));
|
1367 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'uint8'), generateFormatForNumberArray('length', 'Uint8Array', 2));
|
1368 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'uint8clamped'), generateFormatForNumberArray('length', 'Uint8ClampedArray', 2));
|
1369 |
|
1370 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'int16'), generateFormatForNumberArray('length', 'Int16Array', 4));
|
1371 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'uint16'), generateFormatForNumberArray('length', 'Uint16Array', 4));
|
1372 |
|
1373 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'int32'), generateFormatForNumberArray('length', 'Int32Array', 8));
|
1374 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.TYPED_ARRAY, 'uint32'), generateFormatForNumberArray('length', 'Uint32Array', 8));
|
1375 |
|
1376 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'bool16x8'), genSimdVectorFormat('Bool16x8', 8));
|
1377 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'bool32x4'), genSimdVectorFormat('Bool32x4', 4));
|
1378 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'bool8x16'), genSimdVectorFormat('Bool8x16', 16));
|
1379 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'float32x4'), genSimdVectorFormat('Float32x4', 4));
|
1380 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'int16x8'), genSimdVectorFormat('Int16x8', 8));
|
1381 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'int32x4'), genSimdVectorFormat('Int32x4', 4));
|
1382 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'int8x16'), genSimdVectorFormat('Int8x16', 16));
|
1383 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'uint16x8'), genSimdVectorFormat('Uint16x8', 8));
|
1384 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'uint32x4'), genSimdVectorFormat('Uint32x4', 4));
|
1385 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SIMD, 'uint8x16'), genSimdVectorFormat('Uint8x16', 16));
|
1386 |
|
1387 |
|
1388 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.PROMISE), function() {
|
1389 | return '[Promise]';
|
1390 | });
|
1391 |
|
1392 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.XHR), function() {
|
1393 | return '[XMLHttpRequest]';
|
1394 | });
|
1395 |
|
1396 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.HTML_ELEMENT), function(value) {
|
1397 | return value.outerHTML;
|
1398 | });
|
1399 |
|
1400 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.HTML_ELEMENT, '#text'), function(value) {
|
1401 | return value.nodeValue;
|
1402 | });
|
1403 |
|
1404 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.HTML_ELEMENT, '#document'), function(value) {
|
1405 | return value.documentElement.outerHTML;
|
1406 | });
|
1407 |
|
1408 | Formatter.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.HOST), function() {
|
1409 | return '[Host]';
|
1410 | });
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 | function isWrapperType(obj) {
|
1419 | return obj instanceof Number || obj instanceof String || obj instanceof Boolean;
|
1420 | }
|
1421 |
|
1422 |
|
1423 | function convertPropertyName(name) {
|
1424 | return typeof name === "symbol" ? name : String(name);
|
1425 | }
|
1426 |
|
1427 | var functionName$1 = defaultFormat.functionName;
|
1428 |
|
1429 | function isPlainObject(obj) {
|
1430 | if (typeof obj == "object" && obj !== null) {
|
1431 | var proto = Object.getPrototypeOf(obj);
|
1432 | return proto === Object.prototype || proto === null;
|
1433 | }
|
1434 |
|
1435 | return false;
|
1436 | }
|
1437 |
|
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 |
|
1445 | var config = {
|
1446 | typeAdaptors: defaultTypeAdaptorStorage,
|
1447 |
|
1448 | getFormatter: function(opts) {
|
1449 | return new defaultFormat.Formatter(opts || config);
|
1450 | }
|
1451 | };
|
1452 |
|
1453 | function format$2(value, opts) {
|
1454 | return config.getFormatter(opts).format(value);
|
1455 | }
|
1456 |
|
1457 | function formatProp(value) {
|
1458 | var formatter = config.getFormatter();
|
1459 | return defaultFormat.formatPlainObjectKey.call(formatter, value);
|
1460 | }
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 |
|
1475 | function AssertionError(options) {
|
1476 | merge(this, options);
|
1477 |
|
1478 | if (!options.message) {
|
1479 | Object.defineProperty(this, "message", {
|
1480 | get: function() {
|
1481 | if (!this._message) {
|
1482 | this._message = this.generateMessage();
|
1483 | this.generatedMessage = true;
|
1484 | }
|
1485 | return this._message;
|
1486 | },
|
1487 | configurable: true,
|
1488 | enumerable: false
|
1489 | });
|
1490 | }
|
1491 |
|
1492 | if (Error.captureStackTrace) {
|
1493 | Error.captureStackTrace(this, this.stackStartFunction);
|
1494 | } else {
|
1495 |
|
1496 | var err = new Error();
|
1497 | if (err.stack) {
|
1498 | var out = err.stack;
|
1499 |
|
1500 | if (this.stackStartFunction) {
|
1501 |
|
1502 | var fn_name = functionName$1(this.stackStartFunction);
|
1503 | var idx = out.indexOf("\n" + fn_name);
|
1504 | if (idx >= 0) {
|
1505 |
|
1506 |
|
1507 | var next_line = out.indexOf("\n", idx + 1);
|
1508 | out = out.substring(next_line + 1);
|
1509 | }
|
1510 | }
|
1511 |
|
1512 | this.stack = out;
|
1513 | }
|
1514 | }
|
1515 | }
|
1516 |
|
1517 | var indent$1 = " ";
|
1518 | function prependIndent(line) {
|
1519 | return indent$1 + line;
|
1520 | }
|
1521 |
|
1522 | function indentLines(text) {
|
1523 | return text
|
1524 | .split("\n")
|
1525 | .map(prependIndent)
|
1526 | .join("\n");
|
1527 | }
|
1528 |
|
1529 |
|
1530 | AssertionError.prototype = Object.create(Error.prototype, {
|
1531 | name: {
|
1532 | value: "AssertionError"
|
1533 | },
|
1534 |
|
1535 | generateMessage: {
|
1536 | value: function() {
|
1537 | if (!this.operator && this.previous) {
|
1538 | return this.previous.message;
|
1539 | }
|
1540 | var actual = format$2(this.actual);
|
1541 | var expected = "expected" in this ? " " + format$2(this.expected) : "";
|
1542 | var details =
|
1543 | "details" in this && this.details ? " (" + this.details + ")" : "";
|
1544 |
|
1545 | var previous = this.previous
|
1546 | ? "\n" + indentLines(this.previous.message)
|
1547 | : "";
|
1548 |
|
1549 | return (
|
1550 | "expected " +
|
1551 | actual +
|
1552 | (this.negate ? " not " : " ") +
|
1553 | this.operator +
|
1554 | expected +
|
1555 | details +
|
1556 | previous
|
1557 | );
|
1558 | }
|
1559 | }
|
1560 | });
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 | function LightAssertionError(options) {
|
1571 | merge(this, options);
|
1572 |
|
1573 | if (!options.message) {
|
1574 | Object.defineProperty(this, "message", {
|
1575 | get: function() {
|
1576 | if (!this._message) {
|
1577 | this._message = this.generateMessage();
|
1578 | this.generatedMessage = true;
|
1579 | }
|
1580 | return this._message;
|
1581 | }
|
1582 | });
|
1583 | }
|
1584 | }
|
1585 |
|
1586 | LightAssertionError.prototype = {
|
1587 | generateMessage: AssertionError.prototype.generateMessage
|
1588 | };
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 | function Assertion(obj) {
|
1598 | this.obj = obj;
|
1599 |
|
1600 | this.anyOne = false;
|
1601 | this.negate = false;
|
1602 |
|
1603 | this.params = { actual: obj };
|
1604 | }
|
1605 |
|
1606 | Assertion.prototype = {
|
1607 | constructor: Assertion,
|
1608 |
|
1609 | /**
|
1610 | * Base method for assertions.
|
1611 | *
|
1612 | * Before calling this method need to fill Assertion#params object. This method usually called from other assertion methods.
|
1613 | * `Assertion#params` can contain such properties:
|
1614 | * * `operator` - required string containing description of this assertion
|
1615 | * * `obj` - optional replacement for this.obj, it is useful if you prepare more clear object then given
|
1616 | * * `message` - if this property filled with string any others will be ignored and this one used as assertion message
|
1617 | * * `expected` - any object used when you need to assert relation between given object and expected. Like given == expected (== is a relation)
|
1618 | * * `details` - additional string with details to generated message
|
1619 | *
|
1620 | * @memberOf Assertion
|
1621 | * @category assertion
|
1622 | * @param {*} expr Any expression that will be used as a condition for asserting.
|
1623 | * @example
|
1624 | *
|
1625 | * var a = new should.Assertion(42);
|
1626 | *
|
1627 | * a.params = {
|
1628 | * operator: 'to be magic number',
|
1629 | * }
|
1630 | *
|
1631 | * a.assert(false);
|
1632 | *
|
1633 | */
|
1634 | assert: function(expr) {
|
1635 | if (expr) {
|
1636 | return this;
|
1637 | }
|
1638 |
|
1639 | var params = this.params;
|
1640 |
|
1641 | if ("obj" in params && !("actual" in params)) {
|
1642 | params.actual = params.obj;
|
1643 | } else if (!("obj" in params) && !("actual" in params)) {
|
1644 | params.actual = this.obj;
|
1645 | }
|
1646 |
|
1647 | params.stackStartFunction = params.stackStartFunction || this.assert;
|
1648 | params.negate = this.negate;
|
1649 |
|
1650 | params.assertion = this;
|
1651 |
|
1652 | if (this.light) {
|
1653 | throw new LightAssertionError(params);
|
1654 | } else {
|
1655 | throw new AssertionError(params);
|
1656 | }
|
1657 | },
|
1658 |
|
1659 | |
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
1672 |
|
1673 |
|
1674 |
|
1675 | fail: function() {
|
1676 | return this.assert(false);
|
1677 | },
|
1678 |
|
1679 | assertZeroArguments: function(args) {
|
1680 | if (args.length !== 0) {
|
1681 | throw new TypeError("This assertion does not expect any arguments. You may need to check your code");
|
1682 | }
|
1683 | }
|
1684 | };
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 | function PromisedAssertion(/* obj */) {
|
1693 | Assertion.apply(this, arguments);
|
1694 | }
|
1695 |
|
1696 |
|
1697 |
|
1698 |
|
1699 |
|
1700 |
|
1701 |
|
1702 | PromisedAssertion.prototype.then = function(resolve, reject) {
|
1703 | return this.obj.then(resolve, reject);
|
1704 | };
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 |
|
1712 |
|
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 |
|
1720 |
|
1721 |
|
1722 |
|
1723 |
|
1724 |
|
1725 |
|
1726 | Assertion.add = function(name, func) {
|
1727 | Object.defineProperty(Assertion.prototype, name, {
|
1728 | enumerable: true,
|
1729 | configurable: true,
|
1730 | value: function() {
|
1731 | var context = new Assertion(this.obj, this, name);
|
1732 | context.anyOne = this.anyOne;
|
1733 | context.onlyThis = this.onlyThis;
|
1734 |
|
1735 | context.light = true;
|
1736 |
|
1737 | try {
|
1738 | func.apply(context, arguments);
|
1739 | } catch (e) {
|
1740 |
|
1741 | if (e instanceof AssertionError || e instanceof LightAssertionError) {
|
1742 |
|
1743 | if (this.negate) {
|
1744 | this.obj = context.obj;
|
1745 | this.negate = false;
|
1746 | return this;
|
1747 | }
|
1748 |
|
1749 | if (context !== e.assertion) {
|
1750 | context.params.previous = e;
|
1751 | }
|
1752 |
|
1753 |
|
1754 | context.negate = false;
|
1755 |
|
1756 | context.light = false;
|
1757 | context.fail();
|
1758 | }
|
1759 |
|
1760 | throw e;
|
1761 | }
|
1762 |
|
1763 |
|
1764 | if (this.negate) {
|
1765 | context.negate = true;
|
1766 | context.params.details = "false negative fail";
|
1767 |
|
1768 | context.light = false;
|
1769 | context.fail();
|
1770 | }
|
1771 |
|
1772 |
|
1773 | if (!this.params.operator) {
|
1774 | this.params = context.params;
|
1775 | }
|
1776 | this.obj = context.obj;
|
1777 | this.negate = false;
|
1778 | return this;
|
1779 | }
|
1780 | });
|
1781 |
|
1782 | Object.defineProperty(PromisedAssertion.prototype, name, {
|
1783 | enumerable: true,
|
1784 | configurable: true,
|
1785 | value: function() {
|
1786 | var args = arguments;
|
1787 | this.obj = this.obj.then(function(a) {
|
1788 | return a[name].apply(a, args);
|
1789 | });
|
1790 |
|
1791 | return this;
|
1792 | }
|
1793 | });
|
1794 | };
|
1795 |
|
1796 |
|
1797 |
|
1798 |
|
1799 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 | Assertion.addChain = function(name, onCall) {
|
1805 | onCall = onCall || function() {};
|
1806 | Object.defineProperty(Assertion.prototype, name, {
|
1807 | get: function() {
|
1808 | onCall.call(this);
|
1809 | return this;
|
1810 | },
|
1811 | enumerable: true
|
1812 | });
|
1813 |
|
1814 | Object.defineProperty(PromisedAssertion.prototype, name, {
|
1815 | enumerable: true,
|
1816 | configurable: true,
|
1817 | get: function() {
|
1818 | this.obj = this.obj.then(function(a) {
|
1819 | return a[name];
|
1820 | });
|
1821 |
|
1822 | return this;
|
1823 | }
|
1824 | });
|
1825 | };
|
1826 |
|
1827 |
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 | Assertion.alias = function(from, to) {
|
1839 | var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
|
1840 | if (!desc) {
|
1841 | throw new Error("Alias " + from + " -> " + to + " could not be created as " + from + " not defined");
|
1842 | }
|
1843 | Object.defineProperty(Assertion.prototype, to, desc);
|
1844 |
|
1845 | var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from);
|
1846 | if (desc2) {
|
1847 | Object.defineProperty(PromisedAssertion.prototype, to, desc2);
|
1848 | }
|
1849 | };
|
1850 |
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 | Assertion.addChain("not", function() {
|
1859 | this.negate = !this.negate;
|
1860 | });
|
1861 |
|
1862 |
|
1863 |
|
1864 |
|
1865 |
|
1866 |
|
1867 |
|
1868 |
|
1869 |
|
1870 | Assertion.addChain("any", function() {
|
1871 | this.anyOne = true;
|
1872 | });
|
1873 |
|
1874 |
|
1875 |
|
1876 |
|
1877 |
|
1878 |
|
1879 |
|
1880 |
|
1881 |
|
1882 | Assertion.addChain("only", function() {
|
1883 | this.onlyThis = true;
|
1884 | });
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 |
|
1891 |
|
1892 |
|
1893 |
|
1894 |
|
1895 |
|
1896 |
|
1897 |
|
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 |
|
1907 |
|
1908 |
|
1909 |
|
1910 |
|
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 | var pSlice = Array.prototype.slice;
|
1916 |
|
1917 |
|
1918 |
|
1919 |
|
1920 |
|
1921 | var assert = ok;
|
1922 |
|
1923 |
|
1924 |
|
1925 |
|
1926 |
|
1927 |
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 |
|
1937 | function fail(actual, expected, message, operator, stackStartFunction) {
|
1938 | var a = new Assertion(actual);
|
1939 | a.params = {
|
1940 | operator: operator,
|
1941 | expected: expected,
|
1942 | message: message,
|
1943 | stackStartFunction: stackStartFunction || fail
|
1944 | };
|
1945 |
|
1946 | a.fail();
|
1947 | }
|
1948 |
|
1949 |
|
1950 | assert.fail = fail;
|
1951 |
|
1952 |
|
1953 |
|
1954 |
|
1955 |
|
1956 |
|
1957 |
|
1958 |
|
1959 |
|
1960 |
|
1961 |
|
1962 |
|
1963 |
|
1964 |
|
1965 |
|
1966 | function ok(value, message) {
|
1967 | if (!value) {
|
1968 | fail(value, true, message, "==", assert.ok);
|
1969 | }
|
1970 | }
|
1971 | assert.ok = ok;
|
1972 |
|
1973 |
|
1974 |
|
1975 |
|
1976 |
|
1977 |
|
1978 |
|
1979 |
|
1980 |
|
1981 |
|
1982 |
|
1983 |
|
1984 |
|
1985 |
|
1986 | assert.equal = function equal(actual, expected, message) {
|
1987 | if (actual != expected) {
|
1988 | fail(actual, expected, message, "==", assert.equal);
|
1989 | }
|
1990 | };
|
1991 |
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 |
|
2003 | assert.notEqual = function notEqual(actual, expected, message) {
|
2004 | if (actual == expected) {
|
2005 | fail(actual, expected, message, "!=", assert.notEqual);
|
2006 | }
|
2007 | };
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 |
|
2021 |
|
2022 | assert.deepEqual = function deepEqual(actual, expected, message) {
|
2023 | if (eq(actual, expected).length !== 0) {
|
2024 | fail(actual, expected, message, "deepEqual", assert.deepEqual);
|
2025 | }
|
2026 | };
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 |
|
2039 |
|
2040 |
|
2041 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
2042 | if (eq(actual, expected).result) {
|
2043 | fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
|
2044 | }
|
2045 | };
|
2046 |
|
2047 |
|
2048 |
|
2049 |
|
2050 |
|
2051 |
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 | assert.strictEqual = function strictEqual(actual, expected, message) {
|
2059 | if (actual !== expected) {
|
2060 | fail(actual, expected, message, "===", assert.strictEqual);
|
2061 | }
|
2062 | };
|
2063 |
|
2064 |
|
2065 |
|
2066 |
|
2067 |
|
2068 |
|
2069 |
|
2070 |
|
2071 |
|
2072 |
|
2073 |
|
2074 |
|
2075 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
2076 | if (actual === expected) {
|
2077 | fail(actual, expected, message, "!==", assert.notStrictEqual);
|
2078 | }
|
2079 | };
|
2080 |
|
2081 | function expectedException(actual, expected) {
|
2082 | if (!actual || !expected) {
|
2083 | return false;
|
2084 | }
|
2085 |
|
2086 | if (Object.prototype.toString.call(expected) == "[object RegExp]") {
|
2087 | return expected.test(actual);
|
2088 | } else if (actual instanceof expected) {
|
2089 | return true;
|
2090 | } else if (expected.call({}, actual) === true) {
|
2091 | return true;
|
2092 | }
|
2093 |
|
2094 | return false;
|
2095 | }
|
2096 |
|
2097 | function _throws(shouldThrow, block, expected, message) {
|
2098 | var actual;
|
2099 |
|
2100 | if (typeof expected == "string") {
|
2101 | message = expected;
|
2102 | expected = null;
|
2103 | }
|
2104 |
|
2105 | try {
|
2106 | block();
|
2107 | } catch (e) {
|
2108 | actual = e;
|
2109 | }
|
2110 |
|
2111 | message =
|
2112 | (expected && expected.name ? " (" + expected.name + ")" : ".") +
|
2113 | (message ? " " + message : ".");
|
2114 |
|
2115 | if (shouldThrow && !actual) {
|
2116 | fail(actual, expected, "Missing expected exception" + message);
|
2117 | }
|
2118 |
|
2119 | if (!shouldThrow && expectedException(actual, expected)) {
|
2120 | fail(actual, expected, "Got unwanted exception" + message);
|
2121 | }
|
2122 |
|
2123 | if (
|
2124 | (shouldThrow &&
|
2125 | actual &&
|
2126 | expected &&
|
2127 | !expectedException(actual, expected)) ||
|
2128 | (!shouldThrow && actual)
|
2129 | ) {
|
2130 | throw actual;
|
2131 | }
|
2132 | }
|
2133 |
|
2134 |
|
2135 |
|
2136 |
|
2137 |
|
2138 |
|
2139 |
|
2140 |
|
2141 |
|
2142 |
|
2143 |
|
2144 |
|
2145 | assert.throws = function(/*block, error, message*/) {
|
2146 | _throws.apply(this, [true].concat(pSlice.call(arguments)));
|
2147 | };
|
2148 |
|
2149 |
|
2150 |
|
2151 |
|
2152 |
|
2153 |
|
2154 |
|
2155 |
|
2156 |
|
2157 |
|
2158 | assert.doesNotThrow = function(/*block, message*/) {
|
2159 | _throws.apply(this, [false].concat(pSlice.call(arguments)));
|
2160 | };
|
2161 |
|
2162 |
|
2163 |
|
2164 |
|
2165 |
|
2166 |
|
2167 |
|
2168 |
|
2169 | assert.ifError = function(err) {
|
2170 | if (err) {
|
2171 | throw err;
|
2172 | }
|
2173 | };
|
2174 |
|
2175 |
|
2176 |
|
2177 |
|
2178 |
|
2179 |
|
2180 |
|
2181 |
|
2182 | function assertExtensions(should) {
|
2183 | var i = should.format;
|
2184 |
|
2185 | |
2186 |
|
2187 |
|
2188 |
|
2189 |
|
2190 |
|
2191 |
|
2192 |
|
2193 |
|
2194 | merge(should, assert);
|
2195 |
|
2196 | |
2197 |
|
2198 |
|
2199 |
|
2200 |
|
2201 |
|
2202 |
|
2203 |
|
2204 |
|
2205 |
|
2206 |
|
2207 |
|
2208 |
|
2209 |
|
2210 | should.exist = should.exists = function(obj, msg) {
|
2211 | if (null == obj) {
|
2212 | throw new AssertionError({
|
2213 | message: msg || "expected " + i(obj) + " to exist",
|
2214 | stackStartFunction: should.exist
|
2215 | });
|
2216 | }
|
2217 | };
|
2218 |
|
2219 | should.not = {};
|
2220 | |
2221 |
|
2222 |
|
2223 |
|
2224 |
|
2225 |
|
2226 |
|
2227 |
|
2228 |
|
2229 |
|
2230 |
|
2231 |
|
2232 |
|
2233 |
|
2234 |
|
2235 | should.not.exist = should.not.exists = function(obj, msg) {
|
2236 | if (null != obj) {
|
2237 | throw new AssertionError({
|
2238 | message: msg || "expected " + i(obj) + " to not exist",
|
2239 | stackStartFunction: should.not.exist
|
2240 | });
|
2241 | }
|
2242 | };
|
2243 | }
|
2244 |
|
2245 |
|
2246 |
|
2247 |
|
2248 |
|
2249 |
|
2250 |
|
2251 |
|
2252 | function chainAssertions(should, Assertion) {
|
2253 | |
2254 |
|
2255 |
|
2256 |
|
2257 |
|
2258 |
|
2259 |
|
2260 |
|
2261 |
|
2262 |
|
2263 |
|
2264 |
|
2265 |
|
2266 |
|
2267 |
|
2268 |
|
2269 |
|
2270 |
|
2271 |
|
2272 |
|
2273 | [
|
2274 | "an",
|
2275 | "of",
|
2276 | "a",
|
2277 | "and",
|
2278 | "be",
|
2279 | "been",
|
2280 | "has",
|
2281 | "have",
|
2282 | "with",
|
2283 | "is",
|
2284 | "which",
|
2285 | "the",
|
2286 | "it"
|
2287 | ].forEach(function(name) {
|
2288 | Assertion.addChain(name);
|
2289 | });
|
2290 | }
|
2291 |
|
2292 |
|
2293 |
|
2294 |
|
2295 |
|
2296 |
|
2297 |
|
2298 |
|
2299 | function booleanAssertions(should, Assertion) {
|
2300 | |
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 |
|
2308 |
|
2309 |
|
2310 |
|
2311 |
|
2312 |
|
2313 |
|
2314 |
|
2315 | Assertion.add("true", function(message) {
|
2316 | this.is.exactly(true, message);
|
2317 | });
|
2318 |
|
2319 | Assertion.alias("true", "True");
|
2320 |
|
2321 | |
2322 |
|
2323 |
|
2324 |
|
2325 |
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 |
|
2331 |
|
2332 |
|
2333 |
|
2334 | Assertion.add("false", function(message) {
|
2335 | this.is.exactly(false, message);
|
2336 | });
|
2337 |
|
2338 | Assertion.alias("false", "False");
|
2339 |
|
2340 | |
2341 |
|
2342 |
|
2343 |
|
2344 |
|
2345 |
|
2346 |
|
2347 |
|
2348 |
|
2349 |
|
2350 |
|
2351 |
|
2352 |
|
2353 |
|
2354 |
|
2355 |
|
2356 | Assertion.add("ok", function() {
|
2357 | this.assertZeroArguments(arguments);
|
2358 | this.params = { operator: "to be truthy" };
|
2359 |
|
2360 | this.assert(this.obj);
|
2361 | });
|
2362 | }
|
2363 |
|
2364 |
|
2365 |
|
2366 |
|
2367 |
|
2368 |
|
2369 |
|
2370 |
|
2371 | function numberAssertions(should, Assertion) {
|
2372 | |
2373 |
|
2374 |
|
2375 |
|
2376 |
|
2377 |
|
2378 |
|
2379 |
|
2380 |
|
2381 |
|
2382 | Assertion.add("NaN", function() {
|
2383 | this.assertZeroArguments(arguments);
|
2384 | this.params = { operator: "to be NaN" };
|
2385 |
|
2386 | this.assert(this.obj !== this.obj);
|
2387 | });
|
2388 |
|
2389 | |
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 |
|
2395 |
|
2396 |
|
2397 |
|
2398 |
|
2399 |
|
2400 | Assertion.add("Infinity", function() {
|
2401 | this.assertZeroArguments(arguments);
|
2402 | this.params = { operator: "to be Infinity" };
|
2403 |
|
2404 | this.is.a
|
2405 | .Number()
|
2406 | .and.not.a.NaN()
|
2407 | .and.assert(!isFinite(this.obj));
|
2408 | });
|
2409 |
|
2410 | |
2411 |
|
2412 |
|
2413 |
|
2414 |
|
2415 |
|
2416 |
|
2417 |
|
2418 |
|
2419 |
|
2420 |
|
2421 |
|
2422 |
|
2423 | Assertion.add("within", function(start, finish, description) {
|
2424 | this.params = {
|
2425 | operator: "to be within " + start + ".." + finish,
|
2426 | message: description
|
2427 | };
|
2428 |
|
2429 | this.assert(this.obj >= start && this.obj <= finish);
|
2430 | });
|
2431 |
|
2432 | |
2433 |
|
2434 |
|
2435 |
|
2436 |
|
2437 |
|
2438 |
|
2439 |
|
2440 |
|
2441 |
|
2442 |
|
2443 |
|
2444 |
|
2445 | Assertion.add("approximately", function(value, delta, description) {
|
2446 | this.params = {
|
2447 | operator: "to be approximately " + value + " ±" + delta,
|
2448 | message: description
|
2449 | };
|
2450 |
|
2451 | this.assert(Math.abs(this.obj - value) <= delta);
|
2452 | });
|
2453 |
|
2454 | |
2455 |
|
2456 |
|
2457 |
|
2458 |
|
2459 |
|
2460 |
|
2461 |
|
2462 |
|
2463 |
|
2464 |
|
2465 |
|
2466 |
|
2467 | Assertion.add("above", function(n, description) {
|
2468 | this.params = { operator: "to be above " + n, message: description };
|
2469 |
|
2470 | this.assert(this.obj > n);
|
2471 | });
|
2472 |
|
2473 | |
2474 |
|
2475 |
|
2476 |
|
2477 |
|
2478 |
|
2479 |
|
2480 |
|
2481 |
|
2482 |
|
2483 |
|
2484 |
|
2485 |
|
2486 | Assertion.add("below", function(n, description) {
|
2487 | this.params = { operator: "to be below " + n, message: description };
|
2488 |
|
2489 | this.assert(this.obj < n);
|
2490 | });
|
2491 |
|
2492 | Assertion.alias("above", "greaterThan");
|
2493 | Assertion.alias("below", "lessThan");
|
2494 |
|
2495 | |
2496 |
|
2497 |
|
2498 |
|
2499 |
|
2500 |
|
2501 |
|
2502 |
|
2503 |
|
2504 |
|
2505 |
|
2506 |
|
2507 |
|
2508 |
|
2509 | Assertion.add("aboveOrEqual", function(n, description) {
|
2510 | this.params = {
|
2511 | operator: "to be above or equal " + n,
|
2512 | message: description
|
2513 | };
|
2514 |
|
2515 | this.assert(this.obj >= n);
|
2516 | });
|
2517 |
|
2518 | |
2519 |
|
2520 |
|
2521 |
|
2522 |
|
2523 |
|
2524 |
|
2525 |
|
2526 |
|
2527 |
|
2528 |
|
2529 |
|
2530 |
|
2531 |
|
2532 | Assertion.add("belowOrEqual", function(n, description) {
|
2533 | this.params = {
|
2534 | operator: "to be below or equal " + n,
|
2535 | message: description
|
2536 | };
|
2537 |
|
2538 | this.assert(this.obj <= n);
|
2539 | });
|
2540 |
|
2541 | Assertion.alias("aboveOrEqual", "greaterThanOrEqual");
|
2542 | Assertion.alias("belowOrEqual", "lessThanOrEqual");
|
2543 | }
|
2544 |
|
2545 |
|
2546 |
|
2547 |
|
2548 |
|
2549 |
|
2550 |
|
2551 |
|
2552 | function typeAssertions(should, Assertion) {
|
2553 | |
2554 |
|
2555 |
|
2556 |
|
2557 |
|
2558 |
|
2559 | Assertion.add("Number", function() {
|
2560 | this.assertZeroArguments(arguments);
|
2561 | this.params = { operator: "to be a number" };
|
2562 |
|
2563 | this.have.type("number");
|
2564 | });
|
2565 |
|
2566 | |
2567 |
|
2568 |
|
2569 |
|
2570 |
|
2571 |
|
2572 |
|
2573 | Assertion.add("arguments", function() {
|
2574 | this.assertZeroArguments(arguments);
|
2575 | this.params = { operator: "to be arguments" };
|
2576 |
|
2577 | this.have.class("Arguments");
|
2578 | });
|
2579 |
|
2580 | Assertion.alias("arguments", "Arguments");
|
2581 |
|
2582 | |
2583 |
|
2584 |
|
2585 |
|
2586 |
|
2587 |
|
2588 |
|
2589 |
|
2590 | Assertion.add("type", function(type, description) {
|
2591 | this.params = { operator: "to have type " + type, message: description };
|
2592 |
|
2593 | should(typeof this.obj).be.exactly(type);
|
2594 | });
|
2595 |
|
2596 | |
2597 |
|
2598 |
|
2599 |
|
2600 |
|
2601 |
|
2602 |
|
2603 |
|
2604 |
|
2605 | Assertion.add("instanceof", function(constructor, description) {
|
2606 | this.params = {
|
2607 | operator: "to be an instance of " + functionName$1(constructor),
|
2608 | message: description
|
2609 | };
|
2610 |
|
2611 | this.assert(Object(this.obj) instanceof constructor);
|
2612 | });
|
2613 |
|
2614 | Assertion.alias("instanceof", "instanceOf");
|
2615 |
|
2616 | /**
|
2617 | * Assert given object is function
|
2618 | * @name Function
|
2619 | * @memberOf Assertion
|
2620 | * @category assertion types
|
2621 | */
|
2622 | Assertion.add("Function", function() {
|
2623 | this.assertZeroArguments(arguments);
|
2624 | this.params = { operator: "to be a function" };
|
2625 |
|
2626 | this.have.type("function");
|
2627 | });
|
2628 |
|
2629 | |
2630 |
|
2631 |
|
2632 |
|
2633 |
|
2634 |
|
2635 | Assertion.add("Object", function() {
|
2636 | this.assertZeroArguments(arguments);
|
2637 | this.params = { operator: "to be an object" };
|
2638 |
|
2639 | this.is.not.null().and.have.type("object");
|
2640 | });
|
2641 |
|
2642 | |
2643 |
|
2644 |
|
2645 |
|
2646 |
|
2647 |
|
2648 | Assertion.add("String", function() {
|
2649 | this.assertZeroArguments(arguments);
|
2650 | this.params = { operator: "to be a string" };
|
2651 |
|
2652 | this.have.type("string");
|
2653 | });
|
2654 |
|
2655 | |
2656 |
|
2657 |
|
2658 |
|
2659 |
|
2660 |
|
2661 | Assertion.add("Array", function() {
|
2662 | this.assertZeroArguments(arguments);
|
2663 | this.params = { operator: "to be an array" };
|
2664 |
|
2665 | this.have.class("Array");
|
2666 | });
|
2667 |
|
2668 | |
2669 |
|
2670 |
|
2671 |
|
2672 |
|
2673 |
|
2674 | Assertion.add("Boolean", function() {
|
2675 | this.assertZeroArguments(arguments);
|
2676 | this.params = { operator: "to be a boolean" };
|
2677 |
|
2678 | this.have.type("boolean");
|
2679 | });
|
2680 |
|
2681 | |
2682 |
|
2683 |
|
2684 |
|
2685 |
|
2686 |
|
2687 | Assertion.add("Error", function() {
|
2688 | this.assertZeroArguments(arguments);
|
2689 | this.params = { operator: "to be an error" };
|
2690 |
|
2691 | this.have.instanceOf(Error);
|
2692 | });
|
2693 |
|
2694 | |
2695 |
|
2696 |
|
2697 |
|
2698 |
|
2699 |
|
2700 | Assertion.add("Date", function() {
|
2701 | this.assertZeroArguments(arguments);
|
2702 | this.params = { operator: "to be a date" };
|
2703 |
|
2704 | this.have.instanceOf(Date);
|
2705 | });
|
2706 |
|
2707 | |
2708 |
|
2709 |
|
2710 |
|
2711 |
|
2712 |
|
2713 |
|
2714 | Assertion.add("null", function() {
|
2715 | this.assertZeroArguments(arguments);
|
2716 | this.params = { operator: "to be null" };
|
2717 |
|
2718 | this.assert(this.obj === null);
|
2719 | });
|
2720 |
|
2721 | Assertion.alias("null", "Null");
|
2722 |
|
2723 | |
2724 |
|
2725 |
|
2726 |
|
2727 |
|
2728 |
|
2729 |
|
2730 | Assertion.add("class", function(cls) {
|
2731 | this.params = { operator: "to have [[Class]] " + cls };
|
2732 |
|
2733 | this.assert(Object.prototype.toString.call(this.obj) === "[object " + cls + "]");
|
2734 | });
|
2735 |
|
2736 | Assertion.alias("class", "Class");
|
2737 |
|
2738 | |
2739 |
|
2740 |
|
2741 |
|
2742 |
|
2743 |
|
2744 |
|
2745 | Assertion.add("undefined", function() {
|
2746 | this.assertZeroArguments(arguments);
|
2747 | this.params = { operator: "to be undefined" };
|
2748 |
|
2749 | this.assert(this.obj === void 0);
|
2750 | });
|
2751 |
|
2752 | Assertion.alias("undefined", "Undefined");
|
2753 |
|
2754 | |
2755 |
|
2756 |
|
2757 |
|
2758 |
|
2759 |
|
2760 |
|
2761 | Assertion.add("iterable", function() {
|
2762 | this.assertZeroArguments(arguments);
|
2763 | this.params = { operator: "to be iterable" };
|
2764 |
|
2765 | should(this.obj)
|
2766 | .have.property(Symbol.iterator)
|
2767 | .which.is.a.Function();
|
2768 | });
|
2769 |
|
2770 | |
2771 |
|
2772 |
|
2773 |
|
2774 |
|
2775 |
|
2776 |
|
2777 | Assertion.add("iterator", function() {
|
2778 | this.assertZeroArguments(arguments);
|
2779 | this.params = { operator: "to be iterator" };
|
2780 |
|
2781 | should(this.obj)
|
2782 | .have.property("next")
|
2783 | .which.is.a.Function();
|
2784 | });
|
2785 |
|
2786 | |
2787 |
|
2788 |
|
2789 |
|
2790 |
|
2791 |
|
2792 | Assertion.add("generator", function() {
|
2793 | this.assertZeroArguments(arguments);
|
2794 | this.params = { operator: "to be generator" };
|
2795 |
|
2796 | should(this.obj).be.iterable.and.iterator.and.it.is.equal(this.obj[Symbol.iterator]());
|
2797 | });
|
2798 | }
|
2799 |
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 |
|
2805 |
|
2806 |
|
2807 | function formatEqlResult(r, a, b) {
|
2808 | return ((r.path.length > 0
|
2809 | ? "at " + r.path.map(formatProp).join(" -> ")
|
2810 | : "") +
|
2811 | (r.a === a ? "" : ", A has " + format$2(r.a)) +
|
2812 | (r.b === b ? "" : " and B has " + format$2(r.b)) +
|
2813 | (r.showReason ? " because " + r.reason : "")).trim();
|
2814 | }
|
2815 |
|
2816 | function equalityAssertions(should, Assertion) {
|
2817 | |
2818 |
|
2819 |
|
2820 |
|
2821 |
|
2822 |
|
2823 |
|
2824 |
|
2825 |
|
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 |
|
2831 |
|
2832 |
|
2833 |
|
2834 |
|
2835 |
|
2836 |
|
2837 |
|
2838 | Assertion.add("eql", function(val, description) {
|
2839 | this.params = { operator: "to equal", expected: val, message: description };
|
2840 | var obj = this.obj;
|
2841 | var fails = eq(this.obj, val, should.config);
|
2842 | this.params.details = fails
|
2843 | .map(function(fail) {
|
2844 | return formatEqlResult(fail, obj, val);
|
2845 | })
|
2846 | .join(", ");
|
2847 |
|
2848 | this.params.showDiff = eq(getGlobalType(obj), getGlobalType(val)).length === 0;
|
2849 |
|
2850 | this.assert(fails.length === 0);
|
2851 | });
|
2852 |
|
2853 | |
2854 |
|
2855 |
|
2856 |
|
2857 |
|
2858 |
|
2859 |
|
2860 |
|
2861 |
|
2862 |
|
2863 |
|
2864 |
|
2865 |
|
2866 |
|
2867 |
|
2868 |
|
2869 |
|
2870 | Assertion.add("equal", function(val, description) {
|
2871 | this.params = { operator: "to be", expected: val, message: description };
|
2872 |
|
2873 | this.params.showDiff = eq(getGlobalType(this.obj), getGlobalType(val)).length === 0;
|
2874 |
|
2875 | this.assert(val === this.obj);
|
2876 | });
|
2877 |
|
2878 | Assertion.alias("equal", "equals");
|
2879 | Assertion.alias("equal", "exactly");
|
2880 | Assertion.alias("eql", "eqls");
|
2881 | Assertion.alias("eql", "deepEqual");
|
2882 |
|
2883 | function addOneOf(name, message, method) {
|
2884 | Assertion.add(name, function(vals) {
|
2885 | if (arguments.length !== 1) {
|
2886 | vals = Array.prototype.slice.call(arguments);
|
2887 | } else {
|
2888 | should(vals).be.Array();
|
2889 | }
|
2890 |
|
2891 | this.params = { operator: message, expected: vals };
|
2892 |
|
2893 | var obj = this.obj;
|
2894 | var found = false;
|
2895 |
|
2896 | forEach(vals, function(val) {
|
2897 | try {
|
2898 | should(val)[method](obj);
|
2899 | found = true;
|
2900 | return false;
|
2901 | } catch (e) {
|
2902 | if (e instanceof should.AssertionError) {
|
2903 | return;
|
2904 | }
|
2905 | throw e;
|
2906 | }
|
2907 | });
|
2908 |
|
2909 | this.assert(found);
|
2910 | });
|
2911 | }
|
2912 |
|
2913 | |
2914 |
|
2915 |
|
2916 |
|
2917 |
|
2918 |
|
2919 |
|
2920 |
|
2921 |
|
2922 |
|
2923 |
|
2924 |
|
2925 | addOneOf("equalOneOf", "to be equals one of", "equal");
|
2926 |
|
2927 | |
2928 |
|
2929 |
|
2930 |
|
2931 |
|
2932 |
|
2933 |
|
2934 |
|
2935 |
|
2936 |
|
2937 |
|
2938 |
|
2939 | addOneOf("oneOf", "to be one of", "eql");
|
2940 | }
|
2941 |
|
2942 |
|
2943 |
|
2944 |
|
2945 |
|
2946 |
|
2947 |
|
2948 |
|
2949 | function promiseAssertions(should, Assertion$$1) {
|
2950 | |
2951 |
|
2952 |
|
2953 |
|
2954 |
|
2955 |
|
2956 |
|
2957 |
|
2958 |
|
2959 |
|
2960 |
|
2961 |
|
2962 | Assertion$$1.add("Promise", function() {
|
2963 | this.assertZeroArguments(arguments);
|
2964 | this.params = { operator: "to be promise" };
|
2965 |
|
2966 | var obj = this.obj;
|
2967 |
|
2968 | should(obj)
|
2969 | .have.property("then")
|
2970 | .which.is.a.Function();
|
2971 | });
|
2972 |
|
2973 | |
2974 |
|
2975 |
|
2976 |
|
2977 |
|
2978 |
|
2979 |
|
2980 |
|
2981 |
|
2982 |
|
2983 |
|
2984 |
|
2985 |
|
2986 |
|
2987 |
|
2988 |
|
2989 |
|
2990 |
|
2991 |
|
2992 | Assertion$$1.prototype.fulfilled = function Assertion$fulfilled() {
|
2993 | this.assertZeroArguments(arguments);
|
2994 | this.params = { operator: "to be fulfilled" };
|
2995 |
|
2996 | should(this.obj).be.a.Promise();
|
2997 |
|
2998 | var that = this;
|
2999 | return this.obj.then(
|
3000 | function next$onResolve(value) {
|
3001 | if (that.negate) {
|
3002 | that.fail();
|
3003 | }
|
3004 | return value;
|
3005 | },
|
3006 | function next$onReject(err) {
|
3007 | if (!that.negate) {
|
3008 | that.params.operator += ", but it was rejected with " + should.format(err);
|
3009 | that.fail();
|
3010 | }
|
3011 | return err;
|
3012 | }
|
3013 | );
|
3014 | };
|
3015 |
|
3016 | Assertion$$1.prototype.resolved = Assertion$$1.prototype.fulfilled;
|
3017 |
|
3018 | |
3019 |
|
3020 |
|
3021 |
|
3022 |
|
3023 |
|
3024 |
|
3025 |
|
3026 |
|
3027 |
|
3028 |
|
3029 |
|
3030 |
|
3031 |
|
3032 |
|
3033 |
|
3034 |
|
3035 |
|
3036 |
|
3037 | Assertion$$1.prototype.rejected = function() {
|
3038 | this.assertZeroArguments(arguments);
|
3039 | this.params = { operator: "to be rejected" };
|
3040 |
|
3041 | should(this.obj).be.a.Promise();
|
3042 |
|
3043 | var that = this;
|
3044 | return this.obj.then(
|
3045 | function(value) {
|
3046 | if (!that.negate) {
|
3047 | that.params.operator += ", but it was fulfilled";
|
3048 | if (arguments.length != 0) {
|
3049 | that.params.operator += " with " + should.format(value);
|
3050 | }
|
3051 | that.fail();
|
3052 | }
|
3053 | return value;
|
3054 | },
|
3055 | function next$onError(err) {
|
3056 | if (that.negate) {
|
3057 | that.fail();
|
3058 | }
|
3059 | return err;
|
3060 | }
|
3061 | );
|
3062 | };
|
3063 |
|
3064 | |
3065 |
|
3066 |
|
3067 |
|
3068 |
|
3069 |
|
3070 |
|
3071 |
|
3072 |
|
3073 |
|
3074 |
|
3075 |
|
3076 |
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 |
|
3082 |
|
3083 |
|
3084 |
|
3085 | Assertion$$1.prototype.fulfilledWith = function(expectedValue) {
|
3086 | this.params = {
|
3087 | operator: "to be fulfilled with " + should.format(expectedValue)
|
3088 | };
|
3089 |
|
3090 | should(this.obj).be.a.Promise();
|
3091 |
|
3092 | var that = this;
|
3093 | return this.obj.then(
|
3094 | function(value) {
|
3095 | if (that.negate) {
|
3096 | that.fail();
|
3097 | }
|
3098 | should(value).eql(expectedValue);
|
3099 | return value;
|
3100 | },
|
3101 | function next$onError(err) {
|
3102 | if (!that.negate) {
|
3103 | that.params.operator += ", but it was rejected with " + should.format(err);
|
3104 | that.fail();
|
3105 | }
|
3106 | return err;
|
3107 | }
|
3108 | );
|
3109 | };
|
3110 |
|
3111 | Assertion$$1.prototype.resolvedWith = Assertion$$1.prototype.fulfilledWith;
|
3112 |
|
3113 | |
3114 |
|
3115 |
|
3116 |
|
3117 |
|
3118 |
|
3119 |
|
3120 |
|
3121 |
|
3122 |
|
3123 |
|
3124 |
|
3125 |
|
3126 |
|
3127 |
|
3128 |
|
3129 |
|
3130 |
|
3131 |
|
3132 |
|
3133 |
|
3134 |
|
3135 |
|
3136 |
|
3137 |
|
3138 |
|
3139 | Assertion$$1.prototype.rejectedWith = function(message, properties) {
|
3140 | this.params = { operator: "to be rejected" };
|
3141 |
|
3142 | should(this.obj).be.a.Promise();
|
3143 |
|
3144 | var that = this;
|
3145 | return this.obj.then(
|
3146 | function(value) {
|
3147 | if (!that.negate) {
|
3148 | that.fail();
|
3149 | }
|
3150 | return value;
|
3151 | },
|
3152 | function next$onError(err) {
|
3153 | if (that.negate) {
|
3154 | that.fail();
|
3155 | }
|
3156 |
|
3157 | var errorMatched = true;
|
3158 | var errorInfo = "";
|
3159 |
|
3160 | if ("string" === typeof message) {
|
3161 | errorMatched = message === err.message;
|
3162 | } else if (message instanceof RegExp) {
|
3163 | errorMatched = message.test(err.message);
|
3164 | } else if ("function" === typeof message) {
|
3165 | errorMatched = err instanceof message;
|
3166 | } else if (message !== null && typeof message === "object") {
|
3167 | try {
|
3168 | should(err).match(message);
|
3169 | } catch (e) {
|
3170 | if (e instanceof should.AssertionError) {
|
3171 | errorInfo = ": " + e.message;
|
3172 | errorMatched = false;
|
3173 | } else {
|
3174 | throw e;
|
3175 | }
|
3176 | }
|
3177 | }
|
3178 |
|
3179 | if (!errorMatched) {
|
3180 | if (typeof message === "string" || message instanceof RegExp) {
|
3181 | errorInfo = " with a message matching " + should.format(message) + ", but got '" + err.message + "'";
|
3182 | } else if ("function" === typeof message) {
|
3183 | errorInfo = " of type " + functionName$1(message) + ", but got " + functionName$1(err.constructor);
|
3184 | }
|
3185 | } else if ("function" === typeof message && properties) {
|
3186 | try {
|
3187 | should(err).match(properties);
|
3188 | } catch (e) {
|
3189 | if (e instanceof should.AssertionError) {
|
3190 | errorInfo = ": " + e.message;
|
3191 | errorMatched = false;
|
3192 | } else {
|
3193 | throw e;
|
3194 | }
|
3195 | }
|
3196 | }
|
3197 |
|
3198 | that.params.operator += errorInfo;
|
3199 |
|
3200 | that.assert(errorMatched);
|
3201 |
|
3202 | return err;
|
3203 | }
|
3204 | );
|
3205 | };
|
3206 |
|
3207 | |
3208 |
|
3209 |
|
3210 |
|
3211 |
|
3212 |
|
3213 |
|
3214 |
|
3215 |
|
3216 |
|
3217 |
|
3218 |
|
3219 |
|
3220 |
|
3221 |
|
3222 |
|
3223 |
|
3224 |
|
3225 |
|
3226 |
|
3227 |
|
3228 | Object.defineProperty(Assertion$$1.prototype, "finally", {
|
3229 | get: function() {
|
3230 | should(this.obj).be.a.Promise();
|
3231 |
|
3232 | var that = this;
|
3233 |
|
3234 | return new PromisedAssertion(
|
3235 | this.obj.then(function(obj) {
|
3236 | var a = should(obj);
|
3237 |
|
3238 | a.negate = that.negate;
|
3239 | a.anyOne = that.anyOne;
|
3240 |
|
3241 | return a;
|
3242 | })
|
3243 | );
|
3244 | }
|
3245 | });
|
3246 |
|
3247 | Assertion$$1.alias("finally", "eventually");
|
3248 | }
|
3249 |
|
3250 |
|
3251 |
|
3252 |
|
3253 |
|
3254 |
|
3255 |
|
3256 |
|
3257 | function stringAssertions(should, Assertion) {
|
3258 | |
3259 |
|
3260 |
|
3261 |
|
3262 |
|
3263 |
|
3264 |
|
3265 |
|
3266 |
|
3267 |
|
3268 |
|
3269 | Assertion.add("startWith", function(str, description) {
|
3270 | this.params = {
|
3271 | operator: "to start with " + should.format(str),
|
3272 | message: description
|
3273 | };
|
3274 |
|
3275 | this.assert(0 === this.obj.indexOf(str));
|
3276 | });
|
3277 |
|
3278 | |
3279 |
|
3280 |
|
3281 |
|
3282 |
|
3283 |
|
3284 |
|
3285 |
|
3286 |
|
3287 |
|
3288 |
|
3289 | Assertion.add("endWith", function(str, description) {
|
3290 | this.params = {
|
3291 | operator: "to end with " + should.format(str),
|
3292 | message: description
|
3293 | };
|
3294 |
|
3295 | this.assert(this.obj.indexOf(str, this.obj.length - str.length) >= 0);
|
3296 | });
|
3297 | }
|
3298 |
|
3299 |
|
3300 |
|
3301 |
|
3302 |
|
3303 |
|
3304 |
|
3305 |
|
3306 | function containAssertions(should, Assertion) {
|
3307 | var i = should.format;
|
3308 |
|
3309 | |
3310 |
|
3311 |
|
3312 |
|
3313 |
|
3314 |
|
3315 |
|
3316 |
|
3317 |
|
3318 |
|
3319 |
|
3320 |
|
3321 |
|
3322 |
|
3323 |
|
3324 |
|
3325 |
|
3326 |
|
3327 |
|
3328 |
|
3329 |
|
3330 |
|
3331 |
|
3332 |
|
3333 | Assertion.add("containEql", function(other) {
|
3334 | this.params = { operator: "to contain " + i(other) };
|
3335 |
|
3336 | this.is.not.null().and.not.undefined();
|
3337 |
|
3338 | var obj = this.obj;
|
3339 |
|
3340 | if (typeof obj == "string") {
|
3341 | this.assert(obj.indexOf(String(other)) >= 0);
|
3342 | } else if (isIterable(obj)) {
|
3343 | this.assert(
|
3344 | some(obj, function(v) {
|
3345 | return eq(v, other).length === 0;
|
3346 | })
|
3347 | );
|
3348 | } else {
|
3349 | forEach(
|
3350 | other,
|
3351 | function(value, key) {
|
3352 | should(obj).have.value(key, value);
|
3353 | },
|
3354 | this
|
3355 | );
|
3356 | }
|
3357 | });
|
3358 |
|
3359 | |
3360 |
|
3361 |
|
3362 |
|
3363 |
|
3364 |
|
3365 |
|
3366 |
|
3367 |
|
3368 |
|
3369 |
|
3370 |
|
3371 |
|
3372 |
|
3373 |
|
3374 |
|
3375 |
|
3376 |
|
3377 |
|
3378 | Assertion.add("containDeepOrdered", function(other) {
|
3379 | this.params = { operator: "to contain " + i(other) };
|
3380 |
|
3381 | var obj = this.obj;
|
3382 | if (typeof obj == "string") {
|
3383 |
|
3384 | this.is.equal(String(other));
|
3385 | } else if (isIterable(obj) && isIterable(other)) {
|
3386 | var objIterator = iterator(obj);
|
3387 | var otherIterator = iterator(other);
|
3388 |
|
3389 | var nextObj = objIterator.next();
|
3390 | var nextOther = otherIterator.next();
|
3391 | while (!nextObj.done && !nextOther.done) {
|
3392 | try {
|
3393 | should(nextObj.value[1]).containDeepOrdered(nextOther.value[1]);
|
3394 | nextOther = otherIterator.next();
|
3395 | } catch (e) {
|
3396 | if (!(e instanceof should.AssertionError)) {
|
3397 | throw e;
|
3398 | }
|
3399 | }
|
3400 | nextObj = objIterator.next();
|
3401 | }
|
3402 |
|
3403 | this.assert(nextOther.done);
|
3404 | } else if (obj != null && typeof obj == "object" && other != null && typeof other == "object") {
|
3405 |
|
3406 | forEach(other, function(value, key) {
|
3407 | should(obj[key]).containDeepOrdered(value);
|
3408 | });
|
3409 |
|
3410 |
|
3411 | if (isEmpty(other)) {
|
3412 | this.eql(other);
|
3413 | }
|
3414 | } else {
|
3415 | this.eql(other);
|
3416 | }
|
3417 | });
|
3418 |
|
3419 | |
3420 |
|
3421 |
|
3422 |
|
3423 |
|
3424 |
|
3425 |
|
3426 |
|
3427 |
|
3428 |
|
3429 |
|
3430 |
|
3431 | Assertion.add("containDeep", function(other) {
|
3432 | this.params = { operator: "to contain " + i(other) };
|
3433 |
|
3434 | var obj = this.obj;
|
3435 | if (typeof obj === "string" && typeof other === "string") {
|
3436 |
|
3437 | this.is.equal(String(other));
|
3438 | } else if (isIterable(obj) && isIterable(other)) {
|
3439 | var usedKeys = {};
|
3440 | forEach(
|
3441 | other,
|
3442 | function(otherItem) {
|
3443 | this.assert(
|
3444 | some(obj, function(item, index) {
|
3445 | if (index in usedKeys) {
|
3446 | return false;
|
3447 | }
|
3448 |
|
3449 | try {
|
3450 | should(item).containDeep(otherItem);
|
3451 | usedKeys[index] = true;
|
3452 | return true;
|
3453 | } catch (e) {
|
3454 | if (e instanceof should.AssertionError) {
|
3455 | return false;
|
3456 | }
|
3457 | throw e;
|
3458 | }
|
3459 | })
|
3460 | );
|
3461 | },
|
3462 | this
|
3463 | );
|
3464 | } else if (obj != null && other != null && typeof obj == "object" && typeof other == "object") {
|
3465 |
|
3466 | forEach(other, function(value, key) {
|
3467 | should(obj[key]).containDeep(value);
|
3468 | });
|
3469 |
|
3470 |
|
3471 | if (isEmpty(other)) {
|
3472 | this.eql(other);
|
3473 | }
|
3474 | } else {
|
3475 | this.eql(other);
|
3476 | }
|
3477 | });
|
3478 | }
|
3479 |
|
3480 |
|
3481 |
|
3482 |
|
3483 |
|
3484 |
|
3485 |
|
3486 |
|
3487 | var aSlice = Array.prototype.slice;
|
3488 |
|
3489 | function propertyAssertions(should, Assertion) {
|
3490 | var i = should.format;
|
3491 | |
3492 |
|
3493 |
|
3494 |
|
3495 |
|
3496 |
|
3497 |
|
3498 |
|
3499 |
|
3500 |
|
3501 |
|
3502 |
|
3503 | Assertion.add("propertyWithDescriptor", function(name, desc) {
|
3504 | this.params = {
|
3505 | actual: this.obj,
|
3506 | operator: "to have own property with descriptor " + i(desc)
|
3507 | };
|
3508 | var obj = this.obj;
|
3509 | this.have.ownProperty(name);
|
3510 | should(Object.getOwnPropertyDescriptor(Object(obj), name)).have.properties(desc);
|
3511 | });
|
3512 |
|
3513 | |
3514 |
|
3515 |
|
3516 |
|
3517 |
|
3518 |
|
3519 |
|
3520 |
|
3521 |
|
3522 |
|
3523 |
|
3524 |
|
3525 | Assertion.add("property", function(name, val) {
|
3526 | name = convertPropertyName(name);
|
3527 | if (arguments.length > 1) {
|
3528 | var p = {};
|
3529 | p[name] = val;
|
3530 | this.have.properties(p);
|
3531 | } else {
|
3532 | this.have.properties(name);
|
3533 | }
|
3534 | this.obj = this.obj[name];
|
3535 | });
|
3536 |
|
3537 | |
3538 |
|
3539 |
|
3540 |
|
3541 |
|
3542 |
|
3543 |
|
3544 |
|
3545 |
|
3546 |
|
3547 |
|
3548 |
|
3549 |
|
3550 | Assertion.add("properties", function(names) {
|
3551 | var values = {};
|
3552 | if (arguments.length > 1) {
|
3553 | names = aSlice.call(arguments);
|
3554 | } else if (!Array.isArray(names)) {
|
3555 | if (typeof names == "string" || typeof names == "symbol") {
|
3556 | names = [names];
|
3557 | } else {
|
3558 | values = names;
|
3559 | names = Object.keys(names);
|
3560 | }
|
3561 | }
|
3562 |
|
3563 | var obj = Object(this.obj),
|
3564 | missingProperties = [];
|
3565 |
|
3566 |
|
3567 | names.forEach(function(name) {
|
3568 | if (!(name in obj)) {
|
3569 | missingProperties.push(formatProp(name));
|
3570 | }
|
3571 | });
|
3572 |
|
3573 | var props = missingProperties;
|
3574 | if (props.length === 0) {
|
3575 | props = names.map(formatProp);
|
3576 | } else if (this.anyOne) {
|
3577 | props = names
|
3578 | .filter(function(name) {
|
3579 | return missingProperties.indexOf(formatProp(name)) < 0;
|
3580 | })
|
3581 | .map(formatProp);
|
3582 | }
|
3583 |
|
3584 | var operator =
|
3585 | (props.length === 1
|
3586 | ? "to have property "
|
3587 | : "to have " + (this.anyOne ? "any of " : "") + "properties ") + props.join(", ");
|
3588 |
|
3589 | this.params = { obj: this.obj, operator: operator };
|
3590 |
|
3591 |
|
3592 |
|
3593 | this.assert(
|
3594 | missingProperties.length === 0 || (this.anyOne && missingProperties.length != names.length)
|
3595 | );
|
3596 |
|
3597 |
|
3598 | var valueCheckNames = Object.keys(values);
|
3599 | if (valueCheckNames.length) {
|
3600 | var wrongValues = [];
|
3601 | props = [];
|
3602 |
|
3603 |
|
3604 | valueCheckNames.forEach(function(name) {
|
3605 | var value = values[name];
|
3606 | if (eq(obj[name], value).length !== 0) {
|
3607 | wrongValues.push(formatProp(name) + " of " + i(value) + " (got " + i(obj[name]) + ")");
|
3608 | } else {
|
3609 | props.push(formatProp(name) + " of " + i(value));
|
3610 | }
|
3611 | });
|
3612 |
|
3613 | if ((wrongValues.length !== 0 && !this.anyOne) || (this.anyOne && props.length === 0)) {
|
3614 | props = wrongValues;
|
3615 | }
|
3616 |
|
3617 | operator =
|
3618 | (props.length === 1
|
3619 | ? "to have property "
|
3620 | : "to have " + (this.anyOne ? "any of " : "") + "properties ") + props.join(", ");
|
3621 |
|
3622 | this.params = { obj: this.obj, operator: operator };
|
3623 |
|
3624 |
|
3625 |
|
3626 | this.assert(
|
3627 | wrongValues.length === 0 || (this.anyOne && wrongValues.length != valueCheckNames.length)
|
3628 | );
|
3629 | }
|
3630 | });
|
3631 |
|
3632 | |
3633 |
|
3634 |
|
3635 |
|
3636 |
|
3637 |
|
3638 |
|
3639 |
|
3640 |
|
3641 |
|
3642 |
|
3643 |
|
3644 |
|
3645 | Assertion.add("length", function(n, description) {
|
3646 | this.have.property("length", n, description);
|
3647 | });
|
3648 |
|
3649 | Assertion.alias("length", "lengthOf");
|
3650 |
|
3651 | |
3652 |
|
3653 |
|
3654 |
|
3655 |
|
3656 |
|
3657 |
|
3658 |
|
3659 |
|
3660 |
|
3661 |
|
3662 |
|
3663 |
|
3664 | Assertion.add("ownProperty", function(name, description) {
|
3665 | name = convertPropertyName(name);
|
3666 | this.params = {
|
3667 | actual: this.obj,
|
3668 | operator: "to have own property " + formatProp(name),
|
3669 | message: description
|
3670 | };
|
3671 |
|
3672 | this.assert(hasOwnProperty$1(this.obj, name));
|
3673 |
|
3674 | this.obj = this.obj[name];
|
3675 | });
|
3676 |
|
3677 | Assertion.alias("ownProperty", "hasOwnProperty");
|
3678 |
|
3679 | |
3680 |
|
3681 |
|
3682 |
|
3683 |
|
3684 |
|
3685 |
|
3686 |
|
3687 |
|
3688 |
|
3689 |
|
3690 |
|
3691 | Assertion.add(
|
3692 | "empty",
|
3693 | function() {
|
3694 | this.params = { operator: "to be empty" };
|
3695 | this.assert(isEmpty(this.obj));
|
3696 | },
|
3697 | true
|
3698 | );
|
3699 |
|
3700 | |
3701 |
|
3702 |
|
3703 |
|
3704 |
|
3705 |
|
3706 |
|
3707 |
|
3708 |
|
3709 |
|
3710 |
|
3711 |
|
3712 |
|
3713 |
|
3714 |
|
3715 |
|
3716 |
|
3717 | Assertion.add("keys", function(keys) {
|
3718 | keys = aSlice.call(arguments);
|
3719 |
|
3720 | var obj = Object(this.obj);
|
3721 |
|
3722 |
|
3723 | var missingKeys = keys.filter(function(key) {
|
3724 | return !has(obj, key);
|
3725 | });
|
3726 |
|
3727 | var verb = "to have " + (this.onlyThis ? "only " : "") + (keys.length === 1 ? "key " : "keys ");
|
3728 |
|
3729 | this.params = { operator: verb + keys.join(", ") };
|
3730 |
|
3731 | if (missingKeys.length > 0) {
|
3732 | this.params.operator += "\n\tmissing keys: " + missingKeys.join(", ");
|
3733 | }
|
3734 |
|
3735 | this.assert(missingKeys.length === 0);
|
3736 |
|
3737 | if (this.onlyThis) {
|
3738 | should(obj).have.size(keys.length);
|
3739 | }
|
3740 | });
|
3741 |
|
3742 | Assertion.add("key", function(key) {
|
3743 | this.have.keys(key);
|
3744 | this.obj = get(this.obj, key);
|
3745 | });
|
3746 |
|
3747 | /**
|
3748 | * Asserts given object has such value for given key
|
3749 | *
|
3750 | * @name value
|
3751 | * @memberOf Assertion
|
3752 | * @category assertion property
|
3753 | * @param {*} key Key to check
|
3754 | * @param {*} value Value to check
|
3755 | * @example
|
3756 | *
|
3757 | * ({ a: 10 }).should.have.value('a', 10);
|
3758 | * (new Map([[1, 2]])).should.have.value(1, 2);
|
3759 | */
|
3760 | Assertion.add("value", function(key, value) {
|
3761 | this.have.key(key).which.is.eql(value);
|
3762 | });
|
3763 |
|
3764 | |
3765 |
|
3766 |
|
3767 |
|
3768 |
|
3769 |
|
3770 |
|
3771 |
|
3772 |
|
3773 |
|
3774 |
|
3775 |
|
3776 | Assertion.add("size", function(s) {
|
3777 | this.params = { operator: "to have size " + s };
|
3778 | should(size(this.obj)).be.exactly(s);
|
3779 | });
|
3780 |
|
3781 | |
3782 |
|
3783 |
|
3784 |
|
3785 |
|
3786 |
|
3787 |
|
3788 |
|
3789 |
|
3790 |
|
3791 |
|
3792 | Assertion.add("propertyByPath", function(properties) {
|
3793 | properties = aSlice.call(arguments);
|
3794 |
|
3795 | var allProps = properties.map(formatProp);
|
3796 |
|
3797 | properties = properties.map(convertPropertyName);
|
3798 |
|
3799 | var obj = should(Object(this.obj));
|
3800 |
|
3801 | var foundProperties = [];
|
3802 |
|
3803 | var currentProperty;
|
3804 | while (properties.length) {
|
3805 | currentProperty = properties.shift();
|
3806 | this.params = {
|
3807 | operator:
|
3808 | "to have property by path " +
|
3809 | allProps.join(", ") +
|
3810 | " - failed on " +
|
3811 | formatProp(currentProperty)
|
3812 | };
|
3813 | obj = obj.have.property(currentProperty);
|
3814 | foundProperties.push(currentProperty);
|
3815 | }
|
3816 |
|
3817 | this.params = {
|
3818 | obj: this.obj,
|
3819 | operator: "to have property by path " + allProps.join(", ")
|
3820 | };
|
3821 |
|
3822 | this.obj = obj.obj;
|
3823 | });
|
3824 | }
|
3825 |
|
3826 |
|
3827 |
|
3828 |
|
3829 |
|
3830 |
|
3831 |
|
3832 | function errorAssertions(should, Assertion) {
|
3833 | var i = should.format;
|
3834 |
|
3835 | |
3836 |
|
3837 |
|
3838 |
|
3839 |
|
3840 |
|
3841 |
|
3842 |
|
3843 |
|
3844 |
|
3845 |
|
3846 |
|
3847 |
|
3848 |
|
3849 |
|
3850 |
|
3851 |
|
3852 |
|
3853 |
|
3854 |
|
3855 |
|
3856 |
|
3857 |
|
3858 |
|
3859 | Assertion.add("throw", function(message, properties) {
|
3860 | var fn = this.obj;
|
3861 | var err = {};
|
3862 | var errorInfo = "";
|
3863 | var thrown = false;
|
3864 |
|
3865 | if (isGeneratorFunction(fn)) {
|
3866 | return should(fn()).throw(message, properties);
|
3867 | } else if (isIterator(fn)) {
|
3868 | return should(fn.next.bind(fn)).throw(message, properties);
|
3869 | }
|
3870 |
|
3871 | this.is.a.Function();
|
3872 |
|
3873 | var errorMatched = true;
|
3874 |
|
3875 | try {
|
3876 | fn();
|
3877 | } catch (e) {
|
3878 | thrown = true;
|
3879 | err = e;
|
3880 | }
|
3881 |
|
3882 | if (thrown) {
|
3883 | if (message) {
|
3884 | if ("string" == typeof message) {
|
3885 | errorMatched = message == err.message;
|
3886 | } else if (message instanceof RegExp) {
|
3887 | errorMatched = message.test(err.message);
|
3888 | } else if ("function" == typeof message) {
|
3889 | errorMatched = err instanceof message;
|
3890 | } else if (null != message) {
|
3891 | try {
|
3892 | should(err).match(message);
|
3893 | } catch (e) {
|
3894 | if (e instanceof should.AssertionError) {
|
3895 | errorInfo = ": " + e.message;
|
3896 | errorMatched = false;
|
3897 | } else {
|
3898 | throw e;
|
3899 | }
|
3900 | }
|
3901 | }
|
3902 |
|
3903 | if (!errorMatched) {
|
3904 | if ("string" == typeof message || message instanceof RegExp) {
|
3905 | errorInfo =
|
3906 | " with a message matching " +
|
3907 | i(message) +
|
3908 | ", but got '" +
|
3909 | err.message +
|
3910 | "'";
|
3911 | } else if ("function" == typeof message) {
|
3912 | errorInfo =
|
3913 | " of type " +
|
3914 | functionName$1(message) +
|
3915 | ", but got " +
|
3916 | functionName$1(err.constructor);
|
3917 | }
|
3918 | } else if ("function" == typeof message && properties) {
|
3919 | try {
|
3920 | should(err).match(properties);
|
3921 | } catch (e) {
|
3922 | if (e instanceof should.AssertionError) {
|
3923 | errorInfo = ": " + e.message;
|
3924 | errorMatched = false;
|
3925 | } else {
|
3926 | throw e;
|
3927 | }
|
3928 | }
|
3929 | }
|
3930 | } else {
|
3931 | errorInfo = " (got " + i(err) + ")";
|
3932 | }
|
3933 | }
|
3934 |
|
3935 | this.params = { operator: "to throw exception" + errorInfo };
|
3936 |
|
3937 | this.assert(thrown);
|
3938 | this.assert(errorMatched);
|
3939 | });
|
3940 |
|
3941 | Assertion.alias("throw", "throwError");
|
3942 | }
|
3943 |
|
3944 |
|
3945 |
|
3946 |
|
3947 |
|
3948 |
|
3949 |
|
3950 |
|
3951 | function matchingAssertions(should, Assertion) {
|
3952 | var i = should.format;
|
3953 |
|
3954 | |
3955 |
|
3956 |
|
3957 |
|
3958 |
|
3959 |
|
3960 |
|
3961 |
|
3962 |
|
3963 |
|
3964 |
|
3965 |
|
3966 |
|
3967 |
|
3968 |
|
3969 |
|
3970 |
|
3971 |
|
3972 |
|
3973 |
|
3974 |
|
3975 |
|
3976 |
|
3977 |
|
3978 |
|
3979 |
|
3980 |
|
3981 |
|
3982 |
|
3983 |
|
3984 |
|
3985 |
|
3986 |
|
3987 |
|
3988 |
|
3989 |
|
3990 |
|
3991 |
|
3992 |
|
3993 |
|
3994 |
|
3995 |
|
3996 |
|
3997 |
|
3998 |
|
3999 |
|
4000 |
|
4001 |
|
4002 |
|
4003 |
|
4004 |
|
4005 |
|
4006 |
|
4007 |
|
4008 |
|
4009 |
|
4010 | Assertion.add("match", function(other, description) {
|
4011 | this.params = { operator: "to match " + i(other), message: description };
|
4012 |
|
4013 | if (eq(this.obj, other).length !== 0) {
|
4014 | if (other instanceof RegExp) {
|
4015 |
|
4016 |
|
4017 | if (typeof this.obj == "string") {
|
4018 | this.assert(other.exec(this.obj));
|
4019 | } else if (null != this.obj && typeof this.obj == "object") {
|
4020 | var notMatchedProps = [],
|
4021 | matchedProps = [];
|
4022 | forEach(
|
4023 | this.obj,
|
4024 | function(value, name) {
|
4025 | if (other.exec(value)) {
|
4026 | matchedProps.push(formatProp(name));
|
4027 | } else {
|
4028 | notMatchedProps.push(formatProp(name) + " (" + i(value) + ")");
|
4029 | }
|
4030 | },
|
4031 | this
|
4032 | );
|
4033 |
|
4034 | if (notMatchedProps.length) {
|
4035 | this.params.operator += "\n not matched properties: " + notMatchedProps.join(", ");
|
4036 | }
|
4037 | if (matchedProps.length) {
|
4038 | this.params.operator += "\n matched properties: " + matchedProps.join(", ");
|
4039 | }
|
4040 |
|
4041 | this.assert(notMatchedProps.length === 0);
|
4042 | } else {
|
4043 |
|
4044 | this.assert(false);
|
4045 | }
|
4046 | } else if (typeof other == "function") {
|
4047 | var res;
|
4048 |
|
4049 | res = other(this.obj);
|
4050 |
|
4051 |
|
4052 | if (typeof res == "boolean") {
|
4053 | this.assert(res);
|
4054 | }
|
4055 | } else if (typeof this.obj == "object" && this.obj != null && (isPlainObject(other) || Array.isArray(other))) {
|
4056 |
|
4057 | notMatchedProps = [];
|
4058 | matchedProps = [];
|
4059 |
|
4060 | forEach(
|
4061 | other,
|
4062 | function(value, key) {
|
4063 | try {
|
4064 | should(this.obj)
|
4065 | .have.property(key)
|
4066 | .which.match(value);
|
4067 | matchedProps.push(formatProp(key));
|
4068 | } catch (e) {
|
4069 | if (e instanceof should.AssertionError) {
|
4070 | notMatchedProps.push(formatProp(key) + " (" + i(this.obj[key]) + ")");
|
4071 | } else {
|
4072 | throw e;
|
4073 | }
|
4074 | }
|
4075 | },
|
4076 | this
|
4077 | );
|
4078 |
|
4079 | if (notMatchedProps.length) {
|
4080 | this.params.operator += "\n not matched properties: " + notMatchedProps.join(", ");
|
4081 | }
|
4082 | if (matchedProps.length) {
|
4083 | this.params.operator += "\n matched properties: " + matchedProps.join(", ");
|
4084 | }
|
4085 |
|
4086 | this.assert(notMatchedProps.length === 0);
|
4087 | } else {
|
4088 | this.assert(false);
|
4089 | }
|
4090 | }
|
4091 | });
|
4092 |
|
4093 | |
4094 |
|
4095 |
|
4096 |
|
4097 |
|
4098 |
|
4099 |
|
4100 |
|
4101 |
|
4102 |
|
4103 |
|
4104 |
|
4105 |
|
4106 |
|
4107 |
|
4108 |
|
4109 |
|
4110 |
|
4111 |
|
4112 |
|
4113 |
|
4114 | Assertion.add("matchEach", function(other, description) {
|
4115 | this.params = {
|
4116 | operator: "to match each " + i(other),
|
4117 | message: description
|
4118 | };
|
4119 |
|
4120 | forEach(
|
4121 | this.obj,
|
4122 | function(value) {
|
4123 | should(value).match(other);
|
4124 | },
|
4125 | this
|
4126 | );
|
4127 | });
|
4128 |
|
4129 | |
4130 |
|
4131 |
|
4132 |
|
4133 |
|
4134 |
|
4135 |
|
4136 |
|
4137 |
|
4138 |
|
4139 |
|
4140 |
|
4141 |
|
4142 |
|
4143 |
|
4144 |
|
4145 |
|
4146 |
|
4147 |
|
4148 |
|
4149 |
|
4150 | Assertion.add("matchAny", function(other, description) {
|
4151 | this.params = {
|
4152 | operator: "to match any " + i(other),
|
4153 | message: description
|
4154 | };
|
4155 |
|
4156 | this.assert(
|
4157 | some(this.obj, function(value) {
|
4158 | try {
|
4159 | should(value).match(other);
|
4160 | return true;
|
4161 | } catch (e) {
|
4162 | if (e instanceof should.AssertionError) {
|
4163 |
|
4164 | return false;
|
4165 | }
|
4166 | throw e;
|
4167 | }
|
4168 | })
|
4169 | );
|
4170 | });
|
4171 |
|
4172 | Assertion.alias("matchAny", "matchSome");
|
4173 | Assertion.alias("matchEach", "matchEvery");
|
4174 | }
|
4175 |
|
4176 |
|
4177 |
|
4178 |
|
4179 |
|
4180 |
|
4181 |
|
4182 |
|
4183 |
|
4184 |
|
4185 |
|
4186 |
|
4187 |
|
4188 |
|
4189 |
|
4190 |
|
4191 |
|
4192 | function should$1(obj) {
|
4193 | return new Assertion(obj);
|
4194 | }
|
4195 |
|
4196 | should$1.AssertionError = AssertionError;
|
4197 | should$1.Assertion = Assertion;
|
4198 |
|
4199 |
|
4200 | should$1.modules = {
|
4201 | format: defaultFormat,
|
4202 | type: getGlobalType,
|
4203 | equal: eq
|
4204 | };
|
4205 | should$1.format = format$2;
|
4206 |
|
4207 |
|
4208 |
|
4209 |
|
4210 |
|
4211 |
|
4212 |
|
4213 |
|
4214 |
|
4215 |
|
4216 |
|
4217 |
|
4218 |
|
4219 |
|
4220 |
|
4221 |
|
4222 |
|
4223 |
|
4224 |
|
4225 |
|
4226 |
|
4227 |
|
4228 |
|
4229 | should$1.config = config;
|
4230 |
|
4231 |
|
4232 |
|
4233 |
|
4234 |
|
4235 |
|
4236 |
|
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 |
|
4244 |
|
4245 |
|
4246 |
|
4247 |
|
4248 |
|
4249 |
|
4250 |
|
4251 | should$1.extend = function(propertyName, proto) {
|
4252 | propertyName = propertyName || "should";
|
4253 | proto = proto || Object.prototype;
|
4254 |
|
4255 | var prevDescriptor = Object.getOwnPropertyDescriptor(proto, propertyName);
|
4256 |
|
4257 | Object.defineProperty(proto, propertyName, {
|
4258 | set: function() {},
|
4259 | get: function() {
|
4260 | return should$1(isWrapperType(this) ? this.valueOf() : this);
|
4261 | },
|
4262 | configurable: true
|
4263 | });
|
4264 |
|
4265 | return { name: propertyName, descriptor: prevDescriptor, proto: proto };
|
4266 | };
|
4267 |
|
4268 |
|
4269 |
|
4270 |
|
4271 |
|
4272 |
|
4273 |
|
4274 |
|
4275 |
|
4276 |
|
4277 |
|
4278 |
|
4279 |
|
4280 |
|
4281 |
|
4282 |
|
4283 |
|
4284 |
|
4285 |
|
4286 |
|
4287 | should$1.noConflict = function(desc) {
|
4288 | desc = desc || should$1._prevShould;
|
4289 |
|
4290 | if (desc) {
|
4291 | delete desc.proto[desc.name];
|
4292 |
|
4293 | if (desc.descriptor) {
|
4294 | Object.defineProperty(desc.proto, desc.name, desc.descriptor);
|
4295 | }
|
4296 | }
|
4297 | return should$1;
|
4298 | };
|
4299 |
|
4300 |
|
4301 |
|
4302 |
|
4303 |
|
4304 |
|
4305 |
|
4306 |
|
4307 |
|
4308 |
|
4309 |
|
4310 |
|
4311 |
|
4312 |
|
4313 |
|
4314 |
|
4315 |
|
4316 |
|
4317 | should$1.use = function(f) {
|
4318 | f(should$1, should$1.Assertion);
|
4319 | return this;
|
4320 | };
|
4321 |
|
4322 | should$1
|
4323 | .use(assertExtensions)
|
4324 | .use(chainAssertions)
|
4325 | .use(booleanAssertions)
|
4326 | .use(numberAssertions)
|
4327 | .use(equalityAssertions)
|
4328 | .use(typeAssertions)
|
4329 | .use(stringAssertions)
|
4330 | .use(propertyAssertions)
|
4331 | .use(errorAssertions)
|
4332 | .use(matchingAssertions)
|
4333 | .use(containAssertions)
|
4334 | .use(promiseAssertions);
|
4335 |
|
4336 | var defaultProto = Object.prototype;
|
4337 | var defaultProperty = "should";
|
4338 |
|
4339 | var freeGlobal =
|
4340 | typeof global == "object" && global && global.Object === Object && global;
|
4341 |
|
4342 |
|
4343 | var freeSelf =
|
4344 | typeof self == "object" && self && self.Object === Object && self;
|
4345 |
|
4346 |
|
4347 | var root = freeGlobal || freeSelf || Function("return this")();
|
4348 |
|
4349 |
|
4350 | try {
|
4351 | var prevShould = should$1.extend(defaultProperty, defaultProto);
|
4352 | should$1._prevShould = prevShould;
|
4353 |
|
4354 | Object.defineProperty(root, "should", {
|
4355 | enumerable: false,
|
4356 | configurable: true,
|
4357 | value: should$1
|
4358 | });
|
4359 | } catch (e) {
|
4360 |
|
4361 | }
|
4362 |
|
4363 | if (typeof define === "function" && define.amd) {
|
4364 | define([], function() {
|
4365 | return should$1;
|
4366 | });
|
4367 | } else if (typeof module === "object" && module.exports) {
|
4368 | module.exports = should$1;
|
4369 | }
|
4370 |
|
4371 | }());
|