From 666674327f009e9b1013218fc384f193b64c6997 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 14 Dec 2025 22:39:18 -0800 Subject: Adds unit tests --- tst/collections_cons_zipper.test.ts | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tst/collections_cons_zipper.test.ts (limited to 'tst/collections_cons_zipper.test.ts') diff --git a/tst/collections_cons_zipper.test.ts b/tst/collections_cons_zipper.test.ts new file mode 100644 index 0000000..3dfe75d --- /dev/null +++ b/tst/collections_cons_zipper.test.ts @@ -0,0 +1,58 @@ +import { Cons, ListZipper } from '../lib/index'; + +describe('types/collections/cons + zipper', () => { + test('Cons.from iterates in order', () => { + const list = Cons.from([1, 2, 3]); + expect(Array.from(list.get())).toEqual([1, 2, 3]); + }); + + test('Cons.replace replaces head value', () => { + const list = Cons.from([1, 2]).get(); + expect(Array.from(list.replace(9))).toEqual([9, 2]); + }); + + test('Cons.before prepends a head chain', () => { + const tail = Cons.from([2, 3]); + const head = new Cons(1).before(tail); + expect(Array.from(head)).toEqual([1, 2, 3]); + }); + + test('Cons.addOnto appends onto tail optional', () => { + const tail = Cons.from([3]); + const list = Cons.addOnto([1, 2], tail).get(); + expect(Array.from(list)).toEqual([1, 2, 3]); + }); + + test('ListZipper navigation and edits', () => { + const zipper = ListZipper.from([1, 2, 3]); + + expect(zipper.read().get()).toBe(1); + const z2 = zipper.next().get() as ListZipper; + expect(z2.read().get()).toBe(2); + + const z3 = z2.replace(9) as ListZipper; + expect(z3.collection()).toEqual([1, 9, 3]); + + const z4 = z3.remove() as ListZipper; + expect(z4.collection()).toEqual([1, 3]); + + const z5 = z4.prependChunk([7, 8]) as ListZipper; + expect(z5.collection()).toEqual([1, 7, 8, 3]); + + const back = (z2.previous().get() as ListZipper).read().get(); + expect(back).toBe(1); + }); + + test('ListZipper iteration yields full list', () => { + const zipper = ListZipper.from([1, 2, 3]); + expect(Array.from(zipper)).toEqual([1, 2, 3]); + + const moved = zipper.next().flatMap((z: any) => z.next()); + expect(moved.present()).toBe(true); + expect(Array.from(moved.get())).toEqual([1, 2, 3]); + + const empty = ListZipper.from([]); + expect(empty.read().present()).toBe(false); + expect(Array.from(empty)).toEqual([]); + }); +}); -- cgit v1.2.3-70-g09d2