]>
Lady’s Gitweb - Pisces/blob - function.test.js
439f9d38c2ad771d0529415c6f5f29d8735f35d3
1 // ♓🌟 Piscēs ∷ function.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/>.
17 } from "./dev-deps.js";
24 createCallableFunction
,
25 createIllegalConstructor
,
30 } from "./function.js";
32 describe("bind", () => {
33 it("[[Call]] binds this", () => {
46 it("[[Call]] binds arguments", () => {
50 return [this, ...args
];
54 ).call("failure", "cmfwyp"),
55 ["etaoin", "shrdlu", "cmfwyp"],
59 it("[[Call]] works with any arraylike third argument", () => {
63 return [this, ...args
];
71 ).call("failure", "cmfwyp"),
72 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
76 it("[[Construct]] throws an error", () => {
77 assertThrows(() => new bind(function () {}));
80 describe(".length", () => {
81 it("[[Get]] returns the correct length", () => {
82 assertStrictEquals(bind
.length
, 3);
86 describe(".name", () => {
87 it("[[Get]] returns the correct name", () => {
88 assertStrictEquals(bind
.name
, "bind");
93 describe("call", () => {
94 it("[[Call]] calls with the provided this value", () => {
107 it("[[Call]] calls with the provided arguments", () => {
111 return [this, ...args
];
114 ["shrdlu", "cmfwyp"],
116 ["etaoin", "shrdlu", "cmfwyp"],
120 it("[[Call]] works with any arraylike third argument", () => {
124 return [this, ...args
];
134 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
138 it("[[Construct]] throws an error", () => {
139 assertThrows(() => new call(function () {}, null, []));
142 describe(".length", () => {
143 it("[[Get]] returns the correct length", () => {
144 assertStrictEquals(call
.length
, 3);
148 describe(".name", () => {
149 it("[[Get]] returns the correct name", () => {
150 assertStrictEquals(call
.name
, "call");
155 describe("completesNormally", () => {
156 it("[[Call]] returns true for functions which complete normally", () => {
157 assertStrictEquals(completesNormally(() => {}), true);
160 it("[[Call]] returns false for functions which throw", () => {
162 completesNormally(() => {
169 it("[[Call]] throws when the argument is not callable", () => {
170 assertThrows(() => completesNormally(null));
173 it("[[Call]] throws when the argument is not provided", () => {
174 assertThrows(() => completesNormally());
177 it("[[Construct]] throws an error", () => {
178 assertThrows(() => new completesNormally(function () {}));
181 describe(".length", () => {
182 it("[[Get]] returns the correct length", () => {
183 assertStrictEquals(completesNormally
.length
, 1);
187 describe(".name", () => {
188 it("[[Get]] returns the correct name", () => {
189 assertStrictEquals(completesNormally
.name
, "completesNormally");
194 describe("construct", () => {
195 it("[[Call]] defaults to the constructor as the target", () => {
196 const constructor = class {};
198 Object
.getPrototypeOf(construct(
202 constructor.prototype,
206 it("[[Call]] constructs with the provided new target", () => {
207 const target = function () {};
220 it("[[Call]] constructs with the provided arguments", () => {
224 return [new.target
.value
, ...args
];
226 ["shrdlu", "cmfwyp"],
227 Object
.assign(function () {}, { value
: "etaoin" }),
229 ["etaoin", "shrdlu", "cmfwyp"],
233 it("[[Call]] works with any arraylike third argument", () => {
237 return [new.target
.value
, ...args
];
245 Object
.assign(function () {}, { value
: "etaoin" }),
247 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
251 it("[[Construct]] throws an error", () => {
252 assertThrows(() => new construct(function () {}, []));
255 describe(".length", () => {
256 it("[[Get]] returns the correct length", () => {
257 assertStrictEquals(construct
.length
, 2);
261 describe(".name", () => {
262 it("[[Get]] returns the correct name", () => {
263 assertStrictEquals(construct
.name
, "construct");
268 describe("createArrowFunction", () => {
269 it("[[Call]] sets this to undefined", () => {
280 it("[[Call]] transfers the provided arguments", () => {
284 return [this, ...args
];
286 ).call("failure", "etaoin", "shrdlu", "cmfwyp"),
287 [undefined, "etaoin", "shrdlu", "cmfwyp"],
291 it("[[Call]] correctly sets the length", () => {
294 function (_a
, _b
, _c
) {},
300 it("[[Call]] correctly sets the name", () => {
303 function etaoin() {},
309 it("[[Call]] allows the length to be overridden", () => {
312 function etaoin() {},
319 it("[[Call]] allows the name to be overridden", () => {
322 function etaoin() {},
329 it("[[Call]] returns an arrow function", () => {
330 assertThrows(() => new (createArrowFunction(function () {}))());
333 it("[[Call]] returns a function with no prototype property", () => {
336 createArrowFunction(function () {}, { prototype: {} })),
340 it("[[Construct]] throws an error", () => {
341 assertThrows(() => new createArrowFunction(function () {}));
344 describe(".length", () => {
345 it("[[Get]] returns the correct length", () => {
346 assertStrictEquals(createArrowFunction
.length
, 1);
350 describe(".name", () => {
351 it("[[Get]] returns the correct name", () => {
353 createArrowFunction
.name
,
354 "createArrowFunction",
360 describe("createCallableFunction", () => {
361 it("[[Call]] transfers the first argument to this", () => {
363 createCallableFunction(
367 ).call("fail", "pass"),
372 it("[[Call]] transfers the remaining arguments", () => {
374 createCallableFunction(
376 return [this, ...args
];
378 ).call("failure", "etaoin", "shrdlu", "cmfwyp"),
379 ["etaoin", "shrdlu", "cmfwyp"],
383 it("[[Call]] correctly sets the length", () => {
385 createCallableFunction(
386 function (_a
, _b
, _c
) {},
392 it("[[Call]] correctly sets the name", () => {
394 createCallableFunction(
395 function etaoin() {},
401 it("[[Call]] allows the length to be overridden", () => {
403 createCallableFunction(
404 function etaoin() {},
411 it("[[Call]] allows the name to be overridden", () => {
413 createCallableFunction(
414 function etaoin() {},
421 it("[[Call]] returns an arrow function", () => {
422 assertThrows(() => new (createCallableFunction(function () {}))());
425 it("[[Call]] returns a function with no prototype property", () => {
428 createCallableFunction(function () {}, { prototype: {} })),
432 it("[[Construct]] throws an error", () => {
433 assertThrows(() => new createCallableFunction(function () {}));
436 describe(".length", () => {
437 it("[[Get]] returns the correct length", () => {
438 assertStrictEquals(createCallableFunction
.length
, 1);
442 describe(".name", () => {
443 it("[[Get]] returns the correct name", () => {
445 createCallableFunction
.name
,
446 "createCallableFunction",
452 describe("createIllegalConstructor", () => {
453 it("[[Call]] returns a constructor", () => {
454 assert(isConstructor(createIllegalConstructor()));
457 it("[[Call]] works as expected when provided with no arguments", () => {
458 const constructor = createIllegalConstructor();
460 Object
.getPrototypeOf(constructor),
464 Object
.getPrototypeOf(constructor.prototype),
468 Object
.getOwnPropertyDescriptors(constructor.prototype),
471 constructor.prototype,
472 Object
.create(Object
.prototype, {
482 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
485 assertStrictEquals(constructor.name
, "");
488 it("[[Call]] returns a correctly‐formed constructor when provided one argument", () => {
489 const constructorPrototype
= Object
.create(null, {
490 name
: { value
: "etaoin" },
491 prototype: { value
: {} },
493 const constructor = createIllegalConstructor(
494 Object
.create(constructorPrototype
),
496 assert(isConstructor(constructor));
498 Object
.getPrototypeOf(constructor),
499 constructorPrototype
,
501 assert(Object
.hasOwn(constructor, "prototype"));
503 constructor.prototype,
504 constructorPrototype
.prototype,
507 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
510 assertStrictEquals(constructor.name
, "etaoin");
513 it("[[Construct]] throws an error", () => {
514 assertThrows(() => new createIllegalConstructor(function () {}));
517 describe("~", () => {
518 it("[[Call]] throws an error", () => {
520 createIllegalConstructor(function () {})();
524 it("[[Construct]] throws an error", () => {
526 createIllegalConstructor(function () {})();
531 describe(".length", () => {
532 it("[[Get]] returns the correct length", () => {
533 assertStrictEquals(createIllegalConstructor
.length
, 1);
537 describe(".name", () => {
538 it("[[Get]] returns the correct name", () => {
540 createIllegalConstructor
.name
,
541 "createIllegalConstructor",
547 describe("identity", () => {
548 it("[[Call]] returns what it is given", () => {
550 assertStrictEquals(identity(value
), value
);
553 it("[[Construct]] is constructable", () => {
555 assertStrictEquals(new identity(value
), value
);
558 it("[[Construct]] is subclassable", () => {
560 assertStrictEquals(new class extends identity
{}(value
), value
);
563 describe(".length", () => {
564 it("[[Get]] returns the correct length", () => {
565 assertStrictEquals(identity
.length
, 1);
569 describe(".name", () => {
570 it("[[Get]] returns the correct name", () => {
571 assertStrictEquals(identity
.name
, "identity");
576 describe("isCallable", () => {
577 it("[[Call]] returns true for ordinary functions", () => {
578 assertStrictEquals(isCallable(function () {}), true);
581 it("[[Call]] returns true for arrow functions", () => {
582 assertStrictEquals(isCallable(() => {}), true);
585 it("[[Call]] returns true for generator functions", () => {
586 assertStrictEquals(isCallable(function* () {}), true);
589 it("[[Call]] returns true for classes", () => {
590 assertStrictEquals(isCallable(class {}), true);
593 it("[[Call]] returns true for builtin functions", () => {
594 assertStrictEquals(isCallable(Math
.ceil
), true);
597 it("[[Call]] returns true for getters", () => {
600 Object
.getOwnPropertyDescriptor({
610 it("[[Call]] returns true for setters", () => {
613 Object
.getOwnPropertyDescriptor({
623 it("[[Call]] returns false for null", () => {
624 assertStrictEquals(isCallable(null), false);
627 it("[[Call]] returns false for objects", () => {
628 assertStrictEquals(isCallable({}), false);
631 it("[[Construct]] throws an error", () => {
632 assertThrows(() => new isCallable(function () {}));
635 describe(".length", () => {
636 it("[[Get]] returns the correct length", () => {
637 assertStrictEquals(isCallable
.length
, 1);
641 describe(".name", () => {
642 it("[[Get]] returns the correct name", () => {
643 assertStrictEquals(isCallable
.name
, "isCallable");
648 describe("isConstructor", () => {
649 it("[[Call]] returns true for ordinary functions", () => {
650 assertStrictEquals(isConstructor(function () {}), true);
653 it("[[Call]] returns false for arrow functions", () => {
654 assertStrictEquals(isConstructor(() => {}), false);
657 it("[[Call]] returns false for generator functions", () => {
658 assertStrictEquals(isConstructor(function* () {}), false);
661 it("[[Call]] returns true for classes", () => {
662 assertStrictEquals(isConstructor(class {}), true);
665 it("[[Call]] returns false for builtin functions", () => {
666 assertStrictEquals(isConstructor(Math
.ceil
), false);
669 it("[[Call]] returns false for getters", () => {
672 Object
.getOwnPropertyDescriptor({
682 it("[[Call]] returns false for setters", () => {
685 Object
.getOwnPropertyDescriptor({
695 it("[[Call]] returns false for null", () => {
696 assertStrictEquals(isConstructor(null), false);
699 it("[[Call]] returns false for objects", () => {
700 assertStrictEquals(isConstructor({}), false);
703 it("[[Construct]] throws an error", () => {
704 assertThrows(() => new isConstructor(function () {}));
707 describe(".length", () => {
708 it("[[Get]] returns the correct length", () => {
709 assertStrictEquals(isConstructor
.length
, 1);
713 describe(".name", () => {
714 it("[[Get]] returns the correct name", () => {
715 assertStrictEquals(isConstructor
.name
, "isConstructor");
720 describe("ordinaryHasInstance", () => {
721 it("[[Call]] walks the prototype chain", () => {
722 const constructor = class {
723 [Symbol
.hasInstance
]() {
730 new class extends constructor {}(),
736 it("[[Construct]] throws an error", () => {
737 assertThrows(() => new ordinaryHasInstance(function () {}, {}));
740 describe(".length", () => {
741 it("[[Get]] returns the correct length", () => {
742 assertStrictEquals(ordinaryHasInstance
.length
, 2);
746 describe(".name", () => {
747 it("[[Get]] returns the correct name", () => {
749 ordinaryHasInstance
.name
,
750 "ordinaryHasInstance",
This page took 0.164887 seconds and 3 git commands to generate.