[0c6b92a] | 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.zip');
|
---|
| 14 | var impl = require('../Iterator.zip/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 (zip, name, t) {
|
---|
| 23 | t['throws'](
|
---|
| 24 | function () { return new zip(); }, // eslint-disable-line new-cap
|
---|
| 25 | TypeError,
|
---|
| 26 | '`' + name + '` itself is not a constructor'
|
---|
| 27 | );
|
---|
| 28 | t['throws'](
|
---|
| 29 | function () { return new zip({}); }, // 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 () { zip(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 () { zip([[], 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 () { zip([string]); },
|
---|
| 56 | TypeError,
|
---|
| 57 | 'non-objects are not considered iterable'
|
---|
| 58 | );
|
---|
| 59 | });
|
---|
| 60 |
|
---|
| 61 | var arrayIt = zip([[1, 2, 3]]);
|
---|
| 62 | st.equal(typeof arrayIt.next, 'function', 'has a `next` function');
|
---|
| 63 |
|
---|
| 64 | st.test('real iterators', { skip: !hasSymbols }, function (s2t) {
|
---|
| 65 | var iter = [1, 2][Symbol.iterator]();
|
---|
| 66 | testIterator(zip([iter, [3, 4]]), [[1, 3], [2, 4]], s2t, 'array iterator + array yields combined results');
|
---|
| 67 |
|
---|
| 68 | s2t.end();
|
---|
| 69 | });
|
---|
| 70 |
|
---|
| 71 | st.test('observability in a replaced String iterator', function (s2t) {
|
---|
| 72 | var originalStringIterator = String.prototype[Symbol.iterator];
|
---|
| 73 | var observedType;
|
---|
| 74 | s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
|
---|
| 75 | get: function () {
|
---|
| 76 | 'use strict'; // eslint-disable-line strict, lines-around-directive
|
---|
| 77 |
|
---|
| 78 | observedType = typeof this;
|
---|
| 79 | return originalStringIterator;
|
---|
| 80 | }
|
---|
| 81 | }));
|
---|
| 82 |
|
---|
| 83 | zip([from('')]);
|
---|
| 84 | s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
|
---|
| 85 | zip([from(Object(''))]);
|
---|
| 86 | s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
|
---|
| 87 |
|
---|
| 88 | s2t.end();
|
---|
| 89 | });
|
---|
| 90 |
|
---|
| 91 | st.end();
|
---|
| 92 | });
|
---|
| 93 | },
|
---|
| 94 | index: function () {
|
---|
| 95 | test('Iterator.zip: index', function (t) {
|
---|
| 96 | module.exports.tests(index, 'Iterator.zip', t);
|
---|
| 97 |
|
---|
| 98 | t.end();
|
---|
| 99 | });
|
---|
| 100 | },
|
---|
| 101 | implementation: function () {
|
---|
| 102 | test('Iterator.zip: implementation', function (t) {
|
---|
| 103 | module.exports.tests(impl, 'Iterator.zip', t);
|
---|
| 104 |
|
---|
| 105 | t.end();
|
---|
| 106 | });
|
---|
| 107 | },
|
---|
| 108 | shimmed: function () {
|
---|
| 109 | test('Iterator.zip: shimmed', function (t) {
|
---|
| 110 | t.test('Function name', { skip: !functionsHaveNames }, function (st) {
|
---|
| 111 | st.equal(Iterator.zip.name, 'zip', 'Iterator.zip has name "zip"');
|
---|
| 112 | st.end();
|
---|
| 113 | });
|
---|
| 114 |
|
---|
| 115 | t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
|
---|
| 116 | et.equal(false, isEnumerable.call(Iterator, 'zip'), 'Iterator.zip is not enumerable');
|
---|
| 117 | et.end();
|
---|
| 118 | });
|
---|
| 119 |
|
---|
| 120 | module.exports.tests(callBind(Iterator.zip, Iterator), 'Iterator.zip', t);
|
---|
| 121 |
|
---|
| 122 | t.end();
|
---|
| 123 | });
|
---|
| 124 | }
|
---|
| 125 | };
|
---|