shim.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(
  9. t(
  10. (function () {
  11. return arguments;
  12. })(3, o, "raz")
  13. ),
  14. [3, o, "raz"],
  15. "Arguments"
  16. );
  17. a.deep(
  18. t(
  19. (function () {
  20. return arguments;
  21. })(3)
  22. ),
  23. [3],
  24. "Arguments with one numeric value"
  25. );
  26. a.deep(t({ 0: "raz", 1: "dwa", length: 2 }), ["raz", "dwa"], "Other");
  27. a.deep(
  28. t(
  29. o,
  30. function (val) {
  31. return (val + 2) * 10;
  32. },
  33. 10
  34. ),
  35. [30, 40, 50],
  36. "Mapping"
  37. );
  38. a.throws(
  39. function () {
  40. t();
  41. },
  42. TypeError,
  43. "Undefined"
  44. );
  45. a.deep(t(3), [], "Primitive");
  46. a(t.length, 1, "Length");
  47. a.deep(t({ length: 0 }), [], "No values Array-like");
  48. a.deep(t({ length: -1 }), [], "Invalid length Array-like");
  49. a.deep(t({ length: -Infinity }), [], "Invalid length Array-like #2");
  50. a.throws(
  51. function () {
  52. t(undefined);
  53. },
  54. TypeError,
  55. "Undefined"
  56. );
  57. a.throws(
  58. function () {
  59. t(null);
  60. },
  61. TypeError,
  62. "Null"
  63. );
  64. a.deep(t(false), [], "Boolean");
  65. a.deep(t(-Infinity), [], "Inifity");
  66. a.deep(t(-0), [], "-0");
  67. a.deep(t(+0), [], "+0");
  68. a.deep(t(1), [], "1");
  69. a.deep(t(Number(Infinity)), [], "+Infinity");
  70. a.deep(t({}), [], "Plain object");
  71. a.deep(t({ length: 1 }), [undefined], "Sparse array-like");
  72. a.deep(
  73. t({ 0: "a", 1: "b", length: 2 }, function (x) {
  74. return x + x;
  75. }),
  76. ["aa", "bb"],
  77. "Map"
  78. );
  79. a.deep(
  80. t(
  81. { 0: "a", 1: "b", length: 2 },
  82. function () {
  83. return String(this);
  84. },
  85. undefined
  86. ),
  87. ["undefined", "undefined"],
  88. "Map context"
  89. );
  90. a.deep(
  91. t(
  92. { 0: "a", 1: "b", length: 2 },
  93. function () {
  94. return String(this);
  95. },
  96. "x"
  97. ),
  98. ["x", "x"],
  99. "Map primitive context"
  100. );
  101. a.throws(
  102. function () {
  103. t({}, "foo", "x");
  104. },
  105. TypeError,
  106. "Non callable for map"
  107. );
  108. a.deep(t({ length: 1, 0: "a" }), ["a"], "Null context");
  109. a(t({ __proto__: { 0: "abc", length: 1 } })[0], "abc", "Values on prototype");
  110. a.throws(
  111. function () {
  112. t.call(function () {
  113. return Object.freeze({});
  114. }, {});
  115. },
  116. TypeError,
  117. "Contructor producing freezed objects"
  118. );
  119. // Ensure no setters are called for the indexes
  120. // Ensure no setters are called for the indexes
  121. MyType = function () {};
  122. Object.defineProperty(MyType.prototype, "0", {
  123. set: function (x) {
  124. throw new Error("Setter called: " + x);
  125. }
  126. });
  127. a.deep(t.call(MyType, { 0: "abc", length: 1 }), { 0: "abc", length: 1 }, "Defined not set");
  128. };