Skip to content

Commit

Permalink
Add UnbiasedProperty class to force non-biased values
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed May 10, 2018
1 parent ddcc39d commit 44d0feb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/check/property/UnbiasedProperty.ts
Original file line number Diff line number Diff line change
@@ -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<Ts> implements IProperty<Ts> {
constructor(readonly property: IProperty<Ts>) {}
isAsync = () => this.property.isAsync();
generate = (mrng: Random, freq?: number) => this.property.generate(mrng);
run = (v: Ts) => this.property.run(v);
}
51 changes: 51 additions & 0 deletions test/unit/check/property/UnbiasedProperty.spec.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
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<number> {
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');
});
});

0 comments on commit 44d0feb

Please sign in to comment.