]> Lady’s Gitweb - Pisces/blob - value.test.js
Minor refactors & adjustments to values.js
[Pisces] / value.test.js
1 // SPDX-FileCopyrightText: 2022, 2023, 2025 Lady <https://www.ladys.computer/about/#lady>
2 // SPDX-License-Identifier: MPL-2.0
3 /**
4 * ⁌ β™“πŸŒŸ PiscΔ“s ∷ value.test.js
5 *
6 * Copyright Β© 2022–2023, 2025 Lady [@ Ladys Computer].
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
11 */
12
13 import {
14 assertEquals,
15 assertStrictEquals,
16 assertThrows,
17 describe,
18 it,
19 } from "./dev-deps.js";
20 import {
21 ASYNC_ITERATOR,
22 completePropertyDescriptor,
23 HAS_INSTANCE,
24 IS_CONCAT_SPREADABLE,
25 isAccessorDescriptor,
26 isDataDescriptor,
27 isFullyPopulatedDescriptor,
28 isGenericDescriptor,
29 ITERATOR,
30 LN_10,
31 LN_2,
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_SQRT_2,
48 REPLACE,
49 sameValue,
50 sameValueZero,
51 SPECIES,
52 SPLIT,
53 SQRT_2,
54 TO_PRIMITIVE,
55 TO_STRING_TAG,
56 toFunctionName,
57 toIndex,
58 toLength,
59 toPrimitive,
60 toPropertyKey,
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("LN_10", () => {
97 it("[[Get]] is ln(10)", () => {
98 assertStrictEquals(LN_10, Math.LN10);
99 });
100 });
101
102 describe("LN_2", () => {
103 it("[[Get]] is ln(2)", () => {
104 assertStrictEquals(LN_2, 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_SQRT_2", () => {
199 it("[[Get]] is sqrt(Β½)", () => {
200 assertStrictEquals(RECIPROCAL_SQRT_2, 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("SQRT_2", () => {
223 it("[[Get]] is sqrt(2)", () => {
224 assertStrictEquals(SQRT_2, 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("ordinaryToPrimitive", () => {
509 it("[[Call]] prefers `valueOf` by default", () => {
510 const obj = {
511 toString() {
512 return "failure";
513 },
514 valueOf() {
515 return "success";
516 },
517 };
518 assertStrictEquals(ordinaryToPrimitive(obj), "success");
519 assertStrictEquals(ordinaryToPrimitive(obj, "default"), "success");
520 });
521
522 it('[[Call]] prefers `valueOf` for a "number" hint', () => {
523 const obj = {
524 toString() {
525 return "failure";
526 },
527 valueOf() {
528 return "success";
529 },
530 };
531 assertStrictEquals(ordinaryToPrimitive(obj, "number"), "success");
532 });
533
534 it('[[Call]] prefers `toString` for a "string" hint', () => {
535 const obj = {
536 toString() {
537 return "success";
538 },
539 valueOf() {
540 return "failure";
541 },
542 };
543 assertStrictEquals(ordinaryToPrimitive(obj, "string"), "success");
544 });
545
546 it("[[Call]] falls back to the other method if the first isn’t callable", () => {
547 const obj = {
548 toString() {
549 return "success";
550 },
551 valueOf: "failure",
552 };
553 assertStrictEquals(ordinaryToPrimitive(obj), "success");
554 });
555
556 it("[[Call]] falls back to the other method if the first returns an object", () => {
557 const obj = {
558 toString() {
559 return "success";
560 },
561 valueOf() {
562 return new String("failure");
563 },
564 };
565 assertStrictEquals(ordinaryToPrimitive(obj), "success");
566 });
567
568 it("[[Call]] throws an error if neither method is callable", () => {
569 const obj = {
570 toString: "failure",
571 valueOf: "failure",
572 };
573 assertThrows(() => ordinaryToPrimitive(obj));
574 });
575
576 it("[[Call]] throws an error if neither method returns an object", () => {
577 const obj = {
578 toString() {
579 return new String("failure");
580 },
581 valueOf() {
582 return new String("failure");
583 },
584 };
585 assertThrows(() => ordinaryToPrimitive(obj));
586 });
587
588 it("[[Construct]] throws an error", () => {
589 assertThrows(() => new ordinaryToPrimitive(""));
590 });
591
592 describe(".length", () => {
593 it("[[Get]] returns the correct length", () => {
594 assertStrictEquals(ordinaryToPrimitive.length, 2);
595 });
596 });
597
598 describe(".name", () => {
599 it("[[Get]] returns the correct name", () => {
600 assertStrictEquals(
601 ordinaryToPrimitive.name,
602 "ordinaryToPrimitive",
603 );
604 });
605 });
606 });
607
608 describe("sameValue", () => {
609 it("[[Call]] returns false for null πŸ†š undefined", () => {
610 assertStrictEquals(sameValue(null, undefined), false);
611 });
612
613 it("[[Call]] returns false for null πŸ†š an object", () => {
614 assertStrictEquals(sameValue(null, {}), false);
615 });
616
617 it("[[Call]] returns true for null πŸ†š null", () => {
618 assertStrictEquals(sameValue(null, null), true);
619 });
620
621 it("[[Call]] returns false for two different objects", () => {
622 assertStrictEquals(sameValue({}, {}), false);
623 });
624
625 it("[[Call]] returns true for the same object", () => {
626 const obj = {};
627 assertStrictEquals(sameValue(obj, obj), true);
628 });
629
630 it("[[Call]] returns false for Β±0", () => {
631 assertStrictEquals(sameValue(0, -0), false);
632 });
633
634 it("[[Call]] returns true for -0", () => {
635 assertStrictEquals(sameValue(-0, -0), true);
636 });
637
638 it("[[Call]] returns true for nan", () => {
639 assertStrictEquals(sameValue(0 / 0, 0 / 0), true);
640 });
641
642 it("[[Call]] returns false for a primitive and its wrapped object", () => {
643 assertStrictEquals(sameValue(false, new Boolean(false)), false);
644 });
645
646 it("[[Construct]] throws an error", () => {
647 assertThrows(() => new sameValue(true, true));
648 });
649
650 describe(".length", () => {
651 it("[[Get]] returns the correct length", () => {
652 assertStrictEquals(sameValue.length, 2);
653 });
654 });
655
656 describe(".name", () => {
657 it("[[Get]] returns the correct name", () => {
658 assertStrictEquals(sameValue.name, "sameValue");
659 });
660 });
661 });
662
663 describe("sameValueZero", () => {
664 it("[[Call]] returns false for null πŸ†š undefined", () => {
665 assertStrictEquals(sameValueZero(null, undefined), false);
666 });
667
668 it("[[Call]] returns false for null πŸ†š an object", () => {
669 assertStrictEquals(sameValueZero(null, {}), false);
670 });
671
672 it("[[Call]] returns true for null πŸ†š null", () => {
673 assertStrictEquals(sameValueZero(null, null), true);
674 });
675
676 it("[[Call]] returns false for two different objects", () => {
677 assertStrictEquals(sameValueZero({}, {}), false);
678 });
679
680 it("[[Call]] returns true for the same object", () => {
681 const obj = {};
682 assertStrictEquals(sameValueZero(obj, obj), true);
683 });
684
685 it("[[Call]] returns true for Β±0", () => {
686 assertStrictEquals(sameValueZero(0, -0), true);
687 });
688
689 it("[[Call]] returns true for -0", () => {
690 assertStrictEquals(sameValueZero(-0, -0), true);
691 });
692
693 it("[[Call]] returns true for nan", () => {
694 assertStrictEquals(sameValueZero(0 / 0, 0 / 0), true);
695 });
696
697 it("[[Call]] returns false for a primitive and its wrapped object", () => {
698 assertStrictEquals(
699 sameValueZero(false, new Boolean(false)),
700 false,
701 );
702 });
703
704 it("[[Construct]] throws an error", () => {
705 assertThrows(() => new sameValueZero(true, true));
706 });
707
708 describe(".length", () => {
709 it("[[Get]] returns the correct length", () => {
710 assertStrictEquals(sameValueZero.length, 2);
711 });
712 });
713
714 describe(".name", () => {
715 it("[[Get]] returns the correct name", () => {
716 assertStrictEquals(sameValueZero.name, "sameValueZero");
717 });
718 });
719 });
720
721 describe("toFunctionName", () => {
722 it("[[Call]] works with strings and no prefix", () => {
723 assertStrictEquals(toFunctionName("etaoin"), "etaoin");
724 });
725
726 it("[[Call]] works with objects and no prefix", () => {
727 assertStrictEquals(
728 toFunctionName({
729 toString() {
730 return "etaoin";
731 },
732 }),
733 "etaoin",
734 );
735 });
736
737 it("[[Call]] works with descriptionless symbols and no prefix", () => {
738 assertStrictEquals(toFunctionName(Symbol()), "");
739 });
740
741 it("[[Call]] works with empty description symbols and no prefix", () => {
742 assertStrictEquals(toFunctionName(Symbol("")), "[]");
743 });
744
745 it("[[Call]] works with described symbols and no prefix", () => {
746 assertStrictEquals(toFunctionName(Symbol("etaoin")), "[etaoin]");
747 });
748
749 it("[[Call]] works with strings and a prefix", () => {
750 assertStrictEquals(toFunctionName("etaoin", "foo"), "foo etaoin");
751 });
752
753 it("[[Call]] works with objects and no prefix", () => {
754 assertStrictEquals(
755 toFunctionName({
756 toString() {
757 return "etaoin";
758 },
759 }, "foo"),
760 "foo etaoin",
761 );
762 });
763
764 it("[[Call]] works with descriptionless symbols and no prefix", () => {
765 assertStrictEquals(toFunctionName(Symbol(), "foo"), "foo ");
766 });
767
768 it("[[Call]] works with empty description symbols and no prefix", () => {
769 assertStrictEquals(toFunctionName(Symbol(""), "foo"), "foo []");
770 });
771
772 it("[[Call]] works with described symbols and no prefix", () => {
773 assertStrictEquals(
774 toFunctionName(Symbol("etaoin"), "foo"),
775 "foo [etaoin]",
776 );
777 });
778
779 it("[[Construct]] throws an error", () => {
780 assertThrows(() => new toFunctionName(""));
781 });
782
783 describe(".length", () => {
784 it("[[Get]] returns the correct length", () => {
785 assertStrictEquals(toFunctionName.length, 1);
786 });
787 });
788
789 describe(".name", () => {
790 it("[[Get]] returns the correct name", () => {
791 assertStrictEquals(
792 toFunctionName.name,
793 "toFunctionName",
794 );
795 });
796 });
797 });
798
799 describe("toIndex", () => {
800 it("[[Call]] returns an index", () => {
801 assertStrictEquals(toIndex(9007199254740991), 9007199254740991);
802 });
803
804 it("[[Call]] returns zero for a zerolike argument", () => {
805 assertStrictEquals(toIndex(NaN), 0);
806 assertStrictEquals(toIndex("failure"), 0);
807 assertStrictEquals(toIndex(-0), 0);
808 });
809
810 it("[[Call]] rounds down to the nearest integer", () => {
811 assertStrictEquals(toIndex(0.25), 0);
812 assertStrictEquals(toIndex(1.1), 1);
813 });
814
815 it("[[Call]] throws when provided a negative number", () => {
816 assertThrows(() => toIndex(-1));
817 assertThrows(() => toIndex(-Infinity));
818 });
819
820 it("[[Call]] throws when provided a number greater than or equal to 2 ** 53", () => {
821 assertThrows(() => toIndex(9007199254740992));
822 assertThrows(() => toIndex(Infinity));
823 });
824
825 it("[[Construct]] throws an error", () => {
826 assertThrows(() => new toIndex(0));
827 });
828
829 describe(".length", () => {
830 it("[[Get]] returns the correct length", () => {
831 assertStrictEquals(toIndex.length, 1);
832 });
833 });
834
835 describe(".name", () => {
836 it("[[Get]] returns the correct name", () => {
837 assertStrictEquals(toIndex.name, "toIndex");
838 });
839 });
840 });
841
842 describe("toLength", () => {
843 it("[[Call]] returns a length", () => {
844 assertStrictEquals(toLength(9007199254740991), 9007199254740991);
845 });
846
847 it("[[Call]] returns zero for a nan argument", () => {
848 assertStrictEquals(toLength(NaN), 0);
849 assertStrictEquals(toLength("failure"), 0);
850 });
851
852 it("[[Call]] rounds down to the nearest integer", () => {
853 assertStrictEquals(toLength(0.25), 0);
854 assertStrictEquals(toLength(1.1), 1);
855 });
856
857 it("[[Call]] returns a result greater than or equal to zero", () => {
858 assertStrictEquals(toLength(-0), 0);
859 assertStrictEquals(toLength(-1), 0);
860 assertStrictEquals(toLength(-Infinity), 0);
861 });
862
863 it("[[Call]] returns a result less than 2 ** 53", () => {
864 assertStrictEquals(toLength(9007199254740992), 9007199254740991);
865 assertStrictEquals(toLength(Infinity), 9007199254740991);
866 });
867
868 it("[[Construct]] throws an error", () => {
869 assertThrows(() => new toLength(0));
870 });
871
872 describe(".length", () => {
873 it("[[Get]] returns the correct length", () => {
874 assertStrictEquals(toLength.length, 1);
875 });
876 });
877
878 describe(".name", () => {
879 it("[[Get]] returns the correct name", () => {
880 assertStrictEquals(toLength.name, "toLength");
881 });
882 });
883 });
884
885 describe("toPrimitive", () => {
886 it("[[Call]] returns the argument when passed a primitive", () => {
887 const value = Symbol();
888 assertStrictEquals(toPrimitive(value), value);
889 });
890
891 it("[[Call]] works with nullish values", () => {
892 assertStrictEquals(toPrimitive(null), null);
893 assertStrictEquals(toPrimitive(), void {});
894 });
895
896 it("[[Call]] calls ordinaryToPrimitive by default", () => {
897 const value = Object.assign(
898 Object.create(null),
899 {
900 valueOf() {
901 return "success";
902 },
903 },
904 );
905 assertStrictEquals(toPrimitive(value), "success");
906 });
907
908 it("[[Call]] accepts a hint", () => {
909 const value = Object.assign(
910 Object.create(null),
911 {
912 toString() {
913 return "success";
914 },
915 valueOf() {
916 return "failure";
917 },
918 },
919 );
920 assertStrictEquals(toPrimitive(value, "string"), "success");
921 });
922
923 it("[[Call]] uses the exotic toPrimitive method if available", () => {
924 const value = Object.assign(
925 Object.create(null),
926 {
927 [Symbol.toPrimitive]() {
928 return "success";
929 },
930 },
931 );
932 assertStrictEquals(toPrimitive(value), "success");
933 });
934
935 it("[[Call]] passes the hint to the exotic toPrimitive", () => {
936 const value = Object.assign(
937 Object.create(null),
938 {
939 [Symbol.toPrimitive](hint) {
940 return hint === "string" ? "success" : "failure";
941 },
942 },
943 );
944 assertStrictEquals(toPrimitive(value, "string"), "success");
945 });
946
947 it('[[Call]] passes a "default" hint by default', () => {
948 const value = Object.assign(
949 Object.create(null),
950 {
951 [Symbol.toPrimitive](hint) {
952 return hint === "default" ? "success" : "failure";
953 },
954 },
955 );
956 assertStrictEquals(toPrimitive(value), "success");
957 });
958
959 it("[[Call]] throws for an invalid hint", () => {
960 const value1 = Object.assign(
961 Object.create(null),
962 {
963 [Symbol.toPrimitive]() {
964 return "success";
965 },
966 },
967 );
968 const value2 = Object.assign(
969 Object.create(null),
970 {
971 valueOf() {
972 return true;
973 },
974 },
975 );
976 assertThrows(() => toPrimitive(value1, "badhint"));
977 assertThrows(() => toPrimitive(value2, "badhint"));
978 assertThrows(() => toPrimitive(true, "badhint"));
979 });
980
981 it("[[Construct]] throws an error", () => {
982 assertThrows(() => new toPrimitive(true));
983 });
984
985 describe(".length", () => {
986 it("[[Get]] returns the correct length", () => {
987 assertStrictEquals(toPrimitive.length, 1);
988 });
989 });
990
991 describe(".name", () => {
992 it("[[Get]] returns the correct name", () => {
993 assertStrictEquals(toPrimitive.name, "toPrimitive");
994 });
995 });
996 });
997
998 describe("toPropertyKey", () => {
999 it("returns a string or symbol", () => {
1000 const sym = Symbol();
1001 assertStrictEquals(toPropertyKey(sym), sym);
1002 assertStrictEquals(
1003 toPropertyKey(new String("success")),
1004 "success",
1005 );
1006 });
1007
1008 it("favours the `toString` representation", () => {
1009 assertStrictEquals(
1010 toPropertyKey({
1011 toString() {
1012 return "success";
1013 },
1014 valueOf() {
1015 return "failure";
1016 },
1017 }),
1018 "success",
1019 );
1020 });
1021
1022 it("[[Construct]] throws an error", () => {
1023 assertThrows(() => new toPropertyKey(""));
1024 });
1025
1026 describe(".length", () => {
1027 it("[[Get]] returns the correct length", () => {
1028 assertStrictEquals(toPropertyKey.length, 1);
1029 });
1030 });
1031
1032 describe(".name", () => {
1033 it("[[Get]] returns the correct name", () => {
1034 assertStrictEquals(toPropertyKey.name, "toPropertyKey");
1035 });
1036 });
1037 });
1038
1039 describe("type", () => {
1040 it('[[Call]] returns "null" for null', () => {
1041 assertStrictEquals(type(null), "null");
1042 });
1043
1044 it('[[Call]] returns "undefined" for undefined', () => {
1045 assertStrictEquals(type(void {}), "undefined");
1046 });
1047
1048 it('[[Call]] returns "object" for non‐callable objects', () => {
1049 assertStrictEquals(type(Object.create(null)), "object");
1050 });
1051
1052 it('[[Call]] returns "object" for callable objects', () => {
1053 assertStrictEquals(type(() => {}), "object");
1054 });
1055
1056 it('[[Call]] returns "object" for constructable objects', () => {
1057 assertStrictEquals(type(class {}), "object");
1058 });
1059
1060 it("[[Construct]] throws an error", () => {
1061 assertThrows(() => new type({}));
1062 });
1063
1064 describe(".length", () => {
1065 it("[[Get]] returns the correct length", () => {
1066 assertStrictEquals(type.length, 1);
1067 });
1068 });
1069
1070 describe(".name", () => {
1071 it("[[Get]] returns the correct name", () => {
1072 assertStrictEquals(type.name, "type");
1073 });
1074 });
1075 });
1076
1077 describe("𝑒", () => {
1078 it("[[Get]] is 𝑒", () => {
1079 assertStrictEquals(𝑒, Math.E);
1080 });
1081 });
1082
1083 describe("πœ€", () => {
1084 it("[[Get]] is πœ€", () => {
1085 assertStrictEquals(πœ€, Number.EPSILON);
1086 });
1087 });
1088
1089 describe("πœ‹", () => {
1090 it("[[Get]] is πœ‹", () => {
1091 assertStrictEquals(πœ‹, Math.PI);
1092 });
1093 });
This page took 0.792597 seconds and 5 git commands to generate.