Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fc.property(fc.integer().noShrink(), fc.nat(100), (seed, numValues) => {
// create unused logOnCheck
const logOnCheck: { data: string[] } = { data: [] };
// generate scenario and simulate execution
const rng = prand.xorshift128plus(seed);
const refArbitrary = commands([
constant(new SuccessCommand(logOnCheck)),
constant(new SkippedCommand(logOnCheck)),
constant(new FailureCommand(logOnCheck)),
nat().map(v => new SuccessIdCommand(v))
]);
const refShrinkable: Shrinkable> = refArbitrary.generate(new Random(rng));
simulateCommands(refShrinkable.value_);
// trigger computation of replayPath
// and extract shrinks for ref
const refShrinks = [
...refShrinkable
.shrink()
.take(numValues)
.map(s => [...s.value_].map(c => c.toString()))
fc.property(fc.integer().noShrink(), fc.nat(), (seed, mod) => {
const mrng = new Random(prand.xorshift128plus(seed));
const generated = smallIntWithShrink.generate(mrng);
const fromValue = smallIntWithShrink.shrinkableFor(generated.value);
// Browsing shrinking trees in order to check if there is a mismatch somewhere
// Only consider a single path of the tree, does not check all the branches
let generatedShrinks = generated.shrink();
let fromValueShrinks = fromValue.shrink();
while (true) {
const generatedTab = Array.from(generatedShrinks);
const fromValueTab = Array.from(fromValueShrinks);
expect(generatedTab.map(s => s.value)).toEqual(fromValueTab.map(s => s.value));
if (generatedTab.length === 0) break;
generatedShrinks = generatedTab[mod % generatedTab.length].shrink();
fromValueShrinks = fromValueTab[mod % fromValueTab.length].shrink();
}
})
({ seed, shrinkPathSeed, bias, arbitrarySettings }) => {
resetAssertFunction();
// Generate base shrinkables
let arbs = arbitraryBuilders.map(b => b(arbitrarySettings));
if (bias !== null) {
arbs = arbs.map(a => a.withBias(bias));
}
const shrinkables: (Shrinkable | null)[] = arbs.map(a =>
a.generate(new Random(prand.xorshift128plus(seed)))
);
expect(shrinkables.every(s => s !== null)).toBe(true);
const shrinkPathRandom = new Random(prand.xorshift128plus(shrinkPathSeed));
while (shrinkables.some(s => s !== null)) {
// Assert
assertFunction(shrinkables.map(s => s!.value), arbitrarySettings);
// Shrink all shrinkables together
const g = zipStreams(shrinkables.map(s => s!.shrink()));
let c = g.next();
let lastCursorValue = c.done ? undefined : c.value;
while (!c.done && shrinkPathRandom.nextDouble() < 0.9) {
c = g.next();
if (!c.done) lastCursorValue = c.value;
}
for (let i = 0; i !== shrinkables.length; ++i)
shrinkables[i] = lastCursorValue !== undefined ? lastCursorValue[i] : null;
}
}
fc.property(fc.integer(), seed => {
const mrng = new Random(prand.xorshift128plus(seed));
let shrinkable = unicodeJson().generate(mrng);
const originalValue = shrinkable.value;
while (shrinkable.shrink().has(v => true)[0]) {
shrinkable = shrinkable.shrink().next().value;
} // only check one shrink path
expect(typeof shrinkable.value).toEqual('string');
assertShrinkedValue(JSON.parse(originalValue), JSON.parse(shrinkable.value));
})
));
it(`should be able to generate ${label}`, () => {
let numTries = 0;
const mrng = new fc.Random(prand.xorshift128plus(seed));
const arb = fc.anything({ withBoxedValues: true, withMap: true, withSet: true, withObjectString: true });
while (++numTries <= 10000) {
const { value } = arb.generate(mrng);
if (typeof value === typeofLabel && Object.prototype.toString.call(value) === toStringLabel) {
return;
}
}
fail(`Was not able to generate ${label}`);
});
};
it('Should be able to resume a stopped run by specifying replayPath in fc.commands', () => {
const mrng = new fc.Random(prand.mersenne(seed));
const out = fc.check(buildProp(undefined, mrng), { seed: seed });
expect(out.failed).toBe(true);
const path = out.counterexamplePath!;
const replayPath = /\/\*replayPath=['"](.*)['"]\*\//.exec(out.counterexample![0].toString())![1];
const outReplayed = fc.check(buildProp(replayPath), { seed, path });
expect(outReplayed.counterexamplePath).toContain(out.counterexamplePath);
expect(outReplayed.counterexamplePath!.startsWith(out.counterexamplePath!)).toBe(true);
});
});
fc.property(fc.integer(), (seed: number) => {
const mrng = new Random(prand.xorshift128plus(seed));
const logOnCheck: { data: string[] } = { data: [] };
const baseCommands = commands([
constant(new SuccessCommand(logOnCheck)),
constant(new SkippedCommand(logOnCheck)),
constant(new FailureCommand(logOnCheck))
]).generate(mrng);
return baseCommands.hasToBeCloned;
})
));
const checkProduce = (settings: ObjectConstraints.Settings, f: (v: any) => boolean) => {
let numRuns = 0;
const seed = 0;
const mrng = new Random(prand.xorshift128plus(seed));
const arb = anything(settings);
while (++numRuns <= 1000) {
if (f(arb.generate(mrng).value)) return;
}
fail('Failed to generate the expected value');
};
const checkProduceBoxed = (className: string, basicValue: T) => {
fc.property(fc.integer(), fc.array(fc.nat(100)), (seed, builderSizes) => {
const totalSize = builderSizes.reduce((a, b) => a + b, 0);
fc.pre(totalSize > 0);
const entries: { num: number; build: (v: number) => any }[] = [];
for (let idx = 0, currentSize = 0; idx !== builderSizes.length; currentSize += builderSizes[idx], ++idx) {
entries.push({ num: builderSizes[idx], build: v => v + currentSize });
}
const refArb = nat(totalSize - 1);
const arb = mapToConstant(...entries);
const refMrng = new Random(prand.xorshift128plus(seed));
const mrng = new Random(prand.xorshift128plus(seed));
expect(arb.generate(mrng).value).toEqual(refArb.generate(refMrng).value);
})
));
fc.property(fc.integer(), fc.integer(0, 100), (seed, num) => {
const mrng = new Random(prand.xorshift128plus(seed));
const g = lorem(num).generate(mrng).value;
expect(g).not.toContain('.');
})
));