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

Commit b0a20bd

Browse files
committed
feat: fluent record fuzzer configuration
1 parent f5c52f0 commit b0a20bd

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ currently:
111111
- maximum array length (`array`, `readonlyArray`, `UnknownArray`)
112112
- extra properties inserted into `partial` and `type` (interface) objects
113113
- type used to fuzz `unknown` types
114+
- maximum record count (`record`, `UnknownRecord`)
114115

115116
### Fuzz Recursive Types
116117

src/registry.ts

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
unknownFuzzer,
99
arrayFuzzer,
1010
coreFuzzers,
11+
recordFuzzer,
12+
unknownRecordFuzzer,
1113
} from './core';
1214

1315
export interface Registry {
@@ -23,6 +25,8 @@ export interface FluentRegistry extends Registry {
2325
withUnknownFuzzer(codec?: t.Decoder<unknown, unknown>): FluentRegistry;
2426
withArrayFuzzer(maxLength?: number): FluentRegistry;
2527
withAnyArrayFuzzer(maxLength?: number): FluentRegistry;
28+
withRecordFuzzer(maxCount?: number): FluentRegistry;
29+
withUnknownRecordFuzzer(maxCount?: number): FluentRegistry;
2630
withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry;
2731
withPartialFuzzer(extra?: t.Props): FluentRegistry;
2832
withInterfaceFuzzer(extra?: t.Props): FluentRegistry;
@@ -63,6 +67,16 @@ class FluentifiedRegistry implements FluentRegistry {
6367
return this;
6468
}
6569

70+
withRecordFuzzer(maxCount?: number): FluentRegistry {
71+
this.register(recordFuzzer(maxCount));
72+
return this;
73+
}
74+
75+
withUnknownRecordFuzzer(maxCount?: number): FluentRegistry {
76+
this.register(unknownRecordFuzzer(maxCount));
77+
return this;
78+
}
79+
6680
withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry {
6781
this.register(readonlyArrayFuzzer(maxLength));
6882
return this;

test/test-registry.ts

+112
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,118 @@ describe('registry', () => {
256256
});
257257
});
258258

259+
describe('#withRecordFuzzer', () => {
260+
describe('on the core registry', () => {
261+
it(`overrides the record fuzzer max count`, () => {
262+
const b = t.record(t.string, t.number);
263+
const r0 = lib.createCoreRegistry();
264+
const r = lib
265+
.fluent(r0)
266+
.withRecordFuzzer(3)
267+
.exampleGenerator(b);
268+
for (let i = 0; i < 100; i++) {
269+
assert.ok(
270+
Object.getOwnPropertyNames(r.encode([
271+
i,
272+
fuzzContext({ maxRecursionHint: 10 }),
273+
]) as object).length <= 3
274+
);
275+
}
276+
});
277+
278+
it(`does not affect unknown records`, () => {
279+
const b = t.UnknownRecord;
280+
const r0 = lib.createCoreRegistry();
281+
const r = lib
282+
.fluent(r0)
283+
.withRecordFuzzer(3)
284+
.exampleGenerator(b);
285+
let ml = 0;
286+
for (let i = 0; i < 100; i++) {
287+
ml = Math.max(
288+
Object.getOwnPropertyNames(r.encode([
289+
i,
290+
fuzzContext({ maxRecursionHint: 10 }),
291+
]) as object).length,
292+
ml
293+
);
294+
}
295+
assert.ok(ml > 3);
296+
});
297+
298+
it(`overrides apply to the underlying registry`, () => {
299+
const b = t.record(t.string, t.number);
300+
const r0 = lib.createCoreRegistry();
301+
lib.fluent(r0).withRecordFuzzer(3);
302+
const r = r0.exampleGenerator(b);
303+
for (let i = 0; i < 100; i++) {
304+
assert.ok(
305+
Object.getOwnPropertyNames(r.encode([
306+
i,
307+
fuzzContext({ maxRecursionHint: 10 }),
308+
]) as object).length <= 3
309+
);
310+
}
311+
});
312+
});
313+
});
314+
315+
describe('#withUnknownRecordFuzzer', () => {
316+
describe('on the core registry', () => {
317+
it(`overrides the unknown record fuzzer max count`, () => {
318+
const b = t.UnknownRecord;
319+
const r0 = lib.createCoreRegistry();
320+
const r = lib
321+
.fluent(r0)
322+
.withUnknownRecordFuzzer(3)
323+
.exampleGenerator(b);
324+
for (let i = 0; i < 100; i++) {
325+
assert.ok(
326+
Object.getOwnPropertyNames(r.encode([
327+
i,
328+
fuzzContext({ maxRecursionHint: 10 }),
329+
]) as object).length <= 3
330+
);
331+
}
332+
});
333+
334+
it(`does not affect records`, () => {
335+
const b = t.record(t.string, t.unknown);
336+
const r0 = lib.createCoreRegistry();
337+
const r = lib
338+
.fluent(r0)
339+
.withUnknownRecordFuzzer(3)
340+
.exampleGenerator(b);
341+
let ml = 0;
342+
for (let i = 0; i < 100; i++) {
343+
ml = Math.max(
344+
Object.getOwnPropertyNames(r.encode([
345+
i,
346+
fuzzContext({ maxRecursionHint: 10 }),
347+
]) as object).length,
348+
ml
349+
);
350+
}
351+
assert.ok(ml > 3);
352+
});
353+
354+
it(`overrides apply to the underlying registry`, () => {
355+
const b = t.UnknownRecord;
356+
const r0 = lib.createCoreRegistry();
357+
lib.fluent(r0).withUnknownRecordFuzzer(3);
358+
const r = r0.exampleGenerator(b);
359+
for (let i = 0; i < 100; i++) {
360+
assert.ok(
361+
Object.getOwnPropertyNames(r.encode([
362+
i,
363+
fuzzContext({ maxRecursionHint: 10 }),
364+
]) as object).length <= 3
365+
);
366+
}
367+
});
368+
});
369+
});
370+
259371
describe('#withAnyArrayFuzzer', () => {
260372
describe('on the core registry', () => {
261373
it(`overrides the any array fuzzer max length`, () => {

0 commit comments

Comments
 (0)