]>
Lady’s Gitweb - Etiquette/blob - memory.test.js
8187635a7cee18320258727de6b027b4674da56b
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("[[Construct]] creates a new Storage", () => {
45 Object
.getPrototypeOf(new Storage()),
50 describe(".toInstance", () => {
51 it("[[Get]] returns a symbol", () => {
52 assertStrictEquals(typeof Storage
.toInstance
, "symbol");
55 it("[[Set]] throws", () => {
56 assertThrows(() => Storage
.toInstance
= null);
60 describe(".toObject", () => {
61 it("[[Get]] returns a symbol", () => {
62 assertStrictEquals(typeof Storage
.toObject
, "symbol");
65 it("[[Set]] throws", () => {
66 assertThrows(() => Storage
.toObject
= null);
70 describe("::add", () => {
74 instance
= new Storage();
77 it("[[Call]] returns an id", () => {
78 const result
= instance
.add(new Storable());
79 assertStrictEquals(typeof result
, "string");
81 /^[0-9A-Z*~$=][0-9A-TV-Z]{2}-[0-9A-TV-Z]{4}$/u.test(result
),
85 it("[[Call]] stores data for retrieval later", () => {
86 const data
= { my
: "data" };
87 const storable
= new Storable(data
);
88 const newID
= instance
.add(storable
);
89 assertEquals(instance
.get(newID
).data
, data
);
92 it("[[Call]] does not store non‐enumerable properties", () => {
93 const data
= Object
.create(null, {
94 gone
: { enumerable
: false },
96 const storable
= new Storable(data
);
97 const newID
= instance
.add(storable
);
98 assert(!("gone" in instance
.get(newID
).data
));
101 it("[[Call]] does not store prototype properties", () => {
102 const data
= Object
.create({ gone
: true });
103 const storable
= new Storable(data
);
104 const newID
= instance
.add(storable
);
105 assert(!("gone" in instance
.get(newID
).data
));
108 it("[[Call]] throws if the provided value is not an object", () => {
114 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
116 instance
.add(Object
.create(null));
121 describe("::delete", () => {
125 instance
= new Storage();
128 it("[[Call]] returns whether the value was assigned", () => {
129 assertStrictEquals(instance
.delete("000-0000"), false);
130 const newID
= instance
.add(new Storable());
131 assertStrictEquals(instance
.delete(newID
), true);
134 it("[[Call]] deletes the value", () => {
135 const newID
= instance
.add(new Storable());
136 instance
.delete(newID
);
137 assertStrictEquals(instance
.get(newID
), null);
140 it("[[Call]] throws if the provided identifier is invalid", () => {
147 describe("::has", () => {
151 instance
= new Storage();
154 it("[[Call]] returns whether the value was assigned", () => {
155 assertStrictEquals(instance
.has("000-0000"), false);
156 instance
.set("000-0000", new Storable());
157 assertStrictEquals(instance
.has("000-0000"), true);
158 instance
.delete("000-0000")
159 assertStrictEquals(instance
.has("000-0000"), false);
162 it("[[Call]] throws if the provided identifier is invalid", () => {
169 describe("::set", () => {
174 instance
= new Storage();
175 newID
= new Storage().add(new Storable());
178 it("[[Call]] returns the instance", () => {
179 const result
= instance
.set(newID
, new Storable());
180 assertStrictEquals(result
, instance
);
183 it("[[Call]] stores data for retrieval later", () => {
184 const data
= { my
: "data" };
185 const storable
= new Storable(data
);
186 instance
.set(newID
, storable
);
187 assertEquals(instance
.get(newID
).data
, data
);
190 it("[[Call]] updates existing data", () => {
191 const data
= { my
: "data" };
192 const storable
= new Storable(data
);
193 instance
.set(newID
, storable
);
195 instance
.set(newID
, storable
);
196 assertEquals(instance
.get(newID
).data
, data
);
199 it("[[Call]] does not store non‐enumerable properties", () => {
200 const data
= Object
.create(null, {
201 gone
: { enumerable
: false },
203 const storable
= new Storable(data
);
204 instance
.set(newID
, storable
);
205 assert(!("gone" in instance
.get(newID
).data
));
208 it("[[Call]] does not store prototype properties", () => {
209 const data
= Object
.create({ gone
: true });
210 const storable
= new Storable(data
);
211 instance
.set(newID
, storable
);
212 assert(!("gone" in instance
.get(newID
).data
));
215 it("[[Call]] throws if the provided identifier is invalid", () => {
217 instance
.set("", new Storable());
221 it("[[Call]] throws if the provided value is not an object", () => {
223 instance
.set(newID
, "");
227 it("[[Call]] throws if the provided value does not implement `[Storage.toObject]`", () => {
229 instance
.set(newID
, Object
.create(null));
234 describe("::size", () => {
238 instance
= new Storage();
241 it("[[Get]] returns the number of stored values", () => {
242 assertStrictEquals(instance
.size
, 0);
243 instance
.add(new Storable());
244 assertStrictEquals(instance
.size
, 1);
245 const newID
= instance
.add(new Storable());
246 assertStrictEquals(instance
.size
, 2);
247 instance
.set(newID
, new Storable());
248 assertStrictEquals(instance
.size
, 2);
251 it("[[Get]] does not count deleted values", () => {
252 assertStrictEquals(instance
.size
, 0);
253 instance
.add(new Storable());
254 assertStrictEquals(instance
.size
, 1);
255 const newID
= instance
.add(new Storable());
256 assertStrictEquals(instance
.size
, 2);
257 instance
.delete(newID
);
258 assertStrictEquals(instance
.size
, 1);
261 it("[[Set]] throws when setting", () => {
This page took 0.116028 seconds and 3 git commands to generate.