]> Lady’s Gitweb - Pisces/blob - collection.test.js
Move arraylike functions into 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 {
20 findIndexedEntry,
21 isCollection,
22 isConcatSpreadable,
23 } from "./collection.js";
24
25 describe("findIndexedEntry", () => {
26 it("[[Call]] returns undefined if no matching entry exists", () => {
27 assertStrictEquals(findIndexedEntry([], () => true), void {});
28 assertStrictEquals(findIndexedEntry([1], () => false), void {});
29 });
30
31 it("[[Call]] returns an entry for the first match", () => {
32 assertEquals(
33 findIndexedEntry([, true, false], ($) => $ ?? true),
34 [0, void {}],
35 );
36 assertEquals(
37 findIndexedEntry(["failure", "success"], ($) => $ == "success"),
38 [1, "success"],
39 );
40 });
41
42 it("[[Call]] works on arraylike objects", () => {
43 assertEquals(
44 findIndexedEntry({ 1: "success", length: 2 }, ($) => $),
45 [1, "success"],
46 );
47 assertEquals(
48 findIndexedEntry({ 1: "failure", length: 1 }, ($) => $),
49 void {},
50 );
51 });
52
53 it("[[Call]] only gets the value once", () => {
54 const get1 = spy(() => true);
55 findIndexedEntry({
56 get 1() {
57 return get1();
58 },
59 length: 2,
60 }, ($) => $);
61 assertSpyCalls(get1, 1);
62 });
63
64 it("[[Call]] passes the value, index, and this value to the callback", () => {
65 const arr = ["failure", "success", "success"];
66 const callback = spy(($) => $ === "success");
67 const thisArg = {};
68 findIndexedEntry(arr, callback, thisArg);
69 assertSpyCalls(callback, 2);
70 assertSpyCall(callback, 0, {
71 args: ["failure", 0, arr],
72 self: thisArg,
73 });
74 assertSpyCall(callback, 1, {
75 args: ["success", 1, arr],
76 self: thisArg,
77 });
78 });
79 });
80
81 describe("isCollection", () => {
82 it("[[Call]] returns false for primitives", () => {
83 assertStrictEquals(isCollection("failure"), false);
84 });
85
86 it("[[Call]] returns false if length throws", () => {
87 assertStrictEquals(
88 isCollection({
89 get length() {
90 throw void {};
91 },
92 }),
93 false,
94 );
95 });
96
97 it("[[Call]] returns false if length is not an integer index and cannot be converted to one", () => {
98 assertStrictEquals(
99 isCollection({ length: -1, [Symbol.isConcatSpreadable]: true }),
100 false,
101 );
102 assertStrictEquals(
103 isCollection({
104 length: Infinity,
105 [Symbol.isConcatSpreadable]: true,
106 }),
107 false,
108 );
109 assertStrictEquals(
110 isCollection({
111 length: 9007199254740992,
112 [Symbol.isConcatSpreadable]: true,
113 }),
114 false,
115 );
116 });
117
118 it("[[Call]] returns true if length is an integer index and the object is concat spreadable", () => {
119 assertStrictEquals(
120 isCollection({ length: 1, [Symbol.isConcatSpreadable]: true }),
121 true,
122 );
123 assertStrictEquals(
124 isCollection({ length: 0, [Symbol.isConcatSpreadable]: true }),
125 true,
126 );
127 assertStrictEquals(
128 isCollection({
129 length: 9007199254740991,
130 [Symbol.isConcatSpreadable]: true,
131 }),
132 true,
133 );
134 });
135
136 it("[[Call]] returns true if length can be converted to an index without throwing an error and the object is concat spreadable", () => {
137 assertStrictEquals(
138 isCollection({ length: -0, [Symbol.isConcatSpreadable]: true }),
139 true,
140 );
141 assertStrictEquals(
142 isCollection({ length: NaN, [Symbol.isConcatSpreadable]: true }),
143 true,
144 );
145 });
146 });
147
148 describe("isConcatSpreadable", () => {
149 it("[[Call]] returns false for primitives", () => {
150 assertStrictEquals(isConcatSpreadable("failure"), false);
151 });
152
153 it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
154 assertStrictEquals(
155 isConcatSpreadable(
156 Object.assign([], { [Symbol.isConcatSpreadable]: null }),
157 ),
158 false,
159 );
160 assertStrictEquals(
161 isConcatSpreadable(
162 Object.assign([], { [Symbol.isConcatSpreadable]: false }),
163 ),
164 false,
165 );
166 });
167
168 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
169 assertStrictEquals(
170 isConcatSpreadable(
171 Object.assign([], { [Symbol.isConcatSpreadable]: undefined }),
172 ),
173 true,
174 );
175 });
176
177 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
178 assertStrictEquals(
179 isConcatSpreadable({ [Symbol.isConcatSpreadable]: true }),
180 true,
181 );
182 });
183 });
This page took 0.062359 seconds and 5 git commands to generate.