-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner.js
71 lines (58 loc) · 1.5 KB
/
runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module.exports = class Runner {
constructor(it, steps, deepEqual) {
this.it = it;
this.steps = steps;
this.deepEqual = deepEqual;
this.results = [];
this.isDone = false;
}
run() {
const { it, steps } = this;
steps.reduce((prevResult, step) => {
if (this.isDone) throwExtraStep(step);
let output;
try {
output = runStep(it, step, prevResult);
runAssertions(step, output, this.deepEqual);
} catch (err) {
err.stack = `${err.message}\n${step.stack}`;
throw err;
}
if (output.done) this.isDone = true;
this.results.push(output);
return step.result;
}, null);
return this.results;
}
}
const runStep = (it, step, prevResult) => {
if (step.error) {
try {
return it.throw(step.error);
} catch (error) {
return {
errorThrown: error,
};
}
}
return it.next(prevResult);
}
const runAssertions = (step, output, deepEqual) => {
if (step.expectedThrow) {
deepEqual(step.error, output.errorThrown);
} else if (output.errorThrown) {
throw output.errorThrown;
}
if (step.expectedDone) {
if (output.done !== true) throwExpectFinish(step);
}
if (step.expectedValue) {
deepEqual(step.expectedValue, output.value);
}
};
const throwExpectFinish = () => {
throw new Error('Expected the generator to finish but it has more step(s) left.');
};
const throwExtraStep = () => {
throw new Error('Too many steps were provided for the generator');
};