shim.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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'))),
  9. [3, o, 'raz'], "Arguments");
  10. a.deep(t((function () { return arguments; }(3))), [3],
  11. "Arguments with one numeric value");
  12. a.deep(t({ 0: 'raz', 1: 'dwa', length: 2 }), ['raz', 'dwa'], "Other");
  13. a.deep(t(o, function (val) { return (val + 2) * 10; }, 10), [30, 40, 50],
  14. "Mapping");
  15. a.throws(function () { t(); }, TypeError, "Undefined");
  16. a.deep(t(3), [], "Primitive");
  17. a(t.length, 1, "Length");
  18. a.deep(t({ length: 0 }), [], "No values Array-like");
  19. a.deep(t({ length: -1 }), [], "Invalid length Array-like");
  20. a.deep(t({ length: -Infinity }), [], "Invalid length Array-like #2");
  21. a.throws(function () { t(undefined); }, TypeError, "Undefined");
  22. a.throws(function () { t(null); }, TypeError, "Null");
  23. a.deep(t(false), [], "Boolean");
  24. a.deep(t(-Infinity), [], "Inifity");
  25. a.deep(t(-0), [], "-0");
  26. a.deep(t(+0), [], "+0");
  27. a.deep(t(1), [], "1");
  28. a.deep(t(+Infinity), [], "+Infinity");
  29. a.deep(t({}), [], "Plain object");
  30. a.deep(t({ length: 1 }), [undefined], "Sparse array-like");
  31. a.deep(t({ '0': 'a', '1': 'b', length: 2 }, function (x) { return x + x; }), ['aa', 'bb'],
  32. "Map");
  33. a.deep(t({ '0': 'a', '1': 'b', length: 2 }, function (x) { return String(this); }, undefined),
  34. ['undefined', 'undefined'], "Map context");
  35. a.deep(t({ '0': 'a', '1': 'b', length: 2 }, function (x) { return String(this); }, 'x'),
  36. ['x', 'x'], "Map primitive context");
  37. a.throws(function () { t({}, 'foo', 'x'); }, TypeError, "Non callable for map");
  38. a.deep(t.call(null, { length: 1, '0': 'a' }), ['a'], "Null context");
  39. a(t({ __proto__: { '0': 'abc', length: 1 } })[0], 'abc', "Values on prototype");
  40. a.throws(function () { t.call(function () { return Object.freeze({}); }, {}); },
  41. TypeError, "Contructor producing freezed objects");
  42. // Ensure no setters are called for the indexes
  43. // Ensure no setters are called for the indexes
  44. MyType = function () {};
  45. Object.defineProperty(MyType.prototype, '0', {
  46. set: function (x) { throw new Error('Setter called: ' + x); }
  47. });
  48. a.deep(t.call(MyType, { '0': 'abc', length: 1 }), { '0': 'abc', length: 1 },
  49. "Defined not set");
  50. };