shim.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 }), [{ 0: "a", 1: "b", 2: "c", length: 3 }],
  24. "Array like"
  25. );
  26. a.deep(
  27. t(undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)),
  28. [undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)], "Falsy arguments"
  29. );
  30. a.h1("Null context");
  31. a.deep(t.call(null, "abc"), ["abc"], "String");
  32. a.deep(t.call(null, undefined), [undefined], "Undefined");
  33. a.deep(t.call(null, null), [null], "Null");
  34. a.deep(t.call(null, false), [false], "Boolean");
  35. a.deep(t.call(null, -Infinity), [-Infinity], "-Infinity");
  36. a.deep(t.call(null, -0), [-0], "-0");
  37. a.deep(t.call(null, +0), [+0], "+0");
  38. a.deep(t.call(null, 1), [1], "1");
  39. a.deep(t.call(null, 1, 2, 3), [1, 2, 3], "Numeric");
  40. a.deep(t.call(null, Number(Infinity)), [Number(Infinity)], "+Infinity");
  41. a.deep(
  42. t.call(null, { 0: "a", 1: "b", 2: "c", length: 3 }),
  43. [{ 0: "a", 1: "b", 2: "c", length: 3 }], "Array-like"
  44. );
  45. a.deep(
  46. t.call(null, undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)),
  47. [undefined, null, false, -Infinity, -0, +0, 1, 2, Number(Infinity)], "Falsy"
  48. );
  49. a.h1("Other constructor context");
  50. a.deep(t.call(Object, 1, 2, 3), { 0: 1, 1: 2, 2: 3, length: 3 }, "Many arguments");
  51. testObject = Object(3);
  52. testObject[0] = 1;
  53. testObject[1] = 2;
  54. testObject[2] = 3;
  55. testObject.length = 3;
  56. a.deep(t.call(Object, 1, 2, 3), testObject, "Test object");
  57. a(t.call(Object).length, 0, "No arguments");
  58. a.throws(
  59. function () {
  60. t.call(function () { return Object.freeze({}); });
  61. },
  62. TypeError,
  63. "Frozen instance"
  64. );
  65. // Ensure no setters are called for the indexes
  66. MyType = function () {};
  67. defineProperty(MyType.prototype, "0", {
  68. set: function (x) { throw new Error("Setter called: " + x); }
  69. });
  70. a.deep(t.call(MyType, "abc"), { 0: "abc", length: 1 }, "Define, not set");
  71. };