]>
Lady’s Gitweb - Etiquette/blob - memory.test.js
1 // SPDX-FileCopyrightText: 2023, 2025 Lady <https://www.ladys.computer/about/#lady>
2 // SPDX-License-Identifier: MPL-2.0
4 * ⁌ 📧🏷️ Étiquette ∷ memory.test.js
6 * Copyright © 2023, 2025 Lady [@ Ladys Computer].
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
21 } from "./dev-deps.js";
22 import { Storage
} from "./memory.js";
24 /** A simple storable class for use in testing. */
26 /** Constructs a new instance with the provided data. */
32 /** Returns a new instance with the provided data and id. */
33 static [Storage
.toInstance
](data
, id
) {
34 const result
= new Storable(data
);
39 /** Returns the data of this instance. */
40 [Storage
.toObject
]() {
45 describe("Storage", () => {
46 it("[[Call]] throws", () => {
52 it("[[Construct]] creates a new Storage", () => {
54 Object
.getPrototypeOf(new Storage()),
59 describe(".toInstance", () => {
60 it("[[Get]] returns a symbol", () => {
61 assertStrictEquals(typeof Storage
.toInstance
, "symbol");
64 it("[[Set]] throws", () => {
65 assertThrows(() => Storage
.toInstance
= null);
69 describe(".toObject", () => {
70 it("[[Get]] returns a symbol", () => {
71 assertStrictEquals(typeof Storage
.toObject
, "symbol");
74 it("[[Set]] throws", () => {
75 assertThrows(() => Storage
.toObject
= null);
79 describe("::add", () => {
83 instance
= new Storage();
86 it("[[Call]] returns an id", () => {
87 const result
= instance
.add(new Storable());
88 assertStrictEquals(typeof result
, "string");
90 /^[0-9A-Z*~$=][0-9A-TV-Z]{2}-[0-9A-TV-Z]{4}$/u.test(result
),
94 it("[[Call]] stores data for retrieval later", () => {
95 const data
= { my: "data" };
96 const storable
= new Storable(data
);
97 const newID
= instance
.add(storable
);
98 assertEquals(instance
.get(newID
).data
, data
);
101 it("[[Call]] does not store non‐enumerable properties", () => {
102 const data
= Object
.create(null, {
103 gone: { enumerable: false },
105 const storable
= new Storable(data
);
106 const newID
= instance
.add(storable
);
107 assert(!("gone" in instance
.get(newID
).data
));
110 it("[[Call]] does not store prototype properties", () => {
111 const data
= Object
.create({ gone: true });
112 const storable
= new Storable(data
);
113 const newID
= instance
.add(storable
);
114 assert(!("gone" in instance
.get(newID
).data
));
117 it("[[Call]] throws if the provided value is not an object", () => {
123 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
125 instance
.add(Object
.create(null));
130 describe("::delete", () => {
134 instance
= new Storage();
137 it("[[Call]] returns whether the value was assigned", () => {
138 assertStrictEquals(instance
.delete("000-0000"), false);
139 const newID
= instance
.add(new Storable());
140 assertStrictEquals(instance
.delete(newID
), true);
143 it("[[Call]] deletes the value", () => {
144 const newID
= instance
.add(new Storable());
145 instance
.delete(newID
);
146 assertStrictEquals(instance
.get(newID
), null);
149 it("[[Call]] throws if the provided identifier is invalid", () => {
156 describe("::has", () => {
160 instance
= new Storage();
163 it("[[Call]] returns whether the value was assigned", () => {
164 assertStrictEquals(instance
.has("000-0000"), false);
165 instance
.set("000-0000", new Storable());
166 assertStrictEquals(instance
.has("000-0000"), true);
167 instance
.delete("000-0000");
168 assertStrictEquals(instance
.has("000-0000"), false);
171 it("[[Call]] throws if the provided identifier is invalid", () => {
178 describe("::set", () => {
183 instance
= new Storage();
184 newID
= new Storage().add(new Storable());
187 it("[[Call]] returns the instance", () => {
188 const result
= instance
.set(newID
, new Storable());
189 assertStrictEquals(result
, instance
);
192 it("[[Call]] stores data for retrieval later", () => {
193 const data
= { my: "data" };
194 const storable
= new Storable(data
);
195 instance
.set(newID
, storable
);
196 assertEquals(instance
.get(newID
).data
, data
);
199 it("[[Call]] updates existing data", () => {
200 const data
= { my: "data" };
201 const storable
= new Storable(data
);
202 instance
.set(newID
, storable
);
203 data
.my
= "new data";
204 instance
.set(newID
, storable
);
205 assertEquals(instance
.get(newID
).data
, data
);
208 it("[[Call]] does not store non‐enumerable properties", () => {
209 const data
= Object
.create(null, {
210 gone: { enumerable: false },
212 const storable
= new Storable(data
);
213 instance
.set(newID
, storable
);
214 assert(!("gone" in instance
.get(newID
).data
));
217 it("[[Call]] does not store prototype properties", () => {
218 const data
= Object
.create({ gone: true });
219 const storable
= new Storable(data
);
220 instance
.set(newID
, storable
);
221 assert(!("gone" in instance
.get(newID
).data
));
224 it("[[Call]] throws if the provided identifier is invalid", () => {
226 instance
.set("", new Storable());
230 it("[[Call]] throws if the provided value is not an object", () => {
232 instance
.set(newID
, "");
236 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
238 instance
.set(newID
, Object
.create(null));
243 describe("::size", () => {
247 instance
= new Storage();
250 it("[[Get]] returns the number of stored values", () => {
251 assertStrictEquals(instance
.size
, 0);
252 instance
.add(new Storable());
253 assertStrictEquals(instance
.size
, 1);
254 const newID
= instance
.add(new Storable());
255 assertStrictEquals(instance
.size
, 2);
256 instance
.set(newID
, new Storable());
257 assertStrictEquals(instance
.size
, 2);
260 it("[[Get]] does not count deleted values", () => {
261 assertStrictEquals(instance
.size
, 0);
262 instance
.add(new Storable());
263 assertStrictEquals(instance
.size
, 1);
264 const newID
= instance
.add(new Storable());
265 assertStrictEquals(instance
.size
, 2);
266 instance
.delete(newID
);
267 assertStrictEquals(instance
.size
, 1);
270 it("[[Set]] throws when setting", () => {
This page took 0.074583 seconds and 5 git commands to generate.