How to use shrinkC1 method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

TupleArbitrary.spec.ts

Source:TupleArbitrary.spec.ts Github

copy

Full Screen

1import { TupleArbitrary } from '../../../../src/arbitrary/_internals/TupleArbitrary';2import { Value } from '../../../../src/check/arbitrary/definition/Value';3import { FakeIntegerArbitrary, fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';4import { fakeRandom } from '../__test-helpers__/RandomHelpers';5import { cloneMethod, hasCloneMethod } from '../../../../src/check/symbols';6import { Stream } from '../../../../src/stream/Stream';7import {8 assertProduceValuesShrinkableWithoutContext,9 assertProduceCorrectValues,10 assertShrinkProducesSameValueWithoutInitialContext,11 assertShrinkProducesStrictlySmallerValue,12 assertProduceSameValueGivenSameSeed,13} from '../__test-helpers__/ArbitraryAssertions';14import { buildShrinkTree, renderTree, walkTree } from '../__test-helpers__/ShrinkTree';15import { Arbitrary } from '../../../../src/check/arbitrary/definition/Arbitrary';16import { Random } from '../../../../src/random/generator/Random';17describe('TupleArbitrary', () => {18 describe('generate', () => {19 it('should merge results coming from underlyings and call them with the exact same inputs as the received ones', () => {20 // Arrange21 const expectedBiasFactor = 48;22 const vA = Symbol();23 const vB = Symbol();24 const vC = Symbol();25 const { instance: instanceA, generate: generateA } = fakeArbitrary<symbol>();26 const { instance: instanceB, generate: generateB } = fakeArbitrary<symbol>();27 const { instance: instanceC, generate: generateC } = fakeArbitrary<symbol>();28 generateA.mockReturnValueOnce(new Value(vA, undefined));29 generateB.mockReturnValueOnce(new Value(vB, undefined));30 generateC.mockReturnValueOnce(new Value(vC, undefined));31 const { instance: mrng } = fakeRandom();32 // Act33 const arb = new TupleArbitrary([instanceA, instanceB, instanceC]);34 const g = arb.generate(mrng, expectedBiasFactor);35 // Assert36 expect(g.value).toEqual([vA, vB, vC]);37 expect(generateA).toHaveBeenCalledWith(mrng, expectedBiasFactor);38 expect(generateB).toHaveBeenCalledWith(mrng, expectedBiasFactor);39 expect(generateC).toHaveBeenCalledWith(mrng, expectedBiasFactor);40 });41 it('should produce a cloneable instance if provided one cloneable underlying', () => {42 // Arrange43 const { instance: fakeArbitraryNotCloneableA, generate: generateA } = fakeArbitrary<string[]>();44 const { instance: fakeArbitraryCloneableB, generate: generateB } = fakeArbitrary<string[]>();45 generateA.mockReturnValue(new Value([], undefined));46 generateB.mockReturnValue(new Value(Object.defineProperty([], cloneMethod, { value: jest.fn() }), undefined));47 const { instance: mrng } = fakeRandom();48 // Act49 const arb = new TupleArbitrary([fakeArbitraryNotCloneableA, fakeArbitraryCloneableB]);50 const g = arb.generate(mrng, undefined);51 // Assert52 expect(g.hasToBeCloned).toBe(true);53 expect(hasCloneMethod(g.value)).toBe(true);54 });55 it('should not produce a cloneable instance if no cloneable underlyings', () => {56 // Arrange57 const { instance: fakeArbitraryNotCloneableA, generate: generateA } = fakeArbitrary<string[]>();58 const { instance: fakeArbitraryNotCloneableB, generate: generateB } = fakeArbitrary<string[]>();59 generateA.mockReturnValue(new Value([], undefined));60 generateB.mockReturnValue(new Value([], undefined));61 const { instance: mrng } = fakeRandom();62 // Act63 const arb = new TupleArbitrary([fakeArbitraryNotCloneableA, fakeArbitraryNotCloneableB]);64 const g = arb.generate(mrng, undefined);65 // Assert66 expect(g.hasToBeCloned).toBe(false);67 expect(hasCloneMethod(g.value)).toBe(false);68 });69 it('should not clone cloneable on generate', () => {70 // Arrange71 const { instance: fakeArbitraryNotCloneableA, generate: generateA } = fakeArbitrary<string[]>();72 const { instance: fakeArbitraryCloneableB, generate: generateB } = fakeArbitrary<string[]>();73 const cloneMethodImpl = jest.fn();74 generateA.mockReturnValue(new Value([], undefined));75 generateB.mockReturnValue(76 new Value(Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }), undefined)77 );78 const { instance: mrng } = fakeRandom();79 // Act80 const arb = new TupleArbitrary([fakeArbitraryNotCloneableA, fakeArbitraryCloneableB]);81 arb.generate(mrng, undefined);82 // Assert83 expect(cloneMethodImpl).not.toHaveBeenCalled();84 });85 });86 describe('canShrinkWithoutContext', () => {87 it.each`88 canA | canB | canC89 ${false} | ${false} | ${false}90 ${false} | ${true} | ${true}91 ${true} | ${false} | ${true}92 ${true} | ${true} | ${false}93 ${true} | ${true} | ${true}94 `(95 'should merge results coming from underlyings for canShrinkWithoutContext if received array has the right size',96 ({ canA, canB, canC }) => {97 // Arrange98 const vA = Symbol();99 const vB = Symbol();100 const vC = Symbol();101 const { instance: instanceA, canShrinkWithoutContext: canShrinkWithoutContextA } = fakeArbitrary<symbol>();102 const { instance: instanceB, canShrinkWithoutContext: canShrinkWithoutContextB } = fakeArbitrary<symbol>();103 const { instance: instanceC, canShrinkWithoutContext: canShrinkWithoutContextC } = fakeArbitrary<symbol>();104 canShrinkWithoutContextA.mockReturnValueOnce(canA);105 canShrinkWithoutContextB.mockReturnValueOnce(canB);106 canShrinkWithoutContextC.mockReturnValueOnce(canC);107 // Act108 const arb = new TupleArbitrary([instanceA, instanceB, instanceC]);109 const out = arb.canShrinkWithoutContext([vA, vB, vC]);110 // Assert111 expect(out).toBe(canA && canB && canC);112 expect(canShrinkWithoutContextA).toHaveBeenCalledWith(vA);113 if (canA) expect(canShrinkWithoutContextB).toHaveBeenCalledWith(vB);114 else expect(canShrinkWithoutContextB).not.toHaveBeenCalled();115 if (canA && canB) expect(canShrinkWithoutContextC).toHaveBeenCalledWith(vC);116 else expect(canShrinkWithoutContextC).not.toHaveBeenCalled();117 }118 );119 it('should not call underlyings on canShrinkWithoutContext if size is invalid', () => {120 // Arrange121 const { instance: instanceA, canShrinkWithoutContext: canShrinkWithoutContextA } = fakeArbitrary<symbol>();122 const { instance: instanceB, canShrinkWithoutContext: canShrinkWithoutContextB } = fakeArbitrary<symbol>();123 const { instance: instanceC, canShrinkWithoutContext: canShrinkWithoutContextC } = fakeArbitrary<symbol>();124 // Act125 const arb = new TupleArbitrary([instanceA, instanceB, instanceC]);126 const out = arb.canShrinkWithoutContext([Symbol(), Symbol(), Symbol(), Symbol()]);127 // Assert128 expect(out).toBe(false);129 expect(canShrinkWithoutContextA).not.toHaveBeenCalled();130 expect(canShrinkWithoutContextB).not.toHaveBeenCalled();131 expect(canShrinkWithoutContextC).not.toHaveBeenCalled();132 });133 });134 describe('shrink', () => {135 it('should call back arbitraries on shrink with the initially returned contextq', () => {136 // Arrange137 const expectedBiasFactor = 48;138 const vA = Symbol();139 const vB = Symbol();140 const vC = Symbol();141 const contextA = Symbol();142 const contextB = Symbol();143 const contextC = Symbol();144 const { instance: instanceA, generate: generateA, shrink: shrinkA } = fakeArbitrary<symbol>();145 const { instance: instanceB, generate: generateB, shrink: shrinkB } = fakeArbitrary<symbol>();146 const { instance: instanceC, generate: generateC, shrink: shrinkC } = fakeArbitrary<symbol>();147 generateA.mockReturnValueOnce(new Value(vA, contextA));148 generateB.mockReturnValueOnce(new Value(vB, contextB));149 generateC.mockReturnValueOnce(new Value(vC, contextC));150 const shrinkA1 = Symbol();151 const shrinkA2 = Symbol();152 const shrinkB1 = Symbol();153 const shrinkC1 = Symbol();154 const shrinkC2 = Symbol();155 const shrinkC3 = Symbol();156 shrinkA.mockReturnValueOnce(Stream.of(new Value(shrinkA1 as symbol, undefined), new Value(shrinkA2, undefined)));157 shrinkB.mockReturnValueOnce(Stream.of(new Value(shrinkB1 as symbol, undefined)));158 shrinkC.mockReturnValueOnce(159 Stream.of(160 new Value(shrinkC1 as symbol, undefined),161 new Value(shrinkC2, undefined),162 new Value(shrinkC3, undefined)163 )164 );165 const { instance: mrng } = fakeRandom();166 // Act167 const arb = new TupleArbitrary([instanceA, instanceB, instanceC]);168 const g = arb.generate(mrng, expectedBiasFactor);169 const shrinks = [...arb.shrink(g.value, g.context)];170 // Assert171 expect(shrinks).toHaveLength(2 /* A */ + 1 /* B */ + 3 /* C */);172 expect(shrinks.map((v) => v.value)).toEqual([173 [shrinkA1, vB, vC],174 [shrinkA2, vB, vC],175 [vA, shrinkB1, vC],176 [vA, vB, shrinkC1],177 [vA, vB, shrinkC2],178 [vA, vB, shrinkC3],179 ]);180 expect(shrinkA).toHaveBeenCalledWith(vA, contextA);181 expect(shrinkB).toHaveBeenCalledWith(vB, contextB);182 expect(shrinkC).toHaveBeenCalledWith(vC, contextC);183 });184 it('should clone cloneable on shrink', () => {185 // Arrange186 const { instance: fakeArbitraryNotCloneableA, generate: generateA, shrink: shrinkA } = fakeArbitrary<string[]>();187 const { instance: fakeArbitraryCloneableB, generate: generateB, shrink: shrinkB } = fakeArbitrary<string[]>();188 const { instance: fakeArbitraryNotCloneableC, generate: generateC, shrink: shrinkC } = fakeArbitrary<string[]>();189 const cloneMethodImpl = jest190 .fn()191 .mockImplementation(() => Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }));192 generateA.mockReturnValue(new Value([], undefined));193 shrinkA.mockReturnValue(Stream.of(new Value([], undefined), new Value([], undefined)));194 generateB.mockReturnValue(195 new Value(Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }), undefined)196 );197 shrinkB.mockReturnValue(198 Stream.of(199 new Value(Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }), undefined),200 new Value(Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }), undefined),201 new Value(Object.defineProperty([], cloneMethod, { value: cloneMethodImpl }), undefined)202 )203 );204 generateC.mockReturnValue(new Value([], undefined));205 shrinkC.mockReturnValue(206 Stream.of(207 new Value([], undefined),208 new Value([], undefined),209 new Value([], undefined),210 new Value([], undefined)211 )212 );213 const { instance: mrng } = fakeRandom();214 // Act215 const arb = new TupleArbitrary([fakeArbitraryNotCloneableA, fakeArbitraryCloneableB, fakeArbitraryNotCloneableC]);216 const g = arb.generate(mrng, undefined);217 expect(cloneMethodImpl).not.toHaveBeenCalled();218 const shrinkLazy = arb.shrink(g.value, g.context);219 expect(cloneMethodImpl).not.toHaveBeenCalled();220 const shrinks = [...shrinkLazy];221 // Assert222 expect(shrinks).toHaveLength(2 /* A */ + 3 /* B */ + 4 /* C */);223 expect(cloneMethodImpl).toHaveBeenCalledTimes(shrinks.length);224 });225 });226});227describe('TupleArbitrary (integration)', () => {228 const isCorrect = (value: number[]) => Array.isArray(value) && value.length === 3;229 const isStrictlySmaller = (t1: number[], t2: number[]) => t1.findIndex((v, idx) => v < t2[idx]) !== -1;230 const tupleBuilder = () =>231 new TupleArbitrary([new FakeIntegerArbitrary(), new FakeIntegerArbitrary(), new FakeIntegerArbitrary()]);232 it('should produce the same values given the same seed', () => {233 assertProduceSameValueGivenSameSeed(tupleBuilder);234 });235 it('should only produce correct values', () => {236 assertProduceCorrectValues(tupleBuilder, isCorrect);237 });238 it('should produce values seen as shrinkable without any context', () => {239 assertProduceValuesShrinkableWithoutContext(tupleBuilder);240 });241 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {242 assertShrinkProducesSameValueWithoutInitialContext(tupleBuilder);243 });244 it('should preserve strictly smaller ordering in shrink (if underlyings do)', () => {245 assertShrinkProducesStrictlySmallerValue(tupleBuilder, isStrictlySmaller);246 });247 it('should produce the right shrinking tree', () => {248 // Arrange249 const arb = new TupleArbitrary([new FirstArbitrary(), new SecondArbitrary()]);250 const { instance: mrng } = fakeRandom();251 // Act252 const g = arb.generate(mrng, undefined);253 const renderedTree = renderTree(buildShrinkTree(arb, g)).join('\n');254 // Assert255 expect(g.hasToBeCloned).toBe(false);256 expect(g.value).toBe(g.value_);257 expect(g.value).toEqual([expectedFirst, expectedSecond]);258 expect(renderedTree).toMatchInlineSnapshot(`259 "[4,97]260 ├> [2,97]261 | ├> [0,97]262 | | ├> [0,99]263 | | └> [0,98]264 | | └> [0,100]265 | ├> [2,99]266 | | └> [0,99]267 | └> [2,98]268 | ├> [0,98]269 | | └> [0,100]270 | └> [2,100]271 | └> [0,100]272 ├> [3,97]273 | ├> [0,97]274 | | ├> [0,99]275 | | └> [0,98]276 | | └> [0,100]277 | ├> [1,97]278 | | ├> [1,99]279 | | └> [1,98]280 | | └> [1,100]281 | ├> [3,99]282 | | ├> [0,99]283 | | └> [1,99]284 | └> [3,98]285 | ├> [0,98]286 | | └> [0,100]287 | ├> [1,98]288 | | └> [1,100]289 | └> [3,100]290 | ├> [0,100]291 | └> [1,100]292 ├> [4,99]293 | ├> [2,99]294 | | └> [0,99]295 | └> [3,99]296 | ├> [0,99]297 | └> [1,99]298 └> [4,98]299 ├> [2,98]300 | ├> [0,98]301 | | └> [0,100]302 | └> [2,100]303 | └> [0,100]304 ├> [3,98]305 | ├> [0,98]306 | | └> [0,100]307 | ├> [1,98]308 | | └> [1,100]309 | └> [3,100]310 | ├> [0,100]311 | └> [1,100]312 └> [4,100]313 ├> [2,100]314 | └> [0,100]315 └> [3,100]316 ├> [0,100]317 └> [1,100]"318 `);319 });320 it('should not re-use twice the same instance of cloneable', () => {321 // Arrange322 const alreadySeenCloneable = new Set<unknown>();323 const arb = new TupleArbitrary([new FirstArbitrary(), new CloneableArbitrary(), new SecondArbitrary()]);324 const { instance: mrng } = fakeRandom();325 // Act326 const g = arb.generate(mrng, undefined);327 const treeA = buildShrinkTree(arb, g);328 const treeB = buildShrinkTree(arb, g);329 // Assert330 walkTree(treeA, ([_first, cloneable, _second]) => {331 expect(alreadySeenCloneable.has(cloneable)).toBe(false);332 alreadySeenCloneable.add(cloneable);333 });334 walkTree(treeB, ([_first, cloneable, _second]) => {335 expect(alreadySeenCloneable.has(cloneable)).toBe(false);336 alreadySeenCloneable.add(cloneable);337 });338 });339});340// Helpers341const expectedFirst = 4;342const expectedSecond = 97;343class FirstArbitrary extends Arbitrary<number> {344 generate(_mrng: Random): Value<number> {345 return new Value(expectedFirst, { step: 2 });346 }347 canShrinkWithoutContext(_value: unknown): _value is number {348 throw new Error('No call expected in that scenario');349 }350 shrink(value: number, context?: unknown): Stream<Value<number>> {351 if (typeof context !== 'object' || context === null || !('step' in context)) {352 throw new Error('Invalid context for FirstArbitrary');353 }354 if (value <= 0) {355 return Stream.nil();356 }357 const currentStep = (context as { step: number }).step;358 const nextStep = currentStep + 1;359 return Stream.of(360 ...(value - currentStep >= 0 ? [new Value(value - currentStep, { step: nextStep })] : []),361 ...(value - currentStep + 1 >= 0 ? [new Value(value - currentStep + 1, { step: nextStep })] : [])362 );363 }364}365class SecondArbitrary extends Arbitrary<number> {366 generate(_mrng: Random): Value<number> {367 return new Value(expectedSecond, { step: 2 });368 }369 canShrinkWithoutContext(_value: unknown): _value is number {370 throw new Error('No call expected in that scenario');371 }372 shrink(value: number, context?: unknown): Stream<Value<number>> {373 if (typeof context !== 'object' || context === null || !('step' in context)) {374 throw new Error('Invalid context for SecondArbitrary');375 }376 if (value >= 100) {377 return Stream.nil();378 }379 const currentStep = (context as { step: number }).step;380 const nextStep = currentStep + 1;381 return Stream.of(382 ...(value + currentStep <= 100 ? [new Value(value + currentStep, { step: nextStep })] : []),383 ...(value + currentStep - 1 <= 100 ? [new Value(value + currentStep - 1, { step: nextStep })] : [])384 );385 }386}387class CloneableArbitrary extends Arbitrary<number[]> {388 private instance() {389 return Object.defineProperty([], cloneMethod, { value: () => this.instance() });390 }391 generate(_mrng: Random): Value<number[]> {392 return new Value(this.instance(), { shrunkOnce: false });393 }394 canShrinkWithoutContext(_value: unknown): _value is number[] {395 throw new Error('No call expected in that scenario');396 }397 shrink(value: number[], context?: unknown): Stream<Value<number[]>> {398 if (typeof context !== 'object' || context === null || !('shrunkOnce' in context)) {399 throw new Error('Invalid context for CloneableArbitrary');400 }401 const safeContext = context as { shrunkOnce: boolean };402 if (safeContext.shrunkOnce) {403 return Stream.nil();404 }405 return Stream.of(new Value(this.instance(), { shrunkOnce: true }));406 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { shrinkC1 } from 'fast-check-monorepo';2import { shrinkC2 } from 'fast-check-monorepo';3import { shrinkC3 } from 'fast-check-monorepo';4import { shrinkC4 } from 'fast-check-monorepo';5import { shrinkC5 } from 'fast-check-monorepo';6import { shrinkC6 } from 'fast-check-monorepo';7import { shrinkC7 } from 'fast-check-monorepo';8import { shrinkC8 } from 'fast-check-monorepo';9import { shrinkC9 } from 'fast-check-monorepo';10import { shrinkC10 } from 'fast-check-monorepo';11import { shrinkC11 } from 'fast-check-monorepo';12import { shrinkC12 } from 'fast-check-monorepo';13import { shrinkC13 } from 'fast-check-monorepo';14import { shrinkC14 } from 'fast-check-monorepo';15import { shrinkC15 } from 'fast-check-monorepo';16import { shrinkC16 } from 'fast-check-monorepo';17import { shrinkC17 } from 'fast-check-monorepo';18import { shrinkC18

Full Screen

Using AI Code Generation

copy

Full Screen

1const { shrinkC1 } = require("fast-check-monorepo");2const { shrinkC2 } = require("fast-check-monorepo");3const { shrinkC3 } = require("fast-check-monorepo");4const { shrinkC4 } = require("fast-check-monorepo");5const { shrinkC5 } = require("fast-check-monorepo");6const { shrinkC6 } = require("fast-check-monorepo");7const { shrinkC7 } = require("fast-check-monorepo");8const { shrinkC8 } = require("fast-check-monorepo");9const { shrinkC9 } = require("fast-check-monorepo");10const { shrinkC10 } = require("fast-check-monorepo");11const { shrinkC11 } = require("fast-check-monorepo");12const { shrinkC12 } = require("fast-check-monorepo");13const { shrinkC13 } = require("fast-check-monorepo");14const { shrinkC14 } = require("fast-check-monorepo");15const { shrinkC15 } = require("fast-check-monorepo");16const { shrinkC16 } = require("fast-check-monorepo");17const { shrinkC17 } = require("fast-check-monorepo");

Full Screen

Using AI Code Generation

copy

Full Screen

1import { shrinkC1 } from 'fast-check-monorepo'2import { shrinkC1 } from 'fast-check'3import { shrinkC1 } from 'fast-check-monorepo'4import { shrinkC1 } from 'fast-check'5import { shrinkC1 } from 'fast-check-monorepo'6import { shrinkC1 } from 'fast-check'7import { shrinkC1 } from 'fast-check-monorepo'8import { shrinkC1 } from 'fast-check'9import { shrinkC1 } from 'fast-check-monorepo'10import { shrinkC1 } from 'fast-check'11import { shrinkC1 } from 'fast-check-monorepo'12import { shrinkC1 } from 'fast-check'13import { shrinkC1 } from 'fast-check-monorepo'14import { shrinkC1 } from 'fast-check'15import { shrinkC1 } from 'fast-check-monorepo'16import { shrinkC1 } from 'fast-check'17import { shrinkC1 } from 'fast-check-monorepo'18import { shrinkC1 } from 'fast-check'19import { shrinkC1 } from 'fast-check-monorepo'20import { shrinkC

Full Screen

Using AI Code Generation

copy

Full Screen

1const { shrinkC1 } = require('fast-check');2const shrink = shrinkC1([1, 2, 3, 4, 5]);3const { fc } = require('fast-check');4const shrink = fc.shrinkC1([1, 2, 3, 4, 5]);5const { shrinkC1 } = require('fast-check');6const shrink = shrinkC1([1, 2, 3, 4, 5]);7const { shrinkC1 } = require('fast-check');8const shrink = shrinkC1([1, 2, 3, 4, 5]);9const { shrinkC1 } = require('fast

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { shrinkC1 } = require('fast-check-monorepo');3const fcShrinkC1 = fc.property(fc.integer(), shrinkC1);4fc.assert(fcShrinkC1);5const fc = require('fast-check');6const { shrinkC1 } = require('fast-check');7const fcShrinkC1 = fc.property(fc.integer(), shrinkC1);8fc.assert(fcShrinkC1);9const fc = require('fast-check');10const { shrinkC1 } = require('fast-check');11const fcShrinkC1 = fc.property(fc.integer(), shrinkC1);12fc.assert(fcShrinkC1);13const fc = require('fast-check');14const { shrinkC1 } = require('fast-check');15const fcShrinkC1 = fc.property(fc.integer(), shrinkC1);16fc.assert(fcShrinkC1);17const fc = require('fast-check');18const { shrinkC1 } = require('fast-check');19const fcShrinkC1 = fc.property(fc.integer(), shrinkC1

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