Projekt

Obecné

Profil

Stáhnout (15.6 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
3
var objectAssign = require('object-assign');
4
5
// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
6
// original notice:
7
8
/*!
9
 * The buffer module from node.js, for the browser.
10
 *
11
 * @author   Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
12
 * @license  MIT
13
 */
14
function compare(a, b) {
15
  if (a === b) {
16
    return 0;
17
  }
18
19
  var x = a.length;
20
  var y = b.length;
21
22
  for (var i = 0, len = Math.min(x, y); i < len; ++i) {
23
    if (a[i] !== b[i]) {
24
      x = a[i];
25
      y = b[i];
26
      break;
27
    }
28
  }
29
30
  if (x < y) {
31
    return -1;
32
  }
33
  if (y < x) {
34
    return 1;
35
  }
36
  return 0;
37
}
38
function isBuffer(b) {
39
  if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
40
    return global.Buffer.isBuffer(b);
41
  }
42
  return !!(b != null && b._isBuffer);
43
}
44
45
// based on node assert, original notice:
46
// NB: The URL to the CommonJS spec is kept just for tradition.
47
//     node-assert has evolved a lot since then, both in API and behavior.
48
49
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
50
//
51
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
52
//
53
// Originally from narwhal.js (http://narwhaljs.org)
54
// Copyright (c) 2009 Thomas Robinson <280north.com>
55
//
56
// Permission is hereby granted, free of charge, to any person obtaining a copy
57
// of this software and associated documentation files (the 'Software'), to
58
// deal in the Software without restriction, including without limitation the
59
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
60
// sell copies of the Software, and to permit persons to whom the Software is
61
// furnished to do so, subject to the following conditions:
62
//
63
// The above copyright notice and this permission notice shall be included in
64
// all copies or substantial portions of the Software.
65
//
66
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
70
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
71
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
72
73
var util = require('util/');
74
var hasOwn = Object.prototype.hasOwnProperty;
75
var pSlice = Array.prototype.slice;
76
var functionsHaveNames = (function () {
77
  return function foo() {}.name === 'foo';
78
}());
79
function pToString (obj) {
80
  return Object.prototype.toString.call(obj);
81
}
82
function isView(arrbuf) {
83
  if (isBuffer(arrbuf)) {
84
    return false;
85
  }
86
  if (typeof global.ArrayBuffer !== 'function') {
87
    return false;
88
  }
89
  if (typeof ArrayBuffer.isView === 'function') {
90
    return ArrayBuffer.isView(arrbuf);
91
  }
92
  if (!arrbuf) {
93
    return false;
94
  }
95
  if (arrbuf instanceof DataView) {
96
    return true;
97
  }
98
  if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) {
99
    return true;
100
  }
101
  return false;
