@@ -5,19 +5,12 @@ import {
5
5
fuzzGenerator ,
6
6
FuzzContext ,
7
7
FuzzerUnit ,
8
- } from '../fuzzer' ;
8
+ concreteFuzzerByName ,
9
+ } from './fuzzer' ;
9
10
import { isLeft , isRight } from 'fp-ts/lib/Either' ;
10
- import * as rnd from 'seedrandom ' ;
11
+ import { rngi , rng } from './rng ' ;
11
12
12
- function rng ( seed : number ) {
13
- return rnd . tychei ( `${ seed } ` , { global : false } ) ;
14
- }
15
-
16
- function rngi ( seed : number ) {
17
- return Math . abs ( rng ( seed ) . int32 ( ) ) ;
18
- }
19
-
20
- export type BasicType =
13
+ type BasicType =
21
14
| t . AnyArrayType
22
15
| t . ArrayType < t . Mixed >
23
16
| t . BooleanType
@@ -43,22 +36,19 @@ export type BasicType =
43
36
| t . DictionaryType < t . Mixed , t . Mixed >
44
37
| t . RefinementType < t . Mixed > ;
45
38
46
- export type basicFuzzGenerator <
39
+ type basicFuzzGenerator <
47
40
T ,
48
41
C extends t . Decoder < unknown , T > & BasicType
49
42
> = fuzzGenerator < T , C > ;
50
43
51
- export type basicLiteralConcreteFuzzer <
44
+ type basicLiteralConcreteFuzzer <
52
45
T ,
53
46
C extends t . Decoder < unknown , T > & BasicType
54
47
> = ConcreteFuzzer < T > [ 'func' ] ;
55
48
56
- export type BasicFuzzer <
57
- T ,
58
- C extends t . Decoder < unknown , T > & BasicType
59
- > = Fuzzer < T , C > ;
49
+ type BasicFuzzer < T , C extends t . Decoder < unknown , T > & BasicType > = Fuzzer < T , C > ;
60
50
61
- export function concrete < T , C extends t . Decoder < unknown , T > & BasicType > (
51
+ function concrete < T , C extends t . Decoder < unknown , T > & BasicType > (
62
52
func : basicLiteralConcreteFuzzer < T , C > ,
63
53
tag : C [ '_tag' ]
64
54
) : BasicFuzzer < T , C > {
@@ -73,22 +63,7 @@ export function concrete<T, C extends t.Decoder<unknown, T> & BasicType>(
73
63
} ;
74
64
}
75
65
76
- export function concreteNamed < T , C extends t . Decoder < unknown , T > > (
77
- func : ConcreteFuzzer < T > [ 'func' ] ,
78
- name : C [ 'name' ]
79
- ) : Fuzzer < T , C > {
80
- return {
81
- impl : {
82
- type : 'fuzzer' ,
83
- func,
84
- mightRecurse : false ,
85
- } ,
86
- id : name ,
87
- idType : 'name' ,
88
- } ;
89
- }
90
-
91
- export function gen < T , C extends t . Decoder < unknown , T > & BasicType > (
66
+ function gen < T , C extends t . Decoder < unknown , T > & BasicType > (
92
67
func : basicFuzzGenerator < T , C > ,
93
68
tag : C [ '_tag' ]
94
69
) : BasicFuzzer < T , C > {
@@ -138,7 +113,7 @@ const fuzzUnknownWithType = (codec: t.Decoder<unknown, unknown>) => (
138
113
mightRecurse : false ,
139
114
children : [ codec ] ,
140
115
func : ( ctx , n , h0 ) =>
141
- ctx . shouldGoDeeper ( ) ? h0 . encode ( [ rngi ( n ) , ctx . deeper ( ) ] ) : rngi ( n ) ,
116
+ ctx . mayRecurse ( ) ? h0 . encode ( [ rngi ( n ) , ctx . recursed ( ) ] ) : rngi ( n ) ,
142
117
} ;
143
118
} ;
144
119
@@ -165,7 +140,7 @@ export function fuzzUnion(b: t.UnionType<t.Mixed[]>): ConcreteFuzzer<unknown> {
165
140
children : b . types ,
166
141
func : ( ctx , n , ...h0 ) => {
167
142
const r = rng ( n ) ;
168
- const h1 = ctx . shouldGoDeeper ( ) ? h0 : h0 . filter ( f => ! f . mightRecurse ) ;
143
+ const h1 = ctx . mayRecurse ( ) ? h0 : h0 . filter ( f => ! f . mightRecurse ) ;
169
144
const h = h1 . length === 0 ? h0 : h1 ;
170
145
return h [ Math . abs ( r . int32 ( ) ) % h . length ] . encode ( [ r . int32 ( ) , ctx ] ) ;
171
146
} ,
@@ -242,7 +217,7 @@ function arrayFuzzFunc(maxLength: number) {
242
217
return ( ctx : FuzzContext , n : number , h0 : FuzzerUnit < unknown > ) => {
243
218
const ret : unknown [ ] = [ ] ;
244
219
const r = rng ( n ) ;
245
- if ( ! ctx . shouldGoDeeper ( ) && h0 . mightRecurse ) {
220
+ if ( ! ctx . mayRecurse ( ) && h0 . mightRecurse ) {
246
221
return ret ;
247
222
}
248
223
const ml = Math . abs ( r . int32 ( ) ) % maxLength ;
@@ -278,7 +253,7 @@ const fuzzReadonlyArrayWithMaxLength = (maxLength: number) => (
278
253
func : ( ctx , n , h0 ) => {
279
254
const ret : unknown [ ] = [ ] ;
280
255
const r = rng ( n ) ;
281
- if ( ! ctx . shouldGoDeeper ( ) && h0 . mightRecurse ) {
256
+ if ( ! ctx . mayRecurse ( ) && h0 . mightRecurse ) {
282
257
return ret ;
283
258
}
284
259
const ml = Math . abs ( r . int32 ( ) ) % maxLength ;
@@ -310,7 +285,7 @@ export function anyArrayFuzzer(maxLength: number = defaultMaxArrayLength) {
310
285
311
286
export const defaultExtraProps = { ___0000_extra_ : t . number } ;
312
287
313
- const fuzzPartialWithExtraCodec = ( extra : t . Props ) => (
288
+ export const fuzzPartialWithExtraCodec = ( extra : t . Props ) => (
314
289
b : t . PartialType < t . Props >
315
290
) : ConcreteFuzzer < unknown > => {
316
291
const kk = Object . getOwnPropertyNames ( b . props ) ;
@@ -328,7 +303,7 @@ const fuzzPartialWithExtraCodec = (extra: t.Props) => (
328
303
const ret = Object . create ( null ) ;
329
304
const r = rng ( n0 ) ;
330
305
h . forEach ( ( v , i ) => {
331
- if ( ( ctx . shouldGoDeeper ( ) || ! v . mightRecurse ) && r . int32 ( ) % 2 === 0 ) {
306
+ if ( ( ctx . mayRecurse ( ) || ! v . mightRecurse ) && r . int32 ( ) % 2 === 0 ) {
332
307
// Only allow key indices from the original type
333
308
// or added keys not present in original type.
334
309
if ( i < kk . length || ! kk . includes ( keys [ i ] ) ) {
@@ -345,7 +320,7 @@ export function partialFuzzer(extra: t.Props = defaultExtraProps) {
345
320
return gen ( fuzzPartialWithExtraCodec ( extra ) , 'PartialType' ) ;
346
321
}
347
322
348
- const fuzzInterfaceWithExtraCodec = ( extra : t . Props ) => (
323
+ export const fuzzInterfaceWithExtraCodec = ( extra : t . Props ) => (
349
324
b : t . InterfaceType < t . Props >
350
325
) : ConcreteFuzzer < unknown > => {
351
326
const kk = Object . getOwnPropertyNames ( b . props ) ;
@@ -366,7 +341,7 @@ const fuzzInterfaceWithExtraCodec = (extra: t.Props) => (
366
341
if ( i < kk . length ) {
367
342
ret [ keys [ i ] ] = v . encode ( [ r . int32 ( ) , ctx ] ) ;
368
343
} else if (
369
- ( ctx . shouldGoDeeper ( ) || ! v . mightRecurse ) &&
344
+ ( ctx . mayRecurse ( ) || ! v . mightRecurse ) &&
370
345
r . int32 ( ) % 2 === 0
371
346
) {
372
347
// Only allow added keys not present in original type.
@@ -417,9 +392,9 @@ export function fuzzIntersection(
417
392
} ;
418
393
}
419
394
420
- export const coreFuzzers = [
395
+ export const coreFuzzers : ReadonlyArray < Fuzzer > = [
421
396
concrete ( fuzzNumber , 'NumberType' ) ,
422
- concreteNamed ( fuzzInt , 'Int' ) ,
397
+ concreteFuzzerByName ( fuzzInt , 'Int' ) ,
423
398
concrete ( fuzzBoolean , 'BooleanType' ) ,
424
399
concrete ( fuzzString , 'StringType' ) ,
425
400
concrete ( fuzzNull , 'NullType' ) ,
@@ -439,4 +414,4 @@ export const coreFuzzers = [
439
414
gen ( fuzzKeyof , 'KeyofType' ) ,
440
415
gen ( fuzzTuple , 'TupleType' ) ,
441
416
gen ( fuzzRecursive , 'RecursiveType' ) ,
442
- ] ;
417
+ ] as ReadonlyArray < Fuzzer > ;
0 commit comments