1
1
import * as t from 'io-ts' ;
2
2
import { Fuzzer , ConcreteFuzzer , fuzzGenerator } from '../fuzzer' ;
3
- import { isLeft } from 'fp-ts/lib/Either' ;
3
+ import { isLeft , isRight } from 'fp-ts/lib/Either' ;
4
4
5
5
export type BasicType =
6
6
| t . NumberType
@@ -18,15 +18,15 @@ export type BasicType =
18
18
| t . LiteralType < string | number | boolean >
19
19
| t . KeyofType < { [ key : string ] : unknown } >
20
20
| t . TupleType < t . Mixed [ ] >
21
+ | t . ExactType < t . Mixed >
21
22
// not yet supported:
22
23
| t . AnyArrayType
23
24
| t . AnyDictionaryType
24
25
| t . RefinementType < t . Mixed >
25
26
| t . RecursiveType < t . Mixed >
26
27
| t . DictionaryType < t . Mixed , t . Mixed >
27
28
| t . ReadonlyType < t . Mixed >
28
- | t . ReadonlyArrayType < t . Mixed >
29
- | t . ExactType < t . Mixed > ;
29
+ | t . ReadonlyArrayType < t . Mixed > ;
30
30
31
31
export type basicFuzzGenerator <
32
32
T ,
@@ -156,6 +156,21 @@ export function fuzzTuple(
156
156
} ;
157
157
}
158
158
159
+ export function fuzzExact ( b : t . ExactC < t . HasProps > ) : ConcreteFuzzer < unknown > {
160
+ return {
161
+ children : [ b . type ] ,
162
+ func : ( n , h0 ) => {
163
+ const r = h0 . encode ( n ) ;
164
+ const d = b . decode ( r ) ;
165
+ /* istanbul ignore if */
166
+ if ( ! isRight ( d ) ) {
167
+ throw new Error ( `codec failed to decode underlying example` ) ;
168
+ }
169
+ return d . right ;
170
+ } ,
171
+ } ;
172
+ }
173
+
159
174
export const defaultMaxArrayLength = 13 ;
160
175
161
176
const fuzzArrayWithMaxLength = ( maxLength : number = defaultMaxArrayLength ) => (
@@ -291,19 +306,20 @@ export function fuzzIntersection(
291
306
292
307
export const coreFuzzers = [
293
308
concrete ( fuzzNumber , 'NumberType' ) ,
309
+ concreteNamed ( fuzzInt , 'Int' ) ,
294
310
concrete ( fuzzBoolean , 'BooleanType' ) ,
295
311
concrete ( fuzzString , 'StringType' ) ,
296
312
concrete ( fuzzNull , 'NullType' ) ,
297
313
concrete ( fuzzUndefined , 'UndefinedType' ) ,
298
314
concrete ( fuzzVoid , 'VoidType' ) ,
299
315
concrete ( fuzzUnknown , 'UnknownType' ) ,
300
- gen ( fuzzUnion , 'UnionType' ) ,
301
316
interfaceFuzzer ( ) ,
302
317
partialFuzzer ( ) ,
303
318
arrayFuzzer ( ) ,
319
+ gen ( fuzzExact , 'ExactType' ) ,
320
+ gen ( fuzzUnion , 'UnionType' ) ,
304
321
gen ( fuzzIntersection , 'IntersectionType' ) ,
305
322
gen ( fuzzLiteral , 'LiteralType' ) ,
306
323
gen ( fuzzKeyof , 'KeyofType' ) ,
307
324
gen ( fuzzTuple , 'TupleType' ) ,
308
- concreteNamed ( fuzzInt , 'Int' ) ,
309
325
] ;
0 commit comments