102
}
103
// 1. The assert module provides functions that throw
104
// AssertionError's when particular conditions are not met. The
105
// assert module must conform to the following interface.
106
107
var assert = module.exports = ok;
108
109
// 2. The AssertionError is defined in assert.
110
// new assert.AssertionError({ message: message,
111
//                             actual: actual,
112
//                             expected: expected })
113
114
var regex = /\s*function\s+([^\(\s]*)\s*/;
115
// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js
116
function getName(func) {
117
  if (!util.isFunction(func)) {
118
    return;
119
  }
120
  if (functionsHaveNames) {
121
    return func.name;
122
  }
123
  var str = func.toString();
124
  var match = str.match(regex);
125
  return match && match[1];
126
}
127
assert.AssertionError = function AssertionError(options) {
128
  this.name = 'AssertionError';
129
  this.actual = options.actual;
130
  this.expected = options.expected;
131
  this.operator = options.operator;
132
  if (options.message) {
133
    this.message = options.message;
134
    this.generatedMessage = false;
135
  } else {
136
    this.message = getMessage(this);
137
    this.generatedMessage = true;
138
  }
139
  var stackStartFunction = options.stackStartFunction || fail;
140
  if (Error.captureStackTrace) {
141
    Error.captureStackTrace(this, stackStartFunction);
142
  } else {
143
    // non v8 browsers so we can have a stacktrace
144
    var err = new Error();
145
    if (err.stack) {
146
      var out = err.stack;
147
148
      // try to strip useless frames
149
      var fn_name = getName(stackStartFunction);
150
      var idx = out.indexOf('\n' + fn_name);
151
      if (idx >= 0) {
152
        // once we have located the function frame
153
        // we need to strip out everything before it (and its line)
154
        var next_line = out.indexOf('\n', idx + 1);
155
        out = out.substring(next_line + 1);
156
      }
157
158
      this.stack = out;
159
    }
160
  }
161
};
162
163
// assert.AssertionError instanceof Error
164
util.inherits(assert.AssertionError, Error);
165
166
function truncate(s, n) {
167
  if (typeof s === 'string') {
168
    return s.length < n ? s : s.slice(0, n);
169
  } else {
170
    return s;
171
  }
172
}
173
function inspect(something) {
174
  if (functionsHaveNames || !util.isFunction(something)) {
175
    return util.inspect(something);
176
  }
177
  var rawname = getName(something);
178
  var name = rawname ? ': ' + rawname : '';
179
  return '[Function' +  name + ']';
180
}
181
function getMessage(self) {
182
  return truncate(inspect(self.actual), 128) + ' ' +
183
         self.operator + ' ' +
184
         truncate(inspect(self.expected), 128);
185
}
186
187
// At present only the three keys mentioned above are used and
188
// understood by the spec. Implementations or sub modules can pass
189
// other keys to the AssertionError's constructor - they will be
190
// ignored.
191
192
// 3. All of the following functions must throw an AssertionError
193
// when a corresponding condition is not met, with a message that
194
// may be undefined if not provided.  All assertion methods provide
195
// both the actual and expected values to the assertion error for
196
// display purposes.
197
198
function fail(actual, expected, message, operator, stackStartFunction) {
199
  throw new assert.AssertionError({
200
    message: message,
201
    actual: actual,
202
    expected: expected,
203
    operator: operator,
204
    stackStartFunction: stackStartFunction
205
  });
206
}
207
208
// EXTENSION! allows for well behaved errors defined elsewhere.
209
assert.fail = fail;
210
211
// 4. Pure assertion tests whether a value is truthy, as determined
212
// by !!guard.
213
// assert.ok(guard, message_opt);
214
// This statement is equivalent to assert.equal(true, !!guard,
215
// message_opt);. To test strictly for the value true, use
216
// assert.strictEqual(true, guard, message_opt);.
217
218
function ok(value, message) {
219
  if (!value) fail(value, true, message, '==', assert.ok);
220
}
221
assert.ok = ok;
222
223
// 5. The equality assertion tests shallow, coercive equality with
224
// ==.
225
// assert.equal(actual, expected, message_opt);
226
227
assert.equal = function equal(actual, expected, message) {
228
  if (actual != expected) fail(actual, expected, message, '==', assert.equal);
229
};
230
231
// 6. The non-equality assertion tests for whether two objects are not equal
232
// with != assert.notEqual(actual, expected, message_opt);
233
234
assert.notEqual = function notEqual(actual, expected, message) {
235
  if (actual == expected) {
236
    fail(actual, expected, message, '!=', assert.notEqual);
237
  }
238
};
239
240
// 7. The equivalence assertion tests a deep equality relation.
241
// assert.deepEqual(actual, expected, message_opt);
242
243
assert.deepEqual = function deepEqual(actual, expected, message) {
244
  if (!_deepEqual(actual, expected, false)) {
245
    fail(actual, expected, message, 'deepEqual', assert.deepEqual);
246
  }
247
};
248
249
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
250
  if (!_deepEqual(actual, expected, true)) {
251
    fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual);
252
  }
