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

Commit 0ff9aa7

Browse files
committed
feat: support UnknownArray
1 parent 279ab9e commit 0ff9aa7

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Currently supports (and their nested closure):
5353
* `t.undefined`
5454
* `t.union`
5555
* `t.unknown`
56+
* `t.UnknownArray`
5657
* `t.void`
5758

5859
## Use Cases

src/core/core.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Fuzzer, ConcreteFuzzer, fuzzGenerator } from '../fuzzer';
33
import { isLeft, isRight } from 'fp-ts/lib/Either';
44

55
export type BasicType =
6+
| t.AnyArrayType
67
| t.ArrayType<t.Mixed>
78
| t.BooleanType
89
| t.ExactType<t.Mixed>
@@ -22,7 +23,6 @@ export type BasicType =
2223
| t.UnknownType
2324
| t.VoidType
2425
// not yet supported:
25-
| t.AnyArrayType
2626
| t.AnyDictionaryType
2727
| t.DictionaryType<t.Mixed, t.Mixed>
2828
| t.RecursiveType<t.Mixed>
@@ -183,20 +183,24 @@ export function fuzzReadonly(
183183
};
184184
}
185185

186+
function arrayFuzzFunc(maxLength: number) {
187+
return (n: number, h0: t.Encoder<number, unknown>) => {
188+
const ret = [];
189+
for (let index = 0; index < n % maxLength; index++) {
190+
ret.push(h0.encode(n + index));
191+
}
192+
return ret;
193+
};
194+
}
195+
186196
export const defaultMaxArrayLength = 13;
187197

188198
const fuzzArrayWithMaxLength = (maxLength: number = defaultMaxArrayLength) => (
189199
b: t.ArrayType<t.Mixed>
190200
): ConcreteFuzzer<unknown[]> => {
191201
return {
192202
children: [b.type],
193-
func: (n, h0) => {
194-
const ret = [];
195-
for (let index = 0; index < n % maxLength; index++) {
196-
ret.push(h0.encode(n + index));
197-
}
198-
return ret;
199-
},
203+
func: arrayFuzzFunc(maxLength),
200204
};
201205
};
202206

@@ -228,6 +232,19 @@ export function readonlyArrayFuzzer(maxLength: number = defaultMaxArrayLength) {
228232
return gen(fuzzReadonlyArrayWithMaxLength(maxLength), 'ReadonlyArrayType');
229233
}
230234

235+
const fuzzAnyArrayWithMaxLength = (maxLength: number) => (
236+
b: t.AnyArrayType
237+
): ConcreteFuzzer<unknown[]> => {
238+
return {
239+
children: [t.unknown],
240+
func: arrayFuzzFunc(maxLength),
241+
};
242+
};
243+
244+
export function anyArrayFuzzer(maxLength: number = defaultMaxArrayLength) {
245+
return gen(fuzzAnyArrayWithMaxLength(maxLength), 'AnyArrayType');
246+
}
247+
231248
export const defaultExtraProps = { ___0000_extra_: t.number };
232249

233250
const fuzzPartialWithExtraCodec = (extra: t.Props = defaultExtraProps) => (
@@ -347,6 +364,7 @@ export const coreFuzzers = [
347364
interfaceFuzzer(),
348365
partialFuzzer(),
349366
arrayFuzzer(),
367+
anyArrayFuzzer(),
350368
gen(fuzzExact, 'ExactType'),
351369
gen(fuzzReadonly, 'ReadonlyType'),
352370
readonlyArrayFuzzer(),

src/registry.ts

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
partialFuzzer,
66
interfaceFuzzer,
77
readonlyArrayFuzzer,
8+
anyArrayFuzzer,
89
} from './core/core';
910

1011
export interface Registry {
@@ -18,6 +19,7 @@ export interface Registry {
1819

1920
export interface FluentRegistry extends Registry {
2021
withArrayFuzzer(maxLength?: number): FluentRegistry;
22+
withAnyArrayFuzzer(maxLength?: number): FluentRegistry;
2123
withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry;
2224
withPartialFuzzer(extra?: t.Props): FluentRegistry;
2325
withInterfaceFuzzer(extra?: t.Props): FluentRegistry;
@@ -48,6 +50,11 @@ class FluentifiedRegistry implements FluentRegistry {
4850
return this;
4951
}
5052

53+
withAnyArrayFuzzer(maxLength?: number): FluentRegistry {
54+
this.register(anyArrayFuzzer(maxLength));
55+
return this;
56+
}
57+
5158
withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry {
5259
this.register(readonlyArrayFuzzer(maxLength));
5360
return this;

test/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const types = [
2121
t.unknown,
2222
t.array(t.string),
2323
t.readonlyArray(t.string),
24+
t.UnknownArray,
2425
t.array(t.partial({ s: t.string, m: t.number })),
2526
t.readonlyArray(t.partial({ s: t.string, m: t.number })),
2627
t.Int,

test/test-registry.ts

+44
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,50 @@ describe('registry', () => {
199199
});
200200
});
201201

202+
describe('#withAnyArrayFuzzer', () => {
203+
describe('on the core registry', () => {
204+
it(`overrides the any array fuzzer max length`, () => {
205+
const b = t.UnknownArray;
206+
const r0 = lib.createCoreRegistry();
207+
const r = lib
208+
.fluent(r0)
209+
.withAnyArrayFuzzer(3)
210+
.exampleGenerator(b);
211+
for (let i = 0; i < 100; i++) {
212+
assert.ok(r.encode(i).length <= 3);
213+
}
214+
});
215+
216+
it(`does not affect specific arrays`, () => {
217+
const b = t.array(t.number);
218+
const r0 = lib.createCoreRegistry();
219+
const r = lib
220+
.fluent(r0)
221+
.withAnyArrayFuzzer(3)
222+
.exampleGenerator(b);
223+
let ml = 0;
224+
for (let i = 0; i < 100; i++) {
225+
ml = Math.max(r.encode(i).length, ml);
226+
}
227+
assert.ok(ml > 3);
228+
});
229+
230+
it(`does not affect readonly arrays`, () => {
231+
const b = t.readonlyArray(t.number);
232+
const r0 = lib.createCoreRegistry();
233+
const r = lib
234+
.fluent(r0)
235+
.withAnyArrayFuzzer(3)
236+
.exampleGenerator(b);
237+
let ml = 0;
238+
for (let i = 0; i < 100; i++) {
239+
ml = Math.max(r.encode(i).length, ml);
240+
}
241+
assert.ok(ml > 3);
242+
});
243+
});
244+
});
245+
202246
describe('#withReadonlyArrayFuzzer', () => {
203247
describe('on the core registry', () => {
204248
it(`overrides the array fuzzer max length`, () => {

0 commit comments

Comments
 (0)