]> Lady’s Gitweb - Pisces/blob - collection.test.js
Add methods for own entries and values to object.js
[Pisces] / collection.test.js
1 // ♓🌟 Piscēs ∷ collection.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022 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 assertEquals,
12 assertSpyCall,
13 assertSpyCalls,
14 assertStrictEquals,
15 describe,
16 it,
17 spy,
18 } from "./dev-deps.js";
19 import { findIndexedEntry, isCollection } from "./collection.js";
20
21 describe("findIndexedEntry", () => {
22 it("[[Call]] returns undefined if no matching entry exists", () => {
23 assertStrictEquals(findIndexedEntry([], () => true), void {});
24 assertStrictEquals(findIndexedEntry([1], () => false), void {});
25 });
26
27 it("[[Call]] returns an entry for the first match", () => {
28 assertEquals(
29 findIndexedEntry([, true, false], ($) => $ ?? true),
30 [0, void {}],
31 );
32 assertEquals(
33 findIndexedEntry(["failure", "success"], ($) => $ == "success"),
34 [1, "success"],
35 );
36 });
37
38 it("[[Call]] works on arraylike objects", () => {
39 assertEquals(
40 findIndexedEntry({ 1: "success", length: 2 }, ($) => $),
41 [1, "success"],
42 );
43 assertEquals(
44 findIndexedEntry({ 1: "failure", length: 1 }, ($) => $),
45 void {},
46 );
47 });
48
49 it("[[Call]] only gets the value once", () => {
50 const get1 = spy(() => true);
51 findIndexedEntry({
52 get 1() {
53 return get1();
54 },
55 length: 2,
56 }, ($) => $);
57 assertSpyCalls(get1, 1);
58 });
59
60 it("[[Call]] passes the value, index, and this value to the callback", () => {
61 const arr = ["failure", "success", "success"];
62 const callback = spy(($) => $ === "success");
63 const thisArg = {};
64 findIndexedEntry(arr, callback, thisArg);
65 assertSpyCalls(callback, 2);
66 assertSpyCall(callback, 0, {
67 args: ["failure", 0, arr],
68 self: thisArg,
69 });
70 assertSpyCall(callback, 1, {
71 args: ["success", 1, arr],
72 self: thisArg,
73 });
74 });
75 });
76
77 describe("isCollection", () => {
78 it("[[Call]] returns false for primitives", () => {
79 assertStrictEquals(isCollection("failure"), false);
80 });
81
82 it("[[Call]] returns false if length throws", () => {
83 assertStrictEquals(
84 isCollection({
85 get length() {
86 throw void {};
87 },
88 }),
89 false,
90 );
91 });
92
93 it("[[Call]] returns false if length is not an integer index and cannot be converted to one", () => {
94 assertStrictEquals(
95 isCollection({ length: -1, [Symbol.isConcatSpreadable]: true }),
96 false,
97 );
98 assertStrictEquals(
99 isCollection({
100 length: Infinity,
101 [Symbol.isConcatSpreadable]: true,
102 }),
103 false,
104 );
105 assertStrictEquals(
106 isCollection({
107 length: 9007199254740992,
108 [Symbol.isConcatSpreadable]: true,
109 }),
110 false,
111 );
112 });
113
114 it("[[Call]] returns true if length is an integer index and the object is concat spreadable", () => {
115 assertStrictEquals(
116 isCollection({ length: 1, [Symbol.isConcatSpreadable]: true }),
117 true,
118 );
119 assertStrictEquals(
120 isCollection({ length: 0, [Symbol.isConcatSpreadable]: true }),
121 true,
122 );
123 assertStrictEquals(
124 isCollection({
125 length: 9007199254740991,
126 [Symbol.isConcatSpreadable]: true,
127 }),
128 true,
129 );
130 });
131
132 it("[[Call]] returns true if length can be converted to an index without throwing an error and the object is concat spreadable", () => {
133 assertStrictEquals(
134 isCollection({ length: -0, [Symbol.isConcatSpreadable]: true }),
135 true,
136 );
137 assertStrictEquals(
138 isCollection({ length: NaN, [Symbol.isConcatSpreadable]: true }),
139 true,
140 );
141 });
142 });
This page took 0.05496 seconds and 5 git commands to generate.