-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
ReplayCommands.spec.ts
50 lines (45 loc) · 1.88 KB
/
ReplayCommands.spec.ts
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
import * as fc from '../../src/fast-check';
// Fake commands
type Model = { counter: number };
type Real = {};
class IncBy implements fc.Command<Model, Real> {
constructor(readonly v: number) {}
check = (m: Readonly<Model>) => true;
run = (m: Model, r: Real) => (m.counter += this.v);
toString = () => `IncBy(${this.v})`;
}
class DecPosBy implements fc.Command<Model, Real> {
constructor(readonly v: number) {}
check = (m: Readonly<Model>) => m.counter > 0;
run = (m: Model, r: Real) => (m.counter -= this.v);
toString = () => `DecPosBy(${this.v})`;
}
class AlwaysPos implements fc.Command<Model, Real> {
check = (m: Readonly<Model>) => true;
run = (m: Model, r: Real) => {
if (m.counter < 0) throw new Error('counter is supposed to be always greater or equal to zero');
};
toString = () => `AlwaysPos()`;
}
const seed = Date.now();
describe(`ReplayCommands (seed: ${seed})`, () => {
it('Should be able to replay commands by specifying replayPath in fc.commands', () => {
const buildProp = (replayPath?: string) => {
return fc.property(
fc.commands(
[fc.nat().map(v => new IncBy(v)), fc.nat().map(v => new DecPosBy(v)), fc.constant(new AlwaysPos())],
{ replayPath }
),
cmds => fc.modelRun(() => ({ model: { counter: 0 }, real: {} }), cmds)
);
};
const out = fc.check(buildProp(), { seed: seed });
expect(out.failed).toBe(true);
const path = out.counterexamplePath!;
const replayPath = /\/\*replayPath=['"](.*)['"]\*\//.exec(out.counterexample![0].toString())![1];
const outReplayed = fc.check(buildProp(replayPath), { seed, path });
expect(outReplayed.counterexamplePath).toEqual(out.counterexamplePath);
expect(outReplayed.counterexample![0].toString()).toEqual(out.counterexample![0].toString());
expect(outReplayed.numRuns).toEqual(1);
});
});