source: imaps-frontend/node_modules/es-iterator-helpers/test/Iterator.from.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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