You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
4.0 KiB
88 lines
4.0 KiB
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<T>(list: List<T>, value: T): List<T> {
|
|
list.prepend(value);
|
|
return list;
|
|
}
|
|
it('0 -> []', () => expect([...prepend(new List<number>([]), 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<T>(list: List<T>, value: T): List<T> {
|
|
list.append(value);
|
|
return list;
|
|
}
|
|
it('[] <- 0', () => expect([...append(new List<number>([]), 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<number>([]).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<T>(list: List<T>, from: T, to: T): List<T> {
|
|
const item: ListItem<T> | undefined = list.itemOf(from);
|
|
if (item) {
|
|
list.set(item, to);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
it('[]: 0 -> 1', () => expect([...update(new List<number>([]), 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<T>(list: List<T>, value: T): List<T> {
|
|
const item: ListItem<T> | undefined = list.itemOf(value);
|
|
if (item) {
|
|
list.delete(item);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
it('[] - 0', () => expect([...remove(new List<number>([]), 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']));
|
|
});
|
|
});
|
|
|