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
const arb = miniFc.integer(0, 100); | |
abr.shrink(64) // potential output: 32, 16, 8, 4, 2, 1, 0 |
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
type Generator<T> = { | |
generate(mrng: Random): T; | |
shrink(value: T): IterableIterator<T>; | |
} |
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
Property failed after 11 runs with value ["","w","vmethwd"] (seed: 42) |
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.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(); | |
} | |
} |
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.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)}`); | |
} | |
} | |
} |
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
declare function assert<T>(property: Property<T>): void; |
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
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) | |
) | |
) |
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); | |
}, | |
run(valueUnderTest) { | |
return predicate(valueUnderTest); | |
} | |
} | |
} |
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
type Property<T> = { | |
generate(mrng: Random): T; | |
run(valueUnderTest: T): boolean; | |
} |
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.string = () => map( | |
miniFc.array(miniFc.character()), | |
characters => characters.join('') | |
) | |
miniFc.dictionary = (valueGenerator) => map( | |
miniFc.array( | |
miniFc.tuple( | |
miniFc.string(), | |
valueGenerator |