shim.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint no-useless-call: "off" */
  2. // Most tests taken from https://github.com/mathiasbynens/Array.of/blob/master/tests/tests.js
  3. // Thanks @mathiasbynens
  4. "use strict";
  5. var defineProperty = Object.defineProperty;
  6. module.exports = function (t, a) {
  7. var x = {}, testObject, MyType;
  8. a.deep(t(), [], "No arguments");
  9. a.deep(t(3), [3], "One numeric argument");
  10. a.deep(t(3, "raz", null, x, undefined), [3, "raz", null, x, undefined], "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(Number(Infinity)), [Number(Infinity)], "+Infinity");
  22. a.deep(
  23. t({ 0: "a", 1: "b", 2: "c", length: 3 }),
  24. [{ 0: "a", 1: "b", 2: "c", length: 3 }],
  25. "Array like"
  26. );
  27. a.deep(
  28. t(undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)),
  29. [undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)],
  30. "Falsy arguments"
  31. );
  32. a.h1("Null context");
  33. a.deep(t.call(null, "abc"), ["abc"], "String");
  34. a.deep(t.call(null, undefined), [undefined], "Undefined");
  35. a.deep(t.call(null, null), [null], "Null");
  36. a.deep(t.call(null, false), [false], "Boolean");
  37. a.deep(t.call(null, -Infinity), [-Infinity], "-Infinity");
  38. a.deep(t.call(null, -0), [-0], "-0");
  39. a.deep(t.call(null, +0), [+0], "+0");
  40. a.deep(t.call(null, 1), [1], "1");
  41. a.deep(t.call(null, 1, 2, 3), [1, 2, 3], "Numeric");
  42. a.deep(t.call(null, Number(Infinity)), [Number(Infinity)], "+Infinity");
  43. a.deep(
  44. t.call(null, { 0: "a", 1: "b", 2: "c", length: 3 }),
  45. [{ 0: "a", 1: "b", 2: "c", length: 3 }],
  46. "Array-like"
  47. );
  48. a.deep(
  49. t.call(null, undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)),
  50. [undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)],
  51. "Falsy"
  52. );
  53. a.h1("Other constructor context");
  54. a.deep(t.call(Object, 1, 2, 3), { 0: 1, 1: 2, 2: 3, length: 3 }, "Many arguments");
  55. testObject = Object(3);
  56. testObject[0] = 1;
  57. testObject[1] = 2;
  58. testObject[2] = 3;
  59. testObject.length = 3;
  60. a.deep(t.call(Object, 1, 2, 3), testObject, "Test object");
  61. a(t.call(Object).length, 0, "No arguments");
  62. a.throws(
  63. function () {
  64. t.call(function () {
  65. return Object.freeze({});
  66. });
  67. },
  68. TypeError,
  69. "Frozen instance"
  70. );
  71. // Ensure no setters are called for the indexes
  72. MyType = function () {};
  73. defineProperty(MyType.prototype, "0", {
  74. set: function (x) {
  75. throw new Error("Setter called: " + x);
  76. }
  77. });
  78. a.deep(t.call(MyType, "abc"), { 0: "abc", length: 1 }, "Define, not set");
  79. };