Skip to content

Commit

Permalink
test: increase test coverage, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvid Nicolaas committed Jan 26, 2024
1 parent 714e66e commit 3e392e5
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 18 deletions.
7 changes: 5 additions & 2 deletions deno_dist/graph/custom/valued/implementation/non-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class ValuedGraphNonEmpty<
node2: N,
options: {
ifNew?: OptLazyOr<V, Token>;
ifExists?: (value: V, remove: Token) => V | Token;
ifExists?: ((value: V, remove: Token) => V | Token) | V;
}
): TpG['nonEmpty'] {
let newConnectionSize = this.connectionSize;
Expand Down Expand Up @@ -312,7 +312,10 @@ export class ValuedGraphNonEmpty<
return newValue;
},
ifExists: (currentValue, remove) => {
const newValue = ifExists(currentValue, remove);
const newValue =
ifExists instanceof Function
? ifExists(currentValue, remove)
: ifExists;

if (Object.is(newValue, currentValue)) return currentValue;

Expand Down
4 changes: 2 additions & 2 deletions deno_dist/list/custom/implementation/tree/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export function treeForEach<

if (state.halted) return;

(reversed ? tree.right : tree.left).forEach(f, { state });
(reversed ? tree.right : tree.left).forEach(f, { reversed, state });

if (state.halted) return;

Expand All @@ -319,5 +319,5 @@ export function treeForEach<

if (state.halted) return;

(reversed ? tree.left : tree.right).forEach(f, { state });
(reversed ? tree.left : tree.right).forEach(f, { reversed, state });
}
7 changes: 5 additions & 2 deletions packages/graph/src/custom/valued/implementation/non-empty.mts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class ValuedGraphNonEmpty<
node2: N,
options: {
ifNew?: OptLazyOr<V, Token>;
ifExists?: (value: V, remove: Token) => V | Token;
ifExists?: ((value: V, remove: Token) => V | Token) | V;
}
): TpG['nonEmpty'] {
let newConnectionSize = this.connectionSize;
Expand Down Expand Up @@ -312,7 +312,10 @@ export class ValuedGraphNonEmpty<
return newValue;
},
ifExists: (currentValue, remove) => {
const newValue = ifExists(currentValue, remove);
const newValue =
ifExists instanceof Function
? ifExists(currentValue, remove)
: ifExists;

Check warning on line 318 in packages/graph/src/custom/valued/implementation/non-empty.mts

View check run for this annotation

Codecov / codecov/patch

packages/graph/src/custom/valued/implementation/non-empty.mts#L318

Added line #L318 was not covered by tests

if (Object.is(newValue, currentValue)) return currentValue;

Expand Down
14 changes: 14 additions & 0 deletions packages/graph/test/arrow-graph-test-standard.mts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ export function runArrowGraphTestsWith(
expect(graph3.disconnectAll(arr3).nodeSize).toBe(3);
});

it('forEach', () => {
let counter = 0;
graphEmpty.forEach(() => counter++);
expect(counter).toEqual(0);

counter = 0;
graph3.forEach(() => counter++);
expect(counter).toEqual(3);

counter = 0;
graph6.forEach(() => counter++);
expect(counter).toEqual(8);
});

