]> Lady’s Gitweb - Pisces/blob - iterable.test.js
Add methods for own entries and values to object.js
[Pisces] / iterable.test.js
1 // ♓🌟 Piscēs ∷ iterable.test.js
2 // ====================================================================
3 //
4 // Copyright © 2023 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assertEquals,
12 assertStrictEquals,
13 assertThrows,
14 describe,
15 it,
16 } from "./dev-deps.js";
17 import {
18 arrayIteratorFunction,
19 generatorIteratorFunction,
20 mapIteratorFunction,
21 setIteratorFunction,
22 stringIteratorFunction,
23 } from "./iterable.js";
24
25 describe("arrayIteratorFunction", () => {
26 it("[[Call]] returns a function", () => {
27 assertStrictEquals(typeof arrayIteratorFunction(), "function");
28 });
29
30 it("[[Call]] returns a value which has a prototype of %FunctionPrototype%", () => {
31 assertStrictEquals(
32 Object.getPrototypeOf(arrayIteratorFunction()),
33 Function.prototype,
34 );
35 });
36
37 it("[[Construct]] throws an error", () => {
38 assertThrows(() => new arrayIteratorFunction());
39 });
40
41 describe(".length", () => {
42 it("[[Get]] returns the correct length", () => {
43 assertStrictEquals(arrayIteratorFunction.length, 1);
44 });
45 });
46
47 describe(".name", () => {
48 it("[[Get]] returns the correct name", () => {
49 assertStrictEquals(
50 arrayIteratorFunction.name,
51 "arrayIteratorFunction",
52 );
53 });
54 });
55
56 describe("()", () => {
57 it("[[Call]] returns a value which inherits from %IteratorPrototype%", () => {
58 const iteratorProto = Object.getPrototypeOf(
59 Object.getPrototypeOf([][Symbol.iterator]),
60 );
61 const iterator = arrayIteratorFunction();
62 assertStrictEquals(
63 iterator([]) instanceof Object.assign(
64 function () {},
65 { prototype: iteratorProto },
66 ),
67 true,
68 );
69 });
70
71 it("[[Call]] returns a value with the provided string tag", () => {
72 const iterator = arrayIteratorFunction(null, "My Iterator");
73 assertStrictEquals(
74 iterator([])[Symbol.toStringTag],
75 "My Iterator",
76 );
77 });
78
79 it("[[Call]] yields the values", () => {
80 const iterator = arrayIteratorFunction();
81 assertEquals(
82 [...iterator(["etaoin", "shrdlu"])],
83 ["etaoin", "shrdlu"],
84 );
85 });
86
87 it("[[Call]] maps the values", () => {
88 const iterator = arrayIteratorFunction(function* ($) {
89 yield $.toUpperCase();
90 });
91 assertEquals(
92 [...iterator(["etaoin", "shrdlu"])],
93 ["ETAOIN", "SHRDLU"],
94 );
95 });
96
97 it("[[Call]] can map to nothing", () => {
98 const iterator = arrayIteratorFunction(function* () {});
99 assertEquals(
100 [...iterator(["etaoin", "shrdlu"])],
101 [],
102 );
103 });
104
105 it("[[Call]] can map to multiple values", () => {
106 const iterator = arrayIteratorFunction(function* ($) {
107 yield* $;
108 });
109 assertEquals(
110 [...iterator(["etaoin", "shrdlu"])],
111 [..."etaoinshrdlu"],
112 );
113 });
114
115 it("[[Call]] throws if not provided with any arguments", () => {
116 const iterator = arrayIteratorFunction();
117 assertThrows(() => {
118 iterator();
119 });
120 });
121
122 it("[[Call]] throws if not provided an arraylike", () => {
123 const iterator = arrayIteratorFunction();
124 assertThrows(() => {
125 iterator(null);
126 });
127 });
128
129 describe("::next", () => {
130 it("[[Call]] throws if there are values and the mapper is not a generator function", () => {
131 const iterator = arrayIteratorFunction(function () {});
132 assertThrows(() => {
133 iterator(["etaoin"]).next();
134 });
135 });
136 });
137 });
138 });
139
140 describe("generatorIteratorFunction", () => {
141 it("[[Call]] returns a function", () => {
142 assertStrictEquals(typeof generatorIteratorFunction(), "function");
143 });
144
145 it("[[Call]] returns a value which has a prototype of %FunctionPrototype%", () => {
146 assertStrictEquals(
147 Object.getPrototypeOf(generatorIteratorFunction()),
148 Function.prototype,
149 );
150 });
151
152 it("[[Construct]] throws an error", () => {
153 assertThrows(() => new generatorIteratorFunction());
154 });
155
156 describe(".length", () => {
157 it("[[Get]] returns the correct length", () => {
158 assertStrictEquals(generatorIteratorFunction.length, 1);
159 });
160 });
161
162 describe(".name", () => {
163 it("[[Get]] returns the correct name", () => {
164 assertStrictEquals(
165 generatorIteratorFunction.name,
166 "generatorIteratorFunction",
167 );
168 });
169 });
170
171 describe("()", () => {
172 it("[[Call]] returns a value which inherits from %IteratorPrototype%", () => {
173 const iteratorProto = Object.getPrototypeOf(
174 Object.getPrototypeOf([][Symbol.iterator]),
175 );
176 const iterator = generatorIteratorFunction();
177 assertStrictEquals(
178 iterator(function* () {}) instanceof Object.assign(
179 function () {},
180 { prototype: iteratorProto },
181 ),
182 true,
183 );
184 });
185
186 it("[[Call]] returns a value with the provided string tag", () => {
187 const iterator = generatorIteratorFunction(null, "My Iterator");
188 assertStrictEquals(
189 iterator(function* () {})[Symbol.toStringTag],
190 "My Iterator",
191 );
192 });
193
194 it("[[Call]] yields the values", () => {
195 const generator = function* () {
196 yield* ["etaoin", "shrdlu"];
197 };
198 const iterator = generatorIteratorFunction();
199 assertEquals(
200 [...iterator(generator)],
201 ["etaoin", "shrdlu"],
202 );
203 });
204
205 it("[[Call]] maps the values", () => {
206 const generator = function* () {
207 yield* ["etaoin", "shrdlu"];
208 };
209 const iterator = generatorIteratorFunction(function* ($) {
210 yield $.toUpperCase();
211 });
212 assertEquals(
213 [...iterator(generator)],
214 ["ETAOIN", "SHRDLU"],
215 );
216 });
217
218 it("[[Call]] can map to nothing", () => {
219 const generator = function* () {
220 yield* ["etaoin", "shrdlu"];
221 };
222 const iterator = generatorIteratorFunction(function* () {});
223 assertEquals(
224 [...iterator(generator)],
225 [],
226 );
227 });
228
229 it("[[Call]] can map to multiple values", () => {
230 const generator = function* () {
231 yield* ["etaoin", "shrdlu"];
232 };
233 const iterator = generatorIteratorFunction(function* ($) {
234 yield* $;
235 });
236 assertEquals(
237 [...iterator(generator)],
238 [..."etaoinshrdlu"],
239 );
240 });
241
242 it("[[Call]] throws if not provided with any arguments", () => {
243 const iterator = generatorIteratorFunction();
244 assertThrows(() => {
245 iterator();
246 });
247 });
248
249 it("[[Call]] throws if not provided a function", () => {
250 const iterator = generatorIteratorFunction();
251 assertThrows(() => {
252 iterator([]);
253 });
254 });
255
256 describe("::next", () => {
257 it("[[Call]] throws if there are values and the mapper is not a generator function", () => {
258 const generator = function* () {
259 yield "etaoin";
260 };
261 const iterator = generatorIteratorFunction(function () {});
262 assertThrows(() => {
263 iterator(generator).next();
264 });
265 });
266
267 it("[[Call]] throws if not constructed with a generator function", () => {
268 const iterator = generatorIteratorFunction();
269 assertThrows(() => {
270 iterator(Array.prototype[Symbol.iterator].bind([])).next();
271 });
272 });
273 });
274 });
275 });
276
277 describe("mapIteratorFunction", () => {
278 it("[[Call]] returns a function", () => {
279 assertStrictEquals(typeof mapIteratorFunction(), "function");
280 });
281
282 it("[[Call]] returns a value which has a prototype of %FunctionPrototype%", () => {
283 assertStrictEquals(
284 Object.getPrototypeOf(mapIteratorFunction()),
285 Function.prototype,
286 );
287 });
288
289 it("[[Construct]] throws an error", () => {
290 assertThrows(() => new mapIteratorFunction());
291 });
292
293 describe(".length", () => {
294 it("[[Get]] returns the correct length", () => {
295 assertStrictEquals(mapIteratorFunction.length, 1);
296 });
297 });
298
299 describe(".name", () => {
300 it("[[Get]] returns the correct name", () => {
301 assertStrictEquals(
302 mapIteratorFunction.name,
303 "mapIteratorFunction",
304 );
305 });
306 });
307
308 describe("()", () => {
309 it("[[Call]] returns a value which inherits from %IteratorPrototype%", () => {
310 const iteratorProto = Object.getPrototypeOf(
311 Object.getPrototypeOf([][Symbol.iterator]),
312 );
313 const iterator = mapIteratorFunction();
314 assertStrictEquals(
315 iterator(new Map()) instanceof Object.assign(
316 function () {},
317 { prototype: iteratorProto },
318 ),
319 true,
320 );
321 });
322
323 it("[[Call]] returns a value with the provided string tag", () => {
324 const iterator = mapIteratorFunction(null, "My Iterator");
325 assertStrictEquals(
326 iterator(new Map())[Symbol.toStringTag],
327 "My Iterator",
328 );
329 });
330
331 it("[[Call]] yields the values", () => {
332 const iterator = mapIteratorFunction();
333 assertEquals(
334 [...iterator(new Map([["etaoin", "shrdlu"]]))],
335 [["etaoin", "shrdlu"]],
336 );
337 });
338
339 it("[[Call]] maps the values", () => {
340 const iterator = mapIteratorFunction(function* ([k, v]) {
341 yield [k.toUpperCase(), v.toUpperCase()];
342 });
343 assertEquals(
344 [...iterator(new Map([["etaoin", "shrdlu"]]))],
345 [["ETAOIN", "SHRDLU"]],
346 );
347 });
348
349 it("[[Call]] can map to nothing", () => {
350 const iterator = mapIteratorFunction(function* () {});
351 assertEquals(
352 [...iterator(new Map([["etaoin", "shrdlu"]]))],
353 [],
354 );
355 });
356
357 it("[[Call]] can map to multiple values", () => {
358 const iterator = mapIteratorFunction(function* ($) {
359 yield* $;
360 });
361 assertEquals(
362 [...iterator(new Map([["etaoin", "shrdlu"]]))],
363 ["etaoin", "shrdlu"],
364 );
365 });
366
367 it("[[Call]] throws if not provided with any arguments", () => {
368 const iterator = mapIteratorFunction();
369 assertThrows(() => {
370 iterator();
371 });
372 });
373
374 it("[[Call]] throws if not provided a map", () => {
375 const iterator = mapIteratorFunction();
376 assertThrows(() => {
377 iterator([]);
378 });
379 });
380
381 describe("::next", () => {
382 it("[[Call]] throws if there are values and the mapper is not a generator function", () => {
383 const iterator = mapIteratorFunction(function () {});
384 assertThrows(() => {
385 iterator(new Map([["etaoin", "shrdlu"]])).next();
386 });
387 });
388 });
389 });
390 });
391
392 describe("setIteratorFunction", () => {
393 it("[[Call]] returns a function", () => {
394 assertStrictEquals(typeof setIteratorFunction(), "function");
395 });
396
397 it("[[Call]] returns a value which has a prototype of %FunctionPrototype%", () => {
398 assertStrictEquals(
399 Object.getPrototypeOf(setIteratorFunction()),
400 Function.prototype,
401 );
402 });
403
404 it("[[Construct]] throws an error", () => {
405 assertThrows(() => new setIteratorFunction());
406 });
407
408 describe(".length", () => {
409 it("[[Get]] returns the correct length", () => {
410 assertStrictEquals(setIteratorFunction.length, 1);
411 });
412 });
413
414 describe(".name", () => {
415 it("[[Get]] returns the correct name", () => {
416 assertStrictEquals(
417 setIteratorFunction.name,
418 "setIteratorFunction",
419 );
420 });
421 });
422
423 describe("()", () => {
424 it("[[Call]] returns a value which inherits from %IteratorPrototype%", () => {
425 const iteratorProto = Object.getPrototypeOf(
426 Object.getPrototypeOf([][Symbol.iterator]),
427 );
428 const iterator = setIteratorFunction();
429 assertStrictEquals(
430 iterator(new Set()) instanceof Object.assign(
431 function () {},
432 { prototype: iteratorProto },
433 ),
434 true,
435 );
436 });
437
438 it("[[Call]] returns a value with the provided string tag", () => {
439 const iterator = setIteratorFunction(null, "My Iterator");
440 assertStrictEquals(
441 iterator(new Set())[Symbol.toStringTag],
442 "My Iterator",
443 );
444 });
445
446 it("[[Call]] yields the values", () => {
447 const iterator = setIteratorFunction();
448 assertEquals(
449 [...iterator(new Set(["etaoin", "shrdlu"]))],
450 ["etaoin", "shrdlu"],
451 );
452 });
453
454 it("[[Call]] maps the values", () => {
455 const iterator = setIteratorFunction(function* ($) {
456 yield $.toUpperCase();
457 });
458 assertEquals(
459 [...iterator(new Set(["etaoin", "shrdlu"]))],
460 ["ETAOIN", "SHRDLU"],
461 );
462 });
463
464 it("[[Call]] can map to nothing", () => {
465 const iterator = setIteratorFunction(function* () {});
466 assertEquals(
467 [...iterator(new Set(["etaoin", "shrdlu"]))],
468 [],
469 );
470 });
471
472 it("[[Call]] can map to multiple values", () => {
473 const iterator = setIteratorFunction(function* ($) {
474 yield* $;
475 });
476 assertEquals(
477 [...iterator(new Set(["etaoin", "shrdlu"]))],
478 [..."etaoinshrdlu"],
479 );
480 });
481
482 it("[[Call]] throws if not provided with any arguments", () => {
483 const iterator = setIteratorFunction();
484 assertThrows(() => {
485 iterator();
486 });
487 });
488
489 it("[[Call]] throws if not provided a set", () => {
490 const iterator = setIteratorFunction();
491 assertThrows(() => {
492 iterator([]);
493 });
494 });
495
496 describe("::next", () => {
497 it("[[Call]] throws if there are values and the mapper is not a generator function", () => {
498 const iterator = setIteratorFunction(function () {});
499 assertThrows(() => {
500 iterator(new Set(["etaoin"])).next();
501 });
502 });
503 });
504 });
505 });
506
507 describe("stringIteratorFunction", () => {
508 it("[[Call]] returns a function", () => {
509 assertStrictEquals(typeof stringIteratorFunction(), "function");
510 });
511
512 it("[[Call]] returns a value which has a prototype of %FunctionPrototype%", () => {
513 assertStrictEquals(
514 Object.getPrototypeOf(stringIteratorFunction()),
515 Function.prototype,
516 );
517 });
518
519 it("[[Construct]] throws an error", () => {
520 assertThrows(() => new stringIteratorFunction());
521 });
522
523 describe(".length", () => {
524 it("[[Get]] returns the correct length", () => {
525 assertStrictEquals(stringIteratorFunction.length, 1);
526 });
527 });
528
529 describe(".name", () => {
530 it("[[Get]] returns the correct name", () => {
531 assertStrictEquals(
532 stringIteratorFunction.name,
533 "stringIteratorFunction",
534 );
535 });
536 });
537
538 describe("()", () => {
539 it("[[Call]] returns a value which inherits from %IteratorPrototype%", () => {
540 const iteratorProto = Object.getPrototypeOf(
541 Object.getPrototypeOf([][Symbol.iterator]),
542 );
543 const iterator = stringIteratorFunction();
544 assertStrictEquals(
545 iterator("") instanceof Object.assign(
546 function () {},
547 { prototype: iteratorProto },
548 ),
549 true,
550 );
551 });
552
553 it("[[Call]] returns a value with the provided string tag", () => {
554 const iterator = stringIteratorFunction(null, "My Iterator");
555 assertStrictEquals(
556 iterator("")[Symbol.toStringTag],
557 "My Iterator",
558 );
559 });
560
561 it("[[Call]] yields the values", () => {
562 const iterator = stringIteratorFunction();
563 assertEquals(
564 [...iterator("etaoin👀")],
565 [..."etaoin👀"],
566 );
567 });
568
569 it("[[Call]] maps the values", () => {
570 const iterator = stringIteratorFunction(function* ($) {
571 yield $.toUpperCase();
572 });
573 assertEquals(
574 [...iterator("etaoin👀")],
575 [..."ETAOIN👀"],
576 );
577 });
578
579 it("[[Call]] can map to nothing", () => {
580 const iterator = stringIteratorFunction(function* () {});
581 assertEquals(
582 [...iterator("etaoin👀")],
583 [],
584 );
585 });
586
587 it("[[Call]] can map to multiple values", () => {
588 const iterator = stringIteratorFunction(function* ($) {
589 yield $;
590 yield $;
591 });
592 assertEquals(
593 [...iterator("etaoin👀")],
594 [..."eettaaooiinn👀👀"],
595 );
596 });
597
598 it("[[Call]] throws if not provided with any arguments", () => {
599 const iterator = stringIteratorFunction();
600 assertThrows(() => {
601 iterator();
602 });
603 });
604
605 it("[[Call]] throws if not provided something convertible to a string", () => {
606 const iterator = stringIteratorFunction();
607 assertThrows(() => {
608 iterator({
609 toString() {
610 throw null;
611 },
612 });
613 });
614 });
615
616 describe("::next", () => {
617 it("[[Call]] throws if there are values and the mapper is not a generator function", () => {
618 const iterator = stringIteratorFunction(function () {});
619 assertThrows(() => {
620 iterator("etaoin").next();
621 });
622 });
623 });
624 });
625 });
This page took 0.181481 seconds and 5 git commands to generate.