Iterator.from.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. s2t.equal(from(iter), iter, 'array iterator becomes itself');
  89. s2t.end();
  90. });
  91. st.test('observability in a replaced String iterator', function (s2t) {
  92. var originalStringIterator = String.prototype[Symbol.iterator];
  93. var observedType;
  94. s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
  95. get: function () {
  96. 'use strict'; // eslint-disable-line strict, lines-around-directive
  97. observedType = typeof this;
  98. return originalStringIterator;
  99. }
  100. }));
  101. from('');
  102. s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
  103. from(Object(''));
  104. s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
  105. s2t.end();
  106. });
  107. st.end();
  108. });
  109. },
  110. index: function () {
  111. test('Iterator.from: index', function (t) {
  112. module.exports.tests(index, 'Iterator.from', t);
  113. t.end();
  114. });
  115. },
  116. implementation: function () {
  117. test('Iterator.from: implementation', function (t) {
  118. module.exports.tests(impl, 'Iterator.from', t);
  119. t.end();
  120. });
  121. },
  122. shimmed: function () {
  123. test('Iterator.from: shimmed', function (t) {
  124. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  125. st.equal(Iterator.from.name, 'from', 'Iterator.from has name "from"');
  126. st.end();
  127. });
  128. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  129. et.equal(false, isEnumerable.call(Iterator, 'from'), 'Iterator.from is not enumerable');
  130. et.end();
  131. });
  132. module.exports.tests(callBind(Iterator.from, Iterator), 'Iterator.from', t);
  133. t.end();
  134. });
  135. }
  136. };