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

Commit

Permalink
feat: fluent record fuzzer configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
holvonixAdvay committed Aug 25, 2019
1 parent f5c52f0 commit b0a20bd
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ currently:
- maximum array length (`array`, `readonlyArray`, `UnknownArray`)
- extra properties inserted into `partial` and `type` (interface) objects
- type used to fuzz `unknown` types
- maximum record count (`record`, `UnknownRecord`)

### Fuzz Recursive Types

Expand Down
14 changes: 14 additions & 0 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
unknownFuzzer,
arrayFuzzer,
coreFuzzers,
recordFuzzer,
unknownRecordFuzzer,
} from './core';

export interface Registry {
Expand All @@ -23,6 +25,8 @@ export interface FluentRegistry extends Registry {
withUnknownFuzzer(codec?: t.Decoder<unknown, unknown>): FluentRegistry;
withArrayFuzzer(maxLength?: number): FluentRegistry;
withAnyArrayFuzzer(maxLength?: number): FluentRegistry;
withRecordFuzzer(maxCount?: number): FluentRegistry;
withUnknownRecordFuzzer(maxCount?: number): FluentRegistry;
withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry;
withPartialFuzzer(extra?: t.Props): FluentRegistry;
withInterfaceFuzzer(extra?: t.Props): FluentRegistry;
Expand Down Expand Up @@ -63,6 +67,16 @@ class FluentifiedRegistry implements FluentRegistry {
return this;
}

withRecordFuzzer(maxCount?: number): FluentRegistry {
this.register(recordFuzzer(maxCount));
return this;
}

withUnknownRecordFuzzer(maxCount?: number): FluentRegistry {
this.register(unknownRecordFuzzer(maxCount));
return this;
}

withReadonlyArrayFuzzer(maxLength?: number): FluentRegistry {
this.register(readonlyArrayFuzzer(maxLength));
return this;
Expand Down
112 changes: 112 additions & 0 deletions test/test-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,118 @@ describe('registry', () => {
});
});

describe('#withRecordFuzzer', () => {
describe('on the core registry', () => {
it(`overrides the record fuzzer max count`, () => {
const b = t.record(t.string, t.number);
const r0 = lib.createCoreRegistry();
const r = lib
.fluent(r0)
.withRecordFuzzer(3)
.exampleGenerator(b);
for (let i = 0; i < 100; i++) {
assert.ok(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length <= 3
);
}
});

it(`does not affect unknown records`, () => {
const b = t.UnknownRecord;
const r0 = lib.createCoreRegistry();
const r = lib
.fluent(r0)
.withRecordFuzzer(3)
.exampleGenerator(b);
let ml = 0;
for (let i = 0; i < 100; i++) {
ml = Math.max(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length,
ml
);
}
assert.ok(ml > 3);
});

it(`overrides apply to the underlying registry`, () => {
const b = t.record(t.string, t.number);
const r0 = lib.createCoreRegistry();
lib.fluent(r0).withRecordFuzzer(3);
const r = r0.exampleGenerator(b);
for (let i = 0; i < 100; i++) {
assert.ok(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length <= 3
);
}
});
});
});

describe('#withUnknownRecordFuzzer', () => {
describe('on the core registry', () => {
it(`overrides the unknown record fuzzer max count`, () => {
const b = t.UnknownRecord;
const r0 = lib.createCoreRegistry();
const r = lib
.fluent(r0)
.withUnknownRecordFuzzer(3)
.exampleGenerator(b);
for (let i = 0; i < 100; i++) {
assert.ok(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length <= 3
);
}
});

it(`does not affect records`, () => {
const b = t.record(t.string, t.unknown);
const r0 = lib.createCoreRegistry();
const r = lib
.fluent(r0)
.withUnknownRecordFuzzer(3)
.exampleGenerator(b);
let ml = 0;
for (let i = 0; i < 100; i++) {
ml = Math.max(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length,
ml
);
}
assert.ok(ml > 3);
});

it(`overrides apply to the underlying registry`, () => {
const b = t.UnknownRecord;
const r0 = lib.createCoreRegistry();
lib.fluent(r0).withUnknownRecordFuzzer(3);
const r = r0.exampleGenerator(b);
for (let i = 0; i < 100; i++) {
assert.ok(
Object.getOwnPropertyNames(r.encode([
i,
fuzzContext({ maxRecursionHint: 10 }),
]) as object).length <= 3
);
}
});
});
});

describe('#withAnyArrayFuzzer', () => {
describe('on the core registry', () => {
it(`overrides the any array fuzzer max length`, () => {
Expand Down

0 comments on commit b0a20bd

Please sign in to comment.