shim.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Some tests taken from: https://github.com/mathiasbynens/Array.from/blob/master/tests/tests.js
  2. "use strict";
  3. module.exports = function (t, a) {
  4. var o = [1, 2, 3], MyType;
  5. a.not(t(o), o, "Array");
  6. a.deep(t(o), o, "Array: same content");
  7. a.deep(t("12r3v"), ["1", "2", "r", "3", "v"], "String");
  8. a.deep(t((function () { return arguments; })(3, o, "raz")), [3, o, "raz"], "Arguments");
  9. a.deep(t((function () { return arguments; })(3)), [3], "Arguments with one numeric value");
  10. a.deep(t({ 0: "raz", 1: "dwa", length: 2 }), ["raz", "dwa"], "Other");
  11. a.deep(t(o, function (val) { return (val + 2) * 10; }, 10), [30, 40, 50], "Mapping");
  12. a.throws(function () { t(); }, TypeError, "Undefined");
  13. a.deep(t(3), [], "Primitive");
  14. a(t.length, 1, "Length");
  15. a.deep(t({ length: 0 }), [], "No values Array-like");
  16. a.deep(t({ length: -1 }), [], "Invalid length Array-like");
  17. a.deep(t({ length: -Infinity }), [], "Invalid length Array-like #2");
  18. a.throws(function () { t(undefined); }, TypeError, "Undefined");
  19. a.throws(function () { t(null); }, TypeError, "Null");
  20. a.deep(t(false), [], "Boolean");
  21. a.deep(t(-Infinity), [], "Inifity");
  22. a.deep(t(-0), [], "-0");
  23. a.deep(t(+0), [], "+0");
  24. a.deep(t(1), [], "1");
  25. a.deep(t(Number(Infinity)), [], "+Infinity");
  26. a.deep(t({}), [], "Plain object");
  27. a.deep(t({ length: 1 }), [undefined], "Sparse array-like");
  28. a.deep(t({ 0: "a", 1: "b", length: 2 }, function (x) { return x + x; }), ["aa", "bb"], "Map");
  29. a.deep(
  30. t({ 0: "a", 1: "b", length: 2 }, function () { return String(this); }, undefined),
  31. ["undefined", "undefined"], "Map context"
  32. );
  33. a.deep(
  34. t({ 0: "a", 1: "b", length: 2 }, function () { return String(this); }, "x"), ["x", "x"],
  35. "Map primitive context"
  36. );
  37. a.throws(function () { t({}, "foo", "x"); }, TypeError, "Non callable for map");
  38. a.deep(t({ length: 1, 0: "a" }), ["a"], "Null context");
  39. a(t({ __proto__: { 0: "abc", length: 1 } })[0], "abc", "Values on prototype");
  40. a.throws(
  41. function () {
  42. t.call(function () { return Object.freeze({}); }, {});
  43. },
  44. TypeError,
  45. "Contructor producing freezed objects"
  46. );
  47. // Ensure no setters are called for the indexes
  48. // Ensure no setters are called for the indexes
  49. MyType = function () {};
  50. Object.defineProperty(MyType.prototype, "0", {
  51. set: function (x) { throw new Error("Setter called: " + x); }
  52. });
  53. a.deep(t.call(MyType, { 0: "abc", length: 1 }), { 0: "abc", length: 1 }, "Defined not set");
  54. };