import { describe, expect, it } from '@jest/globals'; import { List, ListItem } from '../src/data/list'; describe('List', () => { describe('from / to array', () => { it('[]', () => expect([...new List([])]).toEqual([])); it('[1]', () => expect([...new List([1])]).toEqual([1])); it('["b", "c", "b", "c"]', () => expect([...new List(['b', 'c', 'b', 'c'])]).toEqual(['b', 'c', 'b', 'c'])); }); describe('length', () => { it('[]', () => expect(new List([]).length).toEqual(0)); it('[1]', () => expect(new List([1]).length).toEqual(1)); it('["b", "c", "b", "c"]', () => expect(new List(['b', 'c', 'b', 'c']).length).toEqual(4)); }); describe('first', () => { it('[]', () => expect(new List([]).first).toBeUndefined()); it('[1]', () => expect(new List([1]).first).toEqual(1)); it('["b", "c", "b", "c"]', () => expect(new List(['b', 'c', 'b', 'c']).first).toEqual('b')); }); describe('last', () => { it('[]', () => expect(new List([]).last).toBeUndefined()); it('[1]', () => expect(new List([1]).last).toEqual(1)); it('["b", "c", "b", "c"]', () => expect(new List(['b', 'c', 'b', 'c']).last).toEqual('c')); }); describe('prepend', () => { function prepend(list: List, value: T): List { list.prepend(value); return list; } it('0 -> []', () => expect([...prepend(new List([]), 0)]).toEqual([0])); it('0 -> [1]', () => expect([...prepend(new List([1]), 0)]).toEqual([0, 1])); it('"d" -> ["b", "c", "b", "c"]', () => expect([...prepend(new List(['b', 'c', 'b', 'c']), 'd')]).toEqual(['d', 'b', 'c', 'b', 'c'])); }); describe('append', () => { function append(list: List, value: T): List { list.append(value); return list; } it('[] <- 0', () => expect([...append(new List([]), 0)]).toEqual([0])); it('[1] <- 0', () => expect([...append(new List([1]), 0)]).toEqual([1, 0])); it('["b", "c", "b", "c"] <- "d"', () => expect([...append(new List(['b', 'c', 'b', 'c']), 'd')]).toEqual(['b', 'c', 'b', 'c', 'd'])); }); describe('includes', () => { it('[] not includes 0', () => expect(new List([]).includes(0)).toBe(false)); it('[1] not includes 0', () => expect(new List([1]).includes(0)).toBe(false)); it('[1] includes 1', () => expect(new List([1]).includes(1)).toBe(true)); it('["b", "c", "b", "c"] not includes "d"', () => expect(new List(['b', 'c', 'b', 'c']).includes('d')).toBe(false)); it('["b", "c", "b", "c"] includes "b"', () => expect(new List(['b', 'c', 'b', 'c']).includes('b')).toBe(true)); }); describe('itemOf / set', () => { function update(list: List, from: T, to: T): List { const item: ListItem | undefined = list.itemOf(from); if (item) { list.set(item, to); } return list; } it('[]: 0 -> 1', () => expect([...update(new List([]), 0, 1)]).toEqual([])); it('[1]: 0 -> 1', () => expect([...update(new List([1]), 0, 1)]).toEqual([1])); it('[1]: 1 -> 0', () => expect([...update(new List([1]), 1, 0)]).toEqual([0])); it('["b", "c", "b", "c"]: "b" -> "d"', () => expect([...update(new List(['b', 'c', 'b', 'c']), 'b', 'd')]).toEqual(['d', 'c', 'b', 'c'])); it('["b", "c", "b", "c"]: "d" -> "b"', () => expect([...update(new List(['b', 'c', 'b', 'c']), 'd', 'b')]).toEqual(['b', 'c', 'b', 'c'])); }); describe('itemOf / delete', () => { function remove(list: List, value: T): List { const item: ListItem | undefined = list.itemOf(value); if (item) { list.delete(item); } return list; } it('[] - 0', () => expect([...remove(new List([]), 0)]).toEqual([])); it('[1] - 0', () => expect([...remove(new List([1]), 0)]).toEqual([1])); it('[1] - 1', () => expect([...remove(new List([1]), 1)]).toEqual([])); it('["b", "c", "b", "c"] - "b"', () => expect([...remove(new List(['b', 'c', 'b', 'c']), 'b')]).toEqual(['c', 'b', 'c'])); it('["b", "c", "b", "c"] - "d"', () => expect([...remove(new List(['b', 'c', 'b', 'c']), 'd')]).toEqual(['b', 'c', 'b', 'c'])); }); });