shim.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Most tests taken from https://github.com/mathiasbynens/Array.of/blob/master/tests/tests.js
  2. // Thanks @mathiasbynens
  3. 'use strict';
  4. var defineProperty = Object.defineProperty;
  5. module.exports = function (t, a) {
  6. var x = {}, testObject, MyType;
  7. a.deep(t(), [], "No arguments");
  8. a.deep(t(3), [3], "One numeric argument");
  9. a.deep(t(3, 'raz', null, x, undefined), [3, 'raz', null, x, undefined],
  10. "Many arguments");
  11. a(t.length, 0, "Length");
  12. a.deep(t('abc'), ['abc'], "String");
  13. a.deep(t(undefined), [undefined], "Undefined");
  14. a.deep(t(null), [null], "Null");
  15. a.deep(t(false), [false], "Boolean");
  16. a.deep(t(-Infinity), [-Infinity], "Infinity");
  17. a.deep(t(-0), [-0], "-0");
  18. a.deep(t(+0), [+0], "+0");
  19. a.deep(t(1), [1], "1");
  20. a.deep(t(1, 2, 3), [1, 2, 3], "Numeric args");
  21. a.deep(t(+Infinity), [+Infinity], "+Infinity");
  22. a.deep(t({ '0': 'a', '1': 'b', '2': 'c', length: 3 }),
  23. [{ '0': 'a', '1': 'b', '2': 'c', length: 3 }], "Array like");
  24. a.deep(t(undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity),
  25. [undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity], "Falsy arguments");
  26. a.h1("Null context");
  27. a.deep(t.call(null, 'abc'), ['abc'], "String");
  28. a.deep(t.call(null, undefined), [undefined], "Undefined");
  29. a.deep(t.call(null, null), [null], "Null");
  30. a.deep(t.call(null, false), [false], "Boolean");
  31. a.deep(t.call(null, -Infinity), [-Infinity], "-Infinity");
  32. a.deep(t.call(null, -0), [-0], "-0");
  33. a.deep(t.call(null, +0), [+0], "+0");
  34. a.deep(t.call(null, 1), [1], "1");
  35. a.deep(t.call(null, 1, 2, 3), [1, 2, 3], "Numeric");
  36. a.deep(t.call(null, +Infinity), [+Infinity], "+Infinity");
  37. a.deep(t.call(null, { '0': 'a', '1': 'b', '2': 'c', length: 3 }),
  38. [{ '0': 'a', '1': 'b', '2': 'c', length: 3 }], "Array-like");
  39. a.deep(t.call(null, undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity),
  40. [undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity], "Falsy");
  41. a.h1("Other constructor context");
  42. a.deep(t.call(Object, 1, 2, 3), { '0': 1, '1': 2, '2': 3, length: 3 }, "Many arguments");
  43. testObject = Object(3);
  44. testObject[0] = 1;
  45. testObject[1] = 2;
  46. testObject[2] = 3;
  47. testObject.length = 3;
  48. a.deep(t.call(Object, 1, 2, 3), testObject, "Test object");
  49. a(t.call(Object).length, 0, "No arguments");
  50. a.throws(function () { t.call(function () { return Object.freeze({}); }); }, TypeError,
  51. "Frozen instance");
  52. // Ensure no setters are called for the indexes
  53. MyType = function () {};
  54. defineProperty(MyType.prototype, '0', {
  55. set: function (x) { throw new Error('Setter called: ' + x); }
  56. });
  57. a.deep(t.call(MyType, 'abc'), { '0': 'abc', length: 1 }, "Define, not set");
  58. };