Skip to content

Commit

Permalink
Ease the instantiation of stubs for arbitraries
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jan 23, 2018
1 parent aed0e1f commit dba24f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
19 changes: 8 additions & 11 deletions test/unit/check/property/Property.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import * as assert from 'power-assert';

import Arbitrary from '../../../../src/check/arbitrary/definition/Arbitrary';
import { property } from '../../../../src/check/property/Property';

import { SingleUseArbitrary } from '../../stubs/arbitraries';
import * as stubArb from '../../stubs/arbitraries';
import * as stubRng from '../../stubs/generators';

function single<T>(id: T) {
return new SingleUseArbitrary<T>(id);
}

describe('Property', () => {
it('Should fail if predicate fails', () => {
const p = property(single(8), (arg: number) => {
const p = property(stubArb.single(8), (arg: number) => {
return false;
});
assert.notEqual(p.run(p.generate(stubRng.mutable.nocall()).value), null, 'Property should fail');
});
it('Should fail if predicate throws', () => {
const p = property(single(8), (arg: number) => {
const p = property(stubArb.single(8), (arg: number) => {
throw 'predicate throws';
});
assert.equal(p.run(p.generate(stubRng.mutable.nocall()).value), 'predicate throws', 'Property should fail and attach the exception as string');
});
it('Should fail if predicate fails on asserts', () => {
const p = property(single(8), (arg: number) => {
const p = property(stubArb.single(8), (arg: number) => {
assert.ok(false);
});
let expected = "";
Expand All @@ -34,18 +31,18 @@ describe('Property', () => {
assert.ok(out!.indexOf('\n\nStack trace:') !== -1, 'Property should include the stack trace when available');
});
it('Should succeed if predicate is true', () => {
const p = property(single(8), (arg: number) => {
const p = property(stubArb.single(8), (arg: number) => {
return true;
});
assert.equal(p.run(p.generate(stubRng.mutable.nocall()).value), null, 'Property should succeed');
});
it('Should succeed if predicate does not return anything', () => {
const p = property(single(8), (arg: number) => {});
const p = property(stubArb.single(8), (arg: number) => {});
assert.equal(p.run(p.generate(stubRng.mutable.nocall()).value), null, 'Property should succeed');
});
it('Should call and forward arbitraries one time', () => {
let one_call_to_predicate = false;
const arbs: [SingleUseArbitrary<number>, SingleUseArbitrary<string>, SingleUseArbitrary<string>] = [single(3), single("hello"), single("world")];
const arbs: [stubArb.SingleUseArbitrary<number>, stubArb.SingleUseArbitrary<string>, stubArb.SingleUseArbitrary<string>] = [stubArb.single(3), stubArb.single("hello"), stubArb.single("world")];
const p = property(arbs[0], arbs[1], arbs[2], (arg1: number, arb2: string, arg3: string) => {
if (one_call_to_predicate) {
throw 'Predicate has already been evaluated once';
Expand Down
14 changes: 7 additions & 7 deletions test/unit/check/runner/Sampler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ import * as fc from '../../../../lib/fast-check';

import { sample } from '../../../../src/check/runner/Sampler';

import { CounterArbitrary, ForwardArbitrary } from '../../stubs/arbitraries';
import * as stubArb from '../../stubs/arbitraries';

const MAX_NUM_RUNS = 1000;
describe('Sampler', () => {
describe('sample', () => {
it('Should produce the same sequence given the same seed', () => {
fc.property(fc.integer(), (seed) => {
const out1 = sample(new ForwardArbitrary(), {seed: seed});
const out2 = sample(new ForwardArbitrary(), {seed: seed});
const out1 = sample(stubArb.forward(), {seed: seed});
const out2 = sample(stubArb.forward(), {seed: seed});
assert.deepEqual(out2, out1, 'Should be the same array');
});
});
it('Should produce the same sequence given the same seed and different lengths', () => {
fc.property(fc.integer(), fc.nat(MAX_NUM_RUNS), fc.nat(MAX_NUM_RUNS), (seed, l1, l2) => {
const out1 = sample(new ForwardArbitrary(), {seed: seed, num_runs: l1});
const out2 = sample(new ForwardArbitrary(), {seed: seed, num_runs: l2});
const out1 = sample(stubArb.forward(), {seed: seed, num_runs: l1});
const out2 = sample(stubArb.forward(), {seed: seed, num_runs: l2});
const lmin = Math.min(l1, l2);
assert.deepEqual(out2.slice(0, lmin), out1.slice(0, lmin), 'Should be the same array');
});
});
it('Should produce exactly the number of outputs', () => {
fc.property(fc.nat(MAX_NUM_RUNS), fc.integer(), (num, start) => {
const arb = new CounterArbitrary(start);
const arb = stubArb.counter(start);
const out = sample(arb, num);
assert.equal(out.length, num, 'Should produce the right number of values');
assert.deepEqual(out, arb.generatedValues, 'Should give back the values in order');
});
});
it('Should produce exactly the number of outputs when called with number', () => {
fc.property(fc.nat(MAX_NUM_RUNS), fc.integer(), (num, start) => {
const arb = new CounterArbitrary(start);
const arb = stubArb.counter(start);
const out = sample(arb, num);
assert.equal(out.length, num, 'Should produce the right number of values');
assert.deepEqual(out, arb.generatedValues, 'Should give back the values in order');
Expand Down
9 changes: 8 additions & 1 deletion test/unit/stubs/arbitraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ class SingleUseArbitrary<T> extends Arbitrary<T> {
}
}

export { CounterArbitrary, ForwardArbitrary, SingleUseArbitrary };
const counter = (value: number) => new CounterArbitrary(value);
const forward = () => new ForwardArbitrary();
const single = <T>(id: T) => new SingleUseArbitrary(id);

export {
counter, forward, single,
SingleUseArbitrary
};

0 comments on commit dba24f6

Please sign in to comment.