Iterator.zip.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. var defineProperties = require('define-properties');
  3. var test = require('tape');
  4. var callBind = require('call-bind');
  5. var functionsHaveNames = require('functions-have-names')();
  6. var forEach = require('for-each');
  7. var debug = require('object-inspect');
  8. var v = require('es-value-fixtures');
  9. var hasSymbols = require('has-symbols/shams')();
  10. var mockProperty = require('mock-property');
  11. var index = require('../Iterator.zip');
  12. var impl = require('../Iterator.zip/implementation');
  13. var from = require('../Iterator.from/polyfill')();
  14. var isEnumerable = Object.prototype.propertyIsEnumerable;
  15. var testIterator = require('./helpers/testIterator');
  16. module.exports = {
  17. tests: function (zip, name, t) {
  18. t['throws'](
  19. function () { return new zip(); }, // eslint-disable-line new-cap
  20. TypeError,
  21. '`' + name + '` itself is not a constructor'
  22. );
  23. t['throws'](
  24. function () { return new zip({}); }, // eslint-disable-line new-cap
  25. TypeError,
  26. '`' + name + '` itself is not a constructor, with an argument'
  27. );
  28. forEach(v.primitives.concat(v.objects), function (nonIterator) {
  29. t['throws'](
  30. function () { zip(nonIterator, []); },
  31. TypeError,
  32. debug(nonIterator) + ' is not an iterable Object'
  33. );
  34. });
  35. t.test('actual iteration', { skip: !hasSymbols }, function (st) {
  36. forEach(v.nonFunctions, function (nonFunction) {
  37. if (nonFunction != null) {
  38. var badIterable = {};
  39. badIterable[Symbol.iterator] = nonFunction;
  40. st['throws'](
  41. function () { zip([[], badIterable, []]).next(); },
  42. TypeError,
  43. debug(badIterable) + ' is not a function'
  44. );
  45. }
  46. });
  47. forEach(v.strings, function (string) {
  48. st['throws'](
  49. function () { zip([string]); },
  50. TypeError,
  51. 'non-objects are not considered iterable'
  52. );
  53. });
  54. var arrayIt = zip([[1, 2, 3]]);
  55. st.equal(typeof arrayIt.next, 'function', 'has a `next` function');
  56. st.test('real iterators', { skip: !hasSymbols }, function (s2t) {
  57. var iter = [1, 2][Symbol.iterator]();
  58. testIterator(zip([iter, [3, 4]]), [[1, 3], [2, 4]], s2t, 'array iterator + array yields combined results');
  59. s2t.end();
  60. });
  61. st.test('observability in a replaced String iterator', function (s2t) {
  62. var originalStringIterator = String.prototype[Symbol.iterator];
  63. var observedType;
  64. s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
  65. get: function () {
  66. 'use strict'; // eslint-disable-line strict, lines-around-directive
  67. observedType = typeof this;
  68. return originalStringIterator;
  69. }
  70. }));
  71. zip([from('')]);
  72. s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
  73. zip([from(Object(''))]);
  74. s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
  75. s2t.end();
  76. });
  77. st.end();
  78. });
  79. },
  80. index: function () {
  81. test('Iterator.zip: index', function (t) {
  82. module.exports.tests(index, 'Iterator.zip', t);
  83. t.end();
  84. });
  85. },
  86. implementation: function () {
  87. test('Iterator.zip: implementation', function (t) {
  88. module.exports.tests(impl, 'Iterator.zip', t);
  89. t.end();
  90. });
  91. },
  92. shimmed: function () {
  93. test('Iterator.zip: shimmed', function (t) {
  94. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  95. st.equal(Iterator.zip.name, 'zip', 'Iterator.zip has name "zip"');
  96. st.end();
  97. });
  98. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  99. et.equal(false, isEnumerable.call(Iterator, 'zip'), 'Iterator.zip is not enumerable');
  100. et.end();
  101. });
  102. module.exports.tests(callBind(Iterator.zip, Iterator), 'Iterator.zip', t);
  103. t.end();
  104. });
  105. }
  106. };