Iterator.from.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 hasProto = require('has-proto')();
  7. var forEach = require('for-each');
  8. var debug = require('object-inspect');
  9. var v = require('es-value-fixtures');
  10. var hasSymbols = require('has-symbols/shams')();
  11. var mockProperty = require('mock-property');
  12. var index = require('../Iterator.from');
  13. var impl = require('../Iterator.from/implementation');
  14. var isEnumerable = Object.prototype.propertyIsEnumerable;
  15. var testIterator = require('./helpers/testIterator');
  16. var $Iterator = require('../Iterator/implementation');
  17. var iterProto = require('iterator.prototype');
  18. var getCodePoints = function getCodePoints(str) {
  19. var chars = [];
  20. for (var i = 0; i < str.length; i++) {
  21. var c1 = str.charCodeAt(i);
  22. if (c1 >= 0xD800 && c1 < 0xDC00 && i + 1 < str.length) {
  23. var c2 = str.charCodeAt(i + 1);
  24. if (c2 >= 0xDC00 && c2 < 0xE000) {
  25. chars.push(str.charAt(i) + str.charAt(i + 1));
  26. i += 1;
  27. continue; // eslint-disable-line no-continue, no-restricted-syntax
  28. }
  29. }
  30. chars.push(str.charAt(i));
  31. }
  32. return chars;
  33. };
  34. module.exports = {
  35. tests: function (from, name, t) {
  36. t['throws'](
  37. function () { return new from(); }, // eslint-disable-line new-cap
  38. TypeError,
  39. '`' + name + '` itself is not a constructor'
  40. );
  41. t['throws'](
  42. function () { return new from({}); }, // eslint-disable-line new-cap
  43. TypeError,
  44. '`' + name + '` itself is not a constructor, with an argument'
  45. );
  46. forEach(v.primitives.concat(v.objects), function (nonIterator) {
  47. if (typeof nonIterator !== 'string') {
  48. t['throws'](
  49. function () { from(nonIterator).next(); },
  50. TypeError,
  51. debug(nonIterator) + ' is not an iterable Object'
  52. );
  53. }
  54. });
  55. t.test('actual iteration', { skip: !hasSymbols }, function (st) {
  56. forEach(v.nonFunctions, function (nonFunction) {
  57. var badIterable = {};
  58. badIterable[Symbol.iterator] = nonFunction;
  59. st['throws'](
  60. function () { from(badIterable).next(); },
  61. TypeError,
  62. debug(badIterable) + ' is not a function'
  63. );
  64. });
  65. // st['throws'](
  66. // function () { return new from([]); }, // eslint-disable-line new-cap
  67. // RangeError,
  68. // '`' + name + '` iterator is not a constructor'
  69. // );
  70. forEach(v.strings, function (string) {
  71. var stringIt = from(string);
  72. testIterator(stringIt, getCodePoints(string), st, 'string iterator: ' + debug(string));
  73. });
  74. var arrayIt = from([1, 2, 3]);
  75. st.equal(typeof arrayIt.next, 'function', 'has a `next` function');
  76. st.test('__proto__ is Iterator.prototype', { skip: !hasProto }, function (s2t) {
  77. var fakeIterator = {
  78. __proto__: iterProto,
  79. next: function () {}
  80. };
  81. s2t.ok(fakeIterator instanceof $Iterator, 'is an instanceof Iterator');
  82. s2t.equal(typeof fakeIterator.next, 'function', 'fake iterator `.next` is a function');
  83. s2t.equal(from(fakeIterator), fakeIterator, 'returns input when it is an instanceof Iterator');
  84. s2t.end();
  85. });
  86. st.test('real iterators', { skip: !hasSymbols }, function (s2t) {
  87. var iter = [][Symbol.iterator]();
  88. // eslint-disable-next-line no-proto
  89. var arrayIterHasIterProto = hasProto && iter.__proto__.__proto__ !== Object.prototype;
  90. s2t.equal(
  91. from(iter),
  92. iter,
  93. 'array iterator becomes itself',
  94. { skip: !arrayIterHasIterProto && 'node 0.12 - 3 do not have Iterator.prototype in the proto chains' }
  95. );
  96. s2t.end();
  97. });
  98. st.test('observability in a replaced String iterator', function (s2t) {
  99. var originalStringIterator = String.prototype[Symbol.iterator];
  100. var observedType;
  101. s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
  102. get: function () {
  103. 'use strict'; // eslint-disable-line strict, lines-around-directive
  104. observedType = typeof this;
  105. return originalStringIterator;
  106. }
  107. }));
  108. from('');
  109. s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
  110. from(Object(''));
  111. s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
  112. s2t.end();
  113. });
  114. st.end();
  115. });
  116. },
  117. index: function () {
  118. test('Iterator.from: index', function (t) {
  119. module.exports.tests(index, 'Iterator.from', t);
  120. t.end();
  121. });
  122. },
  123. implementation: function () {
  124. test('Iterator.from: implementation', function (t) {
  125. module.exports.tests(impl, 'Iterator.from', t);
  126. t.end();
  127. });
  128. },
  129. shimmed: function () {
  130. test('Iterator.from: shimmed', function (t) {
  131. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  132. st.equal(Iterator.from.name, 'from', 'Iterator.from has name "from"');
  133. st.end();
  134. });
  135. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  136. et.equal(false, isEnumerable.call(Iterator, 'from'), 'Iterator.from is not enumerable');
  137. et.end();
  138. });
  139. module.exports.tests(callBind(Iterator.from, Iterator), 'Iterator.from', t);
  140. t.end();
  141. });
  142. }
  143. };