]>
Lady’s Gitweb - Pisces/blob - function.test.js
2260b5a216ab666c818e7a89f9ce3e2e8960db7d
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";
27 makeIllegalConstructor
,
29 } from "./function.js";
31 describe("bind", () => {
32 it("[[Call]] binds this", () => {
45 it("[[Call]] binds arguments", () => {
49 return [this, ...args
];
53 ).call("failure", "cmfwyp"),
54 ["etaoin", "shrdlu", "cmfwyp"],
58 it("[[Call]] works with any arraylike third argument", () => {
62 return [this, ...args
];
70 ).call("failure", "cmfwyp"),
71 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
75 it("[[Construct]] throws an error", () => {
76 assertThrows(() => new bind(function () {}));
79 describe(".name", () => {
80 it("[[Get]] returns the correct name", () => {
81 assertStrictEquals(bind
.name
, "bind");
86 describe("call", () => {
87 it("[[Call]] calls with the provided this value", () => {
100 it("[[Call]] calls with the provided arguments", () => {
104 return [this, ...args
];
107 ["shrdlu", "cmfwyp"],
109 ["etaoin", "shrdlu", "cmfwyp"],
113 it("[[Call]] works with any arraylike third argument", () => {
117 return [this, ...args
];
127 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
131 it("[[Construct]] throws an error", () => {
132 assertThrows(() => new call(function () {}, null, []));
135 describe(".name", () => {
136 it("[[Get]] returns the correct name", () => {
137 assertStrictEquals(call
.name
, "call");
142 describe("completesNormally", () => {
143 it("[[Call]] returns true for functions which complete normally", () => {
144 assertStrictEquals(completesNormally(() => {}), true);
147 it("[[Call]] returns false for functions which throw", () => {
149 completesNormally(() => {
156 it("[[Call]] throws when the argument is not callable", () => {
157 assertThrows(() => completesNormally(null));
160 it("[[Call]] throws when the argument is not provided", () => {
161 assertThrows(() => completesNormally());
164 it("[[Construct]] throws an error", () => {
165 assertThrows(() => new completesNormally(function () {}));
168 describe(".name", () => {
169 it("[[Get]] returns the correct name", () => {
170 assertStrictEquals(completesNormally
.name
, "completesNormally");
175 describe("construct", () => {
176 it("[[Call]] defaults to the constructor as the target", () => {
177 const constructor = class {};
179 Object
.getPrototypeOf(construct(
183 constructor.prototype,
187 it("[[Call]] constructs with the provided new target", () => {
188 const target = function () {};
201 it("[[Call]] constructs with the provided arguments", () => {
205 return [new.target
.value
, ...args
];
207 ["shrdlu", "cmfwyp"],
208 Object
.assign(function () {}, { value
: "etaoin" }),
210 ["etaoin", "shrdlu", "cmfwyp"],
214 it("[[Call]] works with any arraylike third argument", () => {
218 return [new.target
.value
, ...args
];
226 Object
.assign(function () {}, { value
: "etaoin" }),
228 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
232 it("[[Construct]] throws an error", () => {
233 assertThrows(() => new construct(function () {}, []));
236 describe(".name", () => {
237 it("[[Get]] returns the correct name", () => {
238 assertStrictEquals(construct
.name
, "construct");
243 describe("identity", () => {
244 it("[[Call]] returns what it is given", () => {
246 assertStrictEquals(identity(value
), value
);
249 it("[[Construct]] is constructable", () => {
251 assertStrictEquals(new identity(value
), value
);
254 it("[[Construct]] is subclassable", () => {
256 assertStrictEquals(new class extends identity
{}(value
), value
);
259 describe(".name", () => {
260 it("[[Get]] returns the correct name", () => {
261 assertStrictEquals(identity
.name
, "identity");
266 describe("isCallable", () => {
267 it("[[Call]] returns true for ordinary functions", () => {
268 assertStrictEquals(isCallable(function () {}), true);
271 it("[[Call]] returns true for arrow functions", () => {
272 assertStrictEquals(isCallable(() => {}), true);
275 it("[[Call]] returns true for generator functions", () => {
276 assertStrictEquals(isCallable(function* () {}), true);
279 it("[[Call]] returns true for classes", () => {
280 assertStrictEquals(isCallable(class {}), true);
283 it("[[Call]] returns true for builtin functions", () => {
284 assertStrictEquals(isCallable(Math
.ceil
), true);
287 it("[[Call]] returns true for getters", () => {
290 Object
.getOwnPropertyDescriptor({
300 it("[[Call]] returns true for setters", () => {
303 Object
.getOwnPropertyDescriptor({
313 it("[[Call]] returns false for null", () => {
314 assertStrictEquals(isCallable(null), false);
317 it("[[Call]] returns false for objects", () => {
318 assertStrictEquals(isCallable({}), false);
321 it("[[Construct]] throws an error", () => {
322 assertThrows(() => new isCallable(function () {}));
325 describe(".name", () => {
326 it("[[Get]] returns the correct name", () => {
327 assertStrictEquals(isCallable
.name
, "isCallable");
332 describe("isConstructor", () => {
333 it("[[Call]] returns true for ordinary functions", () => {
334 assertStrictEquals(isConstructor(function () {}), true);
337 it("[[Call]] returns false for arrow functions", () => {
338 assertStrictEquals(isConstructor(() => {}), false);
341 it("[[Call]] returns false for generator functions", () => {
342 assertStrictEquals(isConstructor(function* () {}), false);
345 it("[[Call]] returns true for classes", () => {
346 assertStrictEquals(isConstructor(class {}), true);
349 it("[[Call]] returns false for builtin functions", () => {
350 assertStrictEquals(isConstructor(Math
.ceil
), false);
353 it("[[Call]] returns false for getters", () => {
356 Object
.getOwnPropertyDescriptor({
366 it("[[Call]] returns false for setters", () => {
369 Object
.getOwnPropertyDescriptor({
379 it("[[Call]] returns false for null", () => {
380 assertStrictEquals(isConstructor(null), false);
383 it("[[Call]] returns false for objects", () => {
384 assertStrictEquals(isConstructor({}), false);
387 it("[[Construct]] throws an error", () => {
388 assertThrows(() => new isConstructor(function () {}));
391 describe(".name", () => {
392 it("[[Get]] returns the correct name", () => {
393 assertStrictEquals(isConstructor
.name
, "isConstructor");
398 describe("makeCallable", () => {
399 it("[[Call]] transfers the first argument to this", () => {
405 ).call("fail", "pass"),
410 it("[[Call]] transfers the remaining arguments", () => {
414 return [this, ...args
];
416 ).call("failure", "etaoin", "shrdlu", "cmfwyp"),
417 ["etaoin", "shrdlu", "cmfwyp"],
421 it("[[Construct]] throws an error", () => {
422 assertThrows(() => new makeCallable(function () {}));
425 describe(".name", () => {
426 it("[[Get]] returns the correct name", () => {
427 assertStrictEquals(makeCallable
.name
, "makeCallable");
432 describe("makeIllegalConstructor", () => {
433 it("[[Call]] returns a constructor", () => {
434 assert(isConstructor(makeIllegalConstructor()));
437 it("[[Call]] works as expected when provided with no arguments", () => {
438 const constructor = makeIllegalConstructor();
440 Object
.getPrototypeOf(constructor),
444 Object
.getPrototypeOf(constructor.prototype),
447 assertEquals(constructor.prototype, {});
449 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
452 assertStrictEquals(constructor.name
, "");
455 it("[[Call]] returns a correctly‐formed constructor when provided one argument", () => {
456 const constructorPrototype
= Object
.create(null, {
457 name
: { value
: "etaoin" },
458 prototype: { value
: {} },
460 const constructor = makeIllegalConstructor(
461 Object
.create(constructorPrototype
),
463 assert(isConstructor(constructor));
465 Object
.getPrototypeOf(constructor),
466 constructorPrototype
,
468 assert(Object
.hasOwn(constructor, "prototype"));
470 constructor.prototype,
471 constructorPrototype
.prototype,
474 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
477 assertStrictEquals(constructor.name
, "etaoin");
480 it("[[Call]] allows the second argument to override the prototype of the constructor", () => {
481 const constructorPrototype
= Object
.create(null, {
482 name
: { value
: "etaoin" },
483 prototype: { value
: {} },
485 const expectedPrototype
= Object
.create(null, {
486 name
: { value
: "shrdlu" },
487 prototype: { value
: {} },
489 const constructor = makeIllegalConstructor(
490 Object
.create(constructorPrototype
),
493 assert(isConstructor(constructor));
495 Object
.getPrototypeOf(constructor),
498 assert(Object
.hasOwn(constructor, "prototype"));
500 constructor.prototype,
501 constructorPrototype
.prototype,
504 !Object
.getOwnPropertyDescriptor(constructor, "prototype")
507 assertStrictEquals(constructor.name
, "etaoin");
510 it("[[Construct]] throws an error", () => {
511 assertThrows(() => new makeIllegalConstructor(function () {}));
514 describe("~", () => {
515 it("[[Call]] throws an error", () => {
517 makeIllegalConstructor(function () {})();
521 it("[[Construct]] throws an error", () => {
523 makeIllegalConstructor(function () {})();
528 describe(".name", () => {
529 it("[[Get]] returns the correct name", () => {
531 makeIllegalConstructor
.name
,
532 "makeIllegalConstructor",
538 describe("ordinaryHasInstance", () => {
539 it("[[Call]] walks the prototype chain", () => {
540 const constructor = class {
541 [Symbol
.hasInstance
]() {
548 new class extends constructor {}(),
554 it("[[Construct]] throws an error", () => {
555 assertThrows(() => new ordinaryHasInstance(function () {}, {}));
558 describe(".name", () => {
559 it("[[Get]] returns the correct name", () => {
561 ordinaryHasInstance
.name
,
562 "ordinaryHasInstance",
This page took 0.083061 seconds and 3 git commands to generate.