index.js
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
var test = require('tape');
var Test = require('tape/lib/test');
var inspect = require('object-inspect');
// eslint-disable-next-line global-require
var hasSymbols = require('has-symbols')() || require('has-symbols/shams')();
var hasBigInts = require('has-bigints')();
var forEach = require('for-each');
var getIterator = process.env.TEST_VARIANT === 'node' ? require('../node') : require('../');
Test.prototype.iterate = function (value, expected, message) {
var i = 0;
this.test(message, function (t) {
var iterator = getIterator(value);
if (!iterator) {
t.fail(inspect(value) + ' is not iterable');
return t.end();
}
if (typeof iterator.next !== 'function') {
t.fail('iterator does not have a next function, got ' + inspect(iterator));
return t.end();
}
var result;
while ((result = iterator.next()) && !result.done) {
t.deepEqual(result.value, expected[i], 'index ' + i + ': expected ' + inspect(expected[i]) + ', got ' + inspect(result.value));
i += 1;
}
t.equal(i, expected.length, 'expected ' + expected.length + ' values, got ' + i + ' values');
t.end();
});
};
Test.prototype.noIterate = function (value) {
this.equal(getIterator(value), undefined, inspect(value) + ' is not iterable');
};
Test.prototype.fakeIterator = function (value) {
this.test(inspect(value) + ' with a fake iterator', { skip: !hasSymbols }, function (t) {
var fakeValues = ['fake', 'iterator', 'scary'];
var o = Object(value);
o[Symbol.iterator] = function () {
return getIterator(fakeValues);
};
t.iterate(o, fakeValues, inspect(o) + ' with an overwritten iterator method, yields those values instead');
t.end();
});
};
var getArguments = function () { return arguments; };
var getSloppyArguments = Function('return arguments');
var collect = function createCollection(C, items) {
var c = new C();
forEach(items, function (item) {
if (c.add) {
c.add(item);
} else {
c.set(item[0], item[1]);
}
});
return c;
};
var runTests = function runTests(t) {
t.test('strings', function (st) {
st.iterate('', [], '"" yields nothing');
st.iterate(Object(''), [], inspect(Object('')) + ' yields nothing');
st.iterate('foo', ['f', 'o', 'o'], '"foo" yields three chars');
st.iterate(Object('foo'), ['f', 'o', 'o'], inspect(Object('foo')) + ' yields three chars');
st.iterate('a💩z', ['a', '💩', 'z'], '"a💩z" yields three code points');
st.iterate(Object('a💩z'), ['a', '💩', 'z'], inspect(Object('a💩z')) + ' yields three code points');
st.iterate('\ud83dX', ['\ud83d', 'X'], inspect('\ud83dX') + ' (lone surrogate followed by "not a lone surrogate ending") yields one code point');
st.fakeIterator('abc');
st.end();
});
t.test('arrays', function (st) {
st.iterate([], [], '[] yields nothing');
st.iterate([1, 2], [1, 2], '[1, 2] yields [1, 2]');
// eslint-disable-next-line no-sparse-arrays
st.iterate([1, , 3], [1, undefined, 3], 'sparse array does not skip holes');
st.fakeIterator([1, 2, 3]);
st.end();
});
t.test('arguments', function (st) {
st.iterate(getArguments(), [], 'empty arguments object yields nothing');
st.iterate(getSloppyArguments(), [], 'empty sloppy arguments object yields nothing');
st.iterate(getArguments(1, 2, 3), [1, 2, 3], 'arguments object yields all args');
st.iterate(getSloppyArguments(1, 2, 3), [1, 2, 3], 'sloppy arguments object yields all args');
st.fakeIterator(getArguments(1, 2, 3));
st.fakeIterator(getSloppyArguments(1, 2, 3));
st.end();
});
t.test('non-iterables', function (st) {
var numbers = [0, -0, NaN, Infinity, 42];
var nonIterables = [
undefined,
null,
true,
false,
{},
/a/g,
function () {}
];
if (hasSymbols) {
nonIterables.push(Symbol.iterator);
}
if (hasBigInts) {
nonIterables.push(BigInt(42), BigInt(0));
}
forEach(nonIterables, function (nonIterable) {
st.noIterate(nonIterable);
if (nonIterable != null) {
st.fakeIterator(nonIterable);
}
});
if (hasSymbols && NaN[Symbol.iterator]) {
st.comment('# SKIP core-js v2 makes numbers iterable, in violation of the spec');
}
forEach(numbers, function (number) {
if (!hasSymbols || !number[Symbol.iterator]) {
st.noIterate(number);
}
st.fakeIterator(number);
});
st.end();
});
t.test('Map', { skip: typeof Map !== 'function' }, function (st) {
st.iterate(new Map(), [], 'empty Map yields nothing');
var entries = [
[1, 'a'],
[2, 'b'],
[3, 'c']
];
var m = collect(Map, entries);
st.iterate(m, entries, inspect(m) + ' yields expected entries');
st.fakeIterator(collect(Map, entries));
st.end();
});
t.test('Set', { skip: typeof Set !== 'function' }, function (st) {
st.iterate(new Set(), [], 'empty Set yields nothing');
var values = [
1,
2,
3
];
var s = collect(Set, values);
st.iterate(s, values, inspect(s) + ' yields expected values');
st.fakeIterator(collect(Set, values));
st.end();
});
};
test((process.env.TEST_VARIANT || 'standard') + ': getIterator tests', function (t) {
runTests(t);
t.end();
});