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

Commit 54a29de

Browse files
committed
feat: support Ints
1 parent 3cfa6b8 commit 54a29de

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
After `yarn add io-ts io-ts-fuzzer`...
99

10-
11-
1210
````typescript
1311
import * as t from 'io-ts';
1412
import * as fuzz from 'io-ts-fuzzer';
@@ -23,7 +21,8 @@ function fuzz() {
2321
// Builds a particular fuzzer from the registry.
2422
const fuzzer = fuzz.exampleGenerator(r, target);
2523

26-
// Make examples.
24+
// Make examples. The input number fully determines
25+
// the output example.
2726
console.log(fuzzer.encode(0));
2827
console.log(fuzzer.encode(1));
2928
console.log(fuzzer.encode(2));
@@ -45,6 +44,16 @@ Currently supports:
4544
* `t.undefined`
4645
* `t.void`
4746
* `t.unknown`
47+
* `t.Int`
48+
49+
## Use Cases
50+
51+
### Verifying Decoder Behavior
52+
53+
Given a `d = t.Decoder<I,A>` (aka a `t.Type`), `fuzz.exampleGenerator` will
54+
build a `t.Encoder<number,A>` that will give example instances of `A`.
55+
The example instances should all pass on `d.decode`, which should return
56+
an identical example. No exceptions should be thrown.
4857

4958

5059
## License

src/core/core.ts

+19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ export function concrete<T, C extends t.Decoder<unknown, T> & BasicType>(
6060
};
6161
}
6262

63+
export function concreteNamed<T, C extends t.Decoder<unknown, T>>(
64+
func: ConcreteFuzzer<T>['func'],
65+
name: C['name']
66+
): Fuzzer<T, C> {
67+
return {
68+
impl: {
69+
type: 'fuzzer',
70+
func,
71+
},
72+
id: name,
73+
idType: 'name',
74+
};
75+
}
76+
6377
export function gen<T, C extends t.Decoder<unknown, T> & BasicType>(
6478
func: basicFuzzGenerator<T, C>,
6579
tag: C['_tag']
@@ -82,6 +96,10 @@ export function fuzzNumber(n: number): number {
8296
return n;
8397
}
8498

99+
export function fuzzInt(n: number): t.TypeOf<typeof t.Int> {
100+
return Math.floor(n) as t.TypeOf<typeof t.Int>;
101+
}
102+
85103
export function fuzzString(n: number): string {
86104
return `${n}`;
87105
}
@@ -185,6 +203,7 @@ export function fuzzIntersection(
185203

186204
export const coreFuzzers = [
187205
concrete(fuzzNumber, 'NumberType'),
206+
concreteNamed(fuzzInt, 'Int'),
188207
concrete(fuzzBoolean, 'BooleanType'),
189208
concrete(fuzzString, 'StringType'),
190209
concrete(fuzzNull, 'NullType'),

test/helpers.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export const types = [
1414
t.void,
1515
t.unknown,
1616
t.array(t.string),
17+
t.Int,
1718

1819
// Complex types
19-
t.type({ s: t.string, m: t.type({ n: t.number }) }),
20+
t.type({ s: t.string, m: t.type({ n: t.Int }) }),
2021
t.type({
2122
s: t.union([t.string, t.number, t.partial({ n: t.number, z: t.string })]),
2223
m: t.type({ n: t.number }),
@@ -33,13 +34,14 @@ export interface WeirdStringBrand {
3334
readonly WeirdString: unique symbol;
3435
}
3536

37+
const weirdString = t.brand(
38+
t.string,
39+
(a): a is t.Branded<string, WeirdStringBrand> => a.length > 4,
40+
'WeirdString'
41+
);
42+
3643
export const unknownTypes = [
37-
t.Int,
38-
t.union([t.string, t.Int]),
39-
t.brand(
40-
t.string,
41-
(a): a is t.Branded<string, WeirdStringBrand> => a.length > 4,
42-
'WeirdString'
43-
),
44+
weirdString,
45+
t.union([t.string, weirdString]),
4446
customStringDecoder,
4547
];

test/test-registry.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ describe('registry', () => {
1111
const r = lib.createCoreRegistry().getFuzzer(b);
1212
assert.ok(r);
1313
const x = r!;
14-
assert.deepStrictEqual(x.idType, 'tag');
15-
assert.deepStrictEqual(x.id, b._tag);
14+
assert.ok(x.idType === 'tag' || x.idType === 'name');
15+
if (x.idType === 'name') {
16+
assert.deepStrictEqual(x.id, b.name);
17+
} else {
18+
assert.deepStrictEqual(x.id, b._tag);
19+
}
1620
});
1721
}
1822
});

0 commit comments

Comments
 (0)