UNPKG

3.6 kBTypeScriptView Raw
1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
2/**
3 * Conjonction of a weight and an arbitrary used by {@link frequency}
4 * in order to generate values
5 *
6 * @remarks Since 1.18.0
7 * @public
8 */
9export interface WeightedArbitrary<T> {
10 /**
11 * Weight to be applied when selecting which arbitrary should be used
12 * @remarks Since 0.0.7
13 */
14 weight: number;
15 /**
16 * Instance of Arbitrary
17 * @remarks Since 0.0.7
18 */
19 arbitrary: Arbitrary<T>;
20}
21/**
22 * Infer the type of the Arbitrary produced by {@link frequency}
23 * given the type of the source arbitraries
24 *
25 * @remarks Since 2.2.0
26 * @public
27 */
28export declare type FrequencyValue<Ts extends WeightedArbitrary<unknown>[]> = {
29 [K in keyof Ts]: Ts[K] extends WeightedArbitrary<infer U> ? U : never;
30}[number];
31/**
32 * Constraints to be applied on {@link frequency}
33 * @remarks Since 2.14.0
34 * @public
35 */
36export declare type FrequencyContraints = {
37 /**
38 * When set to true, the shrinker of frequency will try to check if the first arbitrary
39 * could have been used to discover an issue. It allows to shrink trees.
40 *
41 * Warning: First arbitrary must be the one resulting in the smallest structures
42 * for usages in deep tree-like structures.
43 *
44 * Warning: First arbitrary will not be used if its weight is set to zero.
45 *
46 * @remarks Since 2.14.0
47 */
48 withCrossShrink?: boolean;
49 /**
50 * While going deeper and deeper within a recursive structure (see {@link letrec}),
51 * this factor will be used to increase the probability to generate instances
52 * of the first passed arbitrary.
53 *
54 * Example of values: 0.1 (small impact as depth increases), 0.5, 1 (huge impact as depth increases).
55 *
56 * Warning: First arbitrary will not be used if its weight is set to zero.
57 *
58 * @remarks Since 2.14.0
59 */
60 depthFactor?: number;
61 /**
62 * Maximal authorized depth.
63 * Once this depth has been reached only the first arbitrary will be used.
64 *
65 * Warning: Contrary to others, first arbitrary will be used even if its weight is set to zero.
66 *
67 * @remarks Since 2.14.0
68 */
69 maxDepth?: number;
70 /**
71 * Depth identifier can be used to share the current depth between several instances.
72 *
73 * By default, if not specified, each instance of frequency will have its own depth.
74 * In other words: you can have depth=1 in one while you have depth=100 in another one.
75 *
76 * @remarks Since 2.14.0
77 */
78 depthIdentifier?: string;
79};
80/**
81 * For one of the values generated by `...warbs` - the probability of selecting the ith warb is of `warb[i].weight / sum(warb[j].weight)`
82 *
83 * **WARNING**: It expects at least one (Arbitrary, weight)
84 *
85 * @param warbs - (Arbitrary, weight)s that might be called to produce a value
86 *
87 * @remarks Since 0.0.7
88 * @public
89 */
90declare function frequency<Ts extends WeightedArbitrary<unknown>[]>(...warbs: Ts): Arbitrary<FrequencyValue<Ts>>;
91/**
92 * For one of the values generated by `...warbs` - the probability of selecting the ith warb is of `warb[i].weight / sum(warb[j].weight)`
93 *
94 * **WARNING**: It expects at least one (Arbitrary, weight)
95 *
96 * @param constraints - Constraints to be applied when generating the values
97 * @param warbs - (Arbitrary, weight)s that might be called to produce a value
98 *
99 * @remarks Since 0.0.7
100 * @public
101 */
102declare function frequency<Ts extends WeightedArbitrary<unknown>[]>(constraints: FrequencyContraints, ...warbs: Ts): Arbitrary<FrequencyValue<Ts>>;
103export { frequency };