]>
Lady’s Gitweb - Etiquette/blob - memory.test.js
1 // 📧🏷️ Étiquette ∷ memory.test.js
2 // ====================================================================
4 // Copyright © 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/>.
18 } from "./dev-deps.js";
19 import { Storage
} from "./memory.js";
21 /** A simple storable class for use in testing. */
23 /** Constructs a new instance with the provided data. */
29 /** Returns a new instance with the provided data and id. */
30 static [Storage
.toInstance
](data
, id
) {
31 const result
= new Storable(data
);
36 /** Returns the data of this instance. */
37 [Storage
.toObject
]() {
42 describe("Storage", () => {
43 it("[[Call]] throws", () => {
49 it("[[Construct]] creates a new Storage", () => {
51 Object
.getPrototypeOf(new Storage()),
56 describe(".toInstance", () => {
57 it("[[Get]] returns a symbol", () => {
58 assertStrictEquals(typeof Storage
.toInstance
, "symbol");
61 it("[[Set]] throws", () => {
62 assertThrows(() => Storage
.toInstance
= null);
66 describe(".toObject", () => {
67 it("[[Get]] returns a symbol", () => {
68 assertStrictEquals(typeof Storage
.toObject
, "symbol");
71 it("[[Set]] throws", () => {
72 assertThrows(() => Storage
.toObject
= null);
76 describe("::add", () => {
80 instance
= new Storage();
83 it("[[Call]] returns an id", () => {
84 const result
= instance
.add(new Storable());
85 assertStrictEquals(typeof result
, "string");
87 /^[0-9A-Z*~$=][0-9A-TV-Z]{2}-[0-9A-TV-Z]{4}$/u.test(result
),
91 it("[[Call]] stores data for retrieval later", () => {
92 const data
= { my
: "data" };
93 const storable
= new Storable(data
);
94 const newID
= instance
.add(storable
);
95 assertEquals(instance
.get(newID
).data
, data
);
98 it("[[Call]] does not store non‐enumerable properties", () => {
99 const data
= Object
.create(null, {
100 gone
: { enumerable
: false },
102 const storable
= new Storable(data
);
103 const newID
= instance
.add(storable
);
104 assert(!("gone" in instance
.get(newID
).data
));
107 it("[[Call]] does not store prototype properties", () => {
108 const data
= Object
.create({ gone
: true });
109 const storable
= new Storable(data
);
110 const newID
= instance
.add(storable
);
111 assert(!("gone" in instance
.get(newID
).data
));
114 it("[[Call]] throws if the provided value is not an object", () => {
120 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
122 instance
.add(Object
.create(null));
127 describe("::delete", () => {
131 instance
= new Storage();
134 it("[[Call]] returns whether the value was assigned", () => {
135 assertStrictEquals(instance
.delete("000-0000"), false);
136 const newID
= instance
.add(new Storable());
137 assertStrictEquals(instance
.delete(newID
), true);
140 it("[[Call]] deletes the value", () => {
141 const newID
= instance
.add(new Storable());
142 instance
.delete(newID
);
143 assertStrictEquals(instance
.get(newID
), null);
146 it("[[Call]] throws if the provided identifier is invalid", () => {
153 describe("::has", () => {
157 instance
= new Storage();
160 it("[[Call]] returns whether the value was assigned", () => {
161 assertStrictEquals(instance
.has("000-0000"), false);
162 instance
.set("000-0000", new Storable());
163 assertStrictEquals(instance
.has("000-0000"), true);
164 instance
.delete("000-0000");
165 assertStrictEquals(instance
.has("000-0000"), false);
168 it("[[Call]] throws if the provided identifier is invalid", () => {
175 describe("::set", () => {
180 instance
= new Storage();
181 newID
= new Storage().add(new Storable());
184 it("[[Call]] returns the instance", () => {
185 const result
= instance
.set(newID
, new Storable());
186 assertStrictEquals(result
, instance
);
189 it("[[Call]] stores data for retrieval later", () => {
190 const data
= { my
: "data" };
191 const storable
= new Storable(data
);
192 instance
.set(newID
, storable
);
193 assertEquals(instance
.get(newID
).data
, data
);
196 it("[[Call]] updates existing data", () => {
197 const data
= { my
: "data" };
198 const storable
= new Storable(data
);
199 instance
.set(newID
, storable
);
200 data
.my
= "new data";
201 instance
.set(newID
, storable
);
202 assertEquals(instance
.get(newID
).data
, data
);
205 it("[[Call]] does not store non‐enumerable properties", () => {
206 const data
= Object
.create(null, {
207 gone
: { enumerable
: false },
209 const storable
= new Storable(data
);
210 instance
.set(newID
, storable
);
211 assert(!("gone" in instance
.get(newID
).data
));
214 it("[[Call]] does not store prototype properties", () => {
215 const data
= Object
.create({ gone
: true });
216 const storable
= new Storable(data
);
217 instance
.set(newID
, storable
);
218 assert(!("gone" in instance
.get(newID
).data
));
221 it("[[Call]] throws if the provided identifier is invalid", () => {
223 instance
.set("", new Storable());
227 it("[[Call]] throws if the provided value is not an object", () => {
229 instance
.set(newID
, "");
233 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
235 instance
.set(newID
, Object
.create(null));
240 describe("::size", () => {
244 instance
= new Storage();
247 it("[[Get]] returns the number of stored values", () => {
248 assertStrictEquals(instance
.size
, 0);
249 instance
.add(new Storable());
250 assertStrictEquals(instance
.size
, 1);
251 const newID
= instance
.add(new Storable());
252 assertStrictEquals(instance
.size
, 2);
253 instance
.set(newID
, new Storable());
254 assertStrictEquals(instance
.size
, 2);
257 it("[[Get]] does not count deleted values", () => {
258 assertStrictEquals(instance
.size
, 0);
259 instance
.add(new Storable());
260 assertStrictEquals(instance
.size
, 1);
261 const newID
= instance
.add(new Storable());
262 assertStrictEquals(instance
.size
, 2);
263 instance
.delete(newID
);
264 assertStrictEquals(instance
.size
, 1);
267 it("[[Set]] throws when setting", () => {
This page took 0.100521 seconds and 5 git commands to generate.