Skip to content

Commit

Permalink
Add utests for shrink on anything/object
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jan 29, 2018
1 parent ce5ee30 commit 6adf6f2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/unit/check/arbitrary/ObjectArbitrary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ describe("ObjectArbitrary", () => {
return evaluateDepth(g) <= depth;
})
));
it('Should shrink towards minimal value of type', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = stubRng.mutable.fastincrease(seed);
let shrinkable = object().generate(mrng);
const originalValue = shrinkable.value;
while (shrinkable.shrink().has(v => true)[0]) {
shrinkable = shrinkable.shrink().next().value;
}// only check one shrink path
switch (typeof originalValue) {
case 'boolean':
return assert.strictEqual(shrinkable.value, false, 'Should have shrinked towards false');
case 'number':
return assert.strictEqual(shrinkable.value, 0, 'Should have shrinked towards zero');
case 'undefined':
return assert.strictEqual(shrinkable.value, undefined, 'Should have shrinked towards undefined');
default:
if (originalValue == null)
return assert.strictEqual(shrinkable.value, null, 'Should have shrinked towards null');
if (Array.isArray(originalValue))
return assert.deepStrictEqual(shrinkable.value, [], 'Should have shrinked towards empty array');
return assert.deepStrictEqual(shrinkable.value, {}, 'Should have shrinked towards empty object');
}
})
));
});
describe('object', () => {
it('Should generate an object', () => fc.assert(
Expand Down Expand Up @@ -103,5 +127,15 @@ describe("ObjectArbitrary", () => {
return evaluateDepth(g) <= depth +1;
})
));
it('Should shrink towards empty object', () => fc.assert(
fc.property(fc.integer(), (seed) => {
const mrng = stubRng.mutable.fastincrease(seed);
let shrinkable = object().generate(mrng);
while (shrinkable.shrink().has(v => true)[0]) {
shrinkable = shrinkable.shrink().next().value;
}// only check one shrink path
return typeof shrinkable.value === 'object' && Object.keys(shrinkable.value).length === 0;
})
));
});
});

0 comments on commit 6adf6f2

Please sign in to comment.