Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Introduce new fc.noShrink arbitrary #5047

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .yarn/versions/5d397bcb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
fast-check: minor

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/vitest"
- "@fast-check/worker"
2 changes: 1 addition & 1 deletion examples/003-misc/mazeGenerator/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('mazeGenerator', () => {

// Helpers

const seedArb = fc.integer().noBias().noShrink();
const seedArb = fc.noShrink(fc.integer().noBias());

const dimensionArb = fc.record({
width: fc.integer({ min: 2, max: 20 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cloneMethod } from '../../../check/symbols';
import { hash } from '../../../utils/hash';
import { stringify } from '../../../utils/stringify';
import { integer } from '../../integer';
import { noShrink } from '../../noShrink';
import { tuple } from '../../tuple';
import { safeJoin } from '../../../utils/globals';

Expand All @@ -14,7 +15,7 @@ const safeObjectKeys = Object.keys;
export function buildCompareFunctionArbitrary<T, TOut>(
cmp: (hA: number, hB: number) => TOut,
): Arbitrary<(a: T, b: T) => TOut> {
return tuple(integer().noShrink(), integer({ min: 1, max: 0xffffffff }).noShrink()).map(([seed, hashEnvSize]) => {
return tuple(noShrink(integer()), noShrink(integer({ min: 1, max: 0xffffffff }))).map(([seed, hashEnvSize]) => {
const producer = () => {
const recorded: { [key: string]: TOut } = {};
const f = (a: T, b: T) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/fast-check/src/arbitrary/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cloneMethod, hasCloneMethod } from '../check/symbols';
import { array } from './array';
import type { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { integer } from './integer';
import { noShrink } from './noShrink';
import { tuple } from './tuple';
import { escapeForMultilineComments } from './_internals/helpers/TextEscaper';
import { safeMap, safeSort } from '../utils/globals';
Expand All @@ -20,7 +21,7 @@ const safeObjectKeys = Object.keys;
* @public
*/
export function func<TArgs extends any[], TOut>(arb: Arbitrary<TOut>): Arbitrary<(...args: TArgs) => TOut> {
return tuple(array(arb, { minLength: 1 }), integer().noShrink()).map(([outs, seed]) => {
return tuple(array(arb, { minLength: 1 }), noShrink(integer())).map(([outs, seed]) => {
const producer = () => {
const recorded: { [key: string]: TOut } = {};
const f = (...args: TArgs) => {
Expand Down
18 changes: 18 additions & 0 deletions packages/fast-check/src/arbitrary/noShrink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Arbitrary } from '../check/arbitrary/definition/Arbitrary';

/**
* Build an arbitrary without shrinking capabilities.
*
* NOTE:
* In most cases, users should avoid disabling shrinking capabilities.
* If the concern is the shrinking process taking too long or being unnecessary in CI environments,
* consider using alternatives like `endOnFailure` or `interruptAfterTimeLimit` instead.
*
* @param arb - The original arbitrary used for generating values. This arbitrary remains unchanged, but its shrinking capabilities will not be included in the new arbitrary.
*
* @remarks Since 3.20.0
* @public
*/
export function noShrink<T>(arb: Arbitrary<T>): Arbitrary<T> {
return arb.noShrink();
}
2 changes: 2 additions & 0 deletions packages/fast-check/src/fast-check-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ import { bigUint64Array } from './arbitrary/bigUint64Array';
import type { SchedulerAct } from './arbitrary/_internals/interfaces/Scheduler';
import type { StringMatchingConstraints } from './arbitrary/stringMatching';
import { stringMatching } from './arbitrary/stringMatching';
import { noShrink } from './arbitrary/noShrink';

// Explicit cast into string to avoid to have __type: "__PACKAGE_TYPE__"
/**
Expand Down Expand Up @@ -383,6 +384,7 @@ export {
option,
oneof,
clone,
noShrink,
shuffledSubarray,
subarray,
array,
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-check/test/e2e/Timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe(`Timeout (seed: ${seed})`, () => {
const afterEach = vi.fn().mockResolvedValue(undefined);
const out = await fc.check(
fc
.asyncProperty(fc.integer().noShrink(), async (_x) => {
.asyncProperty(fc.noShrink(fc.integer()), async (_x) => {
++numRuns;
await new Promise(() => {}); // never ending promise
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function assertProduceSameValueGivenSameSeed<T, U = never>(
fc.assert(
fc
.property(
fc.integer().noShrink(),
fc.noShrink(fc.integer()),
biasFactorArbitrary(),
fc.infiniteStream(fc.nat({ max: 20 })),
extra,
Expand Down Expand Up @@ -88,7 +88,7 @@ export function assertProduceCorrectValues<T, U = never>(
fc.assert(
fc
.property(
fc.integer().noShrink(),
fc.noShrink(fc.integer()),
biasFactorArbitrary(),
fc.infiniteStream(fc.nat({ max: 20 })),
extra,
Expand Down Expand Up @@ -129,7 +129,7 @@ export function assertGenerateEquivalentTo<T, U = never>(
} = options;
fc.assert(
fc
.property(fc.integer().noShrink(), biasFactorArbitrary(), extra, (seed, biasFactor, extraParameters) => {
.property(fc.noShrink(fc.integer()), biasFactorArbitrary(), extra, (seed, biasFactor, extraParameters) => {
// Arrange
const arbA = arbitraryBuilderA(extraParameters);
const arbB = arbitraryBuilderB(extraParameters);
Expand Down
6 changes: 3 additions & 3 deletions packages/fast-check/test/unit/arbitrary/commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('commands (integration)', () => {
};
fc.assert(
fc.property(
fc.integer().noShrink(),
fc.noShrink(fc.integer()),
fc.infiniteStream(fc.nat()),
fc.option(fc.integer({ min: 2 }), { nil: undefined }),
(seed, shrinkPath, biasFactor) => {
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('commands (integration)', () => {
const commandsArb = commands([nat(3).map((id) => new SuccessIdCommand(id))]);
fc.assert(
fc.property(
fc.integer().noShrink(),
fc.noShrink(fc.integer()),
fc.infiniteStream(fc.nat()),
fc.option(fc.integer({ min: 2 }), { nil: undefined }),
(seed, shrinkPath, biasFactor) => {
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('commands (integration)', () => {
it('should shrink the same way when based on replay data', () => {
fc.assert(
fc.property(
fc.integer().noShrink(),
fc.noShrink(fc.integer()),
fc.nat(100),
fc.option(fc.integer({ min: 2 }), { nil: undefined }),
(seed, numValues, biasFactor) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/fast-check/test/unit/check/runner/Sampler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fc from 'fast-check';
import { sample, statistics } from '../../../../src/check/runner/Sampler';

import * as stubArb from '../../stubs/arbitraries';
import { noShrink } from '../../../../src/arbitrary/noShrink';
import { cloneMethod } from '../../../../src/check/symbols';
import { fakeArbitrary } from '../../arbitrary/__test-helpers__/ArbitraryHelpers';
import { Value } from '../../../../src/check/arbitrary/definition/Value';
Expand Down Expand Up @@ -56,12 +57,12 @@ describe('Sampler', () => {
}),
));
it('Should throw on wrong path (too deep)', () => {
const arb = stubArb.forward().noShrink();
const arb = noShrink(stubArb.forward());
expect(() => sample(arb, { seed: 42, path: '0:0:0' })).toThrowError();
// 0:1 should not throw but retrieve an empty set
});
it('Should throw on invalid path', () => {
const arb = stubArb.forward().noShrink();
const arb = noShrink(stubArb.forward());
expect(() => sample(arb, { seed: 42, path: 'invalid' })).toThrowError();
});
it('Should not call clone on cloneable instances', () => {
Expand Down
24 changes: 13 additions & 11 deletions website/docs/advanced/fake-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ import { faker } from '@faker-js/faker';

const fakerToArb = (fakerGen) => {
return fc
.integer()
.noBias() // same probability to generate each of the allowed integers
.noShrink() // shrink on a seed makes no sense
.noShrink(
// shrink on a seed makes no sense
fc.integer().noBias(), // same probability to generate each of the allowed integers
)
.map((seed) => {
faker.seed(seed); // seed the generator
return fakerGen(); // call it
Expand All @@ -145,15 +146,16 @@ import fc from 'fast-check';
import { loremIpsum } from 'lorem-ipsum';

const loremArb = fc
.infiniteStream(
// Arbitrary generating 32-bit floating point numbers
// between 0 (included) and 1 (excluded) (uniform distribution)
fc
.integer({ min: 0, max: (1 << 24) - 1 })
.map((v) => v / (1 << 24))
.noBias(),
.noShrink(
fc.infiniteStream(
// Arbitrary generating 32-bit floating point numbers
// between 0 (included) and 1 (excluded) (uniform distribution)
fc
.integer({ min: 0, max: (1 << 24) - 1 })
.map((v) => v / (1 << 24))
.noBias(),
),
)
.noShrink()
.map((s) => {
const rng = () => s.next().value; // prng like Math.random but controlled by fast-check
return loremIpsum({ random: rng });
Expand Down
24 changes: 23 additions & 1 deletion website/docs/core-blocks/arbitraries/combiners/any.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ fc.clone(fc.nat(), 3);
Resources: [API reference](https://fast-check.dev/api-reference/functions/clone.html).
Available since 2.5.0.

## noShrink

Drop shrinking capabilities from an existing arbitrary.

**Signatures:**

- `fc.noShrink(arb)`

**with:**

- `arb` — _arbitrary instance responsible to generate values_

**Usages:**

```js
fc.noShrink(fc.nat());
// Examples of generated values: 1395148595, 7, 1743838935, 879259091, 2147483640…
```

Resources: [API reference](https://fast-check.dev/api-reference/classes/Arbitrary.html#noShrink).
Available since 3.20.0.

## .filter

Filter an existing arbitrary.
Expand Down Expand Up @@ -251,7 +273,7 @@ Drop shrinking capabilities from an existing arbitrary.

**Signatures:**

- `.noShrink()`
- `.noShrink()` — _deprecated since v3.20.0 ([#5047](https://github.com/dubzzz/fast-check/pull/5047))_

**Usages:**

Expand Down
Loading