Skip to content

Instantly share code, notes, and snippets.

const arb = miniFc.integer(0, 100);
abr.shrink(64) // potential output: 32, 16, 8, 4, 2, 1, 0
type Generator<T> = {
generate(mrng: Random): T;
shrink(value: T): IterableIterator<T>;
}
Property failed after 11 runs with value ["","w","vmethwd"] (seed: 42)
miniFc.assert = (property, { seed = Date.now() } = {}) => {
let rng = prand.xoroshiro128plus(seed);
for (let runId = 0 ; runId !== 100 ; ++runId) {
const valueUnderTest = property.generate(new Random(rng));
if (!property.run(valueUnderTest)) {
throw new Error(`Property failed after ${runId + 1} runs with value ${JSON.stringify(valueUnderTest)} (seed: ${seed})`);
}
rng = rng.jump();
}
}
miniFc.assert = property => {
for (let runId = 0 ; runId !== 100 ; ++runId) {
const seed = runId;
const mrng = new Random(prand.xoroshiro128plus(seed));
const valueUnderTest = property.generate(mrng);
if (!property.run(valueUnderTest)) {
throw new Error(`Property failed after ${runId + 1} runs with value ${JSON.stringify(valueUnderTest)}`);
}
}
}
declare function assert<T>(property: Property<T>): void;
const isSubstring = (pattern, text) => {
return text.indexOf(pattern) > 0;
}
miniFc.assert(
miniFc.property(
miniFc.tuple(miniFc.string(), miniFc.string(), miniFc.string()),
([a, b, c]) => isSubstring(b, a + b + c)
)
)
miniFc.property = (generator, predicate) => {
return {
generate(mrng) {
return generator.generate(mrng);
},
run(valueUnderTest) {
return predicate(valueUnderTest);
}
}
}
type Property<T> = {
generate(mrng: Random): T;
run(valueUnderTest: T): boolean;
}
miniFc.string = () => map(
miniFc.array(miniFc.character()),
characters => characters.join('')
)
miniFc.dictionary = (valueGenerator) => map(
miniFc.array(
miniFc.tuple(
miniFc.string(),
valueGenerator