Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 1a433d1

Browse files
committed
feat: support tuple type
1 parent 92bbc99 commit 1a433d1

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Currently supports (and their nested closure):
5050
* `t.Int`
5151
* `t.literal`
5252
* `t.keyof`
53+
* `t.tuple`
5354

5455
## Use Cases
5556

src/core/core.ts

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,30 @@ import * as t from 'io-ts';
22
import { Fuzzer, ConcreteFuzzer, fuzzGenerator } from '../fuzzer';
33

44
export type BasicType =
5+
| t.NumberType
6+
| t.BooleanType
7+
| t.StringType
58
| t.NullType
69
| t.UndefinedType
710
| t.VoidType
811
| t.UnknownType
9-
| t.StringType
10-
| t.NumberType
11-
| t.BooleanType
12+
| t.UnionType<t.Mixed[]>
13+
| t.InterfaceType<unknown>
14+
| t.PartialType<unknown>
15+
| t.ArrayType<t.Mixed>
16+
| t.IntersectionType<t.Mixed[]>
17+
| t.LiteralType<string | number | boolean>
18+
| t.KeyofType<{ [key: string]: unknown }>
19+
| t.TupleType<t.Mixed[]>
20+
// not yet supported:
1221
| t.AnyArrayType
1322
| t.AnyDictionaryType
1423
| t.RefinementType<t.Mixed>
15-
| t.LiteralType<string | number | boolean>
16-
| t.KeyofType<{ [key: string]: unknown }>
1724
| t.RecursiveType<t.Mixed>
18-
| t.ArrayType<t.Mixed>
19-
| t.InterfaceType<unknown>
20-
| t.PartialType<unknown>
2125
| t.DictionaryType<t.Mixed, t.Mixed>
22-
| t.UnionType<t.Mixed[]>
23-
| t.IntersectionType<t.Mixed[]>
24-
| t.InterfaceType<unknown>
25-
| t.TupleType<t.Mixed[]>
2626
| t.ReadonlyType<t.Mixed>
2727
| t.ReadonlyArrayType<t.Mixed>
28-
| t.ExactType<t.Mixed>
29-
| t.UnknownType;
28+
| t.ExactType<t.Mixed>;
3029

3130
export type basicFuzzGenerator<
3231
T,
@@ -162,6 +161,17 @@ export function fuzzInterface(
162161
};
163162
}
164163

164+
export function fuzzTuple(
165+
b: t.TupleType<t.Mixed[]>
166+
): ConcreteFuzzer<unknown[]> {
167+
return {
168+
children: b.types,
169+
func: (n, ...h) => {
170+
return h.map((v, i) => v.encode(n + i));
171+
},
172+
};
173+
}
174+
165175
export const defaultMaxArrayLength = 13;
166176

167177
const fuzzArrayWithMaxLength = (maxLength: number = defaultMaxArrayLength) => (
@@ -228,7 +238,6 @@ export function fuzzIntersection(
228238

229239
export const coreFuzzers = [
230240
concrete(fuzzNumber, 'NumberType'),
231-
concreteNamed(fuzzInt, 'Int'),
232241
concrete(fuzzBoolean, 'BooleanType'),
233242
concrete(fuzzString, 'StringType'),
234243
concrete(fuzzNull, 'NullType'),
@@ -242,4 +251,6 @@ export const coreFuzzers = [
242251
gen(fuzzIntersection, 'IntersectionType'),
243252
gen(fuzzLiteral, 'LiteralType'),
244253
gen(fuzzKeyof, 'KeyofType'),
254+
gen(fuzzTuple, 'TupleType'),
255+
concreteNamed(fuzzInt, 'Int'),
245256
];

test/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const types = [
2626
this: null,
2727
0.3: null,
2828
}),
29+
t.tuple([t.number, t.string, t.boolean]),
2930
// Complex types
3031
t.type({ s: t.string, m: t.type({ n: t.Int }) }),
3132
t.type({

0 commit comments

Comments
 (0)