How to use generatorArbitrary method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

WithShrinkFromOtherArbitrary.spec.ts

Source:WithShrinkFromOtherArbitrary.spec.ts Github

copy

Full Screen

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 });...

Full Screen

Full Screen

WithShrinkFromOtherArbitrary.ts

Source:WithShrinkFromOtherArbitrary.ts Github

copy

Full Screen

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 }...

Full Screen

Full Screen

RestrictedIntegerArbitraryBuilder.ts

Source:RestrictedIntegerArbitraryBuilder.ts Github

copy

Full Screen

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);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { generatorArbitrary } = require('fast-check-arbitrary-generator');2const { generatorArbitrary } = require('fast-check-arbitrary-generator');3const { generatorArbitrary } = require('fast-check-arbitrary-generator');4const { generatorArbitrary } = require('fast-check-arbitrary-generator');5const { generatorArbitrary } = require('fast-check-arbitrary-generator');6const { generatorArbitrary } = require('fast-check-arbitrary-generator');7const { generatorArbitrary } = require('fast-check-arbitrary-generator');8const { generatorArbitrary } = require('fast-check-arbitrary-generator');9const { generatorArbitrary } = require('fast-check-arbitrary-generator');10const { generatorArbitrary } = require('fast-check-arbitrary-generator');11const { generatorArbitrary } = require('fast-check-arbitrary-generator');12const { generatorArbitrary } = require('fast-check-arbitrary-generator');13const { generatorArbitrary } = require('fast-check-arbitrary-generator');14const { generatorArbitrary } = require('fast-check-arbitrary-generator');15const { generatorArbitrary } = require('fast-check-arbitrary-generator');16const { generatorArbitrary } = require('fast-check-arbitrary-generator');17const { generatorArbitrary } = require('fast-check-arbitrary-generator');18const { generatorArbitrary } = require('fast-check-arbitrary-generator');19const { generatorArbitrary } = require('fast-check-arbitrary-generator');20const { generatorArbitrary } = require('fast-check-arbitrary-generator');21const { generatorArbitrary } = require('fast-check-arbitrary-generator');22const { generatorArbitrary } = require('fast-check-arbitrary-generator');23const { generatorArbitrary } = require('fast-check-arbitrary-generator');24const { generatorArbitrary } = require('fast-check-arbitrary-generator');25const { generatorArbitrary }

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check')2const { generatorArbitrary } = require('fast-check-monorepo')3const { generator: gen } = require('./generator')4const arb = generatorArbitrary(gen)5fc.assert(fc.property(arb, (x) => x))6const fc = require('fast-check')7const { generatorArbitrary } = require('fast-check')8const { generator: gen } = require('./generator')9const arb = generatorArbitrary(gen)10fc.assert(fc.property(arb, (x) => x))11const fc = require('fast-check-monorepo')12const { generator: gen } = require('./generator')13const arb = fc.generatorArbitrary(gen)14fc.assert(fc.property(arb, (x) => x))15const fc = require('fast-check')16const { generator: gen } = require('./generator')17const arb = fc.generatorArbitrary(gen)18fc.assert(fc.property(arb, (x) => x))

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { generatorArbitrary } = require('fast-check-monorepo');3const { string } = require('fast-check-monorepo');4const { generator } = require('fast-check-monorepo');5const { Arbitrary } = require('fast-check-monorepo');6const { stringOf } = require('fast-check-monorepo');7const { constantFrom } = require('fast-check-monorepo');8const { convertFromNext } = require('fast-check-monorepo');9const { convertToNext } = require('fast-check-monorepo');10const { buildNextArbitrary } = require('fast-check-monorepo');11const { buildNextShrinkable } = require('fast-check-monorepo');12const { NextValue } = require('fast-check-monorepo');13const { Shrinkable } = require('fast-check-monorepo');14const { Random } = require('fast-check-monorepo');15const { Stream } = require('fast-check-monorepo');16const stringOfGenerator = (g) => {17 const arb = convertFromNext(generatorArbitrary(g));18 return convertToNext(stringOf(arb));19};20const generatorOf = (arb) => {21 const nextArb = convertFromNext(arb);22 const nextG = generator(nextArb);23 return convertToNext(nextG);24};25const stringOfGenerator2 = (g) => {26 return generatorOf(stringOf(g));27};28const stringOfGenerator3 = (g) => {29 const nextG = convertFromNext(g);30 const nextArb = buildNextArbitrary((mrng) => {31 return buildNextShrinkable(32 nextG.generate(mrng),33 (v) => nextG.shrink(v).map((s) => buildNextShrinkable(s))34 );35 });36 return convertToNext(generator(nextArb));37};38const stringOfGenerator4 = (g) => {39 const nextG = convertFromNext(g);40 const nextArb = new Arbitrary((mrng) => {41 return new Shrinkable(42 nextG.generate(mrng),43 (v) => nextG.shrink(v).map((s) => new Shrinkable(s))44 );45 });46 return convertToNext(generator(nextArb));47};48const stringOfGenerator5 = (g) =>

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const arbitraryGenerator = fc.generatorArbitrary(3 fc.integer(),4 fc.integer(),5 fc.integer(),6 (a, b, c) => a + b + c7);8fc.assert(fc.property(arbitraryGenerator, (n) => n > 0));