it('getConnectionStreamFrom', () => {
expect(graphEmpty.getConnectionStreamFrom('a')).toBe(Stream.empty());
expect(graph3.getConnectionStreamFrom('z')).toBe(Stream.empty());
Expand Down
14 changes: 14 additions & 0 deletions packages/graph/test/arrow-valued-graph-test-standard.mts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ export function runGraphTestsWith(
expect(graph3.disconnectAll(arr3).nodeSize).toBe(3);
});

it('forEach', () => {
let counter = 0;
graphEmpty.forEach(() => counter++);
expect(counter).toEqual(0);

counter = 0;
graph3.forEach(() => counter++);
expect(counter).toEqual(3);

counter = 0;
graph6.forEach(() => counter++);
expect(counter).toEqual(8);
});

it('getConnectionStreamFrom', () => {
expect(graphEmpty.getConnectionStreamFrom('a')).toBe(Stream.empty());
expect(graph3.getConnectionStreamFrom('z')).toBe(Stream.empty());
Expand Down
14 changes: 14 additions & 0 deletions packages/graph/test/edge-graph-test-standard.mts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ export function runEdgeGraphTestsWith(name: string, G: EdgeGraph.Context<any>) {
expect(graph3.disconnectAll(arr3).nodeSize).toBe(3);
});

it('forEach', () => {
let counter = 0;
graphEmpty.forEach(() => counter++);
expect(counter).toEqual(0);

counter = 0;
graph3.forEach(() => counter++);
expect(counter).toEqual(6);

counter = 0;
graph6.forEach(() => counter++);
expect(counter).toEqual(12);
});

it('getConnectionStreamFrom', () => {
expect(graphEmpty.getConnectionStreamFrom('a')).toBe(Stream.empty());
expect(graph3.getConnectionStreamFrom('z')).toBe(Stream.empty());
Expand Down
14 changes: 14 additions & 0 deletions packages/graph/test/edge-valued-graph-test-standard.mts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ export function runGraphTestsWith(
expect(graph3.disconnectAll(arr3).nodeSize).toBe(3);
});

it('forEach', () => {
let counter = 0;
graphEmpty.forEach(() => counter++);
expect(counter).toEqual(0);

counter = 0;
graph3.forEach(() => counter++);
expect(counter).toEqual(6);

counter = 0;
graph6.forEach(() => counter++);
expect(counter).toEqual(12);
});

it('getConnectionStreamFrom', () => {
expect(graphEmpty.getConnectionStreamFrom('a')).toBe(Stream.empty());
expect(graph3.getConnectionStreamFrom('z')).toBe(Stream.empty());
Expand Down
4 changes: 2 additions & 2 deletions packages/list/src/custom/implementation/tree/operations.mts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export function treeForEach<

if (state.halted) return;

(reversed ? tree.right : tree.left).forEach(f, { state });
(reversed ? tree.right : tree.left).forEach(f, { reversed, state });

if (state.halted) return;

Expand All @@ -319,5 +319,5 @@ export function treeForEach<

if (state.halted) return;

(reversed ? tree.left : tree.right).forEach(f, { state });
(reversed ? tree.left : tree.right).forEach(f, { reversed, state });
}
3 changes: 3 additions & 0 deletions packages/list/test/gen-builder.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ describe('GenBuilder', () => {
const f = vi.fn();
b.forEach(f);
expect(f).not.toHaveBeenCalled();

b.forEach(f, { reversed: true });
expect(f).not.toHaveBeenCalled();
}
{
// has sub builder
Expand Down
22 changes: 18 additions & 4 deletions packages/list/test/leaf-block-builder.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,22 @@ describe('LeafBlockBuilder', () => {

it('forEach', () => {
{
const b = context.leafBlockBuilder([1, 2, 3]);
const b = context.leafBlockBuilder([1, 2, 3, 4]);
const cb = vi.fn();
b.forEach(cb);
expect(cb).toBeCalledTimes(3);
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(2);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(3);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach((_, __, halt) => {
halt();
cb();
Expand All @@ -177,15 +184,22 @@ describe('LeafBlockBuilder', () => {
expect(cb).toBeCalledTimes(1);
}
{
const b = context.leafBlockBuilderSource(context.leafBlock([1, 2, 3]));
const b = context.leafBlockBuilderSource(context.leafBlock([1, 2, 3, 4]));
const cb = vi.fn();
b.forEach(cb);
expect(cb).toBeCalledTimes(3);
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(2);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(3);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach((_, __, halt) => {
halt();
cb();
Expand Down
11 changes: 9 additions & 2 deletions packages/list/test/leaf-block.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,22 @@ function runLeafBlockTests(
});

it('forEach', () => {
const b3 = createBlock(1, 2, 3);
const b3 = createBlock(1, 2, 3, 4);
const cb = vi.fn();
b3.forEach(cb);
expect(cb).toBeCalledTimes(3);
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(2);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b3.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(4);
expect(cb.mock.calls[1][0]).toBe(3);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b3.forEach((_, __, halt) => {
halt();
cb();
Expand Down
7 changes: 7 additions & 0 deletions packages/list/test/leaf-tree-builder.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ describe('LeafTreeBuilder', () => {

cb.mockReset();

t.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(8);
expect(cb.mock.calls[1][0]).toBe(13);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

t.forEach((_, __, halt) => {
halt();
cb();
Expand Down
11 changes: 9 additions & 2 deletions packages/list/test/leaf-tree.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,15 @@ function runLeafTreeTests(
const cb = vi.fn();
t6.forEach(cb);
expect(cb).toBeCalledTimes(6);
expect(cb.mock.calls[1][0]).toBe(2);
expect(cb.mock.calls[1][1]).toBe(1);
expect(cb.mock.calls[2][0]).toBe(3);
expect(cb.mock.calls[2][1]).toBe(2);

cb.mockReset();

t6.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(6);
expect(cb.mock.calls[2][0]).toBe(1);
expect(cb.mock.calls[2][1]).toBe(2);

cb.mockReset();

Expand Down
46 changes: 46 additions & 0 deletions packages/list/test/list.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,52 @@ describe('List methods', () => {
expect(result).toEqual([1]);
});

it('forEach reversed', () => {
let result = [] as number[];
listEmpty.forEach((v) => result.push(v), { reversed: true });
expect(result).toEqual([]);
result = [];
list3_1.forEach((v, i) => result.push(v + i), { reversed: true });
expect(result).toEqual([3, 3, 3]);

result = [];
list3_2.forEach((v, i) => result.push(v + i), { reversed: true });
expect(result).toEqual([3, 3, 3]);

result = [];
list6_1.forEach((v, i) => result.push(v + i), { reversed: true });
expect(result).toEqual([6, 6, 6, 6, 6, 6]);

result = [];
list6_2.forEach((v, i) => result.push(v + i), { reversed: true });
expect(result).toEqual([6, 6, 6, 6, 6, 6]);

const onlyFirst = (v: number, i: number, halt: () => void) => {
halt();
result.push(v + i);
};

result = [];
listEmpty.forEach(onlyFirst, { reversed: true });
expect(result).toEqual([]);

result = [];
list3_1.forEach(onlyFirst, { reversed: true });
expect(result).toEqual([3]);

result = [];
list3_2.forEach(onlyFirst, { reversed: true });
expect(result).toEqual([3]);

result = [];
list6_1.forEach(onlyFirst, { reversed: true });
expect(result).toEqual([6]);

result = [];
list6_2.forEach(onlyFirst, { reversed: true });
expect(result).toEqual([6]);
});

it('map', () => {
expect(listEmpty.map((v, i) => v + i)).toBe(listEmpty);
expect(list3_1.map((v, i) => v + i).toArray()).toEqual([1, 3, 5]);
Expand Down
14 changes: 14 additions & 0 deletions packages/list/test/nonleaf-block-builder.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ describe('NonLeafBlockBuilder', () => {

cb.mockReset();

b.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(8);
expect(cb.mock.calls[1][0]).toBe(13);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach((_, __, halt) => {
halt();
cb();
Expand All @@ -239,6 +246,13 @@ describe('NonLeafBlockBuilder', () => {

cb.mockReset();

b.forEach(cb, { reversed: true });
expect(cb).toBeCalledTimes(7);
expect(cb.mock.calls[1][0]).toBe(13);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

b.forEach((_, __, halt) => {
halt();
cb();
Expand Down
11 changes: 9 additions & 2 deletions packages/list/test/nonleaf-block.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,15 @@ describe('NonLeafBlock', () => {
const cb = vi.fn();
nl.forEach(cb, { reversed: false, state: TraverseState() });
expect(cb).toBeCalledTimes(9);
expect(cb.mock.calls[1][0]).toBe(2);
expect(cb.mock.calls[1][1]).toBe(1);
expect(cb.mock.calls[2][0]).toBe(3);
expect(cb.mock.calls[2][1]).toBe(2);

cb.mockReset();

nl.forEach(cb, { reversed: true, state: TraverseState() });
expect(cb).toBeCalledTimes(9);
expect(cb.mock.calls[2][0]).toBe(1);
expect(cb.mock.calls[2][1]).toBe(2);

cb.mockReset();

Expand Down
7 changes: 7 additions & 0 deletions packages/list/test/nonleaf-tree.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ describe('NonLeafTree', () => {

cb.mockReset();

t.forEach(cb, { reversed: true, state: TraverseState() });
expect(cb).toBeCalledTimes(36);
expect(cb.mock.calls[1][0]).toBe(8);
expect(cb.mock.calls[1][1]).toBe(1);

cb.mockReset();

t.forEach(
(_, __, halt) => {
halt();
Expand Down

0 comments on commit 3e392e5

Please sign in to comment.