Created
July 2, 2020 18:17
-
-
Save dubzzz/dffecfc7156900a802c010deb61b0561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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