Skip to content

Commit

Permalink
Add validation of inputs on property
Browse files Browse the repository at this point in the history
Related to issue #59
  • Loading branch information
dubzzz committed Apr 13, 2018
1 parent b960938 commit 6f35cdd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/check/arbitrary/TupleArbitrary.generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Shrinkable from './definition/Shrinkable';
class GenericTupleArbitrary<Ts> extends Arbitrary<Ts[]> {
constructor(readonly arbs: Arbitrary<Ts>[]) {
super();
for (let idx = 0; idx !== arbs.length; ++idx) {
const arb = arbs[idx];
if (arb == null || arb.generate == null)
throw new Error(`Invalid parameter encountered at index ${idx}: expecting an Arbitrary`);
}
}
private static wrapper<Ts>(shrinkables: Shrinkable<Ts>[]): Shrinkable<Ts[]> {
return new Shrinkable(shrinkables.map(s => s.value), () =>
Expand Down
5 changes: 5 additions & 0 deletions test/unit/check/arbitrary/TupleArbitrary.generic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as assert from 'power-assert';
import fc from '../../../../lib/fast-check';
import {
dummy,
Expand All @@ -6,6 +7,7 @@ import {
propertyShrinkInRange
} from './TupleArbitrary.properties';

import Arbitrary from '../../../../src/check/arbitrary/definition/Arbitrary';
import { char } from '../../../../src/check/arbitrary/CharacterArbitrary';
import { genericTuple } from '../../../../src/check/arbitrary/TupleArbitrary';

Expand All @@ -19,6 +21,9 @@ describe('TupleArbitrary', () => {
fc.assert(propertyShrinkInRange([dummy(42), dummy(8)], true)));
it('Should not suggest input in tuple shrinked values', () =>
fc.assert(propertyNotSuggestInputInShrink([dummy(42), dummy(8)], true)));
it('Should throw on null arbitrary', () => assert.throws(() => genericTuple([dummy(1), dummy(2), null])));
it('Should throw on invalid arbitrary', () =>
assert.throws(() => genericTuple([dummy(1), dummy(2), <Arbitrary<any>>{}])));
genericHelper.testNoImpactOfMutation(genericTuple([char(), char()]), tab => {
for (let idx = 0; idx !== tab.length; ++idx) tab[idx] = '.';
});
Expand Down
4 changes: 4 additions & 0 deletions test/unit/check/property/AsyncProperty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ describe('AsyncProperty', () => {
assert.equal(runnerHasCompleted, true, 'Runner should have completed');
assert.equal(await runner, null, 'Property should succeed');
});
it('Should throw on null arbitrary', () =>
assert.throws(() => asyncProperty(stubArb.single(8), stubArb.single(8), null, async () => {})));
it('Should throw on invalid arbitrary', () =>
assert.throws(() => asyncProperty(stubArb.single(8), stubArb.single(8), <Arbitrary<any>>{}, async () => {})));
});
4 changes: 4 additions & 0 deletions test/unit/check/property/Property.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ describe('Property', () => {
assert.ok(arbs[idx].called_once, `Generator #${idx + 1} should have been called by run`);
}
});
it('Should throw on null arbitrary', () =>
assert.throws(() => property(stubArb.single(8), stubArb.single(8), null, () => {})));
it('Should throw on invalid arbitrary', () =>
assert.throws(() => property(stubArb.single(8), stubArb.single(8), <Arbitrary<any>>{}, () => {})));
});

0 comments on commit 6f35cdd

Please sign in to comment.