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

Commit aa46fc6

Browse files
committed
feat: fluent Registry#register function and a Registry#exampleGenerator implementation
1 parent 5c3aa19 commit aa46fc6

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE) [![npm](https://img.shields.io/npm/v/io-ts-fuzzer.svg)](https://www.npmjs.com/package/io-ts-fuzzer) [![Build Status](https://travis-ci.com/holvonix-open/io-ts-fuzzer.svg?branch=master)](https://travis-ci.com/holvonix-open/io-ts-fuzzer) [![GitHub last commit](https://img.shields.io/github/last-commit/holvonix-open/io-ts-fuzzer.svg)](https://github.com/holvonix-open/io-ts-fuzzer/commits) [![codecov](https://codecov.io/gh/holvonix-open/io-ts-fuzzer/branch/master/graph/badge.svg)](https://codecov.io/gh/holvonix-open/io-ts-fuzzer) [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=holvonix-open/io-ts-fuzzer)](https://dependabot.com) [![DeepScan grade](https://deepscan.io/api/teams/4465/projects/6653/branches/56883/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=4465&pid=6653&bid=56883) [![Code Style: Google](https://img.shields.io/badge/code%20style-google-blueviolet.svg)](https://github.com/google/gts) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
44

5+
`io-ts-fuzzer` lets you generate examples of a given `io-ts` type.
56

67
## Quick Start
78

@@ -30,6 +31,8 @@ function fuzz() {
3031
}
3132
````
3233

34+
## Types Supported
35+
3336
Currently supports:
3437

3538
* `t.number`
@@ -48,13 +51,15 @@ Currently supports:
4851

4952
## Use Cases
5053

51-
### Verifying Decoder Behavior
54+
### Generating Conforming Examples and Verifying Decoder Behavior
5255

5356
Given a `d = t.Decoder<I,A>` (aka a `t.Type`), `fuzz.exampleGenerator` will
5457
build a `t.Encoder<number,A>` that will give example instances of `A`.
5558
The example instances should all pass on `d.decode`, which should return
5659
an identical example. No exceptions should be thrown.
5760

61+
### Fuzzing a Type (TODO)
62+
5863

5964
## License
6065

src/registry.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Fuzzer } from './fuzzer';
1+
import { Fuzzer, ExampleGenerator, exampleGenerator } from './fuzzer';
22
import * as t from 'io-ts';
33
import { coreFuzzers } from './core/';
44

55
export interface Registry {
66
// tslint:disable-next-line:no-any
7-
register(...vv: Array<Fuzzer<any, t.Decoder<unknown, any>>>): void;
7+
register(...vv: Array<Fuzzer<any, t.Decoder<unknown, any>>>): Registry;
8+
9+
exampleGenerator<T>(d: t.Decoder<unknown, T>): ExampleGenerator<T>;
810

911
getFuzzer<T>(
1012
a: t.Decoder<unknown, T>
@@ -35,11 +37,22 @@ class RegistryC implements Registry {
3537
Fuzzer<any, t.Decoder<unknown, unknown>>
3638
> = new Map();
3739

40+
readonly exampleGenerator: <T>(
41+
d: t.Decoder<unknown, T>
42+
) => ExampleGenerator<T>;
43+
44+
constructor() {
45+
this.exampleGenerator = exampleGenerator.bind(null, this) as <T>(
46+
d: t.Decoder<unknown, T>
47+
) => ExampleGenerator<T>;
48+
}
49+
3850
// tslint:disable-next-line:no-any
3951
register(...vv: Array<Fuzzer<any, t.Decoder<unknown, any>>>) {
4052
for (const v of vv) {
4153
this.fuzzers.set(fuzzerKey(v), v);
4254
}
55+
return this;
4356
}
4457

4558
getFuzzer<T>(a: t.Decoder<unknown, T>) {

test/test-registry.ts

+9
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,14 @@ describe('registry', () => {
2020
});
2121
}
2222
});
23+
24+
describe('#exampleGenerator', () => {
25+
for (const b of types) {
26+
it(`can create an example generator for \`${b.name}\` type`, () => {
27+
const r = lib.createCoreRegistry().exampleGenerator(b);
28+
assert.ok(r);
29+
});
30+
}
31+
});
2332
});
2433
});

0 commit comments

Comments
 (0)