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