diff options
Diffstat (limited to 'tst/collections_cons_zipper.test.ts')
| -rw-r--r-- | tst/collections_cons_zipper.test.ts | 58 |
1 files changed, 58 insertions, 0 deletions
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<number>; + expect(z2.read().get()).toBe(2); + + const z3 = z2.replace(9) as ListZipper<number>; + expect(z3.collection()).toEqual([1, 9, 3]); + + const z4 = z3.remove() as ListZipper<number>; + expect(z4.collection()).toEqual([1, 3]); + + const z5 = z4.prependChunk([7, 8]) as ListZipper<number>; + expect(z5.collection()).toEqual([1, 7, 8, 3]); + + const back = (z2.previous().get() as ListZipper<number>).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<number>([]); + expect(empty.read().present()).toBe(false); + expect(Array.from(empty)).toEqual([]); + }); +}); |
