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

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 5.0 KB
Line 
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 // eslint-disable-next-line no-proto
104 var arrayIterHasIterProto = hasProto && iter.__proto__.__proto__ !== Object.prototype;
105 s2t.equal(
106 from(iter),
107 iter,
108 'array iterator becomes itself',
109 { skip: !arrayIterHasIterProto && 'node 0.12 - 3 do not have Iterator.prototype in the proto chains' }
110 );
111
112 s2t.end();
113 });
114
115 st.test('observability in a replaced String iterator', function (s2t) {
116 var originalStringIterator = String.prototype[Symbol.iterator];
117 var observedType;
118 s2t.teardown(mockProperty(String.prototype, Symbol.iterator, {
119 get: function () {
120 'use strict'; // eslint-disable-line strict, lines-around-directive
121
122 observedType = typeof this;
123 return originalStringIterator;
124 }
125 }));
126
127 from('');
128 s2t.equal(observedType, 'string', 'string primitive -> primitive receiver in Symbol.iterator getter');
129 from(Object(''));
130 s2t.equal(observedType, 'object', 'boxed string -> boxed string in Symbol.iterator getter');
131
132 s2t.end();
133 });
134
135 st.end();
136 });
137 },
138 index: function () {
139 test('Iterator.from: index', function (t) {
140 module.exports.tests(index, 'Iterator.from', t);
141
142 t.end();
143 });
144 },
145 implementation: function () {
146 test('Iterator.from: implementation', function (t) {
147 module.exports.tests(impl, 'Iterator.from', t);
148
149 t.end();
150 });
151 },
152 shimmed: function () {
153 test('Iterator.from: shimmed', function (t) {
154 t.test('Function name', { skip: !functionsHaveNames }, function (st) {
155 st.equal(Iterator.from.name, 'from', 'Iterator.from has name "from"');
156 st.end();
157 });
158
159 t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
160 et.equal(false, isEnumerable.call(Iterator, 'from'), 'Iterator.from is not enumerable');
161 et.end();
162 });
163
164 module.exports.tests(callBind(Iterator.from, Iterator), 'Iterator.from', t);
165
166 t.end();
167 });
168 }
169};
Note: See TracBrowser for help on using the repository browser.