UNPKG

6.58 kBTypeScriptView Raw
1import type { Random } from '../../../random/generator/Random.js';
2import { Stream } from '../../../stream/Stream.js';
3import { Value } from './Value.js';
4/**
5 * Abstract class able to generate values on type `T`
6 *
7 * The values generated by an instance of Arbitrary can be previewed - with {@link sample} - or classified - with {@link statistics}.
8 *
9 * @remarks Since 0.0.7
10 * @public
11 */
12export declare abstract class Arbitrary<T> {
13 /**
14 * Generate a value of type `T` along with its context (if any)
15 * based on the provided random number generator
16 *
17 * @param mrng - Random number generator
18 * @param biasFactor - If taken into account 1 value over biasFactor must be biased. Either integer value greater or equal to 2 (bias) or undefined (no bias)
19 * @returns Random value of type `T` and its context
20 *
21 * @remarks Since 0.0.1 (return type changed in 3.0.0)
22 */
23 abstract generate(mrng: Random, biasFactor: number | undefined): Value<T>;
24 /**
25 * Check if a given value could be pass to `shrink` without providing any context.
26 *
27 * In general, `canShrinkWithoutContext` is not designed to be called for each `shrink` but rather on very special cases.
28 * Its usage must be restricted to `canShrinkWithoutContext` or in the rare* contexts of a `shrink` method being called without
29 * any context. In this ill-formed case of `shrink`, `canShrinkWithoutContext` could be used or called if needed.
30 *
31 * *we fall in that case when fast-check is asked to shrink a value that has been provided manually by the user,
32 * in other words: a value not coming from a call to `generate` or a normal `shrink` with context.
33 *
34 * @param value - Value to be assessed
35 * @returns `true` if and only if the value could have been generated by this instance
36 *
37 * @remarks Since 3.0.0
38 */
39 abstract canShrinkWithoutContext(value: unknown): value is T;
40 /**
41 * Shrink a value of type `T`, may rely on the context previously provided to shrink efficiently
42 *
43 * Must never be called with possibly invalid values and no context without ensuring that such call is legal
44 * by calling `canShrinkWithoutContext` first on the value.
45 *
46 * @param value - The value to shrink
47 * @param context - Its associated context (the one returned by generate) or `undefined` if no context but `canShrinkWithoutContext(value) === true`
48 * @returns Stream of shrinks for value based on context (if provided)
49 *
50 * @remarks Since 3.0.0
51 */
52 abstract shrink(value: T, context: unknown | undefined): Stream<Value<T>>;
53 /**
54 * Create another arbitrary by filtering values against `predicate`
55 *
56 * All the values produced by the resulting arbitrary
57 * satisfy `predicate(value) == true`
58 *
59 * Be aware that using filter may highly impact the time required to generate a valid entry
60 *
61 * @example
62 * ```typescript
63 * const integerGenerator: Arbitrary<number> = ...;
64 * const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);
65 * // new Arbitrary only keeps even values
66 * ```
67 *
68 * @param refinement - Predicate, to test each produced element. Return true to keep the element, false otherwise
69 * @returns New arbitrary filtered using predicate
70 *
71 * @remarks Since 1.23.0
72 */
73 filter<U extends T>(refinement: (t: T) => t is U): Arbitrary<U>;
74 /**
75 * Create another arbitrary by filtering values against `predicate`
76 *
77 * All the values produced by the resulting arbitrary
78 * satisfy `predicate(value) == true`
79 *
80 * Be aware that using filter may highly impact the time required to generate a valid entry
81 *
82 * @example
83 * ```typescript
84 * const integerGenerator: Arbitrary<number> = ...;
85 * const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);
86 * // new Arbitrary only keeps even values
87 * ```
88 *
89 * @param predicate - Predicate, to test each produced element. Return true to keep the element, false otherwise
90 * @returns New arbitrary filtered using predicate
91 *
92 * @remarks Since 0.0.1
93 */
94 filter(predicate: (t: T) => boolean): Arbitrary<T>;
95 /**
96 * Create another arbitrary by mapping all produced values using the provided `mapper`
97 * Values produced by the new arbitrary are the result of applying `mapper` value by value
98 *
99 * @example
100 * ```typescript
101 * const rgbChannels: Arbitrary<{r:number,g:number,b:number}> = ...;
102 * const color: Arbitrary<string> = rgbChannels.map(ch => `#${(ch.r*65536 + ch.g*256 + ch.b).toString(16).padStart(6, '0')}`);
103 * // transform an Arbitrary producing {r,g,b} integers into an Arbitrary of '#rrggbb'
104 * ```
105 *
106 * @param mapper - Map function, to produce a new element based on an old one
107 * @param unmapper - Optional unmap function, it will never be used except when shrinking user defined values. Must throw if value is not compatible (since 3.0.0)
108 * @returns New arbitrary with mapped elements
109 *
110 * @remarks Since 0.0.1
111 */
112 map<U>(mapper: (t: T) => U, unmapper?: (possiblyU: unknown) => T): Arbitrary<U>;
113 /**
114 * Create another arbitrary by mapping a value from a base Arbirary using the provided `fmapper`
115 * Values produced by the new arbitrary are the result of the arbitrary generated by applying `fmapper` to a value
116 * @example
117 * ```typescript
118 * const arrayAndLimitArbitrary = fc.nat().chain((c: number) => fc.tuple( fc.array(fc.nat(c)), fc.constant(c)));
119 * ```
120 *
121 * @param chainer - Chain function, to produce a new Arbitrary using a value from another Arbitrary
122 * @returns New arbitrary of new type
123 *
124 * @remarks Since 1.2.0
125 */
126 chain<U>(chainer: (t: T) => Arbitrary<U>): Arbitrary<U>;
127 /**
128 * Create another Arbitrary with no shrink values
129 *
130 * @example
131 * ```typescript
132 * const dataGenerator: Arbitrary<string> = ...;
133 * const unshrinkableDataGenerator: Arbitrary<string> = dataGenerator.noShrink();
134 * // same values no shrink
135 * ```
136 *
137 * @returns Create another arbitrary with no shrink values
138 * @remarks Since 0.0.9
139 */
140 noShrink(): Arbitrary<T>;
141 /**
142 * Create another Arbitrary that cannot be biased
143 *
144 * @param freq - The biased version will be used one time over freq - if it exists
145 * @remarks Since 1.1.0
146 */
147 noBias(): Arbitrary<T>;
148}
149
\No newline at end of file