From 44d0feb7aa7e6bac3785b82104275867e4dc5706 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 10 May 2018 18:40:25 +0200 Subject: [PATCH] Add UnbiasedProperty class to force non-biased values --- src/check/property/UnbiasedProperty.ts | 12 +++++ .../check/property/UnbiasedProperty.spec.ts | 51 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/check/property/UnbiasedProperty.ts create mode 100644 test/unit/check/property/UnbiasedProperty.spec.ts diff --git a/src/check/property/UnbiasedProperty.ts b/src/check/property/UnbiasedProperty.ts new file mode 100644 index 00000000000..ecd6dfcb491 --- /dev/null +++ b/src/check/property/UnbiasedProperty.ts @@ -0,0 +1,12 @@ +import Random from '../../random/generator/Random'; +import Arbitrary from '../arbitrary/definition/Arbitrary'; +import Shrinkable from '../arbitrary/definition/Shrinkable'; +import IProperty from './IProperty'; + +/** @hidden */ +export class UnbiasedProperty implements IProperty { + constructor(readonly property: IProperty) {} + isAsync = () => this.property.isAsync(); + generate = (mrng: Random, freq?: number) => this.property.generate(mrng); + run = (v: Ts) => this.property.run(v); +} diff --git a/test/unit/check/property/UnbiasedProperty.spec.ts b/test/unit/check/property/UnbiasedProperty.spec.ts new file mode 100644 index 00000000000..4ff90af69cf --- /dev/null +++ b/test/unit/check/property/UnbiasedProperty.spec.ts @@ -0,0 +1,51 @@ +import * as assert from 'assert'; + +import Arbitrary from '../../../../src/check/arbitrary/definition/Arbitrary'; +import Shrinkable from '../../../../src/check/arbitrary/definition/Shrinkable'; +import IProperty from '../../../../src/check/property/IProperty'; +import { UnbiasedProperty } from '../../../../src/check/property/UnbiasedProperty'; + +import * as stubRng from '../../stubs/generators'; + +describe('UnbiasedProperty', () => { + it('Should forward parameters correctly (asynchronous property)', () => { + let calledWithFreq: number | undefined = undefined; + const pAsync = new class implements IProperty { + isAsync = () => true; + generate = (mrng: any, freq?: number) => { + calledWithFreq = freq; + return new Shrinkable(42); + }; + run = (v: number) => 'pAsync:' + v; + }(); + + const unbiasedAsyncProp = new UnbiasedProperty(pAsync); + + assert.equal(unbiasedAsyncProp.isAsync(), true); + assert.equal(unbiasedAsyncProp.generate(stubRng.mutable.nocall()).value, 42); + assert.strictEqual(calledWithFreq, undefined); + assert.equal(unbiasedAsyncProp.generate(stubRng.mutable.nocall(), 52).value, 42); + assert.strictEqual(calledWithFreq, undefined); + assert.equal(unbiasedAsyncProp.run(47), 'pAsync:47'); + }); + it('Should forward parameters correctly (synchronous property)', () => { + let calledWithFreq: number | undefined = undefined; + const pSync = new class implements IProperty { + isAsync = () => false; + generate = (mrng: any, freq?: number) => { + calledWithFreq = freq; + return new Shrinkable(48); + }; + run = (v: number) => 'pSync:' + v; + }(); + + const unbiasedSyncProp = new UnbiasedProperty(pSync); + + assert.equal(unbiasedSyncProp.isAsync(), false); + assert.equal(unbiasedSyncProp.generate(stubRng.mutable.nocall()).value, 48); + assert.strictEqual(calledWithFreq, undefined); + assert.equal(unbiasedSyncProp.generate(stubRng.mutable.nocall(), 52).value, 48); + assert.strictEqual(calledWithFreq, undefined); + assert.equal(unbiasedSyncProp.run(29), 'pSync:29'); + }); +});