Best JavaScript code snippet using fast-check-monorepo
WithShrinkFromOtherArbitrary.spec.ts
Source:WithShrinkFromOtherArbitrary.spec.ts
1import { WithShrinkFromOtherArbitrary } from '../../../../src/arbitrary/_internals/WithShrinkFromOtherArbitrary';2import { Value } from '../../../../src/check/arbitrary/definition/Value';3import { fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';4import { fakeRandom } from '../__test-helpers__/RandomHelpers';5import { Stream } from '../../../../src/stream/Stream';6import fc from 'fast-check';7describe('WithShrinkFromOtherArbitrary', () => {8 describe('generate', () => {9 it('should only use the first arbitrary to generate values', () => {10 fc.assert(11 fc.property(12 fc.option(fc.integer({ min: 2 }), { nil: undefined }),13 fc.anything(),14 fc.anything(),15 (biasFactor, vA, cA) => {16 // Arrange17 const {18 instance: generatorArbitrary,19 generate: generateA,20 shrink: shrinkA,21 canShrinkWithoutContext: canShrinkWithoutContextA,22 } = fakeArbitrary();23 const {24 instance: shrinkerArbitrary,25 generate: generateB,26 shrink: shrinkB,27 canShrinkWithoutContext: canShrinkWithoutContextB,28 } = fakeArbitrary();29 generateA.mockReturnValueOnce(new Value(vA, cA));30 const { instance: mrng } = fakeRandom();31 // Act32 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);33 const g = arb.generate(mrng, biasFactor);34 // Assert35 expect(g.value).toBe(vA);36 expect(generateA).toHaveBeenCalledWith(mrng, biasFactor);37 expect(shrinkA).not.toHaveBeenCalled();38 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();39 expect(generateB).not.toHaveBeenCalled();40 expect(shrinkB).not.toHaveBeenCalled();41 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();42 }43 )44 );45 });46 });47 describe('canShrinkWithoutContext', () => {48 it.each`49 canShrink50 ${false}51 ${true}52 `(53 'should only use the second arbitrary to check if it can shrink without context (with canShrink=$canShrink)',54 ({ canShrink }) => {55 // Arrange56 const vA = Symbol();57 const {58 instance: generatorArbitrary,59 generate: generateA,60 shrink: shrinkA,61 canShrinkWithoutContext: canShrinkWithoutContextA,62 } = fakeArbitrary();63 const {64 instance: shrinkerArbitrary,65 generate: generateB,66 shrink: shrinkB,67 canShrinkWithoutContext: canShrinkWithoutContextB,68 } = fakeArbitrary();69 canShrinkWithoutContextB.mockReturnValueOnce(canShrink);70 // Act71 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);72 const out = arb.canShrinkWithoutContext(vA);73 // Assert74 expect(out).toBe(canShrink);75 expect(generateA).not.toHaveBeenCalled();76 expect(shrinkA).not.toHaveBeenCalled();77 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();78 expect(generateB).not.toHaveBeenCalled();79 expect(shrinkB).not.toHaveBeenCalled();80 expect(canShrinkWithoutContextB).toHaveBeenCalledWith(vA);81 }82 );83 });84 describe('shrink', () => {85 it('should only use the first arbitrary for values it generated (coming with the context)', () => {86 fc.assert(87 fc.property(88 fc.option(fc.integer({ min: 2 }), { nil: undefined }),89 fc.anything(),90 fc.anything(),91 fc.anything(),92 fc.anything(),93 fc.anything(),94 fc.anything(),95 (biasFactor, vA, cA, vAA, cAA, vAB, cAB) => {96 // Arrange97 const {98 instance: generatorArbitrary,99 generate: generateA,100 shrink: shrinkA,101 canShrinkWithoutContext: canShrinkWithoutContextA,102 } = fakeArbitrary();103 const {104 instance: shrinkerArbitrary,105 generate: generateB,106 shrink: shrinkB,107 canShrinkWithoutContext: canShrinkWithoutContextB,108 } = fakeArbitrary();109 generateA.mockReturnValueOnce(new Value(vA, cA));110 shrinkA.mockReturnValueOnce(Stream.of(new Value(vAA, cAA), new Value(vAB, cAB)));111 const { instance: mrng } = fakeRandom();112 // Act113 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);114 const g = arb.generate(mrng, biasFactor);115 const shrinks = [...arb.shrink(g.value, g.context)];116 // Assert117 expect(shrinks).toHaveLength(2);118 expect(shrinks[0].value).toBe(vAA);119 expect(shrinks[1].value).toBe(vAB);120 expect(generateA).toHaveBeenCalledWith(mrng, biasFactor);121 expect(shrinkA).toHaveBeenCalledWith(vA, cA);122 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();123 expect(generateB).not.toHaveBeenCalled();124 expect(shrinkB).not.toHaveBeenCalled();125 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();126 }127 )128 );129 });130 it('should only use the first arbitrary for values it shrunk (coming with the context)', () => {131 fc.assert(132 fc.property(133 fc.option(fc.integer({ min: 2 }), { nil: undefined }),134 fc.anything(),135 fc.anything(),136 fc.anything(),137 fc.anything(),138 fc.anything(),139 fc.anything(),140 fc.anything(),141 fc.anything(),142 fc.anything(),143 fc.anything(),144 (biasFactor, vA, cA, vAA, cAA, vAB, cAB, vAC, cAC, vABA, cABA) => {145 // Arrange146 const {147 instance: generatorArbitrary,148 generate: generateA,149 shrink: shrinkA,150 canShrinkWithoutContext: canShrinkWithoutContextA,151 } = fakeArbitrary();152 const {153 instance: shrinkerArbitrary,154 generate: generateB,155 shrink: shrinkB,156 canShrinkWithoutContext: canShrinkWithoutContextB,157 } = fakeArbitrary();158 generateA.mockReturnValueOnce(new Value(vA, cA));159 shrinkA.mockReturnValueOnce(Stream.of(new Value(vAA, cAA), new Value(vAB, cAB), new Value(vAC, cAC)));160 shrinkA.mockReturnValueOnce(Stream.of(new Value(vABA, cABA)));161 const { instance: mrng } = fakeRandom();162 // Act163 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);164 const g = arb.generate(mrng, biasFactor);165 const g2 = [...arb.shrink(g.value, g.context)][1];166 const shrinks = [...arb.shrink(g2.value, g2.context)];167 // Assert168 expect(shrinks).toHaveLength(1);169 expect(shrinks[0].value).toBe(vABA);170 expect(generateA).toHaveBeenCalledWith(mrng, biasFactor);171 expect(shrinkA).toHaveBeenCalledWith(vA, cA);172 expect(shrinkA).toHaveBeenCalledWith(vAB, cAB);173 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();174 expect(generateB).not.toHaveBeenCalled();175 expect(shrinkB).not.toHaveBeenCalled();176 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();177 }178 )179 );180 });181 it('should only use the second arbitrary for values coming without any context', () => {182 fc.assert(183 fc.property(184 fc.anything(),185 fc.anything(),186 fc.anything(),187 fc.anything(),188 fc.anything(),189 (vA, vAA, cAA, vAB, cAB) => {190 // Arrange191 const {192 instance: generatorArbitrary,193 generate: generateA,194 shrink: shrinkA,195 canShrinkWithoutContext: canShrinkWithoutContextA,196 } = fakeArbitrary();197 const {198 instance: shrinkerArbitrary,199 generate: generateB,200 shrink: shrinkB,201 canShrinkWithoutContext: canShrinkWithoutContextB,202 } = fakeArbitrary();203 shrinkB.mockReturnValueOnce(Stream.of(new Value(vAA, cAA), new Value(vAB, cAB)));204 // Act205 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);206 const shrinks = [...arb.shrink(vA, undefined)];207 // Assert208 expect(shrinks).toHaveLength(2);209 expect(shrinks[0].value).toBe(vAA);210 expect(shrinks[1].value).toBe(vAB);211 expect(generateA).not.toHaveBeenCalled();212 expect(shrinkA).not.toHaveBeenCalled();213 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();214 expect(generateB).not.toHaveBeenCalled();215 expect(shrinkB).toHaveBeenCalledWith(vA, undefined);216 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();217 }218 )219 );220 });221 it('should only use the second arbitrary for values shrunk by it (coming with the context)', () => {222 fc.assert(223 fc.property(224 fc.option(fc.integer({ min: 2 }), { nil: undefined }),225 fc.anything(),226 fc.anything(),227 fc.anything(),228 fc.anything(),229 fc.anything(),230 fc.anything(),231 fc.anything(),232 fc.anything(),233 fc.anything(),234 (biasFactor, vA, vAA, cAA, vAB, cAB, vAC, cAC, vABA, cABA) => {235 // Arrange236 const {237 instance: generatorArbitrary,238 generate: generateA,239 shrink: shrinkA,240 canShrinkWithoutContext: canShrinkWithoutContextA,241 } = fakeArbitrary();242 const {243 instance: shrinkerArbitrary,244 generate: generateB,245 shrink: shrinkB,246 canShrinkWithoutContext: canShrinkWithoutContextB,247 } = fakeArbitrary();248 shrinkB.mockReturnValueOnce(Stream.of(new Value(vAA, cAA), new Value(vAB, cAB), new Value(vAC, cAC)));249 shrinkB.mockReturnValueOnce(Stream.of(new Value(vABA, cABA)));250 // Act251 const arb = new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);252 const g2 = [...arb.shrink(vA, undefined)][1];253 const shrinks = [...arb.shrink(g2.value, g2.context)];254 // Assert255 expect(shrinks).toHaveLength(1);256 expect(shrinks[0].value).toBe(vABA);257 expect(generateA).not.toHaveBeenCalled();258 expect(shrinkA).not.toHaveBeenCalled();259 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();260 expect(generateB).not.toHaveBeenCalled();261 expect(shrinkB).toHaveBeenCalledWith(vA, undefined);262 expect(shrinkB).toHaveBeenCalledWith(vAB, cAB);263 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();264 }265 )266 );267 });268 });...
WithShrinkFromOtherArbitrary.ts
Source:WithShrinkFromOtherArbitrary.ts
1import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';2import { Value } from '../../check/arbitrary/definition/Value';3import { Random } from '../../random/generator/Random';4import { Stream } from '../../stream/Stream';5/** @internal */6function isSafeContext(context: unknown): context is { generatorContext: unknown } | { shrinkerContext: unknown } {7 return context !== undefined;8}9/** @internal */10function toGeneratorValue<T>(value: Value<T>): Value<T> {11 if (value.hasToBeCloned) {12 return new Value(value.value_, { generatorContext: value.context }, () => value.value);13 }14 return new Value(value.value_, { generatorContext: value.context });15}16/** @internal */17function toShrinkerValue<T>(value: Value<T>): Value<T> {18 if (value.hasToBeCloned) {19 return new Value(value.value_, { shrinkerContext: value.context }, () => value.value);20 }21 return new Value(value.value_, { shrinkerContext: value.context });22}23/** @internal */24export class WithShrinkFromOtherArbitrary<T> extends Arbitrary<T> {25 constructor(private readonly generatorArbitrary: Arbitrary<T>, private readonly shrinkerArbitrary: Arbitrary<T>) {26 super();27 }28 generate(mrng: Random, biasFactor: number | undefined): Value<T> {29 return toGeneratorValue(this.generatorArbitrary.generate(mrng, biasFactor));30 }31 canShrinkWithoutContext(value: unknown): value is T {32 return this.shrinkerArbitrary.canShrinkWithoutContext(value);33 }34 shrink(value: T, context: unknown): Stream<Value<T>> {35 if (!isSafeContext(context)) {36 return this.shrinkerArbitrary.shrink(value, undefined).map(toShrinkerValue);37 }38 if ('generatorContext' in context) {39 return this.generatorArbitrary.shrink(value, context.generatorContext).map(toGeneratorValue);40 }41 return this.shrinkerArbitrary.shrink(value, context.shrinkerContext).map(toShrinkerValue);42 }...
RestrictedIntegerArbitraryBuilder.ts
Source:RestrictedIntegerArbitraryBuilder.ts
1import { Arbitrary } from '../../../check/arbitrary/definition/Arbitrary';2import { integer } from '../../integer';3import { WithShrinkFromOtherArbitrary } from '../WithShrinkFromOtherArbitrary';4/** @internal */5export function restrictedIntegerArbitraryBuilder(min: number, maxGenerated: number, max: number): Arbitrary<number> {6 const generatorArbitrary = integer({ min, max: maxGenerated });7 if (maxGenerated === max) {8 return generatorArbitrary;9 }10 const shrinkerArbitrary = integer({ min, max });11 return new WithShrinkFromOtherArbitrary(generatorArbitrary, shrinkerArbitrary);...
Using AI Code Generation
1const fc = require("fast-check");2const { shrinkerArbitrary } = require("fast-check-monorepo");3const { shrinker } = require("./shrinker");4const { arbitrary } = require("./arbitrary");5const { shrinker: shrinker2 } = require("./shrinker2");6const { arbitrary: arbitrary2 } = require("./arbitrary2");7const { shrinker: shrinker3 } = require("./shrinker3");8const { arbitrary: arbitrary3 } = require("./arbitrary3");9fc.assert(10 fc.property(arbitrary, shrinkerArbitrary(shrinker), (x, y) => {11 console.log("x = ", x);12 console.log("y = ", y);13 return x === y;14 })15);16fc.assert(17 fc.property(arbitrary2, shrinkerArbitrary(shrinker2), (x, y) => {18 console.log("x = ", x);19 console.log("y = ", y);20 return x === y;21 })22);23fc.assert(24 fc.property(arbitrary3, shrinkerArbitrary(shrinker3), (x, y) => {25 console.log("x = ", x);26 console.log("y = ", y);27 return x === y;28 })29);30const fc = require("fast-check");31const shrinker = fc.shrinkArray(fc.shrinkNumber);32module.exports = {33};34const fc = require("fast-check");35const arbitrary = fc.array(fc.integer());36module.exports = {37};38const fc = require("fast-check");39const shrinker = fc.shrinkArray(fc.shrinkNumber);40module.exports = {41};42const fc = require("fast-check");43const arbitrary = fc.array(fc.integer());44module.exports = {45};46const fc = require("fast-check");47const shrinker = fc.shrinkArray(fc.shrinkNumber);48module.exports = {49};50const fc = require("fast-check");
Using AI Code Generation
1import { shrinkerArbitrary } from 'fast-check'2import { shrinkerArbitrary as shrinkerArbitrary2 } from 'fast-check/lib/types/arbitrary/Shrinkable'3const shr = shrinkerArbitrary2([1, 2, 3])4shr.shrink(1)5shr.shrink(2)6shr.shrink(3)7const shr2 = shrinkerArbitrary([1, 2, 3])8shr2.shrink(1)9shr2.shrink(2)10shr2.shrink(3)11import { shrinkerArbitrary } from 'fast-check'12import { shrinkerArbitrary as shrinkerArbitrary2 } from 'fast-check/lib/types/arbitrary/Shrinkable'13const shr = shrinkerArbitrary2([1, 2, 3])14shr.shrink(1)15shr.shrink(2)16shr.shrink(3)17const shr2 = shrinkerArbitrary([1, 2, 3])18shr2.shrink(1)19shr2.shrink(2)20shr2.shrink(3)21 at Object.<anonymous> (/Users/.../test3.js:8:8)22 at Module._compile (internal/modules/cjs/loader.js:1138:30)23 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)24 at Module.load (internal/modules/cjs/loader.js:986:32)25 at Function.Module._load (internal/modules/cjs/loader.js:879:14)26 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Using AI Code Generation
1const { shrinkerArbitrary } = require('fast-check');2const { generate } = require('fast-check/lib/types/arbitrary/Arbitrary');3const { Shrinkable } = require('fast-check/lib/check/arbitrary/definition/Shrinkable');4const { range } = require('lodash');5const generateRange = (min, max) => {6 const rangeArb = shrinkerArbitrary(range(min, max));7 const gen = generate(rangeArb);8 return gen.next().value.value;9};10const shrinkRange = (min, max) => {11 const rangeArb = shrinkerArbitrary(range(min, max));12 const gen = generate(rangeArb);13 const { shrink } = gen.next().value;14 return shrink().map((s) => s.value);15};16const shrinkableRange = (min, max) => {17 const rangeArb = shrinkerArbitrary(range(min, max));18 const gen = generate(rangeArb);19 const { value } = gen.next();20 return value;21};22const min = 0;23const max = 100;24const value = generateRange(min, max);25const shrunk = shrinkRange(min, max);26const shrinkable = shrinkableRange(min, max);27console.log(value);28console.log(shrunk);29console.log(shrinkable);30const { shrinkerArbitrary } = require('fast-check');31const { generate } = require('fast-check/lib/types/arbitrary/Arbitrary');32const { Shrinkable } = require('fast-check/lib/check/arbitrary/definition/Shrinkable');33const { range } = require('lodash');34const generateRange = (min, max) => {35 const rangeArb = shrinkerArbitrary(range(min, max));36 const gen = generate(rangeArb);37 return gen.next().value.value;38};39const shrinkRange = (min, max) => {
Using AI Code Generation
1import * as fc from 'fast-check';2function shrinkerArbitrary(arb) {3 .oneof(4 .oneof(5 fc.integer(0, 10).map((v) => v * 1000),6 fc.integer(-10, 0).map((v) => v * 1000)7 .map((v) => new Date(v))8 .filter((d) => d.getTime() > 0),9 fc.constant(new Date(NaN))10 .filter((d) => !isNaN(d.getTime()));11}12const property = fc.property(fc.date(), (d) => {13 return d.getTime() >= 0;14});15const shrinker = shrinkerArbitrary(fc.date());16fc.assert(17 fc.property(fc.date(), (d) => {18 return d.getTime() >= 0;19 }),20 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true }21);22fc.assert(23 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true },24);25fc.assert(26 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true },27 shrinkerArbitrary(fc.date())28);29fc.assert(30 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true },31 fc.shrinkDate()32);33fc.assert(34 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true },35 fc.shrinkNumber()36);37fc.assert(38 { seed: 42, path: 'test3.js', endOnFailure: true, verbose: true },39 fc.shrinkInteger()40);41fc.assert(42 { seed:
Using AI Code Generation
1const fc = require('fast-check');2const shrinkerArbitrary = fc.shrinkerArbitrary;3const nat = fc.nat;4const arb = nat(100);5const shrinker = shrinkerArbitrary(arb);6const value = 42;7const shrinked = shrinker(value);8console.log(shrinked);
Using AI Code Generation
1const { shrinkerArbitrary } = require('fast-check');2const { shrinker } = require('fast-check/lib/types/arbitrary/Shrinkable');3const obj = {4 a: {5 },6 e: {7 }8};9const shrunkObj = shrinkerArbitrary(obj);10console.log(shrunkObj);11const { shrinkerArbitrary } = require('fast-check');12const { shrinker } = require('fast-check/lib/types/arbitrary/Shrinkable');13const arr = [1, 2, 3, 4, 5];14const shrunkArr = shrinkerArbitrary(arr);15console.log(shrunkArr);16const { shrinkerArbitrary } = require('fast-check');17const { shrinker } = require('fast-check/lib/types/arbitrary/Shrinkable');18const func = () => {19 console.log('hello world');20};21const shrunkFunc = shrinkerArbitrary(func);22console.log(shrunkFunc);
Using AI Code Generation
1const fc = require('fast-check');2const { shrinkerArbitrary } = require('fast-check');3const testShrinker = shrinkerArbitrary(4 fc.string(),5 (s) => s.length > 0,6 (s) => s.length7);8const test3 = () => {9 fc.assert(10 fc.property(testShrinker, (s) => {11 return s.length > 0;12 })13 );14};15test3();16const fc = require('fast-check');17const { shrinkerArbitrary } = require('fast-check');18const testShrinker = shrinkerArbitrary(19 fc.string(),20 (s) => s.length > 0,21 (s) => s.length22);23const test4 = () => {24 fc.assert(25 fc.property(testShrinker, (s) => {26 return s.length > 0;27 })28 );29};30test4();31const fc = require('fast-check');32const { shrinkerArbitrary } = require('fast-check');33const testShrinker = shrinkerArbitrary(34 fc.string(),35 (s) => s.length > 0,36 (s) => s.length37);38const test5 = () => {39 fc.assert(40 fc.property(testShrinker, (s) => {41 return s.length > 0;42 })43 );44};45test5();46const fc = require('fast-check');47const { shrinkerArbitrary } = require('fast-check');48const testShrinker = shrinkerArbitrary(49 fc.string(),50 (s) => s.length > 0,51 (
Using AI Code Generation
1import { shrinkerArbitrary } from "fast-check";2export const shrinker = shrinkerArbitrary(3 fc.string(),4 fc.string(),5 fc.string(),6 fc.string(),7 fc.string()8);9const test = () => {10 const randomString = shrinker();11 console.log(randomString);12};13test();14import { shrinkerArbitrary } from "fast-check";15export const shrinker = shrinkerArbitrary(16 fc.string(),17 fc.string(),18 fc.string(),19 fc.string(),20 fc.string(),21 fc.string()22);23const test = () => {24 const randomString = shrinker();25 console.log(randomString);26};27test();28import { shrinkerArbitrary } from "fast-check";29export const shrinker = shrinkerArbitrary(30 fc.string(),31 fc.string(),32 fc.string(),33 fc.string(),34 fc.string(),35 fc.string(),36 fc.string()37);38const test = () => {39 const randomString = shrinker();40 console.log(randomString);41};42test();43import { shrinkerArbitrary } from "fast-check";44export const shrinker = shrinkerArbitrary(45 fc.string(),46 fc.string(),47 fc.string(),48 fc.string(),49 fc.string(),50 fc.string(),51 fc.string(),52 fc.string()53);54const test = () => {55 const randomString = shrinker();56 console.log(randomString);57};58test();59import { shrinkerArbitrary } from "fast-check";60export const shrinker = shrinkerArbitrary(
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!