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

Commit 9769c74

Browse files
authored
Fix resolvesNext not accepting sync iterable and improve examples (#26)
1 parent be8293c commit 9769c74

11 files changed

+340
-364
lines changed

README.md

Lines changed: 126 additions & 177 deletions
Large diffs are not rendered by default.

callbacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function returnsNext<T>(
4040
}
4141

4242
export function resolvesNext<T>(
43-
iterable: AsyncIterable<T>,
43+
iterable: Iterable<T> | AsyncIterable<T>,
4444
// deno-lint-ignore no-explicit-any
4545
): (...args: any[]) => Promise<T | void> {
4646
const gen = (async function* returnsValue() {

callbacks_test.ts

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Deno.test("returnsArgs", () => {
4545
assertEquals(callback("d", "e", "f", "g"), ["e", "f"]);
4646
});
4747

48-
Deno.test("returnsNext array", () => {
48+
Deno.test("returnsNext with array", () => {
4949
let results: number[] = [1, 2, 3];
5050
let callback = returnsNext(results);
5151
assertEquals(callback(), 1);
@@ -66,20 +66,113 @@ Deno.test("returnsNext array", () => {
6666
assertEquals(callback(), undefined);
6767
});
6868

69-
Deno.test("resolvesNext array", async () => {
69+
Deno.test("returnsNext with iterator", () => {
7070
let results: number[] = [1, 2, 3];
71-
const asyncIterator = async function* () {
72-
await delay(0);
71+
let callback = returnsNext(results.values());
72+
assertEquals(callback(), 1);
73+
assertEquals(callback(), 2);
74+
assertEquals(callback(), 3);
75+
assertEquals(callback(), undefined);
76+
77+
results = [];
78+
callback = returnsNext(results.values());
79+
results.push(1, 2, 3);
80+
assertEquals(callback(), 1);
81+
assertEquals(callback(), 2);
82+
assertEquals(callback(), 3);
83+
results.push(4);
84+
assertEquals(callback(), 4);
85+
assertEquals(callback(), undefined);
86+
results.push(5);
87+
assertEquals(callback(), undefined);
88+
});
89+
90+
Deno.test("returnsNext with generator", () => {
91+
let results: number[] = [1, 2, 3];
92+
const generator = function* () {
7393
yield* results;
7494
};
75-
let callback = resolvesNext(asyncIterator());
95+
let callback = returnsNext(generator());
96+
assertEquals(callback(), 1);
97+
assertEquals(callback(), 2);
98+
assertEquals(callback(), 3);
99+
assertEquals(callback(), undefined);
100+
101+
results = [];
102+
callback = returnsNext(generator());
103+
results.push(1, 2, 3);
104+
assertEquals(callback(), 1);
105+
assertEquals(callback(), 2);
106+
assertEquals(callback(), 3);
107+
results.push(4);
108+
assertEquals(callback(), 4);
109+
assertEquals(callback(), undefined);
110+
results.push(5);
111+
assertEquals(callback(), undefined);
112+
});
113+
114+
Deno.test("resolvesNext with array", async () => {
115+
let results: number[] = [1, 2, 3];
116+
let callback = resolvesNext(results);
117+
const value = callback();
118+
assertEquals(Promise.resolve(value), value);
119+
assertEquals(await value, 1);
120+
assertEquals(await callback(), 2);
121+
assertEquals(await callback(), 3);
122+
assertEquals(await callback(), undefined);
123+
124+
results = [];
125+
callback = resolvesNext(results);
126+
results.push(1, 2, 3);
127+
assertEquals(await callback(), 1);
128+
assertEquals(await callback(), 2);
129+
assertEquals(await callback(), 3);
130+
results.push(4);
131+
assertEquals(await callback(), 4);
132+
assertEquals(await callback(), undefined);
133+
results.push(5);
134+
assertEquals(await callback(), undefined);
135+
});
136+
137+
Deno.test("resolvesNext with iterator", async () => {
138+
let results: number[] = [1, 2, 3];
139+
let callback = resolvesNext(results.values());
140+
const value = callback();
141+
assertEquals(Promise.resolve(value), value);
142+
assertEquals(await value, 1);
143+
assertEquals(await callback(), 2);
144+
assertEquals(await callback(), 3);
145+
assertEquals(await callback(), undefined);
146+
147+
results = [];
148+
callback = resolvesNext(results.values());
149+
results.push(1, 2, 3);
76150
assertEquals(await callback(), 1);
77151
assertEquals(await callback(), 2);
78152
assertEquals(await callback(), 3);
153+
results.push(4);
154+
assertEquals(await callback(), 4);
155+
assertEquals(await callback(), undefined);
156+
results.push(5);
157+
assertEquals(await callback(), undefined);
158+
});
159+
160+
Deno.test("resolvesNext with async generator", async () => {
161+
let results: number[] = [1, 2, 3];
162+
const asyncGenerator = async function* () {
163+
await delay(0);
164+
yield* results;
165+
};
166+
let callback = resolvesNext(asyncGenerator());
167+
const value = callback();
168+
assertEquals(Promise.resolve(value), value);
169+
assertEquals(await value, 1);
170+
assertEquals(await callback(), 2);
171+
assertEquals(await callback(), 3);
79172
assertEquals(await callback(), undefined);
80173

81174
results = [];
82-
callback = resolvesNext(asyncIterator());
175+
callback = resolvesNext(asyncGenerator());
83176
results.push(1, 2, 3);
84177
assertEquals(await callback(), 1);
85178
assertEquals(await callback(), 2);

deps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { delay } from "https://deno.land/std@0.114.0/async/delay.ts";
2-
export type { DelayOptions } from "https://deno.land/std@0.114.0/async/delay.ts";
1+
export { delay } from "https://deno.land/std@0.115.1/async/delay.ts";
2+
export type { DelayOptions } from "https://deno.land/std@0.115.1/async/delay.ts";
33

44
export {
55
assert,
@@ -10,7 +10,7 @@ export {
1010
assertRejects,
1111
assertStrictEquals,
1212
assertThrows,
13-
} from "https://deno.land/std@0.114.0/testing/asserts.ts";
13+
} from "https://deno.land/std@0.115.1/testing/asserts.ts";
1414

1515
export { RBTree } from "https://deno.land/x/[email protected]/trees/rb_tree.ts";
1616
export { ascend } from "https://deno.land/x/[email protected]/comparators.ts";

examples/spy_default_test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assertEquals } from "../deps.ts";
2-
import { assertSpyCall } from "../asserts.ts";
3-
import { Spy, spy } from "../spy.ts";
2+
import { assertSpyCall, Spy, spy } from "../mod.ts";
43

54
function add(
65
a: number,

examples/spy_function_test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "../deps.ts";
2-
import { Spy, spy } from "../spy.ts";
2+
import { assertSpyCall, assertSpyCalls, Spy, spy } from "../mod.ts";
33

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

1616
assertEquals(filter(values, callback), [6, 8]);
17-
assertEquals(callback.calls, [
18-
{ args: [5, 0, values], returned: false },
19-
{ args: [6, 1, values], returned: true },
20-
{ args: [7, 2, values], returned: false },
21-
{ args: [8, 3, values], returned: true },
22-
]);
17+
assertSpyCall(callback, 0, { args: [5, 0, values], returned: false });
18+
assertSpyCall(callback, 1, { args: [6, 1, values], returned: true });
19+
assertSpyCall(callback, 2, { args: [7, 2, values], returned: false });
20+
assertSpyCall(callback, 3, { args: [8, 3, values], returned: true });
21+
assertSpyCalls(callback, 4);
2322
});

examples/spy_instance_method_test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "../deps.ts";
2-
import { Spy, spy } from "../spy.ts";
2+
import { assertSpyCall, assertSpyCalls, Spy, spy } from "../mod.ts";
33

44
class Database {
55
// deno-lint-ignore no-explicit-any
@@ -16,6 +16,7 @@ class Database {
1616
},
1717
};
1818
}
19+
1920
// deno-lint-ignore no-explicit-any
2021
query(query: string, params: any[]): any[][] {
2122
return this.queries[query][params[0]]; // implementation not important for example
@@ -46,31 +47,30 @@ Deno.test("functions call db.query", () => {
4647

4748
try {
4849
assertEquals(getNamesByFirstName(db, "Jane"), ["Jane Doe", "Jane Smith"]);
50+
assertSpyCall(query, 0, {
51+
args: ["select id, last_name from USERS where first_name=?", ["Jane"]],
52+
self: db,
53+
returned: [[1, "Doe"], [3, "Smith"]],
54+
});
4955
assertEquals(getNamesByLastName(db, "Doe"), ["Jane Doe", "John Doe"]);
56+
assertSpyCall(query, 1, {
57+
args: ["select id, first_name from USERS where last_name=?", ["Doe"]],
58+
self: db,
59+
returned: [[1, "Jane"], [2, "John"]],
60+
});
5061
assertEquals(getNamesByFirstName(db, "John"), ["John Doe"]);
62+
assertSpyCall(query, 2, {
63+
args: ["select id, last_name from USERS where first_name=?", ["John"]],
64+
self: db,
65+
returned: [[2, "Doe"]],
66+
});
5167
assertEquals(getNamesByLastName(db, "Smith"), ["Jane Smith"]);
52-
assertEquals(query.calls, [
53-
{
54-
args: ["select id, last_name from USERS where first_name=?", ["Jane"]],
55-
self: db,
56-
returned: [[1, "Doe"], [3, "Smith"]],
57-
},
58-
{
59-
args: ["select id, first_name from USERS where last_name=?", ["Doe"]],
60-
self: db,
61-
returned: [[1, "Jane"], [2, "John"]],
62-
},
63-
{
64-
args: ["select id, last_name from USERS where first_name=?", ["John"]],
65-
self: db,
66-
returned: [[2, "Doe"]],
67-
},
68-
{
69-
args: ["select id, first_name from USERS where last_name=?", ["Smith"]],
70-
self: db,
71-
returned: [[3, "Jane"]],
72-
},
73-
]);
68+
assertSpyCall(query, 3, {
69+
args: ["select id, first_name from USERS where last_name=?", ["Smith"]],
70+
self: db,
71+
returned: [[3, "Jane"]],
72+
});
73+
assertSpyCalls(query, 4);
7474
} finally {
7575
query.restore();
7676
}

examples/stub_function_test.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

examples/stub_instance_method_test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { assertEquals } from "../deps.ts";
2+
import {
3+
assertSpyCallAsync,
4+
assertSpyCalls,
5+
resolvesNext,
6+
Stub,
7+
stub,
8+
} from "../mod.ts";
9+
10+
class Database {
11+
query(_query: string, _params: unknown[]): Promise<unknown[][]> {
12+
throw new Error("unimplemented");
13+
}
14+
}
15+
16+
async function getUsers(
17+
db: Database,
18+
lastName: string,
19+
firstName?: string,
20+
): Promise<string[]> {
21+
return (await db
22+
.query(
23+
"SELECT id, username FROM users WHERE last_name=?" +
24+
(firstName ? " and first_name=?" : ""),
25+
firstName ? [lastName, firstName] : [lastName],
26+
))
27+
.map((row) => `${row[0]} ${row[1]}`);
28+
}
29+
30+
Deno.test("getUsers", async () => {
31+
const db: Database = new Database();
32+
const resolves: [number, string][][] = [
33+
[[1, "jd"], [2, "johnd"], [3, "janedoe"]],
34+
[[2, "johnd"]],
35+
];
36+
const query: Stub<Database> = stub(db, "query", resolvesNext(resolves));
37+
38+
try {
39+
assertEquals(await getUsers(db, "doe"), ["1 jd", "2 johnd", "3 janedoe"]);
40+
assertEquals(await getUsers(db, "doe", "john"), ["2 johnd"]);
41+
42+
resolves.push([[3, "janedoe"]]);
43+
assertEquals(await getUsers(db, "doe"), ["3 janedoe"]);
44+
45+
await assertSpyCallAsync(query, 0, {
46+
args: [
47+
"SELECT id, username FROM users WHERE last_name=?",
48+
["doe"],
49+
],
50+
self: db,
51+
returned: [[1, "jd"], [2, "johnd"], [3, "janedoe"]],
52+
});
53+
54+
await assertSpyCallAsync(query, 1, {
55+
args: [
56+
"SELECT id, username FROM users WHERE last_name=? and first_name=?",
57+
["doe", "john"],
58+
],
59+
self: db,
60+
returned: [[2, "johnd"]],
61+
});
62+
63+
await assertSpyCallAsync(query, 2, {
64+
args: [
65+
"SELECT id, username FROM users WHERE last_name=?",
66+
["doe"],
67+
],
68+
self: db,
69+
returned: [[3, "janedoe"]],
70+
});
71+
72+
assertSpyCalls(query, 3);
73+
} finally {
74+
query.restore();
75+
}
76+
});

0 commit comments

Comments
 (0)