Skip to content

Instantly share code, notes, and snippets.

View dubzzz's full-sized avatar
🍼
Limited availability

Nicolas DUBIEN dubzzz

🍼
Limited availability
View GitHub Profile
miniFc.tuple = (...itemGenerators) => {
return {
generate(mrng) {
return itemGenerators.map(g => g.generate(mrng));
},
*shrink(value) {
for (let index = 0 ; index !== itemGenerators.length ; ++index) {
const currentGenerator = itemGenerators[index];
const currentValue = value[index];
for (const shrunkValue of currentGenerator.shrink(currentValue)) {
miniFc.integer = (min, max) => {
return {
generate(mrng) {
return mrng.next(min, max);
},
*shrink(value) {
while (value !== min) {
value = min + Math.floor((value - min) / 2);
yield value;
}
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);
}
}
}