-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaying previously executed runs is a must have feature for property based testing frameworks. Because of the specificities of commands, commands were not eligible to replay. This commit adds the replay capabilities to commands by specifying an extra parameter when defining them (replayPath). Please note that commands arbitraries should not be shared accross multiple runs. Related to #251
- Loading branch information
Showing
9 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @hidden */ | ||
export class ReplayPath { | ||
static parse(replayPathStr: string): boolean[] { | ||
return [...replayPathStr].map(v => v === '1'); | ||
} | ||
static stringify(replayPath: boolean[]): string { | ||
return replayPath.map(s => (s ? '1' : '0')).join(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface CommandsSettings { | ||
maxCommands?: number; | ||
disableReplayLog?: boolean; | ||
replayPath?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as fc from '../../../../lib/fast-check'; | ||
|
||
import { ReplayPath } from '../../../../src/check/model/ReplayPath'; | ||
|
||
describe('ReplayPath', () => { | ||
it('Should be able to read back itself', () => | ||
fc.assert( | ||
fc.property(fc.array(fc.boolean(), 0, 1000), (replayPath: boolean[]) => { | ||
expect(ReplayPath.parse(ReplayPath.stringify(replayPath))).toEqual(replayPath); | ||
}) | ||
)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters