Iterator.zipKeyed.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.zipKeyed');
  12. var impl = require('../Iterator.zipKeyed/implementation');
  13. var from = require('../Iterator.from/polyfill')();
  14. var testIterator = require('./helpers/testIterator');
  15. var isEnumerable = Object.prototype.propertyIsEnumerable;
  16. module.exports = {
  17. tests: function (zipKeyed, name, t) {
  18. t['throws'](
  19. function () { return new zipKeyed(); }, // eslint-disable-line new-cap
  20. TypeError,
  21. '`' + name + '` itself is not a constructor'
  22. );
  23. t['throws'](
  24. function () { return new zipKeyed({}); }, // eslint-disable-line new-cap
  25. TypeError,
  26. '`' + name + '` itself is not a constructor, with an argument'
  27. );
  28. forEach(v.primitives, function (primitive) {
  29. t['throws'](
  30. function () { zipKeyed(primitive); },
  31. TypeError,
  32. debug(primitive) + ' is not an Object'
  33. );
  34. if (primitive != null) {
  35. t['throws'](
  36. function () { zipKeyed({ a: primitive }); },
  37. TypeError,
  38. 'key "a" on iterables object is ' + debug(primitive) + ' which is not an iterable Object'
  39. );
  40. }
  41. });
  42. forEach(v.objects, function (nonIterator) {
  43. t.doesNotThrow(function () { zipKeyed({ a: nonIterator }); }, 'does not throw until `.next()`');
  44. t['throws'](
  45. function () { zipKeyed({ a: nonIterator }).next(); },
  46. TypeError,
  47. 'key "a" on iterables object is ' + debug(nonIterator) + ' which is not an iterable Object'
  48. );
  49. });
  50. t.test('actual iteration', { skip: !hasSymbols }, function (st) {
  51. forEach(v.nonFunctions, function (nonFunction) {
  52. if (nonFunction != null) {
  53. var badIterable = {};
  54. badIterable[Symbol.iterator] = nonFunction;
  55. st['throws'](
  56. function () { zipKeyed({ a: [], b: badIterable, c: [] }).next(); },
  57. TypeError,
  58. 'key "b" on iterables object is ' + debug(badIterable) + ' is not a function'
  59. );
  60. }
  61. });
  62. forEach(v.strings, function (string) {
  63. st['throws'](
  64. function () { zipKeyed({ a: string }); },
  65. TypeError,
  66. 'key "a" on iterables object is an iterable primitive, but non-objects are not considered iterable'
  67. );
  68. });
  69. st.test('real iterators', { skip: !hasSymbols }, function (s2t) {
  70. var iter = [['a', 1], ['b', 2]][Symbol.iterator]();
  71. var iterator = zipKeyed({ a: iter, b: ['a', 3], c: ['b', 4] });
  72. testIterator(
  73. iterator,
  74. [
  75. { __proto__: null, a: ['a', 1], b: 'a', c: 'b' },
  76. { __proto__: null, a: ['b', 2], b: 3, c: 4 }
  77. ],
  78. s2t,
  79. 'array iterator + array yields combined results'
  80. );
  81. s2t.end();
  82. });
  83. st.test('observability in a replaced String iterator', function (s2t) {
  84. var originalStringIterator = String.prototype[Symbol.iterator];
  85. var observedType;
  86. s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
  87. get: function () {
  88. 'use strict'; // eslint-disable-line strict, lines-around-directive
  89. observedType = typeof this;
  90. return originalStringIterator;
  91. }
  92. }));
  93. zipKeyed([from('')]);
  94. s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
  95. zipKeyed([from(Object(''))]);
  96. s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
  97. s2t.end();
  98. });
  99. st.end();
  100. });
  101. },
  102. index: function () {
  103. test('Iterator.zipKeyed: index', function (t) {
  104. module.exports.tests(index, 'Iterator.zipKeyed', t);
  105. t.end();
  106. });
  107. },
  108. implementation: function () {
  109. test('Iterator.zipKeyed: implementation', function (t) {
  110. module.exports.tests(impl, 'Iterator.zipKeyed', t);
  111. t.end();
  112. });
  113. },
  114. shimmed: function () {
  115. test('Iterator.zipKeyed: shimmed', function (t) {
  116. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  117. st.equal(Iterator.zipKeyed.name, 'zipKeyed', 'Iterator.zipKeyed has name "zipKeyed"');
  118. st.end();
  119. });
  120. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  121. et.equal(false, isEnumerable.call(Iterator, 'zipKeyed'), 'Iterator.zipKeyed is not enumerable');
  122. et.end();
  123. });
  124. module.exports.tests(callBind(Iterator.zipKeyed, Iterator), 'Iterator.zipKeyed', t);
  125. t.end();
  126. });
  127. }
  128. };