Skip to content

Commit df835ba

Browse files
committed
Clarify tests
This commit factors out details and adds comments by applying the four-phase testing pattern to increase the readability and understanding of the tests. Signed-off-by: Frederik Krautwald <[email protected]>
1 parent ffc5ce7 commit df835ba

File tree

1 file changed

+122
-62
lines changed

1 file changed

+122
-62
lines changed

src/index.__test__.ts

Lines changed: 122 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import {
1010
import { constant, periodic, runEffects, take, tap } from "@most/core";
1111
import { newDefaultScheduler } from "@most/scheduler";
1212
import { Stream } from "@most/types";
13-
import { describe, it } from "@typed/test";
13+
import { describe, it, given } from "@typed/test";
14+
15+
const TEST_EVENT_VALUE = "test";
1416

1517
const collect = async <A>(n: number, s: Stream<A>): Promise<A[]> => {
16-
const eventValues: A[] = [];
17-
const collectStream = tap(x => eventValues.push(x), s);
18-
await runEffects(take(n, collectStream), newDefaultScheduler());
19-
return eventValues;
18+
const xs: A[] = [];
19+
const sa = tap(x => xs.push(x), s);
20+
await runEffects(take(n, sa), newDefaultScheduler());
21+
return xs;
2022
};
2123

2224
const range = (start: number, n: number): number[] =>
@@ -26,79 +28,137 @@ const randomInt = (min: number, max: number) =>
2628
min + Math.floor(Math.random() * (max - min));
2729

2830
export const indexTests = describe("index", [
29-
it("replaces events with 0-based index", async ({ equal }) => {
30-
const n = randomInt(10, 20);
31-
const events = await collect(n, index(periodic(1)));
32-
equal(range(0, n), events);
33-
})
31+
given("a stream", [
32+
it("replaces events with 0-based index", async ({ equal }) => {
33+
// Fixture setup
34+
const s = periodic(1);
35+
const n = randomInt(10, 20);
36+
const expectedEvents = range(0, n);
37+
// Exercise system
38+
const result = index(s);
39+
// Verify outcome
40+
const actualEvents = await collect(n, result);
41+
equal(expectedEvents, actualEvents);
42+
})
43+
])
3444
]);
3545

3646
export const withIndexTests = describe("withIndex", [
37-
it("pairs events with 0-based count", async ({ equal }) => {
38-
const n = randomInt(10, 20);
39-
const events = await collect(n, withIndex(constant("test", periodic(1))));
40-
equal(range(0, n).map(x => [x, "test"]), events);
41-
})
47+
given("a stream", [
48+
it("pairs events with 0-based count", async ({ equal }) => {
49+
// Fixture setup
50+
const s = constant(TEST_EVENT_VALUE, periodic(1));
51+
const n = randomInt(10, 20);
52+
const expectedEvents = range(0, n).map<[number, string]>(n => [
53+
n,
54+
TEST_EVENT_VALUE
55+
]);
56+
// Exercise system
57+
const result = withIndex(s);
58+
// Verify outcome
59+
const actualEvents = await collect(n, result);
60+
equal(expectedEvents, actualEvents);
61+
})
62+
])
4263
]);
4364

4465
export const countTests = describe("count", [
45-
it("replaces events with 1-based count", async ({ equal }) => {
46-
const n = randomInt(10, 20);
47-
const events = await collect(n, count(periodic(1)));
48-
equal(range(1, n), events);
49-
})
66+
given("a stream", [
67+
it("replaces events with 1-based count", async ({ equal }) => {
68+
// Fixture setup
69+
const s = periodic(1);
70+
const n = randomInt(10, 20);
71+
const expectedEvents = range(1, n);
72+
// Exercise system
73+
const result = count(s);
74+
// Verify outcome
75+
const actualEvents = await collect(n, result);
76+
equal(expectedEvents, actualEvents);
77+
})
78+
])
5079
]);
5180

5281
export const withCountTests = describe("withCount", [
53-
it("pairs events with 1-based count", async ({ equal }) => {
54-
const n = randomInt(10, 20);
55-
const events = await collect(n, withCount(constant("test", periodic(1))));
56-
equal(range(1, n).map(x => [x, "test"]), events);
57-
})
82+
given("a stream", [
83+
it("pairs events with 1-based count", async ({ equal }) => {
84+
// Fixture setup
85+
const s = constant(TEST_EVENT_VALUE, periodic(1));
86+
const n = randomInt(10, 20);
87+
const expectedEvents = range(1, n).map<[number, string]>(n => [
88+
n,
89+
TEST_EVENT_VALUE
90+
]);
91+
// Exercise system
92+
const result = withCount(s);
93+
// Verify outcome
94+
const actualEvents = await collect(n, result);
95+
equal(expectedEvents, actualEvents);
96+
})
97+
])
5898
]);
5999

60100
export const withIndexStartTests = describe("withIndexStart", [
61-
it("pairs events with start-based index", async ({ equal }) => {
62-
const start = randomInt(0, 10000);
63-
const n = randomInt(10, 20);
64-
const events = await collect(
65-
n,
66-
withIndexStart(start, constant("test", periodic(1)))
67-
);
68-
equal(range(start, n).map(x => [x, "test"]), events);
69-
})
101+
given("a start index and a stream", [
102+
it("pairs events with start-based index", async ({ equal }) => {
103+
// Fixture setup
104+
const idx = randomInt(0, 10000);
105+
const s = constant(TEST_EVENT_VALUE, periodic(1));
106+
const n = randomInt(10, 20);
107+
const expectedEvents = range(idx, n).map<[number, string]>(n => [
108+
n,
109+
TEST_EVENT_VALUE
110+
]);
111+
// Exercise system
112+
const result = withIndexStart(idx, s);
113+
// Verify outcome
114+
const actualEvents = await collect(n, result);
115+
equal(expectedEvents, actualEvents);
116+
})
117+
])
70118
]);
71119

72120
export const indexedTests = describe("indexed", [
73-
it("pairs events with computed index", async ({ equal }) => {
74-
const n = randomInt(10, 20);
75-
76-
const s = Array(n)
77-
.fill("a")
78-
.join("");
79-
const expected = range(0, n).map((_, i) => [s.slice(0, i), "test"]);
80-
81-
const stringIndex = (s: string) => (prev: string): [string, string] => [
82-
prev,
83-
prev + s
84-
];
85-
86-
const events = await collect(
87-
n,
88-
indexed(stringIndex("a"), "", constant("test", periodic(1)))
89-
);
90-
equal(expected, events);
91-
})
121+
given("a function, an initial value, and a stream", [
122+
it("should pair events with computed index", async ({ equal }) => {
123+
// Fixture setup
124+
const n = randomInt(10, 20);
125+
const arbitraryChar = "a";
126+
const strOfLenN = Array(n)
127+
.fill(arbitraryChar)
128+
.join("");
129+
const stringIndex = (s: string) => (prev: string): [string, string] => [
130+
prev,
131+
prev + s
132+
];
133+
const f = stringIndex(arbitraryChar);
134+
const init = "";
135+
const s = constant(TEST_EVENT_VALUE, periodic(1));
136+
const expectedEvents = range(0, n).map<[string, string]>((_, i) => [
137+
strOfLenN.slice(0, i),
138+
TEST_EVENT_VALUE
139+
]);
140+
// Exercise system
141+
const result = indexed(f, init, s);
142+
// Verify outcome
143+
const actualEvents = await collect(n, result);
144+
equal(expectedEvents, actualEvents);
145+
})
146+
])
92147
]);
93148

94149
export const keepIndexTests = describe("keepIndex", [
95-
it("keeps index and discards value", async ({ equal }) => {
96-
const start = randomInt(0, 10000);
97-
const n = randomInt(10, 20);
98-
const events = await collect(
99-
n,
100-
keepIndex(withIndexStart(start, constant("test", periodic(1))))
101-
);
102-
equal(range(start, n), events);
103-
})
150+
given("a stream of an [index, value] pair", [
151+
it("should keep index and discard value", async ({ equal }) => {
152+
// Fixture setup
153+
const idx = randomInt(0, 10000);
154+
const s = withIndexStart(idx, constant(TEST_EVENT_VALUE, periodic(1)));
155+
const n = randomInt(10, 20);
156+
const expectedEvents = range(idx, n);
157+
// Exercise system
158+
const result = keepIndex(s);
159+
// Verify outcome
160+
const actualEvents = await collect(n, result);
161+
equal(expectedEvents, actualEvents);
162+
})
163+
])
104164
]);

0 commit comments

Comments
 (0)