Skip to content

Commit

Permalink
Extract RunDetails construction outside of Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jan 30, 2018
1 parent 2d86acd commit 89f7787
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/check/runner/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Shrinkable from '../arbitrary/definition/Shrinkable'
import { RandomGenerator, skip_n } from '../../random/generator/RandomGenerator'
import IProperty from '../property/IProperty'
import toss from './Tosser'
import { Parameters, QualifiedParameters, pretty } from './utils/utils'
import { Parameters, QualifiedParameters, RunDetails, successFor, failureFor, throwIfFailed } from './utils/utils'

function shrinkIt<Ts>(property: IProperty<Ts>, value: Shrinkable<Ts>, error: string, num_shrinks: number = 0): [Ts, number, string] {
for (const v of value.shrink()) {
Expand All @@ -14,7 +14,7 @@ function shrinkIt<Ts>(property: IProperty<Ts>, value: Shrinkable<Ts>, error: str
return [value.value, num_shrinks, error];
}

function check<Ts>(property: IProperty<Ts>, params?: Parameters) {
function check<Ts>(property: IProperty<Ts>, params?: Parameters): RunDetails<Ts> {
const qParams = QualifiedParameters.read(params);
const generator = toss(property, qParams.seed);

Expand All @@ -23,17 +23,15 @@ function check<Ts>(property: IProperty<Ts>, params?: Parameters) {
const out = property.run(g.value);
if (out != null) {
const [shrinkedValue, numShrinks, error] = shrinkIt(property, g, out as string);
return {failed: true, num_runs: idx+1, num_shrinks: numShrinks, seed: qParams.seed, counterexample: shrinkedValue, error: error};
return failureFor(qParams, idx+1, numShrinks, shrinkedValue, error);
}
}
return {failed: false, num_runs: qParams.num_runs, num_shrinks: 0, seed: qParams.seed, counterexample: null, error: null};
return successFor<Ts>(qParams);
}

function assert<Ts>(property: IProperty<Ts>, params?: Parameters) {
const out = check(property, params);
if (out.failed) {
throw `Property failed after ${out.num_runs} tests (seed: ${out.seed}): ${pretty(out.counterexample)}\nGot error: ${out.error}`;
}
throwIfFailed(out);
}

export { check, assert };
24 changes: 23 additions & 1 deletion src/check/runner/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ class QualifiedParameters {
}
}

interface RunDetails<Ts> {
failed: boolean,
num_runs: number,
num_shrinks: number,
seed: number,
counterexample: Ts|null,
error: string|null,
}

function successFor<Ts>(qParams: QualifiedParameters): RunDetails<Ts> {
return {failed: false, num_runs: qParams.num_runs, num_shrinks: 0, seed: qParams.seed, counterexample: null, error: null};
}
function failureFor<Ts>(qParams: QualifiedParameters, num_runs: number, num_shrinks: number, counterexample: Ts, error: string): RunDetails<Ts> {
return {failed: true, num_runs, num_shrinks, seed: qParams.seed, counterexample, error};
}

function prettyOne(value: any): string {
const defaultRepr: string = `${value}`;
if (/^\[object (Object|Null|Undefined)\]$/.exec(defaultRepr) === null)
Expand All @@ -43,4 +59,10 @@ function pretty<Ts>(value: any): string {
return prettyOne(value);
}

export { Parameters, QualifiedParameters, pretty };
function throwIfFailed<Ts>(out: RunDetails<Ts>) {
if (out.failed) {
throw `Property failed after ${out.num_runs} tests (seed: ${out.seed}): ${pretty(out.counterexample)}\nGot error: ${out.error}`;
}
}

export { Parameters, QualifiedParameters, RunDetails, successFor, failureFor, throwIfFailed };
4 changes: 2 additions & 2 deletions src/fast-check.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { check, assert } from './check/runner/Runner'
import { sample, statistics } from './check/runner/Sampler'
import { Parameters } from './check/runner/utils/utils'
import { Parameters, RunDetails } from './check/runner/utils/utils'
import { property } from './check/property/Property'

import Arbitrary from './check/arbitrary/definition/Arbitrary'
Expand Down Expand Up @@ -46,7 +46,7 @@ export {
// extend the framework
Arbitrary, Shrinkable,
// interfaces
ObjectConstraints, Parameters,
ObjectConstraints, Parameters, RunDetails,
UniformDistribution, LinearCongruential, MersenneTwister, MutableRandomGenerator, RandomGenerator,
Stream, stream,
};

0 comments on commit 89f7787

Please sign in to comment.