Full Screen

Using AI Code Generation

copy

Full Screen

1import { generatorArbitrary } from "@arbitrary-js/arbitrary";2import { generatorString } from "@arbitrary-js/string";3const generator = generatorArbitrary(generatorString());4console.log(generator.next().value);5import { generatorArbitrary } from "@arbitrary-js/arbitrary";6import { generatorString } from "@arbitrary-js/string";7const generator = generatorArbitrary(generatorString());8console.log(generator.next().value);9import { generatorArbitrary } from "@arbitrary-js/arbitrary";10import { generatorString } from "@arbitrary-js/string";11const generator = generatorArbitrary(generatorString());12console.log(generator.next().value);13import { generatorArbitrary } from "@arbitrary-js/arbitrary";14import { generatorString } from "@arbitrary-js/string";15const generator = generatorArbitrary(generatorString());16console.log(generator.next().value);17import { generatorArbitrary } from "@arbitrary-js/arbitrary";18import { generatorString } from "@arbitrary-js/string";19const generator = generatorArbitrary(generatorString());20console.log(generator.next().value);21import { generatorArbitrary } from "@arbitrary-js/arbitrary";22import { generatorString } from "@arbitrary-js/string";23const generator = generatorArbitrary(generatorString());24console.log(generator.next().value);25import { generatorArbitrary } from "@arbitrary-js/arbitrary";26import { generatorString } from "@arbitrary-js/string";27const generator = generatorArbitrary(generatorString());28console.log(generator.next().value);29import { generatorArbitrary } from "@arbitrary-js/arbitrary";30import { generatorString } from "@arbitrary-js/string";31const generator = generatorArbitrary(generatorString());32console.log(generator.next().value);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { generatorArbitrary } = require('fast-check');2const { generator } = require('./test2.js');3const arb = generatorArbitrary(generator);4const fc = require('fast-check');5const generator = fc.object({6 a: fc.nat(),7 b: fc.string(),8 c: fc.constantFrom('a', 'b', 'c')9});10module.exports = { generator };11export const myFunction = (a, b) => {12 return a + b;13};14import { myFunction } from '../MyModule';15jest.mock('../MyModule');16describe('MyModule', () => {17 it('should return 2', () => {18 expect(myFunction(1, 1)).toBe(2);19 });20});21 at Object.<anonymous> (src/MyModule.js:3:12)22 at Object.<anonymous> (src/__tests__/MyModule.test.js:1:1)23import { myFunction } from '../MyModule';24jest.mock('../MyModule

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful