Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Fix resolvesNext not accepting sync iterable and improve examples (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune authored Nov 24, 2021
1 parent be8293c commit 9769c74
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 364 deletions.
303 changes: 126 additions & 177 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function returnsNext<T>(
}

export function resolvesNext<T>(
iterable: AsyncIterable<T>,
iterable: Iterable<T> | AsyncIterable<T>,
// deno-lint-ignore no-explicit-any
): (...args: any[]) => Promise<T | void> {
const gen = (async function* returnsValue() {
Expand Down
105 changes: 99 additions & 6 deletions callbacks_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Deno.test("returnsArgs", () => {
assertEquals(callback("d", "e", "f", "g"), ["e", "f"]);
});

Deno.test("returnsNext array", () => {
Deno.test("returnsNext with array", () => {
let results: number[] = [1, 2, 3];
let callback = returnsNext(results);
assertEquals(callback(), 1);
Expand All @@ -66,20 +66,113 @@ Deno.test("returnsNext array", () => {
assertEquals(callback(), undefined);
});

Deno.test("resolvesNext array", async () => {
Deno.test("returnsNext with iterator", () => {
let results: number[] = [1, 2, 3];
const asyncIterator = async function* () {
await delay(0);
let callback = returnsNext(results.values());
assertEquals(callback(), 1);
assertEquals(callback(), 2);
assertEquals(callback(), 3);
assertEquals(callback(), undefined);

results = [];
callback = returnsNext(results.values());
results.push(1, 2, 3);
assertEquals(callback(), 1);
assertEquals(callback(), 2);
assertEquals(callback(), 3);
results.push(4);
assertEquals(callback(), 4);
assertEquals(callback(), undefined);
results.push(5);
assertEquals(callback(), undefined);
});

Deno.test("returnsNext with generator", () => {
let results: number[] = [1, 2, 3];
const generator = function* () {
yield* results;
};
let callback = resolvesNext(asyncIterator());
let callback = returnsNext(generator());
assertEquals(callback(), 1);
assertEquals(callback(), 2);
assertEquals(callback(), 3);
assertEquals(callback(), undefined);

results = [];
callback = returnsNext(generator());
results.push(1, 2, 3);
assertEquals(callback(), 1);
assertEquals(callback(), 2);
assertEquals(callback(), 3);
results.push(4);
assertEquals(callback(), 4);
assertEquals(callback(), undefined);
results.push(5);
assertEquals(callback(), undefined);
});

Deno.test("resolvesNext with array", async () => {
let results: number[] = [1, 2, 3];
let callback = resolvesNext(results);
const value = callback();
assertEquals(Promise.resolve(value), value);
assertEquals(await value, 1);
assertEquals(await callback(), 2);
assertEquals(await callback(), 3);
assertEquals(await callback(), undefined);

results = [];
callback = resolvesNext(results);
results.push(1, 2, 3);
assertEquals(await callback(), 1);
assertEquals(await callback(), 2);
assertEquals(await callback(), 3);
results.push(4);
assertEquals(await callback(), 4);
assertEquals(await callback(), undefined);
results.push(5);
assertEquals(await callback(), undefined);
});

Deno.test("resolvesNext with iterator", async () => {
let results: number[] = [1, 2, 3];
let callback = resolvesNext(results.values());
const value = callback();
assertEquals(Promise.resolve(value), value);
assertEquals(await value, 1);
assertEquals(await callback(), 2);
assertEquals(await callback(), 3);
assertEquals(await callback(), undefined);

results = [];
callback = resolvesNext(results.values());
results.push(1, 2, 3);
assertEquals(await callback(), 1);
assertEquals(await callback(), 2);
assertEquals(await callback(), 3);
results.push(4);
assertEquals(await callback(), 4);
assertEquals(await callback(), undefined);
results.push(5);
assertEquals(await callback(), undefined);
});

Deno.test("resolvesNext with async generator", async () => {
let results: number[] = [1, 2, 3];
const asyncGenerator = async function* () {
await delay(0);
yield* results;
};
let callback = resolvesNext(asyncGenerator());
const value = callback();
assertEquals(Promise.resolve(value), value);
assertEquals(await value, 1);
assertEquals(await callback(), 2);
assertEquals(await callback(), 3);
assertEquals(await callback(), undefined);

results = [];
callback = resolvesNext(asyncIterator());
callback = resolvesNext(asyncGenerator());
results.push(1, 2, 3);
assertEquals(await callback(), 1);
assertEquals(await callback(), 2);
Expand Down
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { delay } from "https://deno.land/std@0.114.0/async/delay.ts";
export type { DelayOptions } from "https://deno.land/std@0.114.0/async/delay.ts";
export { delay } from "https://deno.land/std@0.115.1/async/delay.ts";
export type { DelayOptions } from "https://deno.land/std@0.115.1/async/delay.ts";

export {
assert,
Expand All @@ -10,7 +10,7 @@ export {
assertRejects,
assertStrictEquals,
assertThrows,
} from "https://deno.land/std@0.114.0/testing/asserts.ts";
} from "https://deno.land/std@0.115.1/testing/asserts.ts";

export { RBTree } from "https://deno.land/x/[email protected]/trees/rb_tree.ts";
export { ascend } from "https://deno.land/x/[email protected]/comparators.ts";
Expand Down
3 changes: 1 addition & 2 deletions examples/spy_default_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertEquals } from "../deps.ts";
import { assertSpyCall } from "../asserts.ts";
import { Spy, spy } from "../spy.ts";
import { assertSpyCall, Spy, spy } from "../mod.ts";

function add(
a: number,
Expand Down
13 changes: 6 additions & 7 deletions examples/spy_function_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "../deps.ts";
import { Spy, spy } from "../spy.ts";
import { assertSpyCall, assertSpyCalls, Spy, spy } from "../mod.ts";

function filter<T>(values: T[], callback: (value: T) => boolean): T[] {
return values.filter(callback);
Expand All @@ -14,10 +14,9 @@ Deno.test("calls real callback", () => {
const values: number[] = [5, 6, 7, 8];

assertEquals(filter(values, callback), [6, 8]);
assertEquals(callback.calls, [
{ args: [5, 0, values], returned: false },
{ args: [6, 1, values], returned: true },
{ args: [7, 2, values], returned: false },
{ args: [8, 3, values], returned: true },
]);
assertSpyCall(callback, 0, { args: [5, 0, values], returned: false });
assertSpyCall(callback, 1, { args: [6, 1, values], returned: true });
assertSpyCall(callback, 2, { args: [7, 2, values], returned: false });
assertSpyCall(callback, 3, { args: [8, 3, values], returned: true });
assertSpyCalls(callback, 4);
});
46 changes: 23 additions & 23 deletions examples/spy_instance_method_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "../deps.ts";
import { Spy, spy } from "../spy.ts";
import { assertSpyCall, assertSpyCalls, Spy, spy } from "../mod.ts";

class Database {
// deno-lint-ignore no-explicit-any
Expand All @@ -16,6 +16,7 @@ class Database {
},
};
}

// deno-lint-ignore no-explicit-any
query(query: string, params: any[]): any[][] {
return this.queries[query][params[0]]; // implementation not important for example
Expand Down Expand Up @@ -46,31 +47,30 @@ Deno.test("functions call db.query", () => {

try {
assertEquals(getNamesByFirstName(db, "Jane"), ["Jane Doe", "Jane Smith"]);
assertSpyCall(query, 0, {
args: ["select id, last_name from USERS where first_name=?", ["Jane"]],
self: db,
returned: [[1, "Doe"], [3, "Smith"]],
});
assertEquals(getNamesByLastName(db, "Doe"), ["Jane Doe", "John Doe"]);
assertSpyCall(query, 1, {
args: ["select id, first_name from USERS where last_name=?", ["Doe"]],
self: db,
returned: [[1, "Jane"], [2, "John"]],
});
assertEquals(getNamesByFirstName(db, "John"), ["John Doe"]);
assertSpyCall(query, 2, {
args: ["select id, last_name from USERS where first_name=?", ["John"]],
self: db,
returned: [[2, "Doe"]],
});
assertEquals(getNamesByLastName(db, "Smith"), ["Jane Smith"]);
assertEquals(query.calls, [
{
args: ["select id, last_name from USERS where first_name=?", ["Jane"]],
self: db,
returned: [[1, "Doe"], [3, "Smith"]],
},
{
args: ["select id, first_name from USERS where last_name=?", ["Doe"]],
self: db,
returned: [[1, "Jane"], [2, "John"]],
},
{
args: ["select id, last_name from USERS where first_name=?", ["John"]],
self: db,
returned: [[2, "Doe"]],
},
{
args: ["select id, first_name from USERS where last_name=?", ["Smith"]],
self: db,
returned: [[3, "Jane"]],
},
]);
assertSpyCall(query, 3, {
args: ["select id, first_name from USERS where last_name=?", ["Smith"]],
self: db,
returned: [[3, "Jane"]],
});
assertSpyCalls(query, 4);
} finally {
query.restore();
}
Expand Down
69 changes: 0 additions & 69 deletions examples/stub_function_test.ts

This file was deleted.

76 changes: 76 additions & 0 deletions examples/stub_instance_method_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { assertEquals } from "../deps.ts";
import {
assertSpyCallAsync,
assertSpyCalls,
resolvesNext,
Stub,
stub,
} from "../mod.ts";

class Database {
query(_query: string, _params: unknown[]): Promise<unknown[][]> {
throw new Error("unimplemented");
}
}

async function getUsers(
db: Database,
lastName: string,
firstName?: string,
): Promise<string[]> {
return (await db
.query(
"SELECT id, username FROM users WHERE last_name=?" +
(firstName ? " and first_name=?" : ""),
firstName ? [lastName, firstName] : [lastName],
))
.map((row) => `${row[0]} ${row[1]}`);
}

Deno.test("getUsers", async () => {
const db: Database = new Database();
const resolves: [number, string][][] = [
[[1, "jd"], [2, "johnd"], [3, "janedoe"]],
[[2, "johnd"]],
];
const query: Stub<Database> = stub(db, "query", resolvesNext(resolves));

try {
assertEquals(await getUsers(db, "doe"), ["1 jd", "2 johnd", "3 janedoe"]);
assertEquals(await getUsers(db, "doe", "john"), ["2 johnd"]);

resolves.push([[3, "janedoe"]]);
assertEquals(await getUsers(db, "doe"), ["3 janedoe"]);

await assertSpyCallAsync(query, 0, {
args: [
"SELECT id, username FROM users WHERE last_name=?",
["doe"],
],
self: db,
returned: [[1, "jd"], [2, "johnd"], [3, "janedoe"]],
});

await assertSpyCallAsync(query, 1, {
args: [
"SELECT id, username FROM users WHERE last_name=? and first_name=?",
["doe", "john"],
],
self: db,
returned: [[2, "johnd"]],
});

await assertSpyCallAsync(query, 2, {
args: [
"SELECT id, username FROM users WHERE last_name=?",
["doe"],
],
self: db,
returned: [[3, "janedoe"]],
});

assertSpyCalls(query, 3);
} finally {
query.restore();
}
});
Loading

0 comments on commit 9769c74

Please sign in to comment.