253
};
254
255
function _deepEqual(actual, expected, strict, memos) {
256
  // 7.1. All identical values are equivalent, as determined by ===.
257
  if (actual === expected) {
258
    return true;
259
  } else if (isBuffer(actual) && isBuffer(expected)) {
260
    return compare(actual, expected) === 0;
261
262
  // 7.2. If the expected value is a Date object, the actual value is
263
  // equivalent if it is also a Date object that refers to the same time.
264
  } else if (util.isDate(actual) && util.isDate(expected)) {
265
    return actual.getTime() === expected.getTime();
266
267
  // 7.3 If the expected value is a RegExp object, the actual value is
268
  // equivalent if it is also a RegExp object with the same source and
269
  // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
270
  } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
271
    return actual.source === expected.source &&
272
           actual.global === expected.global &&
273
           actual.multiline === expected.multiline &&
274
           actual.lastIndex === expected.lastIndex &&
275
           actual.ignoreCase === expected.ignoreCase;
276
277
  // 7.4. Other pairs that do not both pass typeof value == 'object',
278
  // equivalence is determined by ==.
279
  } else if ((actual === null || typeof actual !== 'object') &&
280
             (expected === null || typeof expected !== 'object')) {
281
    return strict ? actual === expected : actual == expected;
282
283
  // If both values are instances of typed arrays, wrap their underlying
284
  // ArrayBuffers in a Buffer each to increase performance
285
  // This optimization requires the arrays to have the same type as checked by
286
  // Object.prototype.toString (aka pToString). Never perform binary
287
  // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
288
  // bit patterns are not identical.
289
  } else if (isView(actual) && isView(expected) &&
290
             pToString(actual) === pToString(expected) &&
291
             !(actual instanceof Float32Array ||
292
               actual instanceof Float64Array)) {
293
    return compare(new Uint8Array(actual.buffer),
294
                   new Uint8Array(expected.buffer)) === 0;
295
296
  // 7.5 For all other Object pairs, including Array objects, equivalence is
297
  // determined by having the same number of owned properties (as verified
298
  // with Object.prototype.hasOwnProperty.call), the same set of keys
299
  // (although not necessarily the same order), equivalent values for every
300
  // corresponding key, and an identical 'prototype' property. Note: this
301
  // accounts for both named and indexed properties on Arrays.
302
  } else if (isBuffer(actual) !== isBuffer(expected)) {
303
    return false;
304
  } else {
305
    memos = memos || {actual: [], expected: []};
306
307
    var actualIndex = memos.actual.indexOf(actual);
308
    if (actualIndex !== -1) {
309
      if (actualIndex === memos.expected.indexOf(expected)) {
310
        return true;
311
      }
312
    }
313
314
    memos.actual.push(actual);
315
    memos.expected.push(expected);
316
317
    return objEquiv(actual, expected, strict, memos);
318
  }
319
}
320
321
function isArguments(object) {
322
  return Object.prototype.toString.call(object) == '[object Arguments]';
323
}
324
325
function objEquiv(a, b, strict, actualVisitedObjects) {
326
  if (a === null || a === undefined || b === null || b === undefined)
327
    return false;
328
  // if one is a primitive, the other must be same
329
  if (util.isPrimitive(a) || util.isPrimitive(b))
330
    return a === b;
331
  if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
332
    return false;
333
  var aIsArgs = isArguments(a);
334
  var bIsArgs = isArguments(b);
335
  if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
336
    return false;
337
  if (aIsArgs) {
338
    a = pSlice.call(a);
339
    b = pSlice.call(b);
340
    return _deepEqual(a, b, strict);
341
  }
342
  var ka = objectKeys(a);
343
  var kb = objectKeys(b);
344
  var key, i;
345
  // having the same number of owned properties (keys incorporates
346
  // hasOwnProperty)
347
  if (ka.length !== kb.length)
348
    return false;
349
  //the same set of keys (although not necessarily the same order),
350
  ka.sort();
351
  kb.sort();
352
  //~~~cheap key test
353
  for (i = ka.length - 1; i >= 0; i--) {
354
    if (ka[i] !== kb[i])
355
      return false;
356
  }
357
  //equivalent values for every corresponding key, and
358
  //~~~possibly expensive deep test
359
  for (i = ka.length - 1; i >= 0; i--) {
360
    key = ka[i];
361
    if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
362
      return false;
363
  }
364
  return true;
365
}
366
367
// 8. The non-equivalence assertion tests for any deep inequality.
368
// assert.notDeepEqual(actual, expected, message_opt);
369
370
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
371
  if (_deepEqual(actual, expected, false)) {
372
    fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
373
  }
