]>
Lady’s Gitweb - Pisces/blob - function.test.js
75ba30008df964ac424e37518f40960576abcf1a
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";
23 createCallableFunction
,
24 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(".name", () => {
81 it("[[Get]] returns the correct name", () => {
82 assertStrictEquals(bind
.name
, "bind");
87 describe("call", () => {
88 it("[[Call]] calls with the provided this value", () => {
101 it("[[Call]] calls with the provided arguments", () => {
105 return [this, ...args
];
108 ["shrdlu", "cmfwyp"],
110 ["etaoin", "shrdlu", "cmfwyp"],
114 it("[[Call]] works with any arraylike third argument", () => {
118 return [this, ...args
];
128 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
132 it("[[Construct]] throws an error", () => {
133 assertThrows(() => new call(function () {}, null, []));
136 describe(".name", () => {
137 it("[[Get]] returns the correct name", () => {
138 assertStrictEquals(call
.name
, "call");
143 describe("completesNormally", () => {
144 it("[[Call]] returns true for functions which complete normally", () => {
145 assertStrictEquals(completesNormally(() => {}), true);
148 it("[[Call]] returns false for functions which throw", () => {
150 completesNormally(() => {
157 it("[[Call]] throws when the argument is not callable", () => {
158 assertThrows(() => completesNormally(null));
161 it("[[Call]] throws when the argument is not provided", () => {
162 assertThrows(() => completesNormally());
165 it("[[Construct]] throws an error", () => {
166 assertThrows(() => new completesNormally(function () {}));
169 describe(".name", () => {
170 it("[[Get]] returns the correct name", () => {
171 assertStrictEquals(completesNormally
.name
, "completesNormally");
176 describe("construct", () => {
177 it("[[Call]] defaults to the constructor as the target", () => {
178 const constructor = class {};
180 Object
.getPrototypeOf(construct(
184 constructor.prototype,
188 it("[[Call]] constructs with the provided new target", () => {
189 const target = function () {};
202 it("[[Call]] constructs with the provided arguments", () => {
206 return [new.target
.value
, ...args
];
208 ["shrdlu", "cmfwyp"],
209 Object
.assign(function () {}, { value
: "etaoin" }),
211 ["etaoin", "shrdlu", "cmfwyp"],
215 it("[[Call]] works with any arraylike third argument", () => {
219 return [new.target
.value
, ...args
];
227 Object
.assign(function () {}, { value
: "etaoin" }),
229 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
233 it("[[Construct]] throws an error", () => {
234 assertThrows(() => new construct(function () {}, []));
237 describe(".name", () => {
238 it("[[Get]] returns the correct name", () => {
239 assertStrictEquals(construct
.name
, "construct");
244 describe("createCallableFunction", () => {
245 it("[[Call]] transfers the first argument to this", () => {
247 createCallableFunction(
251 ).call("fail", "pass"),
256 it("[[Call]] transfers the remaining arguments", () => {
258 createCallableFunction(
260 return [this, ...args
];
262 ).call("failure", "etaoin", "shrdlu", "cmfwyp"),
263 ["etaoin", "shrdlu", "cmfwyp"],
267 it("[[Construct]] throws an error", () => {
268 assertThrows(() => new createCallableFunction(function () {}));
271 describe(".name", () => {
272 it("[[Get]] returns the correct name", () => {
274 createCallableFunction
.name
,
275 "createCallableFunction",
281 describe("createIllegalConstructor", () => {
282 it("[[Call]] returns a constructor", () => {
283 assert(isConstructor(createIllegalConstructor()));
286 it("[[Call]] works as expected when provided with no arguments", () => {
287 const constructor = createIllegalConstructor();
289 Object
.getPrototypeOf(constructor),
293 Object
.getPrototypeOf(constructor.prototype),
296 assertEquals(constructor.prototype, {});
298 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
301 assertStrictEquals(constructor.name
, "");
304 it("[[Call]] returns a correctly‐formed constructor when provided one argument", () => {
305 const constructorPrototype
= Object
.create(null, {
306 name
: { value
: "etaoin" },
307 prototype: { value
: {} },
309 const constructor = createIllegalConstructor(
310 Object
.create(constructorPrototype
),
312 assert(isConstructor(constructor));
314 Object
.getPrototypeOf(constructor),
315 constructorPrototype
,
317 assert(Object
.hasOwn(constructor, "prototype"));
319 constructor.prototype,
320 constructorPrototype
.prototype,
323 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
326 assertStrictEquals(constructor.name
, "etaoin");
329 it("[[Call]] allows the second argument to override the prototype of the constructor", () => {
330 const constructorPrototype
= Object
.create(null, {
331 name
: { value
: "etaoin" },
332 prototype: { value
: {} },
334 const expectedPrototype
= Object
.create(null, {
335 name
: { value
: "shrdlu" },
336 prototype: { value
: {} },
338 const constructor = createIllegalConstructor(
339 Object
.create(constructorPrototype
),
342 assert(isConstructor(constructor));
344 Object
.getPrototypeOf(constructor),
347 assert(Object
.hasOwn(constructor, "prototype"));
349 constructor.prototype,
350 constructorPrototype
.prototype,
353 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
356 assertStrictEquals(constructor.name
, "etaoin");
359 it("[[Construct]] throws an error", () => {
360 assertThrows(() => new createIllegalConstructor(function () {}));
363 describe("~", () => {
364 it("[[Call]] throws an error", () => {
366 createIllegalConstructor(function () {})();
370 it("[[Construct]] throws an error", () => {
372 createIllegalConstructor(function () {})();
377 describe(".name", () => {
378 it("[[Get]] returns the correct name", () => {
380 createIllegalConstructor
.name
,
381 "createIllegalConstructor",
387 describe("identity", () => {
388 it("[[Call]] returns what it is given", () => {
390 assertStrictEquals(identity(value
), value
);
393 it("[[Construct]] is constructable", () => {
395 assertStrictEquals(new identity(value
), value
);
398 it("[[Construct]] is subclassable", () => {
400 assertStrictEquals(new class extends identity
{}(value
), value
);
403 describe(".name", () => {
404 it("[[Get]] returns the correct name", () => {
405 assertStrictEquals(identity
.name
, "identity");
410 describe("isCallable", () => {
411 it("[[Call]] returns true for ordinary functions", () => {
412 assertStrictEquals(isCallable(function () {}), true);
415 it("[[Call]] returns true for arrow functions", () => {
416 assertStrictEquals(isCallable(() => {}), true);
419 it("[[Call]] returns true for generator functions", () => {
420 assertStrictEquals(isCallable(function* () {}), true);
423 it("[[Call]] returns true for classes", () => {
424 assertStrictEquals(isCallable(class {}), true);
427 it("[[Call]] returns true for builtin functions", () => {
428 assertStrictEquals(isCallable(Math
.ceil
), true);
431 it("[[Call]] returns true for getters", () => {
434 Object
.getOwnPropertyDescriptor({
444 it("[[Call]] returns true for setters", () => {
447 Object
.getOwnPropertyDescriptor({
457 it("[[Call]] returns false for null", () => {
458 assertStrictEquals(isCallable(null), false);
461 it("[[Call]] returns false for objects", () => {
462 assertStrictEquals(isCallable({}), false);
465 it("[[Construct]] throws an error", () => {
466 assertThrows(() => new isCallable(function () {}));
469 describe(".name", () => {
470 it("[[Get]] returns the correct name", () => {
471 assertStrictEquals(isCallable
.name
, "isCallable");
476 describe("isConstructor", () => {
477 it("[[Call]] returns true for ordinary functions", () => {
478 assertStrictEquals(isConstructor(function () {}), true);
481 it("[[Call]] returns false for arrow functions", () => {
482 assertStrictEquals(isConstructor(() => {}), false);
485 it("[[Call]] returns false for generator functions", () => {
486 assertStrictEquals(isConstructor(function* () {}), false);
489 it("[[Call]] returns true for classes", () => {
490 assertStrictEquals(isConstructor(class {}), true);
493 it("[[Call]] returns false for builtin functions", () => {
494 assertStrictEquals(isConstructor(Math
.ceil
), false);
497 it("[[Call]] returns false for getters", () => {
500 Object
.getOwnPropertyDescriptor({
510 it("[[Call]] returns false for setters", () => {
513 Object
.getOwnPropertyDescriptor({
523 it("[[Call]] returns false for null", () => {
524 assertStrictEquals(isConstructor(null), false);
527 it("[[Call]] returns false for objects", () => {
528 assertStrictEquals(isConstructor({}), false);
531 it("[[Construct]] throws an error", () => {
532 assertThrows(() => new isConstructor(function () {}));
535 describe(".name", () => {
536 it("[[Get]] returns the correct name", () => {
537 assertStrictEquals(isConstructor
.name
, "isConstructor");
542 describe("ordinaryHasInstance", () => {
543 it("[[Call]] walks the prototype chain", () => {
544 const constructor = class {
545 [Symbol
.hasInstance
]() {
552 new class extends constructor {}(),
558 it("[[Construct]] throws an error", () => {
559 assertThrows(() => new ordinaryHasInstance(function () {}, {}));
562 describe(".name", () => {
563 it("[[Get]] returns the correct name", () => {
565 ordinaryHasInstance
.name
,
566 "ordinaryHasInstance",
572 describe("toFunctionName", () => {
573 it("[[Call]] works with strings and no prefix", () => {
574 assertStrictEquals(toFunctionName("etaoin"), "etaoin");
577 it("[[Call]] works with objects and no prefix", () => {
588 it("[[Call]] works with descriptionless symbols and no prefix", () => {
589 assertStrictEquals(toFunctionName(Symbol()), "");
592 it("[[Call]] works with empty description symbols and no prefix", () => {
593 assertStrictEquals(toFunctionName(Symbol("")), "[]");
596 it("[[Call]] works with described symbols and no prefix", () => {
597 assertStrictEquals(toFunctionName(Symbol("etaoin")), "[etaoin]");
600 it("[[Call]] works with strings and a prefix", () => {
601 assertStrictEquals(toFunctionName("etaoin", "foo"), "foo etaoin");
604 it("[[Call]] works with objects and no prefix", () => {
615 it("[[Call]] works with descriptionless symbols and no prefix", () => {
616 assertStrictEquals(toFunctionName(Symbol(), "foo"), "foo ");
619 it("[[Call]] works with empty description symbols and no prefix", () => {
620 assertStrictEquals(toFunctionName(Symbol(""), "foo"), "foo []");
623 it("[[Call]] works with described symbols and no prefix", () => {
625 toFunctionName(Symbol("etaoin"), "foo"),
630 it("[[Construct]] throws an error", () => {
631 assertThrows(() => new toFunctionName(""));
634 describe(".name", () => {
635 it("[[Get]] returns the correct name", () => {
This page took 0.097253 seconds and 3 git commands to generate.