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