]>
Lady’s Gitweb - Pisces/blob - function.test.js
35f7f37bc385dd538b82803e945449e20d9a90b7
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/>.
16 } from "./dev-deps.js";
27 } from "./function.js";
29 describe("bind", () => {
30 it("[[Call]] binds this", () => {
43 it("[[Call]] binds arguments", () => {
47 return [this, ...args
];
51 ).call("failure", "cmfwyp"),
52 ["etaoin", "shrdlu", "cmfwyp"],
56 it("[[Call]] works with any arraylike third argument", () => {
60 return [this, ...args
];
68 ).call("failure", "cmfwyp"),
69 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
73 it("[[Construct]] throws an error", () => {
74 assertThrows(() => new bind(function () {}));
77 describe(".name", () => {
78 it("[[Get]] returns the correct name", () => {
79 assertStrictEquals(bind
.name
, "bind");
84 describe("call", () => {
85 it("[[Call]] calls with the provided this value", () => {
98 it("[[Call]] calls with the provided arguments", () => {
102 return [this, ...args
];
105 ["shrdlu", "cmfwyp"],
107 ["etaoin", "shrdlu", "cmfwyp"],
111 it("[[Call]] works with any arraylike third argument", () => {
115 return [this, ...args
];
125 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
129 it("[[Construct]] throws an error", () => {
130 assertThrows(() => new call(function () {}, null, []));
133 describe(".name", () => {
134 it("[[Get]] returns the correct name", () => {
135 assertStrictEquals(call
.name
, "call");
140 describe("completesNormally", () => {
141 it("[[Call]] returns true for functions which complete normally", () => {
142 assertStrictEquals(completesNormally(() => {}), true);
145 it("[[Call]] returns false for functions which throw", () => {
147 completesNormally(() => {
154 it("[[Call]] throws when the argument is not callable", () => {
155 assertThrows(() => completesNormally(null));
158 it("[[Call]] throws when the argument is not provided", () => {
159 assertThrows(() => completesNormally());
162 it("[[Construct]] throws an error", () => {
163 assertThrows(() => new completesNormally(function () {}));
166 describe(".name", () => {
167 it("[[Get]] returns the correct name", () => {
168 assertStrictEquals(completesNormally
.name
, "completesNormally");
173 describe("construct", () => {
174 it("[[Call]] defaults to the constructor as the target", () => {
175 const constructor = class {};
177 Object
.getPrototypeOf(construct(
181 constructor.prototype,
185 it("[[Call]] constructs with the provided new target", () => {
186 const target = function () {};
199 it("[[Call]] constructs with the provided arguments", () => {
203 return [new.target
.value
, ...args
];
205 ["shrdlu", "cmfwyp"],
206 Object
.assign(function () {}, { value
: "etaoin" }),
208 ["etaoin", "shrdlu", "cmfwyp"],
212 it("[[Call]] works with any arraylike third argument", () => {
216 return [new.target
.value
, ...args
];
224 Object
.assign(function () {}, { value
: "etaoin" }),
226 ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
230 it("[[Construct]] throws an error", () => {
231 assertThrows(() => new construct(function () {}, []));
234 describe(".name", () => {
235 it("[[Get]] returns the correct name", () => {
236 assertStrictEquals(construct
.name
, "construct");
241 describe("identity", () => {
242 it("[[Call]] returns what it is given", () => {
244 assertStrictEquals(identity(value
), value
);
247 it("[[Construct]] is constructable", () => {
249 assertStrictEquals(new identity(value
), value
);
252 it("[[Construct]] is subclassable", () => {
254 assertStrictEquals(new class extends identity
{}(value
), value
);
257 describe(".name", () => {
258 it("[[Get]] returns the correct name", () => {
259 assertStrictEquals(identity
.name
, "identity");
264 describe("isCallable", () => {
265 it("[[Call]] returns true for ordinary functions", () => {
266 assertStrictEquals(isCallable(function () {}), true);
269 it("[[Call]] returns true for arrow functions", () => {
270 assertStrictEquals(isCallable(() => {}), true);
273 it("[[Call]] returns true for generator functions", () => {
274 assertStrictEquals(isCallable(function* () {}), true);
277 it("[[Call]] returns true for classes", () => {
278 assertStrictEquals(isCallable(class {}), true);
281 it("[[Call]] returns true for builtin functions", () => {
282 assertStrictEquals(isCallable(Math
.ceil
), true);
285 it("[[Call]] returns true for getters", () => {
288 Object
.getOwnPropertyDescriptor({
298 it("[[Call]] returns true for setters", () => {
301 Object
.getOwnPropertyDescriptor({
311 it("[[Call]] returns false for null", () => {
312 assertStrictEquals(isCallable(null), false);
315 it("[[Call]] returns false for objects", () => {
316 assertStrictEquals(isCallable({}), false);
319 it("[[Construct]] throws an error", () => {
320 assertThrows(() => new isCallable(function () {}));
323 describe(".name", () => {
324 it("[[Get]] returns the correct name", () => {
325 assertStrictEquals(isCallable
.name
, "isCallable");
330 describe("isConstructor", () => {
331 it("[[Call]] returns true for ordinary functions", () => {
332 assertStrictEquals(isConstructor(function () {}), true);
335 it("[[Call]] returns false for arrow functions", () => {
336 assertStrictEquals(isConstructor(() => {}), false);
339 it("[[Call]] returns false for generator functions", () => {
340 assertStrictEquals(isConstructor(function* () {}), false);
343 it("[[Call]] returns true for classes", () => {
344 assertStrictEquals(isConstructor(class {}), true);
347 it("[[Call]] returns false for builtin functions", () => {
348 assertStrictEquals(isConstructor(Math
.ceil
), false);
351 it("[[Call]] returns false for getters", () => {
354 Object
.getOwnPropertyDescriptor({
364 it("[[Call]] returns false for setters", () => {
367 Object
.getOwnPropertyDescriptor({
377 it("[[Call]] returns false for null", () => {
378 assertStrictEquals(isConstructor(null), false);
381 it("[[Call]] returns false for objects", () => {
382 assertStrictEquals(isConstructor({}), false);
385 it("[[Construct]] throws an error", () => {
386 assertThrows(() => new isConstructor(function () {}));
389 describe(".name", () => {
390 it("[[Get]] returns the correct name", () => {
391 assertStrictEquals(isConstructor
.name
, "isConstructor");
396 describe("makeCallable", () => {
397 it("[[Call]] transfers the first argument to this", () => {
403 ).call("fail", "pass"),
408 it("[[Call]] transfers the remaining arguments", () => {
412 return [this, ...args
];
414 ).call("failure", "etaoin", "shrdlu", "cmfwyp"),
415 ["etaoin", "shrdlu", "cmfwyp"],
419 it("[[Construct]] throws an error", () => {
420 assertThrows(() => new makeCallable(function () {}));
423 describe(".name", () => {
424 it("[[Get]] returns the correct name", () => {
425 assertStrictEquals(makeCallable
.name
, "makeCallable");
430 describe("ordinaryHasInstance", () => {
431 it("[[Call]] walks the prototype chain", () => {
432 const constructor = class {
433 [Symbol
.hasInstance
]() {
440 new class extends constructor {}(),
446 it("[[Construct]] throws an error", () => {
447 assertThrows(() => new ordinaryHasInstance(function () {}, {}));
450 describe(".name", () => {
451 it("[[Get]] returns the correct name", () => {
453 ordinaryHasInstance
.name
,
454 "ordinaryHasInstance",
This page took 0.075779 seconds and 3 git commands to generate.