]> Lady’s Gitweb - Pisces/blob - value.test.js
Add methods for own entries and values to object.js
[Pisces] / value.test.js
1 // ♓🌟 Piscēs ∷ value.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022–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 ASYNC_ITERATOR,
19 completePropertyDescriptor,
20 HAS_INSTANCE,
21 IS_CONCAT_SPREADABLE,
22 isAccessorDescriptor,
23 isDataDescriptor,
24 isFullyPopulatedDescriptor,
25 isGenericDescriptor,
26 ITERATOR,
27 LN10,
28 LN2,
29 LOG10ℇ,
30 LOG2ℇ,
31 MATCH,
32 MATCH_ALL,
33 MAXIMUM_NUMBER,
34 MAXIMUM_SAFE_INTEGRAL_NUMBER,
35 MINIMUM_NUMBER,
36 MINIMUM_SAFE_INTEGRAL_NUMBER,
37 NAN,
38 NEGATIVE_INFINITY,
39 NEGATIVE_ZERO,
40 NULL,
41 ordinaryToPrimitive,
42 POSITIVE_INFINITY,
43 POSITIVE_ZERO,
44 RECIPROCAL_SQRT2,
45 REPLACE,
46 sameValue,
47 sameValueZero,
48 SPECIES,
49 SPLIT,
50 SQRT2,
51 TO_PRIMITIVE,
52 TO_STRING_TAG,
53 toFunctionName,
54 toIndex,
55 toLength,
56 toPrimitive,
57 toPropertyKey,
58 type,
59 UNDEFINED,
60 UNSCOPABLES,
61 Ε,
62 Π,
63 ℇ,
64 } from "./value.js";
65
66 describe("ASYNC_ITERATOR", () => {
67 it("[[Get]] is @@asyncIterator", () => {
68 assertStrictEquals(ASYNC_ITERATOR, Symbol.asyncIterator);
69 });
70 });
71
72 describe("HAS_INSTANCE", () => {
73 it("[[Get]] is @@hasInstance", () => {
74 assertStrictEquals(HAS_INSTANCE, Symbol.hasInstance);
75 });
76 });
77
78 describe("IS_CONCAT_SPREADABLE", () => {
79 it("[[Get]] is @@isConcatSpreadable", () => {
80 assertStrictEquals(
81 IS_CONCAT_SPREADABLE,
82 Symbol.isConcatSpreadable,
83 );
84 });
85 });
86
87 describe("ITERATOR", () => {
88 it("[[Get]] is @@iterator", () => {
89 assertStrictEquals(ITERATOR, Symbol.iterator);
90 });
91 });
92
93 describe("LN10", () => {
94 it("[[Get]] is ln(10)", () => {
95 assertStrictEquals(LN10, Math.LN10);
96 });
97 });
98
99 describe("LN2", () => {
100 it("[[Get]] is ln(2)", () => {
101 assertStrictEquals(LN2, Math.LN2);
102 });
103 });
104
105 describe("LOG10ℇ", () => {
106 it("[[Get]] is log10(ℇ)", () => {
107 assertStrictEquals(LOG10ℇ, Math.LOG10E);
108 });
109 });
110
111 describe("LOG2ℇ", () => {
112 it("[[Get]] is log2(ℇ)", () => {
113 assertStrictEquals(LOG2ℇ, Math.LOG2E);
114 });
115 });
116
117 describe("MATCH", () => {
118 it("[[Get]] is @@match", () => {
119 assertStrictEquals(MATCH, Symbol.match);
120 });
121 });
122
123 describe("MATCH_ALL", () => {
124 it("[[Get]] is @@matchAll", () => {
125 assertStrictEquals(MATCH_ALL, Symbol.matchAll);
126 });
127 });
128
129 describe("MAXIMUM_NUMBER", () => {
130 it("[[Get]] is the maximum number", () => {
131 assertStrictEquals(MAXIMUM_NUMBER, Number.MAX_VALUE);
132 });
133 });
134
135 describe("MAXIMUM_SAFE_INTEGRAL_NUMBER", () => {
136 it("[[Get]] is the maximum safe integral number", () => {
137 assertStrictEquals(
138 MAXIMUM_SAFE_INTEGRAL_NUMBER,
139 Number.MAX_SAFE_INTEGER,
140 );
141 });
142 });
143
144 describe("MINIMUM_NUMBER", () => {
145 it("[[Get]] is the minimum number", () => {
146 assertStrictEquals(MINIMUM_NUMBER, Number.MIN_VALUE);
147 });
148 });
149
150 describe("MINIMUM_SAFE_INTEGRAL_NUMBER", () => {
151 it("[[Get]] is the minimum safe integral number", () => {
152 assertStrictEquals(
153 MINIMUM_SAFE_INTEGRAL_NUMBER,
154 Number.MIN_SAFE_INTEGER,
155 );
156 });
157 });
158
159 describe("NAN", () => {
160 it("[[Get]] is nan", () => {
161 assertStrictEquals(NAN, NaN);
162 });
163 });
164
165 describe("NEGATIVE_INFINITY", () => {
166 it("[[Get]] is negative infinity", () => {
167 assertStrictEquals(NEGATIVE_INFINITY, -Infinity);
168 });
169 });
170
171 describe("NEGATIVE_ZERO", () => {
172 it("[[Get]] is negative zero", () => {
173 assertStrictEquals(NEGATIVE_ZERO, -0);
174 });
175 });
176
177 describe("NULL", () => {
178 it("[[Get]] is null", () => {
179 assertStrictEquals(NULL, null);
180 });
181 });
182
183 describe("POSITIVE_INFINITY", () => {
184 it("[[Get]] is negative infinity", () => {
185 assertStrictEquals(POSITIVE_INFINITY, Infinity);
186 });
187 });
188
189 describe("POSITIVE_ZERO", () => {
190 it("[[Get]] is positive zero", () => {
191 assertStrictEquals(POSITIVE_ZERO, 0);
192 });
193 });
194
195 describe("RECIPROCAL_SQRT2", () => {
196 it("[[Get]] is sqrt(½)", () => {
197 assertStrictEquals(RECIPROCAL_SQRT2, Math.SQRT1_2);
198 });
199 });
200
201 describe("REPLACE", () => {
202 it("[[Get]] is @@replace", () => {
203 assertStrictEquals(REPLACE, Symbol.replace);
204 });
205 });
206
207 describe("SPECIES", () => {
208 it("[[Get]] is @@species", () => {
209 assertStrictEquals(SPECIES, Symbol.species);
210 });
211 });
212
213 describe("SPLIT", () => {
214 it("[[Get]] is @@split", () => {
215 assertStrictEquals(SPLIT, Symbol.split);
216 });
217 });
218
219 describe("SQRT2", () => {
220 it("[[Get]] is sqrt(2)", () => {
221 assertStrictEquals(SQRT2, Math.SQRT2);
222 });
223 });
224
225 describe("TO_PRIMITIVE", () => {
226 it("[[Get]] is @@toPrimitive", () => {
227 assertStrictEquals(TO_PRIMITIVE, Symbol.toPrimitive);
228 });
229 });
230
231 describe("TO_STRING_TAG", () => {
232 it("[[Get]] is @@toStringTag", () => {
233 assertStrictEquals(TO_STRING_TAG, Symbol.toStringTag);
234 });
235 });
236
237 describe("UNDEFINED", () => {
238 it("[[Get]] is undefined", () => {
239 assertStrictEquals(UNDEFINED, void {});
240 });
241 });
242
243 describe("UNSCOPABLES", () => {
244 it("[[Get]] is @@unscopables", () => {
245 assertStrictEquals(UNSCOPABLES, Symbol.unscopables);
246 });
247 });
248
249 describe("completePropertyDescriptor", () => {
250 it("[[Call]] completes a generic descriptor", () => {
251 const desc = {};
252 completePropertyDescriptor(desc);
253 assertEquals(desc, {
254 configurable: false,
255 enumerable: false,
256 value: undefined,
257 writable: false,
258 });
259 });
260
261 it("[[Call]] completes a data descriptor", () => {
262 const desc = { value: undefined };
263 completePropertyDescriptor(desc);
264 assertEquals(desc, {
265 configurable: false,
266 enumerable: false,
267 value: undefined,
268 writable: false,
269 });
270 });
271
272 it("[[Call]] completes an accessor descriptor", () => {
273 const desc = { get: undefined };
274 completePropertyDescriptor(desc);
275 assertEquals(desc, {
276 configurable: false,
277 enumerable: false,
278 get: undefined,
279 set: undefined,
280 });
281 });
282
283 it("[[Call]] throws an error when the descriptor is undefined", () => {
284 assertThrows(() => new completePropertyDescriptor(undefined));
285 });
286
287 it("[[Construct]] throws an error", () => {
288 assertThrows(() => new completePropertyDescriptor({}));
289 });
290
291 describe(".length", () => {
292 it("[[Get]] returns the correct length", () => {
293 assertStrictEquals(completePropertyDescriptor.length, 1);
294 });
295 });
296
297 describe(".name", () => {
298 it("[[Get]] returns the correct name", () => {
299 assertStrictEquals(
300 completePropertyDescriptor.name,
301 "completePropertyDescriptor",
302 );
303 });
304 });
305 });
306
307 describe("isAccessorDescriptor", () => {
308 it("[[Call]] returns false for a generic descriptor", () => {
309 assertStrictEquals(isAccessorDescriptor({}), false);
310 });
311
312 it("[[Get]] returns false for a data descriptor", () => {
313 assertStrictEquals(
314 isAccessorDescriptor({ value: undefined }),
315 false,
316 );
317 assertStrictEquals(
318 isAccessorDescriptor({ writable: undefined }),
319 false,
320 );
321 });
322
323 it("[[Get]] returns true for an accessor descriptor", () => {
324 assertStrictEquals(isAccessorDescriptor({ get: undefined }), true);
325 assertStrictEquals(isAccessorDescriptor({ set: undefined }), true);
326 });
327
328 it("[[Get]] returns false for undefined", () => {
329 assertStrictEquals(isAccessorDescriptor(undefined), false);
330 });
331
332 it("[[Construct]] throws an error", () => {
333 assertThrows(() => new isAccessorDescriptor({}));
334 });
335
336 describe(".length", () => {
337 it("[[Get]] returns the correct length", () => {
338 assertStrictEquals(isAccessorDescriptor.length, 1);
339 });
340 });
341
342 describe(".name", () => {
343 it("[[Get]] returns the correct name", () => {
344 assertStrictEquals(
345 isAccessorDescriptor.name,
346 "isAccessorDescriptor",
347 );
348 });
349 });
350 });
351
352 describe("isDataDescriptor", () => {
353 it("[[Call]] returns false for a generic descriptor", () => {
354 assertStrictEquals(isDataDescriptor({}), false);
355 });
356
357 it("[[Get]] returns true for a data descriptor", () => {
358 assertStrictEquals(isDataDescriptor({ value: undefined }), true);
359 assertStrictEquals(isDataDescriptor({ writable: true }), true);
360 });
361
362 it("[[Get]] returns false for an accessor descriptor", () => {
363 assertStrictEquals(isDataDescriptor({ get: undefined }), false);
364 assertStrictEquals(isDataDescriptor({ set: undefined }), false);
365 });
366
367 it("[[Get]] returns false for undefined", () => {
368 assertStrictEquals(isDataDescriptor(undefined), false);
369 });
370
371 it("[[Construct]] throws an error", () => {
372 assertThrows(() => new isDataDescriptor({}));
373 });
374
375 describe(".length", () => {
376 it("[[Get]] returns the correct length", () => {
377 assertStrictEquals(isDataDescriptor.length, 1);
378 });
379 });
380
381 describe(".name", () => {
382 it("[[Get]] returns the correct name", () => {
383 assertStrictEquals(isDataDescriptor.name, "isDataDescriptor");
384 });
385 });
386 });
387
388 describe("isFullyPopulatedDescriptor", () => {
389 it("[[Call]] returns false for a generic descriptor", () => {
390 assertStrictEquals(isFullyPopulatedDescriptor({}), false);
391 });
392
393 it("[[Get]] returns false for a non‐fully‐populated data descriptor", () => {
394 assertStrictEquals(
395 isFullyPopulatedDescriptor({ value: undefined }),
396 false,
397 );
398 assertStrictEquals(
399 isFullyPopulatedDescriptor({ writable: true }),
400 false,
401 );
402 });
403
404 it("[[Get]] returns true for a fully‐populated data descriptor", () => {
405 assertStrictEquals(
406 isFullyPopulatedDescriptor({
407 configurable: true,
408 enumerable: true,
409 value: undefined,
410 writable: true,
411 }),
412 true,
413 );
414 });
415
416 it("[[Get]] returns false for a non‐fully‐populated accessor descriptor", () => {
417 assertStrictEquals(
418 isFullyPopulatedDescriptor({ get: undefined }),
419 false,
420 );
421 assertStrictEquals(
422 isFullyPopulatedDescriptor({ set: undefined }),
423 false,
424 );
425 });
426
427 it("[[Get]] returns true for a fully‐populated accessor descriptor", () => {
428 assertStrictEquals(
429 isFullyPopulatedDescriptor({
430 configurable: true,
431 enumerable: true,
432 get: undefined,
433 set: undefined,
434 }),
435 true,
436 );
437 });
438
439 it("[[Get]] returns false for undefined", () => {
440 assertStrictEquals(isFullyPopulatedDescriptor(undefined), false);
441 });
442
443 it("[[Construct]] throws an error", () => {
444 assertThrows(() => new isFullyPopulatedDescriptor({}));
445 });
446
447 describe(".length", () => {
448 it("[[Get]] returns the correct length", () => {
449 assertStrictEquals(isFullyPopulatedDescriptor.length, 1);
450 });
451 });
452
453 describe(".name", () => {
454 it("[[Get]] returns the correct name", () => {
455 assertStrictEquals(
456 isFullyPopulatedDescriptor.name,
457 "isFullyPopulatedDescriptor",
458 );
459 });
460 });
461 });
462
463 describe("isGenericDescriptor", () => {
464 it("[[Call]] returns true for a generic descriptor", () => {
465 assertStrictEquals(isGenericDescriptor({}), true);
466 });
467
468 it("[[Get]] returns false for a data descriptor", () => {
469 assertStrictEquals(
470 isGenericDescriptor({ value: undefined }),
471 false,
472 );
473 assertStrictEquals(isGenericDescriptor({ writable: true }), false);
474 });
475
476 it("[[Get]] returns false for an accessor descriptor", () => {
477 assertStrictEquals(isGenericDescriptor({ get: undefined }), false);
478 assertStrictEquals(isGenericDescriptor({ set: undefined }), false);
479 });
480
481 it("[[Get]] returns false for undefined", () => {
482 assertStrictEquals(isGenericDescriptor(undefined), false);
483 });
484
485 it("[[Construct]] throws an error", () => {
486 assertThrows(() => new isGenericDescriptor({}));
487 });
488
489 describe(".length", () => {
490 it("[[Get]] returns the correct length", () => {
491 assertStrictEquals(isGenericDescriptor.length, 1);
492 });
493 });
494
495 describe(".name", () => {
496 it("[[Get]] returns the correct name", () => {
497 assertStrictEquals(
498 isGenericDescriptor.name,
499 "isGenericDescriptor",
500 );
501 });
502 });
503 });
504
505 describe("ordinaryToPrimitive", () => {
506 it("[[Call]] prefers `valueOf` by default", () => {
507 const obj = {
508 toString() {
509 return "failure";
510 },
511 valueOf() {
512 return "success";
513 },
514 };
515 assertStrictEquals(ordinaryToPrimitive(obj), "success");
516 assertStrictEquals(ordinaryToPrimitive(obj, "default"), "success");
517 });
518
519 it('[[Call]] prefers `valueOf` for a "number" hint', () => {
520 const obj = {
521 toString() {
522 return "failure";
523 },
524 valueOf() {
525 return "success";
526 },
527 };
528 assertStrictEquals(ordinaryToPrimitive(obj, "number"), "success");
529 });
530
531 it('[[Call]] prefers `toString` for a "string" hint', () => {
532 const obj = {
533 toString() {
534 return "success";
535 },
536 valueOf() {
537 return "failure";
538 },
539 };
540 assertStrictEquals(ordinaryToPrimitive(obj, "string"), "success");
541 });
542
543 it("[[Call]] falls back to the other method if the first isn’t callable", () => {
544 const obj = {
545 toString() {
546 return "success";
547 },
548 valueOf: "failure",
549 };
550 assertStrictEquals(ordinaryToPrimitive(obj), "success");
551 });
552
553 it("[[Call]] falls back to the other method if the first returns an object", () => {
554 const obj = {
555 toString() {
556 return "success";
557 },
558 valueOf() {
559 return new String("failure");
560 },
561 };
562 assertStrictEquals(ordinaryToPrimitive(obj), "success");
563 });
564
565 it("[[Call]] throws an error if neither method is callable", () => {
566 const obj = {
567 toString: "failure",
568 valueOf: "failure",
569 };
570 assertThrows(() => ordinaryToPrimitive(obj));
571 });
572
573 it("[[Call]] throws an error if neither method returns an object", () => {
574 const obj = {
575 toString() {
576 return new String("failure");
577 },
578 valueOf() {
579 return new String("failure");
580 },
581 };
582 assertThrows(() => ordinaryToPrimitive(obj));
583 });
584
585 it("[[Construct]] throws an error", () => {
586 assertThrows(() => new ordinaryToPrimitive(""));
587 });
588
589 describe(".length", () => {
590 it("[[Get]] returns the correct length", () => {
591 assertStrictEquals(ordinaryToPrimitive.length, 2);
592 });
593 });
594
595 describe(".name", () => {
596 it("[[Get]] returns the correct name", () => {
597 assertStrictEquals(
598 ordinaryToPrimitive.name,
599 "ordinaryToPrimitive",
600 );
601 });
602 });
603 });
604
605 describe("sameValue", () => {
606 it("[[Call]] returns false for null 🆚 undefined", () => {
607 assertStrictEquals(sameValue(null, undefined), false);
608 });
609
610 it("[[Call]] returns false for null 🆚 an object", () => {
611 assertStrictEquals(sameValue(null, {}), false);
612 });
613
614 it("[[Call]] returns true for null 🆚 null", () => {
615 assertStrictEquals(sameValue(null, null), true);
616 });
617
618 it("[[Call]] returns false for two different objects", () => {
619 assertStrictEquals(sameValue({}, {}), false);
620 });
621
622 it("[[Call]] returns true for the same object", () => {
623 const obj = {};
624 assertStrictEquals(sameValue(obj, obj), true);
625 });
626
627 it("[[Call]] returns false for ±0", () => {
628 assertStrictEquals(sameValue(0, -0), false);
629 });
630
631 it("[[Call]] returns true for -0", () => {
632 assertStrictEquals(sameValue(-0, -0), true);
633 });
634
635 it("[[Call]] returns true for nan", () => {
636 assertStrictEquals(sameValue(0 / 0, 0 / 0), true);
637 });
638
639 it("[[Call]] returns false for a primitive and its wrapped object", () => {
640 assertStrictEquals(sameValue(false, new Boolean(false)), false);
641 });
642
643 it("[[Construct]] throws an error", () => {
644 assertThrows(() => new sameValue(true, true));
645 });
646
647 describe(".length", () => {
648 it("[[Get]] returns the correct length", () => {
649 assertStrictEquals(sameValue.length, 2);
650 });
651 });
652
653 describe(".name", () => {
654 it("[[Get]] returns the correct name", () => {
655 assertStrictEquals(sameValue.name, "sameValue");
656 });
657 });
658 });
659
660 describe("sameValueZero", () => {
661 it("[[Call]] returns false for null 🆚 undefined", () => {
662 assertStrictEquals(sameValueZero(null, undefined), false);
663 });
664
665 it("[[Call]] returns false for null 🆚 an object", () => {
666 assertStrictEquals(sameValueZero(null, {}), false);
667 });
668
669 it("[[Call]] returns true for null 🆚 null", () => {
670 assertStrictEquals(sameValueZero(null, null), true);
671 });
672
673 it("[[Call]] returns false for two different objects", () => {
674 assertStrictEquals(sameValueZero({}, {}), false);
675 });
676
677 it("[[Call]] returns true for the same object", () => {
678 const obj = {};
679 assertStrictEquals(sameValueZero(obj, obj), true);
680 });
681
682 it("[[Call]] returns true for ±0", () => {
683 assertStrictEquals(sameValueZero(0, -0), true);
684 });
685
686 it("[[Call]] returns true for -0", () => {
687 assertStrictEquals(sameValueZero(-0, -0), true);
688 });
689
690 it("[[Call]] returns true for nan", () => {
691 assertStrictEquals(sameValueZero(0 / 0, 0 / 0), true);
692 });
693
694 it("[[Call]] returns false for a primitive and its wrapped object", () => {
695 assertStrictEquals(
696 sameValueZero(false, new Boolean(false)),
697 false,
698 );
699 });
700
701 it("[[Construct]] throws an error", () => {
702 assertThrows(() => new sameValueZero(true, true));
703 });
704
705 describe(".length", () => {
706 it("[[Get]] returns the correct length", () => {
707 assertStrictEquals(sameValueZero.length, 2);
708 });
709 });
710
711 describe(".name", () => {
712 it("[[Get]] returns the correct name", () => {
713 assertStrictEquals(sameValueZero.name, "sameValueZero");
714 });
715 });
716 });
717
718 describe("toFunctionName", () => {
719 it("[[Call]] works with strings and no prefix", () => {
720 assertStrictEquals(toFunctionName("etaoin"), "etaoin");
721 });
722
723 it("[[Call]] works with objects and no prefix", () => {
724 assertStrictEquals(
725 toFunctionName({
726 toString() {
727 return "etaoin";
728 },
729 }),
730 "etaoin",
731 );
732 });
733
734 it("[[Call]] works with descriptionless symbols and no prefix", () => {
735 assertStrictEquals(toFunctionName(Symbol()), "");
736 });
737
738 it("[[Call]] works with empty description symbols and no prefix", () => {
739 assertStrictEquals(toFunctionName(Symbol("")), "[]");
740 });
741
742 it("[[Call]] works with described symbols and no prefix", () => {
743 assertStrictEquals(toFunctionName(Symbol("etaoin")), "[etaoin]");
744 });
745
746 it("[[Call]] works with strings and a prefix", () => {
747 assertStrictEquals(toFunctionName("etaoin", "foo"), "foo etaoin");
748 });
749
750 it("[[Call]] works with objects and no prefix", () => {
751 assertStrictEquals(
752 toFunctionName({
753 toString() {
754 return "etaoin";
755 },
756 }, "foo"),
757 "foo etaoin",
758 );
759 });
760
761 it("[[Call]] works with descriptionless symbols and no prefix", () => {
762 assertStrictEquals(toFunctionName(Symbol(), "foo"), "foo ");
763 });
764
765 it("[[Call]] works with empty description symbols and no prefix", () => {
766 assertStrictEquals(toFunctionName(Symbol(""), "foo"), "foo []");
767 });
768
769 it("[[Call]] works with described symbols and no prefix", () => {
770 assertStrictEquals(
771 toFunctionName(Symbol("etaoin"), "foo"),
772 "foo [etaoin]",
773 );
774 });
775
776 it("[[Construct]] throws an error", () => {
777 assertThrows(() => new toFunctionName(""));
778 });
779
780 describe(".length", () => {
781 it("[[Get]] returns the correct length", () => {
782 assertStrictEquals(toFunctionName.length, 1);
783 });
784 });
785
786 describe(".name", () => {
787 it("[[Get]] returns the correct name", () => {
788 assertStrictEquals(
789 toFunctionName.name,
790 "toFunctionName",
791 );
792 });
793 });
794 });
795
796 describe("toIndex", () => {
797 it("[[Call]] returns an index", () => {
798 assertStrictEquals(toIndex(9007199254740991), 9007199254740991);
799 });
800
801 it("[[Call]] returns zero for a zerolike argument", () => {
802 assertStrictEquals(toIndex(NaN), 0);
803 assertStrictEquals(toIndex("failure"), 0);
804 assertStrictEquals(toIndex(-0), 0);
805 });
806
807 it("[[Call]] rounds down to the nearest integer", () => {
808 assertStrictEquals(toIndex(0.25), 0);
809 assertStrictEquals(toIndex(1.1), 1);
810 });
811
812 it("[[Call]] throws when provided a negative number", () => {
813 assertThrows(() => toIndex(-1));
814 assertThrows(() => toIndex(-Infinity));
815 });
816
817 it("[[Call]] throws when provided a number greater than or equal to 2 ** 53", () => {
818 assertThrows(() => toIndex(9007199254740992));
819 assertThrows(() => toIndex(Infinity));
820 });
821
822 it("[[Construct]] throws an error", () => {
823 assertThrows(() => new toIndex(0));
824 });
825
826 describe(".length", () => {
827 it("[[Get]] returns the correct length", () => {
828 assertStrictEquals(toIndex.length, 1);
829 });
830 });
831
832 describe(".name", () => {
833 it("[[Get]] returns the correct name", () => {
834 assertStrictEquals(toIndex.name, "toIndex");
835 });
836 });
837 });
838
839 describe("toLength", () => {
840 it("[[Call]] returns a length", () => {
841 assertStrictEquals(toLength(9007199254740991), 9007199254740991);
842 });
843
844 it("[[Call]] returns zero for a nan argument", () => {
845 assertStrictEquals(toLength(NaN), 0);
846 assertStrictEquals(toLength("failure"), 0);
847 });
848
849 it("[[Call]] rounds down to the nearest integer", () => {
850 assertStrictEquals(toLength(0.25), 0);
851 assertStrictEquals(toLength(1.1), 1);
852 });
853
854 it("[[Call]] returns a result greater than or equal to zero", () => {
855 assertStrictEquals(toLength(-0), 0);
856 assertStrictEquals(toLength(-1), 0);
857 assertStrictEquals(toLength(-Infinity), 0);
858 });
859
860 it("[[Call]] returns a result less than 2 ** 53", () => {
861 assertStrictEquals(toLength(9007199254740992), 9007199254740991);
862 assertStrictEquals(toLength(Infinity), 9007199254740991);
863 });
864
865 it("[[Construct]] throws an error", () => {
866 assertThrows(() => new toLength(0));
867 });
868
869 describe(".length", () => {
870 it("[[Get]] returns the correct length", () => {
871 assertStrictEquals(toLength.length, 1);
872 });
873 });
874
875 describe(".name", () => {
876 it("[[Get]] returns the correct name", () => {
877 assertStrictEquals(toLength.name, "toLength");
878 });
879 });
880 });
881
882 describe("toPrimitive", () => {
883 it("[[Call]] returns the argument when passed a primitive", () => {
884 const value = Symbol();
885 assertStrictEquals(toPrimitive(value), value);
886 });
887
888 it("[[Call]] works with nullish values", () => {
889 assertStrictEquals(toPrimitive(null), null);
890 assertStrictEquals(toPrimitive(), void {});
891 });
892
893 it("[[Call]] calls ordinaryToPrimitive by default", () => {
894 const value = Object.assign(
895 Object.create(null),
896 {
897 valueOf() {
898 return "success";
899 },
900 },
901 );
902 assertStrictEquals(toPrimitive(value), "success");
903 });
904
905 it("[[Call]] accepts a hint", () => {
906 const value = Object.assign(
907 Object.create(null),
908 {
909 toString() {
910 return "success";
911 },
912 valueOf() {
913 return "failure";
914 },
915 },
916 );
917 assertStrictEquals(toPrimitive(value, "string"), "success");
918 });
919
920 it("[[Call]] uses the exotic toPrimitive method if available", () => {
921 const value = Object.assign(
922 Object.create(null),
923 {
924 [Symbol.toPrimitive]() {
925 return "success";
926 },
927 },
928 );
929 assertStrictEquals(toPrimitive(value), "success");
930 });
931
932 it("[[Call]] passes the hint to the exotic toPrimitive", () => {
933 const value = Object.assign(
934 Object.create(null),
935 {
936 [Symbol.toPrimitive](hint) {
937 return hint === "string" ? "success" : "failure";
938 },
939 },
940 );
941 assertStrictEquals(toPrimitive(value, "string"), "success");
942 });
943
944 it('[[Call]] passes a "default" hint by default', () => {
945 const value = Object.assign(
946 Object.create(null),
947 {
948 [Symbol.toPrimitive](hint) {
949 return hint === "default" ? "success" : "failure";
950 },
951 },
952 );
953 assertStrictEquals(toPrimitive(value), "success");
954 });
955
956 it("[[Call]] throws for an invalid hint", () => {
957 const value1 = Object.assign(
958 Object.create(null),
959 {
960 [Symbol.toPrimitive]() {
961 return "success";
962 },
963 },
964 );
965 const value2 = Object.assign(
966 Object.create(null),
967 {
968 valueOf() {
969 return true;
970 },
971 },
972 );
973 assertThrows(() => toPrimitive(value1, "badhint"));
974 assertThrows(() => toPrimitive(value2, "badhint"));
975 assertThrows(() => toPrimitive(true, "badhint"));
976 });
977
978 it("[[Construct]] throws an error", () => {
979 assertThrows(() => new toPrimitive(true));
980 });
981
982 describe(".length", () => {
983 it("[[Get]] returns the correct length", () => {
984 assertStrictEquals(toPrimitive.length, 1);
985 });
986 });
987
988 describe(".name", () => {
989 it("[[Get]] returns the correct name", () => {
990 assertStrictEquals(toPrimitive.name, "toPrimitive");
991 });
992 });
993 });
994
995 describe("toPropertyKey", () => {
996 it("returns a string or symbol", () => {
997 const sym = Symbol();
998 assertStrictEquals(toPropertyKey(sym), sym);
999 assertStrictEquals(
1000 toPropertyKey(new String("success")),
1001 "success",
1002 );
1003 });
1004
1005 it("favours the `toString` representation", () => {
1006 assertStrictEquals(
1007 toPropertyKey({
1008 toString() {
1009 return "success";
1010 },
1011 valueOf() {
1012 return "failure";
1013 },
1014 }),
1015 "success",
1016 );
1017 });
1018
1019 it("[[Construct]] throws an error", () => {
1020 assertThrows(() => new toPropertyKey(""));
1021 });
1022
1023 describe(".length", () => {
1024 it("[[Get]] returns the correct length", () => {
1025 assertStrictEquals(toPropertyKey.length, 1);
1026 });
1027 });
1028
1029 describe(".name", () => {
1030 it("[[Get]] returns the correct name", () => {
1031 assertStrictEquals(toPropertyKey.name, "toPropertyKey");
1032 });
1033 });
1034 });
1035
1036 describe("type", () => {
1037 it('[[Call]] returns "null" for null', () => {
1038 assertStrictEquals(type(null), "null");
1039 });
1040
1041 it('[[Call]] returns "undefined" for undefined', () => {
1042 assertStrictEquals(type(void {}), "undefined");
1043 });
1044
1045 it('[[Call]] returns "object" for non‐callable objects', () => {
1046 assertStrictEquals(type(Object.create(null)), "object");
1047 });
1048
1049 it('[[Call]] returns "object" for callable objects', () => {
1050 assertStrictEquals(type(() => {}), "object");
1051 });
1052
1053 it('[[Call]] returns "object" for constructable objects', () => {
1054 assertStrictEquals(type(class {}), "object");
1055 });
1056
1057 it("[[Construct]] throws an error", () => {
1058 assertThrows(() => new type({}));
1059 });
1060
1061 describe(".length", () => {
1062 it("[[Get]] returns the correct length", () => {
1063 assertStrictEquals(type.length, 1);
1064 });
1065 });
1066
1067 describe(".name", () => {
1068 it("[[Get]] returns the correct name", () => {
1069 assertStrictEquals(type.name, "type");
1070 });
1071 });
1072 });
1073
1074 describe("Ε", () => {
1075 it("[[Get]] is ε", () => {
1076 assertStrictEquals(Ε, Number.EPSILON);
1077 });
1078 });
1079
1080 describe("Π", () => {
1081 it("[[Get]] is π", () => {
1082 assertStrictEquals(Π, Math.PI);
1083 });
1084 });
1085
1086 describe("ℇ", () => {
1087 it("[[Get]] is ℇ", () => {
1088 assertStrictEquals(ℇ, Math.E);
1089 });
1090 });
This page took 0.153221 seconds and 5 git commands to generate.