]> Lady’s Gitweb - Pisces/blob - value.test.js
Test value.js non·constructor constructability
[Pisces] / value.test.js
1 // ♓🌟 Piscēs ∷ value.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022–2023 Lady [@ Lady’s Computer].
5 //
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/>.
9
10 import {
11 assertStrictEquals,
12 assertThrows,
13 describe,
14 it,
15 } from "./dev-deps.js";
16 import {
17 ASYNC_ITERATOR,
18 HAS_INSTANCE,
19 IS_CONCAT_SPREADABLE,
20 ITERATOR,
21 MATCH,
22 MATCH_ALL,
23 NULL,
24 ordinaryToPrimitive,
25 REPLACE,
26 sameValue,
27 sameValueZero,
28 SPECIES,
29 SPLIT,
30 TO_PRIMITIVE,
31 TO_STRING_TAG,
32 toPrimitive,
33 type,
34 UNDEFINED,
35 UNSCOPABLES,
36 } from "./value.js";
37
38 describe("ASYNC_ITERATOR", () => {
39 it("[[Get]] is @@asyncIterator", () => {
40 assertStrictEquals(ASYNC_ITERATOR, Symbol.asyncIterator);
41 });
42 });
43
44 describe("HAS_INSTANCE", () => {
45 it("[[Get]] is @@hasInstance", () => {
46 assertStrictEquals(HAS_INSTANCE, Symbol.hasInstance);
47 });
48 });
49
50 describe("IS_CONCAT_SPREADABLE", () => {
51 it("[[Get]] is @@isConcatSpreadable", () => {
52 assertStrictEquals(
53 IS_CONCAT_SPREADABLE,
54 Symbol.isConcatSpreadable,
55 );
56 });
57 });
58
59 describe("ITERATOR", () => {
60 it("[[Get]] is @@iterator", () => {
61 assertStrictEquals(ITERATOR, Symbol.iterator);
62 });
63 });
64
65 describe("MATCH", () => {
66 it("[[Get]] is @@match", () => {
67 assertStrictEquals(MATCH, Symbol.match);
68 });
69 });
70
71 describe("MATCH_ALL", () => {
72 it("[[Get]] is @@matchAll", () => {
73 assertStrictEquals(MATCH_ALL, Symbol.matchAll);
74 });
75 });
76
77 describe("NULL", () => {
78 it("[[Get]] is null", () => {
79 assertStrictEquals(NULL, null);
80 });
81 });
82
83 describe("REPLACE", () => {
84 it("[[Get]] is @@replace", () => {
85 assertStrictEquals(REPLACE, Symbol.replace);
86 });
87 });
88
89 describe("SPECIES", () => {
90 it("[[Get]] is @@species", () => {
91 assertStrictEquals(SPECIES, Symbol.species);
92 });
93 });
94
95 describe("SPLIT", () => {
96 it("[[Get]] is @@split", () => {
97 assertStrictEquals(SPLIT, Symbol.split);
98 });
99 });
100
101 describe("TO_PRIMITIVE", () => {
102 it("[[Get]] is @@toPrimitive", () => {
103 assertStrictEquals(TO_PRIMITIVE, Symbol.toPrimitive);
104 });
105 });
106
107 describe("TO_STRING_TAG", () => {
108 it("[[Get]] is @@toStringTag", () => {
109 assertStrictEquals(TO_STRING_TAG, Symbol.toStringTag);
110 });
111 });
112
113 describe("UNDEFINED", () => {
114 it("[[Get]] is undefined", () => {
115 assertStrictEquals(UNDEFINED, void {});
116 });
117 });
118
119 describe("UNSCOPABLES", () => {
120 it("[[Get]] is @@unscopables", () => {
121 assertStrictEquals(UNSCOPABLES, Symbol.unscopables);
122 });
123 });
124
125 describe("ordinaryToPrimitive", () => {
126 it("[[Call]] prefers `valueOf` by default", () => {
127 const obj = {
128 toString() {
129 return "failure";
130 },
131 valueOf() {
132 return "success";
133 },
134 };
135 assertStrictEquals(ordinaryToPrimitive(obj), "success");
136 assertStrictEquals(ordinaryToPrimitive(obj, "default"), "success");
137 });
138
139 it('[[Call]] prefers `valueOf` for a "number" hint', () => {
140 const obj = {
141 toString() {
142 return "failure";
143 },
144 valueOf() {
145 return "success";
146 },
147 };
148 assertStrictEquals(ordinaryToPrimitive(obj, "number"), "success");
149 });
150
151 it('[[Call]] prefers `toString` for a "string" hint', () => {
152 const obj = {
153 toString() {
154 return "success";
155 },
156 valueOf() {
157 return "failure";
158 },
159 };
160 assertStrictEquals(ordinaryToPrimitive(obj, "string"), "success");
161 });
162
163 it("[[Call]] falls back to the other method if the first isn’t callable", () => {
164 const obj = {
165 toString() {
166 return "success";
167 },
168 valueOf: "failure",
169 };
170 assertStrictEquals(ordinaryToPrimitive(obj), "success");
171 });
172
173 it("[[Call]] falls back to the other method if the first returns an object", () => {
174 const obj = {
175 toString() {
176 return "success";
177 },
178 valueOf() {
179 return new String("failure");
180 },
181 };
182 assertStrictEquals(ordinaryToPrimitive(obj), "success");
183 });
184
185 it("[[Call]] throws an error if neither method is callable", () => {
186 const obj = {
187 toString: "failure",
188 valueOf: "failure",
189 };
190 assertThrows(() => ordinaryToPrimitive(obj));
191 });
192
193 it("[[Call]] throws an error if neither method returns an object", () => {
194 const obj = {
195 toString() {
196 return new String("failure");
197 },
198 valueOf() {
199 return new String("failure");
200 },
201 };
202 assertThrows(() => ordinaryToPrimitive(obj));
203 });
204
205 it("[[Construct]] throws an error", () => {
206 assertThrows(() => new ordinaryToPrimitive(""));
207 });
208
209 describe(".name", () => {
210 it("[[Get]] returns the correct name", () => {
211 assertStrictEquals(
212 ordinaryToPrimitive.name,
213 "ordinaryToPrimitive",
214 );
215 });
216 });
217 });
218
219 describe("sameValue", () => {
220 it("[[Call]] returns false for null 🆚 undefined", () => {
221 assertStrictEquals(sameValue(null, undefined), false);
222 });
223
224 it("[[Call]] returns false for null 🆚 an object", () => {
225 assertStrictEquals(sameValue(null, {}), false);
226 });
227
228 it("[[Call]] returns true for null 🆚 null", () => {
229 assertStrictEquals(sameValue(null, null), true);
230 });
231
232 it("[[Call]] returns false for two different objects", () => {
233 assertStrictEquals(sameValue({}, {}), false);
234 });
235
236 it("[[Call]] returns true for the same object", () => {
237 const obj = {};
238 assertStrictEquals(sameValue(obj, obj), true);
239 });
240
241 it("[[Call]] returns false for ±0", () => {
242 assertStrictEquals(sameValue(0, -0), false);
243 });
244
245 it("[[Call]] returns true for -0", () => {
246 assertStrictEquals(sameValue(-0, -0), true);
247 });
248
249 it("[[Call]] returns true for nan", () => {
250 assertStrictEquals(sameValue(0 / 0, 0 / 0), true);
251 });
252
253 it("[[Call]] returns false for a primitive and its wrapped object", () => {
254 assertStrictEquals(sameValue(false, new Boolean(false)), false);
255 });
256
257 it("[[Construct]] throws an error", () => {
258 assertThrows(() => new sameValue(true, true));
259 });
260
261 describe(".name", () => {
262 it("[[Get]] returns the correct name", () => {
263 assertStrictEquals(sameValue.name, "sameValue");
264 });
265 });
266 });
267
268 describe("sameValueZero", () => {
269 it("[[Call]] returns false for null 🆚 undefined", () => {
270 assertStrictEquals(sameValueZero(null, undefined), false);
271 });
272
273 it("[[Call]] returns false for null 🆚 an object", () => {
274 assertStrictEquals(sameValueZero(null, {}), false);
275 });
276
277 it("[[Call]] returns true for null 🆚 null", () => {
278 assertStrictEquals(sameValueZero(null, null), true);
279 });
280
281 it("[[Call]] returns false for two different objects", () => {
282 assertStrictEquals(sameValueZero({}, {}), false);
283 });
284
285 it("[[Call]] returns true for the same object", () => {
286 const obj = {};
287 assertStrictEquals(sameValueZero(obj, obj), true);
288 });
289
290 it("[[Call]] returns true for ±0", () => {
291 assertStrictEquals(sameValueZero(0, -0), true);
292 });
293
294 it("[[Call]] returns true for -0", () => {
295 assertStrictEquals(sameValueZero(-0, -0), true);
296 });
297
298 it("[[Call]] returns true for nan", () => {
299 assertStrictEquals(sameValueZero(0 / 0, 0 / 0), true);
300 });
301
302 it("[[Call]] returns false for a primitive and its wrapped object", () => {
303 assertStrictEquals(
304 sameValueZero(false, new Boolean(false)),
305 false,
306 );
307 });
308
309 it("[[Construct]] throws an error", () => {
310 assertThrows(() => new sameValueZero(true, true));
311 });
312
313 describe(".name", () => {
314 it("[[Get]] returns the correct name", () => {
315 assertStrictEquals(sameValueZero.name, "sameValueZero");
316 });
317 });
318 });
319
320 describe("toPrimitive", () => {
321 it("[[Call]] returns the argument when passed a primitive", () => {
322 const value = Symbol();
323 assertStrictEquals(toPrimitive(value), value);
324 });
325
326 it("[[Call]] works with nullish values", () => {
327 assertStrictEquals(toPrimitive(null), null);
328 assertStrictEquals(toPrimitive(), void {});
329 });
330
331 it("[[Call]] calls ordinaryToPrimitive by default", () => {
332 const value = Object.assign(
333 Object.create(null),
334 {
335 valueOf() {
336 return "success";
337 },
338 },
339 );
340 assertStrictEquals(toPrimitive(value), "success");
341 });
342
343 it("[[Call]] accepts a hint", () => {
344 const value = Object.assign(
345 Object.create(null),
346 {
347 toString() {
348 return "success";
349 },
350 valueOf() {
351 return "failure";
352 },
353 },
354 );
355 assertStrictEquals(toPrimitive(value, "string"), "success");
356 });
357
358 it("[[Call]] uses the exotic toPrimitive method if available", () => {
359 const value = Object.assign(
360 Object.create(null),
361 {
362 [Symbol.toPrimitive]() {
363 return "success";
364 },
365 },
366 );
367 assertStrictEquals(toPrimitive(value), "success");
368 });
369
370 it("[[Call]] passes the hint to the exotic toPrimitive", () => {
371 const value = Object.assign(
372 Object.create(null),
373 {
374 [Symbol.toPrimitive](hint) {
375 return hint === "string" ? "success" : "failure";
376 },
377 },
378 );
379 assertStrictEquals(toPrimitive(value, "string"), "success");
380 });
381
382 it('[[Call]] passes a "default" hint by default', () => {
383 const value = Object.assign(
384 Object.create(null),
385 {
386 [Symbol.toPrimitive](hint) {
387 return hint === "default" ? "success" : "failure";
388 },
389 },
390 );
391 assertStrictEquals(toPrimitive(value), "success");
392 });
393
394 it("[[Call]] throws for an invalid hint", () => {
395 const value1 = Object.assign(
396 Object.create(null),
397 {
398 [Symbol.toPrimitive]() {
399 return "success";
400 },
401 },
402 );
403 const value2 = Object.assign(
404 Object.create(null),
405 {
406 valueOf() {
407 return true;
408 },
409 },
410 );
411 assertThrows(() => toPrimitive(value1, "badhint"));
412 assertThrows(() => toPrimitive(value2, "badhint"));
413 assertThrows(() => toPrimitive(true, "badhint"));
414 });
415
416 it("[[Construct]] throws an error", () => {
417 assertThrows(() => new toPrimitive(true));
418 });
419
420 describe(".name", () => {
421 it("[[Get]] returns the correct name", () => {
422 assertStrictEquals(toPrimitive.name, "toPrimitive");
423 });
424 });
425 });
426
427 describe("type", () => {
428 it('[[Call]] returns "null" for null', () => {
429 assertStrictEquals(type(null), "null");
430 });
431
432 it('[[Call]] returns "undefined" for undefined', () => {
433 assertStrictEquals(type(void {}), "undefined");
434 });
435
436 it('[[Call]] returns "object" for non‐callable objects', () => {
437 assertStrictEquals(type(Object.create(null)), "object");
438 });
439
440 it('[[Call]] returns "object" for callable objects', () => {
441 assertStrictEquals(type(() => {}), "object");
442 });
443
444 it('[[Call]] returns "object" for constructable objects', () => {
445 assertStrictEquals(type(class {}), "object");
446 });
447
448 it("[[Construct]] throws an error", () => {
449 assertThrows(() => new type({}));
450 });
451
452 describe(".name", () => {
453 it("[[Get]] returns the correct name", () => {
454 assertStrictEquals(type.name, "type");
455 });
456 });
457 });
This page took 0.145668 seconds and 5 git commands to generate.