374
};
375
376
assert.notDeepStrictEqual = notDeepStrictEqual;
377
function notDeepStrictEqual(actual, expected, message) {
378
  if (_deepEqual(actual, expected, true)) {
379
    fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
380
  }
381
}
382
383
384
// 9. The strict equality assertion tests strict equality, as determined by ===.
385
// assert.strictEqual(actual, expected, message_opt);
386
387
assert.strictEqual = function strictEqual(actual, expected, message) {
388
  if (actual !== expected) {
389
    fail(actual, expected, message, '===', assert.strictEqual);
390
  }
391
};
392
393
// 10. The strict non-equality assertion tests for strict inequality, as
394
// determined by !==.  assert.notStrictEqual(actual, expected, message_opt);
395
396
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
397
  if (actual === expected) {
398
    fail(actual, expected, message, '!==', assert.notStrictEqual);
399
  }
400
};
401
402
function expectedException(actual, expected) {
403
  if (!actual || !expected) {
404
    return false;
405
  }
406
407
  if (Object.prototype.toString.call(expected) == '[object RegExp]') {
408
    return expected.test(actual);
409
  }
410
411
  try {
412
    if (actual instanceof expected) {
413
      return true;
414
    }
415
  } catch (e) {
416
    // Ignore.  The instanceof check doesn't work for arrow functions.
417
  }
418
419
  if (Error.isPrototypeOf(expected)) {
420
    return false;
421
  }
422
423
  return expected.call({}, actual) === true;
424
}
425
426
function _tryBlock(block) {
427
  var error;
428
  try {
429
    block();
430
  } catch (e) {
431
    error = e;
432
  }
433
  return error;
434
}
435
436
function _throws(shouldThrow, block, expected, message) {
437
  var actual;
438
439
  if (typeof block !== 'function') {
440
    throw new TypeError('"block" argument must be a function');
441
  }
442
443
  if (typeof expected === 'string') {
444
    message = expected;
445
    expected = null;
446
  }
447
448
  actual = _tryBlock(block);
449
450
  message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
451
            (message ? ' ' + message : '.');
452
453
  if (shouldThrow && !actual) {
454
    fail(actual, expected, 'Missing expected exception' + message);
455
  }
456
457
  var userProvidedMessage = typeof message === 'string';
458
  var isUnwantedException = !shouldThrow && util.isError(actual);
459
  var isUnexpectedException = !shouldThrow && actual && !expected;
460
461
  if ((isUnwantedException &&
462
      userProvidedMessage &&
463
      expectedException(actual, expected)) ||
464
      isUnexpectedException) {
465
    fail(actual, expected, 'Got unwanted exception' + message);
466
  }
467
468
  if ((shouldThrow && actual && expected &&
469
      !expectedException(actual, expected)) || (!shouldThrow && actual)) {
470
    throw actual;
471
  }
472
}
473
474
// 11. Expected to throw an error:
475
// assert.throws(block, Error_opt, message_opt);
476
477
assert.throws = function(block, /*optional*/error, /*optional*/message) {
478
  _throws(true, block, error, message);
479
};
480
481
// EXTENSION! This is annoying to write outside this module.
482
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
483
  _throws(false, block, error, message);
484
};
485
486
assert.ifError = function(err) { if (err) throw err; };
487
488
// Expose a strict only variant of assert
489
function strict(value, message) {
490
  if (!value) fail(value, true, message, '==', strict);
491
}
492
assert.strict = objectAssign(strict, assert, {
493
  equal: assert.strictEqual,
494
  deepEqual: assert.deepStrictEqual,
495
  notEqual: assert.notStrictEqual,
496
  notDeepEqual: assert.notDeepStrictEqual
497
});
498
assert.strict.strict = assert.strict;
499
500
var objectKeys = Object.keys || function (obj) {
501
  var keys = [];
502
  for (var key in obj) {
503
    if (hasOwn.call(obj, key)) keys.push(key);
504
  }
505
  return keys;
506
};