1 // ♓🌟 Piscēs ∷ value.test.js
2 // ====================================================================
4 // Copyright © 2022–2023 Lady [@ Lady’s Computer].
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/>.
15 } from "./dev-deps.js";
18 canonicalNumericIndexString
,
42 describe("ASYNC_ITERATOR", () => {
43 it("[[Get]] is @@asyncIterator", () => {
44 assertStrictEquals(ASYNC_ITERATOR
, Symbol
.asyncIterator
);
48 describe("HAS_INSTANCE", () => {
49 it("[[Get]] is @@hasInstance", () => {
50 assertStrictEquals(HAS_INSTANCE
, Symbol
.hasInstance
);
54 describe("IS_CONCAT_SPREADABLE", () => {
55 it("[[Get]] is @@isConcatSpreadable", () => {
58 Symbol
.isConcatSpreadable
,
63 describe("ITERATOR", () => {
64 it("[[Get]] is @@iterator", () => {
65 assertStrictEquals(ITERATOR
, Symbol
.iterator
);
69 describe("MATCH", () => {
70 it("[[Get]] is @@match", () => {
71 assertStrictEquals(MATCH
, Symbol
.match
);
75 describe("MATCH_ALL", () => {
76 it("[[Get]] is @@matchAll", () => {
77 assertStrictEquals(MATCH_ALL
, Symbol
.matchAll
);
81 describe("NULL", () => {
82 it("[[Get]] is null", () => {
83 assertStrictEquals(NULL
, null);
87 describe("REPLACE", () => {
88 it("[[Get]] is @@replace", () => {
89 assertStrictEquals(REPLACE
, Symbol
.replace
);
93 describe("SPECIES", () => {
94 it("[[Get]] is @@species", () => {
95 assertStrictEquals(SPECIES
, Symbol
.species
);
99 describe("SPLIT", () => {
100 it("[[Get]] is @@split", () => {
101 assertStrictEquals(SPLIT
, Symbol
.split
);
105 describe("TO_PRIMITIVE", () => {
106 it("[[Get]] is @@toPrimitive", () => {
107 assertStrictEquals(TO_PRIMITIVE
, Symbol
.toPrimitive
);
111 describe("TO_STRING_TAG", () => {
112 it("[[Get]] is @@toStringTag", () => {
113 assertStrictEquals(TO_STRING_TAG
, Symbol
.toStringTag
);
117 describe("UNDEFINED", () => {
118 it("[[Get]] is undefined", () => {
119 assertStrictEquals(UNDEFINED
, void {});
123 describe("UNSCOPABLES", () => {
124 it("[[Get]] is @@unscopables", () => {
125 assertStrictEquals(UNSCOPABLES
, Symbol
.unscopables
);
129 describe("canonicalNumericIndexString", () => {
130 it("[[Call]] returns undefined for nonstrings", () => {
131 assertStrictEquals(canonicalNumericIndexString(1), void {});
134 it("[[Call]] returns undefined for noncanonical strings", () => {
135 assertStrictEquals(canonicalNumericIndexString(""), void {});
136 assertStrictEquals(canonicalNumericIndexString("01"), void {});
138 canonicalNumericIndexString("9007199254740993"),
143 it('[[Call]] returns -0 for "-0"', () => {
144 assertStrictEquals(canonicalNumericIndexString("-0"), -0);
147 it("[[Call]] returns the corresponding number for canonical strings", () => {
148 assertStrictEquals(canonicalNumericIndexString("0"), 0);
149 assertStrictEquals(canonicalNumericIndexString("-0.25"), -0.25);
151 canonicalNumericIndexString("9007199254740992"),
154 assertStrictEquals(canonicalNumericIndexString("NaN"), 0 / 0);
155 assertStrictEquals(canonicalNumericIndexString("Infinity"), 1 / 0);
157 canonicalNumericIndexString("-Infinity"),
162 it("[[Construct]] throws an error", () => {
163 assertThrows(() => new canonicalNumericIndexString(""));
166 describe(".length", () => {
167 it("[[Get]] returns the correct length", () => {
168 assertStrictEquals(canonicalNumericIndexString
.length
, 1);
172 describe(".name", () => {
173 it("[[Get]] returns the correct name", () => {
175 canonicalNumericIndexString
.name
,
176 "canonicalNumericIndexString",
182 describe("lengthOfArraylike", () => {
183 it("[[Call]] returns the length", () => {
185 lengthOfArraylike({ length
: 9007199254740991 }),
190 it("[[Call]] returns a non·nan result", () => {
191 assertStrictEquals(lengthOfArraylike({ length
: NaN
}), 0);
192 assertStrictEquals(lengthOfArraylike({ length
: "failure" }), 0);
195 it("[[Call]] returns an integral result", () => {
196 assertStrictEquals(lengthOfArraylike({ length
: 0.25 }), 0);
197 assertStrictEquals(lengthOfArraylike({ length
: 1.1 }), 1);
200 it("[[Call]] returns a result greater than or equal to zero", () => {
201 assertStrictEquals(lengthOfArraylike({ length
: -0 }), 0);
202 assertStrictEquals(lengthOfArraylike({ length
: -1 }), 0);
203 assertStrictEquals(lengthOfArraylike({ length
: -Infinity
}), 0);
206 it("[[Call]] returns a result less than 2 ** 53", () => {
208 lengthOfArraylike({ length
: 9007199254740992 }),
212 lengthOfArraylike({ length
: Infinity
}),
217 it("[[Call]] does not require an object argument", () => {
218 assertStrictEquals(lengthOfArraylike("string"), 6);
219 assertStrictEquals(lengthOfArraylike(Symbol()), 0);
222 it("[[Construct]] throws an error", () => {
223 assertThrows(() => new lengthOfArraylike(""));
226 describe(".length", () => {
227 it("[[Get]] returns the correct length", () => {
228 assertStrictEquals(lengthOfArraylike
.length
, 1);
232 describe(".name", () => {
233 it("[[Get]] returns the correct name", () => {
234 assertStrictEquals(lengthOfArraylike
.name
, "lengthOfArraylike");
239 describe("ordinaryToPrimitive", () => {
240 it("[[Call]] prefers `valueOf` by default", () => {
249 assertStrictEquals(ordinaryToPrimitive(obj
), "success");
250 assertStrictEquals(ordinaryToPrimitive(obj
, "default"), "success");
253 it('[[Call]] prefers `valueOf` for a "number" hint', () => {
262 assertStrictEquals(ordinaryToPrimitive(obj
, "number"), "success");
265 it('[[Call]] prefers `toString` for a "string" hint', () => {
274 assertStrictEquals(ordinaryToPrimitive(obj
, "string"), "success");
277 it("[[Call]] falls back to the other method if the first isn’t callable", () => {
284 assertStrictEquals(ordinaryToPrimitive(obj
), "success");
287 it("[[Call]] falls back to the other method if the first returns an object", () => {
293 return new String("failure");
296 assertStrictEquals(ordinaryToPrimitive(obj
), "success");
299 it("[[Call]] throws an error if neither method is callable", () => {
304 assertThrows(() => ordinaryToPrimitive(obj
));
307 it("[[Call]] throws an error if neither method returns an object", () => {
310 return new String("failure");
313 return new String("failure");
316 assertThrows(() => ordinaryToPrimitive(obj
));
319 it("[[Construct]] throws an error", () => {
320 assertThrows(() => new ordinaryToPrimitive(""));
323 describe(".length", () => {
324 it("[[Get]] returns the correct length", () => {
325 assertStrictEquals(ordinaryToPrimitive
.length
, 2);
329 describe(".name", () => {
330 it("[[Get]] returns the correct name", () => {
332 ordinaryToPrimitive
.name
,
333 "ordinaryToPrimitive",
339 describe("sameValue", () => {
340 it("[[Call]] returns false for null 🆚 undefined", () => {
341 assertStrictEquals(sameValue(null, undefined), false);
344 it("[[Call]] returns false for null 🆚 an object", () => {
345 assertStrictEquals(sameValue(null, {}), false);
348 it("[[Call]] returns true for null 🆚 null", () => {
349 assertStrictEquals(sameValue(null, null), true);
352 it("[[Call]] returns false for two different objects", () => {
353 assertStrictEquals(sameValue({}, {}), false);
356 it("[[Call]] returns true for the same object", () => {
358 assertStrictEquals(sameValue(obj
, obj
), true);
361 it("[[Call]] returns false for ±0", () => {
362 assertStrictEquals(sameValue(0, -0), false);
365 it("[[Call]] returns true for -0", () => {
366 assertStrictEquals(sameValue(-0, -0), true);
369 it("[[Call]] returns true for nan", () => {
370 assertStrictEquals(sameValue(0 / 0, 0 / 0), true);
373 it("[[Call]] returns false for a primitive and its wrapped object", () => {
374 assertStrictEquals(sameValue(false, new Boolean(false)), false);
377 it("[[Construct]] throws an error", () => {
378 assertThrows(() => new sameValue(true, true));
381 describe(".length", () => {
382 it("[[Get]] returns the correct length", () => {
383 assertStrictEquals(sameValue
.length
, 2);
387 describe(".name", () => {
388 it("[[Get]] returns the correct name", () => {
389 assertStrictEquals(sameValue
.name
, "sameValue");
394 describe("sameValueZero", () => {
395 it("[[Call]] returns false for null 🆚 undefined", () => {
396 assertStrictEquals(sameValueZero(null, undefined), false);
399 it("[[Call]] returns false for null 🆚 an object", () => {
400 assertStrictEquals(sameValueZero(null, {}), false);
403 it("[[Call]] returns true for null 🆚 null", () => {
404 assertStrictEquals(sameValueZero(null, null), true);
407 it("[[Call]] returns false for two different objects", () => {
408 assertStrictEquals(sameValueZero({}, {}), false);
411 it("[[Call]] returns true for the same object", () => {
413 assertStrictEquals(sameValueZero(obj
, obj
), true);
416 it("[[Call]] returns true for ±0", () => {
417 assertStrictEquals(sameValueZero(0, -0), true);
420 it("[[Call]] returns true for -0", () => {
421 assertStrictEquals(sameValueZero(-0, -0), true);
424 it("[[Call]] returns true for nan", () => {
425 assertStrictEquals(sameValueZero(0 / 0, 0 / 0), true);
428 it("[[Call]] returns false for a primitive and its wrapped object", () => {
430 sameValueZero(false, new Boolean(false)),
435 it("[[Construct]] throws an error", () => {
436 assertThrows(() => new sameValueZero(true, true));
439 describe(".length", () => {
440 it("[[Get]] returns the correct length", () => {
441 assertStrictEquals(sameValueZero
.length
, 2);
445 describe(".name", () => {
446 it("[[Get]] returns the correct name", () => {
447 assertStrictEquals(sameValueZero
.name
, "sameValueZero");
452 describe("toIndex", () => {
453 it("[[Call]] returns an index", () => {
454 assertStrictEquals(toIndex(9007199254740991), 9007199254740991);
457 it("[[Call]] returns zero for a zerolike argument", () => {
458 assertStrictEquals(toIndex(NaN
), 0);
459 assertStrictEquals(toIndex("failure"), 0);
460 assertStrictEquals(toIndex(-0), 0);
463 it("[[Call]] rounds down to the nearest integer", () => {
464 assertStrictEquals(toIndex(0.25), 0);
465 assertStrictEquals(toIndex(1.1), 1);
468 it("[[Call]] throws when provided a negative number", () => {
469 assertThrows(() => toIndex(-1));
470 assertThrows(() => toIndex(-Infinity
));
473 it("[[Call]] throws when provided a number greater than or equal to 2 ** 53", () => {
474 assertThrows(() => toIndex(9007199254740992));
475 assertThrows(() => toIndex(Infinity
));
478 it("[[Construct]] throws an error", () => {
479 assertThrows(() => new toIndex(0));
482 describe(".length", () => {
483 it("[[Get]] returns the correct length", () => {
484 assertStrictEquals(toIndex
.length
, 1);
488 describe(".name", () => {
489 it("[[Get]] returns the correct name", () => {
490 assertStrictEquals(toIndex
.name
, "toIndex");
495 describe("toLength", () => {
496 it("[[Call]] returns a length", () => {
497 assertStrictEquals(toLength(9007199254740991), 9007199254740991);
500 it("[[Call]] returns zero for a nan argument", () => {
501 assertStrictEquals(toLength(NaN
), 0);
502 assertStrictEquals(toLength("failure"), 0);
505 it("[[Call]] rounds down to the nearest integer", () => {
506 assertStrictEquals(toLength(0.25), 0);
507 assertStrictEquals(toLength(1.1), 1);
510 it("[[Call]] returns a result greater than or equal to zero", () => {
511 assertStrictEquals(toLength(-0), 0);
512 assertStrictEquals(toLength(-1), 0);
513 assertStrictEquals(toLength(-Infinity
), 0);
516 it("[[Call]] returns a result less than 2 ** 53", () => {
517 assertStrictEquals(toLength(9007199254740992), 9007199254740991);
518 assertStrictEquals(toLength(Infinity
), 9007199254740991);
521 it("[[Construct]] throws an error", () => {
522 assertThrows(() => new toLength(0));
525 describe(".length", () => {
526 it("[[Get]] returns the correct length", () => {
527 assertStrictEquals(toLength
.length
, 1);
531 describe(".name", () => {
532 it("[[Get]] returns the correct name", () => {
533 assertStrictEquals(toLength
.name
, "toLength");
538 describe("toPrimitive", () => {
539 it("[[Call]] returns the argument when passed a primitive", () => {
540 const value
= Symbol();
541 assertStrictEquals(toPrimitive(value
), value
);
544 it("[[Call]] works with nullish values", () => {
545 assertStrictEquals(toPrimitive(null), null);
546 assertStrictEquals(toPrimitive(), void {});
549 it("[[Call]] calls ordinaryToPrimitive by default", () => {
550 const value
= Object
.assign(
558 assertStrictEquals(toPrimitive(value
), "success");
561 it("[[Call]] accepts a hint", () => {
562 const value
= Object
.assign(
573 assertStrictEquals(toPrimitive(value
, "string"), "success");
576 it("[[Call]] uses the exotic toPrimitive method if available", () => {
577 const value
= Object
.assign(
580 [Symbol
.toPrimitive
]() {
585 assertStrictEquals(toPrimitive(value
), "success");
588 it("[[Call]] passes the hint to the exotic toPrimitive", () => {
589 const value
= Object
.assign(
592 [Symbol
.toPrimitive
](hint
) {
593 return hint
=== "string" ? "success" : "failure";
597 assertStrictEquals(toPrimitive(value
, "string"), "success");
600 it('[[Call]] passes a "default" hint by default', () => {
601 const value
= Object
.assign(
604 [Symbol
.toPrimitive
](hint
) {
605 return hint
=== "default" ? "success" : "failure";
609 assertStrictEquals(toPrimitive(value
), "success");
612 it("[[Call]] throws for an invalid hint", () => {
613 const value1
= Object
.assign(
616 [Symbol
.toPrimitive
]() {
621 const value2
= Object
.assign(
629 assertThrows(() => toPrimitive(value1
, "badhint"));
630 assertThrows(() => toPrimitive(value2
, "badhint"));
631 assertThrows(() => toPrimitive(true, "badhint"));
634 it("[[Construct]] throws an error", () => {
635 assertThrows(() => new toPrimitive(true));
638 describe(".length", () => {
639 it("[[Get]] returns the correct length", () => {
640 assertStrictEquals(toPrimitive
.length
, 1);
644 describe(".name", () => {
645 it("[[Get]] returns the correct name", () => {
646 assertStrictEquals(toPrimitive
.name
, "toPrimitive");
651 describe("type", () => {
652 it('[[Call]] returns "null" for null', () => {
653 assertStrictEquals(type(null), "null");
656 it('[[Call]] returns "undefined" for undefined', () => {
657 assertStrictEquals(type(void {}), "undefined");
660 it('[[Call]] returns "object" for non‐callable objects', () => {
661 assertStrictEquals(type(Object
.create(null)), "object");
664 it('[[Call]] returns "object" for callable objects', () => {
665 assertStrictEquals(type(() => {}), "object");
668 it('[[Call]] returns "object" for constructable objects', () => {
669 assertStrictEquals(type(class {}), "object");
672 it("[[Construct]] throws an error", () => {
673 assertThrows(() => new type({}));
676 describe(".length", () => {
677 it("[[Get]] returns the correct length", () => {
678 assertStrictEquals(type
.length
, 1);
682 describe(".name", () => {
683 it("[[Get]] returns the correct name", () => {
684 assertStrictEquals(type
.name
, "type");