Skip to content

Commit

Permalink
Add minimum and maximum validation to integer and nat (#370)
Browse files Browse the repository at this point in the history
* Add minimum and maximum validation to integer and nat

* PR request changes

* Add name to contributors list
  • Loading branch information
diegopedro94 authored and dubzzz committed May 28, 2019
1 parent cfdae2c commit ffc60f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ This file contains a list of people who've made non-trivial contribution to **fa
People who commit code to the project are encouraged to add their names here. Please keep the list sorted by first names.

- [Aaron Elligsen](https://github.com/hath995)
- [Diego Pedro](https://github/diegopedro94)
- [Nicolas Dubien](https://github.com/dubzzz)
- [Gjorgji Kjosev](https://github.com/spion)
- [Trey Davis](https://github.com/treydavis)
3 changes: 3 additions & 0 deletions src/check/arbitrary/IntegerArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function integer(max: number): ArbitraryWithShrink<number>;
*/
function integer(min: number, max: number): ArbitraryWithShrink<number>;
function integer(a?: number, b?: number): ArbitraryWithShrink<number> {
if (a !== undefined && b !== undefined && a > b)
throw new Error('fc.integer maximum value should be equal or greater than the minimum one');
return b === undefined ? new IntegerArbitrary(undefined, a) : new IntegerArbitrary(a, b);
}

Expand All @@ -79,6 +81,7 @@ function nat(): ArbitraryWithShrink<number>;
*/
function nat(max: number): ArbitraryWithShrink<number>;
function nat(a?: number): ArbitraryWithShrink<number> {
if (a !== undefined && a < 0) throw new Error('fc.nat value should be greater than or equal to 0');
return new IntegerArbitrary(0, a);
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/check/arbitrary/IntegerArbitrary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ describe('IntegerArbitrary', () => {
return -log2(-min) <= g && g <= log2(max);
})
));
it('Should throw when minimum number is greater than maximum one', () =>
fc.assert(
fc.property(fc.integer(), fc.integer(), (a, b) => {
fc.pre(a !== b);
if (a < b) expect(() => integer(b, a)).toThrowError();
else expect(() => integer(a, b)).toThrowError();
})
));
describe('Given no constraints [between -2**31 and 2**31 -1]', () => {
genericHelper.isValidArbitrary(() => integer(), {
isStrictlySmallerValue: isStrictlySmallerInteger,
Expand Down Expand Up @@ -181,6 +189,12 @@ describe('IntegerArbitrary', () => {
});
});
describe('nat', () => {
it('Should throw when the number is less than 0', () =>
fc.assert(
fc.property(fc.integer(Number.MIN_SAFE_INTEGER, -1), n => {
expect(() => nat(n)).toThrowError();
})
));
describe('Given no constraints [between 0 and 2**31 -1]', () => {
genericHelper.isValidArbitrary(() => nat(), {
isStrictlySmallerValue: isStrictlySmallerInteger,
Expand Down

0 comments on commit ffc60f9

Please sign in to comment.