Skip to content

Instantly share code, notes, and snippets.

@dubzzz
Created July 2, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubzzz/dffecfc7156900a802c010deb61b0561 to your computer and use it in GitHub Desktop.
Save dubzzz/dffecfc7156900a802c010deb61b0561 to your computer and use it in GitHub Desktop.
miniFc.property = (generator, predicate) => {
return {
generate(mrng) {
return generator.generate(mrng);
},
shrink(value) {
return generator.shrink(value);
},
run(valueUnderTest) {
return predicate(valueUnderTest);
}
}
}
function executeAndShrink(valueUnderTest, property) {
if (!property.run(valueUnderTest)) {
for (const shrunkValue of property.shrink(valueUnderTest)) {
const shrunkResults = executeAndShrink(shrunkValue, property);
if (shrunkResults.failed) {
return shrunkResults;
}
}
return { failed: true, value: valueUnderTest };
}
return { failed: false };
}
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));
const testResults = executeAndShrink(valueUnderTest, property);
if (testResults.failed) {
throw new Error(`Property failed after ${runId + 1} runs with value ${JSON.stringify(testResults.value)} (seed: ${seed})`);
}
rng = rng.jump();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment