Skip to content

Commit b448744

Browse files
committed
made options optional on StoreStack
1 parent 908ad6d commit b448744

2 files changed

Lines changed: 82 additions & 8 deletions

File tree

mod.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,9 @@ export class StoreStack {
377377
*
378378
* @throws {MemoryAllocationError} If the address is already allocated and `override` isn't set to `true`.
379379
*/
380-
public addStoreAtPointer(newItem: AnyStore, pointer: Pointer, options: { override?: boolean, verbose?: boolean }): void {
381-
const { override, verbose } = options;
382-
383-
if (typeof this.#stores[pointer] !== "undefined" && !override) {
384-
if (verbose) {
380+
public addStoreAtPointer(newItem: AnyStore, pointer: Pointer, options?: { override?: boolean, verbose?: boolean }): void {
381+
if (typeof this.#stores[pointer] !== "undefined" && !options?.override) {
382+
if (options?.verbose) {
385383
console.error('Error: Cannot add store at pointer, address is already allocated.');
386384
}
387385

@@ -425,9 +423,9 @@ export class StoreStack {
425423
*
426424
* @throws {NullPointerError} If attempting to delete a store at unallocated memory address.
427425
*/
428-
public removeStore(ptr: Pointer, options: { verbose?: boolean }): void {
426+
public removeStore(ptr: Pointer, options?: { verbose?: boolean }): void {
429427
if (typeof this.#stores[ptr] === "undefined") {
430-
if (options.verbose) {
428+
if (options?.verbose) {
431429
console.error('Error: The pointer address points to unallocated memory.');
432430
}
433431

tests/mod.test.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals, assertInstanceOf, assertNotEquals, assertThrows } from "https://deno.land/std@0.151.0/testing/asserts.ts";
1+
import { assertEquals, assertExists, assertInstanceOf, assertNotEquals, assertThrows } from "https://deno.land/std@0.151.0/testing/asserts.ts";
22
import * as mod from "../mod.ts";
33

44
// Mark: Test Custom Errors
@@ -152,4 +152,80 @@ Deno.test("An unattached observer cannot be removed", () => {
152152
const observer = new ConcreteObserver();
153153

154154
assertThrows(() => store.detach(observer), mod.UnknownObserverError, "Attempted removal of unattached observer.", "Should throw on removal of unattached observer.")
155+
});
156+
157+
// Mark: StoreStack tests
158+
Deno.test("An instance of StoreStack can be instantiated", () => {
159+
const stack = new mod.StoreStack() as unknown;
160+
161+
assertInstanceOf(stack, mod.StoreStack);
162+
});
163+
164+
Deno.test("A store can be added to a stack without providing a pointer, accessed with the returned pointer and can be removed", () => {
165+
const stack = new mod.StoreStack();
166+
167+
const store = new mod.Store(0);
168+
const ptr = stack.addStore(store);
169+
170+
const store2 = new mod.Store(1);
171+
172+
assertExists(ptr, "ptr should not be undefined.");
173+
assertExists(stack.get<number>(ptr), "The pointer should be allocated");
174+
assertEquals(stack.get<number>(ptr)!.state, store.state, "The states should match");
175+
176+
// Cannot be added twice at address without override
177+
assertThrows(() => stack.addStoreAtPointer(store, ptr), mod.MemoryAllocationError, "Attempted to insert store at already allocated memory address without explicit override.", "Should not be able to add at address without override.");
178+
179+
// Can be overriden
180+
stack.addStoreAtPointer(store2, ptr, { override: true });
181+
182+
assertExists(stack.get<number>(ptr), "The pointer should be allocated");
183+
assertEquals(stack.get<number>(ptr)!.state, store2.state, "The states should match");
184+
185+
stack.removeStore(ptr);
186+
187+
assertEquals(typeof stack.get(ptr), "undefined", "The pointer should be unallocated");
188+
189+
// But cannot be removed twice
190+
assertThrows(() => stack.removeStore(ptr), mod.NullPointerError, "Attempted to access unallocated memory address.", "Should not be able to delete the store twice.");
191+
});
192+
193+
Deno.test("upsert test", () => {
194+
const stack = new mod.StoreStack();
195+
196+
const pointer1 = crypto.randomUUID();
197+
198+
const defaultValue1 = 0;
199+
const defaultValue2 = 1;
200+
201+
class ConcreteObserver implements mod.Observer<number> {
202+
public update(subject: mod.Store<number>): void {
203+
observerOuput = subject.state;
204+
}
205+
}
206+
207+
const observer = new ConcreteObserver();
208+
let observerOuput = defaultValue1;
209+
210+
// Pre assertions
211+
assertEquals(typeof stack.get(pointer1), "undefined", "Pointer should not already be allocated");
212+
213+
// Creating a store
214+
stack.upsert(defaultValue1, pointer1);
215+
216+
assertExists(stack.get(pointer1), "Store should now exist");
217+
assertEquals(stack.get(pointer1)!.state, defaultValue1, "State should equal " + defaultValue1);
218+
219+
stack.upsert(defaultValue2, pointer1);
220+
221+
assertNotEquals(stack.get(pointer1)!.state, defaultValue2, "Because the store already exists, it should not override its value");
222+
223+
// Attaching an observer
224+
stack.upsert(defaultValue1, pointer1, observer);
225+
stack.get<number>(pointer1)!.set(defaultValue2);
226+
227+
assertEquals(observerOuput, defaultValue2, "observerOutput and defaultValue2 should match");
228+
229+
// Throwing on reattaching observer
230+
assertThrows(() => stack.upsert(defaultValue1, pointer1, observer), mod.DuplicateObserverError, "This observer is already attached to the store.", "Should trigger a duplicate observer error");
155231
});

0 commit comments

Comments
 (0)