How to use getDepthContextFor method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

FrequencyArbitrary.spec.ts

Source:FrequencyArbitrary.spec.ts Github

copy

Full Screen

1import * as fc from 'fast-check';2import { FrequencyArbitrary, _Constraints } from '../../../../src/arbitrary/_internals/FrequencyArbitrary';3import { Value } from '../../../../src/check/arbitrary/definition/Value';4import { fakeRandom } from '../__test-helpers__/RandomHelpers';5import { FakeIntegerArbitrary, fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';6import {7 assertProduceSameValueGivenSameSeed,8 assertProduceValuesShrinkableWithoutContext,9 assertProduceCorrectValues,10 assertShrinkProducesStrictlySmallerValue,11} from '../__test-helpers__/ArbitraryAssertions';12import * as DepthContextMock from '../../../../src/arbitrary/_internals/helpers/DepthContext';13import { Stream } from '../../../../src/stream/Stream';14import { sizeArb } from '../__test-helpers__/SizeHelpers';15function beforeEachHook() {16 jest.resetModules();17 jest.restoreAllMocks();18 fc.configureGlobal({ beforeEach: beforeEachHook });19}20beforeEach(beforeEachHook);21const frequencyValidInputsArb = fc22 .tuple(23 fc.record({ weight: fc.integer({ min: 1 }), arbitraryValue: fc.integer() }),24 fc.array(fc.record({ weight: fc.integer({ min: 1 }), arbitraryValue: fc.integer() })),25 fc.array(fc.record({ weight: fc.integer({ min: 1 }), arbitraryValue: fc.integer() }))26 )27 .map(([positiveWeightMeta, headingWeightsMeta, traillingWeightsMeta]) => [28 ...headingWeightsMeta,29 positiveWeightMeta,30 ...traillingWeightsMeta,31 ]);32const fromValidInputs = (metas: { weight: number; arbitraryValue: number }[]) =>33 metas.map((meta) => {34 const expectedContext = Symbol();35 const arbitraryMeta = fakeArbitrary<number>();36 arbitraryMeta.generate.mockReturnValue(new Value(meta.arbitraryValue, expectedContext));37 return {38 weight: meta.weight,39 arbitraryMeta,40 arbitrary: arbitraryMeta.instance,41 expectedValue: meta.arbitraryValue,42 expectedContext,43 fallbackValue: undefined as { default: number } | undefined,44 };45 });46const frequencyConstraintsArbFor = (keys: {47 forbidden?: (keyof _Constraints)[];48 required?: (keyof _Constraints)[];49}): fc.Arbitrary<_Constraints> => {50 const { forbidden = [], required = [] } = keys;51 return fc.record(52 {53 ...(!forbidden.includes('depthIdentifier') ? { depthIdentifier: fc.string() } : {}),54 ...(!forbidden.includes('depthSize')55 ? { depthSize: fc.oneof(fc.double({ min: 0, max: 100, noNaN: true }), sizeArb) }56 : {}),57 ...(!forbidden.includes('maxDepth') ? { maxDepth: fc.nat() } : {}),58 ...(!forbidden.includes('withCrossShrink') ? { withCrossShrink: fc.boolean() } : {}),59 },60 { requiredKeys: required }61 );62};63describe('FrequencyArbitrary', () => {64 describe('from', () => {65 it('should build instances of FrequencyArbitrary', () =>66 fc.assert(67 fc.property(68 frequencyValidInputsArb,69 frequencyConstraintsArbFor({}),70 fc.nat(),71 (validInputs, constraints, depth) => {72 // Arrange73 const warbs = fromValidInputs(validInputs);74 const depthContext = { depth };75 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');76 getDepthContextFor.mockReturnValue(depthContext);77 // Act78 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');79 // Assert80 expect(arb).toBeInstanceOf(FrequencyArbitrary);81 }82 )83 ));84 it('should always use the context coming from getDepthContextFor', () =>85 fc.assert(86 fc.property(87 frequencyValidInputsArb,88 frequencyConstraintsArbFor({}),89 fc.nat(),90 (validInputs, constraints, depth) => {91 // Arrange92 const warbs = fromValidInputs(validInputs);93 const depthContext = { depth };94 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');95 getDepthContextFor.mockReturnValue(depthContext);96 // Act97 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');98 const typedArb = arb as FrequencyArbitrary<number>;99 // Assert100 expect(getDepthContextFor).toHaveBeenCalledTimes(1);101 expect(getDepthContextFor).toHaveBeenCalledWith(constraints.depthIdentifier);102 expect(typedArb.context).toBe(depthContext);103 }104 )105 ));106 it('should reject calls without any weighted arbitraries', () => {107 // Arrange / Act / Assert108 expect(() => FrequencyArbitrary.from([], {}, 'test')).toThrowError();109 });110 it('should reject calls without weight', () => {111 // Arrange / Act / Assert112 expect(() =>113 FrequencyArbitrary.from([{ arbitrary: fakeArbitrary(), weight: undefined! }], {}, 'test')114 ).toThrowError(/expects weights to be integer values/);115 });116 it('should reject calls without arbitrary', () => {117 // Arrange / Act / Assert118 expect(() => FrequencyArbitrary.from([{ arbitrary: undefined!, weight: 1 }], {}, 'test')).toThrowError(119 /expects arbitraries to be specified/120 );121 });122 it('should reject calls including at least one strictly negative weight', () =>123 fc.assert(124 fc.property(125 fc.integer({ max: -1 }),126 fc.array(fc.nat()),127 fc.array(fc.nat()),128 (negativeWeight, headingWeights, traillingWeights) => {129 // Arrange130 const weightedArbs = [...headingWeights, negativeWeight, ...traillingWeights].map((weight) => ({131 weight,132 arbitrary: fakeArbitrary(),133 }));134 // Act / Assert135 expect(() => FrequencyArbitrary.from(weightedArbs, {}, 'test')).toThrowError();136 }137 )138 ));139 it('should reject calls having a total weight of zero', () =>140 fc.assert(141 fc.property(fc.nat({ max: 1000 }), (numEntries) => {142 // Arrange143 const weightedArbs = [...Array(numEntries)].map(() => ({144 weight: 0,145 arbitrary: fakeArbitrary(),146 }));147 // Act / Assert148 // Combined with: 'Should reject calls including at one strictly negative weight'149 // it means that we have: 'Should reject calls having a total weight inferior or equal to zero'150 expect(() => FrequencyArbitrary.from(weightedArbs, {}, 'test')).toThrowError();151 })152 ));153 it('should not reject calls defining a strictly positive total weight without any negative weights', () =>154 fc.assert(155 fc.property(156 fc.integer({ min: 1 }),157 fc.array(fc.nat()),158 fc.array(fc.nat()),159 (positiveWeight, headingWeights, traillingWeights) => {160 // Arrange161 const weightedArbs = [...headingWeights, positiveWeight, ...traillingWeights].map((weight) => ({162 weight,163 arbitrary: fakeArbitrary(),164 }));165 // Act / Assert166 expect(() => FrequencyArbitrary.from(weightedArbs, {}, 'test')).not.toThrowError();167 }168 )169 ));170 });171 describe('generate', () => {172 it('should call Random generator to generate values between 0 and total weight (not included)', () =>173 fc.assert(174 fc.property(175 frequencyValidInputsArb,176 frequencyConstraintsArbFor({}),177 fc.option(fc.integer({ min: 2 }), { nil: undefined }),178 fc.nat(),179 (validInputs, constraints, biasFactor, generateSeed) => {180 // Arrange181 fc.pre(constraints.maxDepth !== 0);182 const warbs = fromValidInputs(validInputs);183 const totalWeight = warbs.reduce((acc, cur) => acc + cur.weight, 0);184 const { instance: mrng, nextInt } = fakeRandom();185 nextInt.mockImplementation((a = 0, b = 0) => a + (generateSeed % (b - a + 1)));186 // Act187 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');188 arb.generate(mrng, biasFactor);189 // Assert190 expect(nextInt).toHaveBeenCalledTimes(1);191 expect(nextInt).toHaveBeenCalledWith(0, totalWeight - 1);192 }193 )194 ));195 it('should call the right arbitrary to generate the value', () =>196 fc.assert(197 fc.property(198 frequencyValidInputsArb,199 frequencyConstraintsArbFor({}),200 fc.option(fc.integer({ min: 2 }), { nil: undefined }),201 fc.nat(),202 fc.nat(),203 (validInputs, constraints, biasFactor, arbitrarySelectionSeed, generateSeed) => {204 // Arrange205 fc.pre(constraints.maxDepth !== 0);206 const warbs = fromValidInputs(validInputs);207 const selectedArbitraryIndex = arbitrarySelectionSeed % warbs.length;208 const selectedArbitrary = warbs[selectedArbitraryIndex];209 fc.pre(selectedArbitrary.weight > 0);210 const totalWeightBefore = warbs.slice(0, selectedArbitraryIndex).reduce((acc, cur) => acc + cur.weight, 0);211 const { instance: mrng, nextInt } = fakeRandom();212 nextInt.mockImplementation(() => totalWeightBefore + (generateSeed % selectedArbitrary.weight));213 // Act214 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');215 const g = arb.generate(mrng, biasFactor).value;216 // Assert217 expect(g).toBe(selectedArbitrary.expectedValue);218 expect(selectedArbitrary.arbitraryMeta.generate).toHaveBeenCalledTimes(1);219 expect(selectedArbitrary.arbitraryMeta.generate).toHaveBeenCalledWith(mrng, biasFactor);220 }221 )222 ));223 it('should always call the first arbitrary to generate the value when maxDepth has been reached', () =>224 fc.assert(225 fc.property(226 frequencyValidInputsArb,227 frequencyConstraintsArbFor({ required: ['maxDepth'] }),228 fc.option(fc.integer({ min: 2 }), { nil: undefined }),229 fc.nat(),230 (validInputs, constraints, biasFactor, overflowFromMaxDepth) => {231 // Arrange232 const warbs = fromValidInputs(validInputs);233 const requestedMaxDepth = constraints.maxDepth!;234 const { instance: mrng, nextInt } = fakeRandom();235 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');236 getDepthContextFor.mockReturnValue({ depth: requestedMaxDepth + overflowFromMaxDepth });237 // Act238 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');239 const g = arb.generate(mrng, biasFactor).value;240 // Assert241 expect(nextInt).not.toHaveBeenCalled();242 expect(g).toBe(warbs[0].expectedValue);243 }244 )245 ));246 it('should increment received depth context when going deeper in the generate-tree then reset it', () =>247 fc.assert(248 fc.property(249 frequencyValidInputsArb,250 frequencyConstraintsArbFor({}),251 fc.option(fc.integer({ min: 2 }), { nil: undefined }),252 fc.nat(),253 fc.nat(),254 (validInputs, constraints, biasFactor, initialDepth, generateSeed) => {255 // Arrange256 let calledOnce = false;257 const warbs = fromValidInputs(validInputs);258 const { instance: mrng, nextInt } = fakeRandom();259 nextInt.mockImplementation((a = 0, b = 0) => a + (generateSeed % (b - a + 1)));260 const depthContext = { depth: initialDepth };261 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');262 getDepthContextFor.mockReturnValue(depthContext);263 for (const { arbitraryMeta, expectedValue } of warbs) {264 arbitraryMeta.generate.mockReset();265 arbitraryMeta.generate.mockImplementation(() => {266 calledOnce = true;267 expect(depthContext).toEqual({ depth: initialDepth + 1 });268 return new Value(expectedValue, undefined);269 });270 }271 // Act272 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');273 arb.generate(mrng, biasFactor);274 // Assert275 expect(calledOnce).toBe(true);276 expect(depthContext).toEqual({ depth: initialDepth });277 }278 )279 ));280 it('should ask ranges containing negative values as we go deeper in the structure if depthSize and first arbitrary has weight >0', () =>281 fc.assert(282 fc.property(283 frequencyValidInputsArb,284 frequencyConstraintsArbFor({ forbidden: ['maxDepth'], required: ['depthSize'] }),285 fc.option(fc.integer({ min: 2 }), { nil: undefined }),286 (validInputs, constraints, biasFactor) => {287 // Arrange288 const warbs = fromValidInputs(validInputs);289 fc.pre(warbs[0].weight > 0);290 const { instance: mrng, nextInt } = fakeRandom();291 nextInt.mockReturnValue(0);292 const depthContext = { depth: 0 };293 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');294 getDepthContextFor.mockReturnValueOnce(depthContext);295 // Act / Assert296 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');297 let currentDepth = 0;298 // eslint-disable-next-line no-constant-condition299 while (true) {300 depthContext.depth = currentDepth;301 arb.generate(mrng, biasFactor);302 expect(depthContext.depth).toBe(currentDepth); // ensures the depth was properly reset303 if (nextInt.mock.calls[nextInt.mock.calls.length - 1][0] < 0) {304 break; // we have been called with a negative value once305 }306 ++currentDepth;307 }308 // first run (depth=0) will always call us with 0->?309 // subsequent calls (depth>0) may call us with negative boundaries310 expect(nextInt).toHaveBeenCalledWith(0, expect.any(Number));311 // but all the runs will call us with the same max312 const distinctMax = new Set(nextInt.mock.calls.map(([_min, max]) => max));313 expect([...distinctMax]).toHaveLength(1);314 const distinctMin = new Set(nextInt.mock.calls.map(([min, _max]) => min));315 expect([...distinctMin]).toHaveLength(2);316 }317 )318 ));319 it('should never ask ranges containing negative values as we go deeper in the structure if first arbitrary has weight of zero', () =>320 fc.assert(321 fc.property(322 frequencyValidInputsArb,323 frequencyConstraintsArbFor({ forbidden: ['maxDepth'], required: ['depthSize'] }),324 fc.option(fc.integer({ min: 2 }), { nil: undefined }),325 (validInputs, constraints, biasFactor) => {326 // Arrange327 const warbs = fromValidInputs(validInputs);328 const { instance: mrng, nextInt } = fakeRandom();329 nextInt.mockReturnValue(0);330 const depthContext = { depth: 0 };331 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');332 getDepthContextFor.mockReturnValueOnce(depthContext);333 // Act / Assert334 const arb = FrequencyArbitrary.from([{ ...warbs[0], weight: 0 }, ...warbs], constraints, 'test');335 for (let currentDepth = 0; currentDepth !== 100; ++currentDepth) {336 depthContext.depth = currentDepth;337 arb.generate(mrng, biasFactor);338 expect(depthContext.depth).toBe(currentDepth); // ensures the depth was properly reset339 }340 const distinctMin = new Set(nextInt.mock.calls.map(([min, _max]) => min));341 expect([...distinctMin]).toHaveLength(1);342 const distinctMax = new Set(nextInt.mock.calls.map(([_min, max]) => max));343 expect([...distinctMax]).toHaveLength(1);344 }345 )346 ));347 });348 describe('canShrinkWithoutContext', () => {349 it('should tell it cannot generate the value if no sub-arbitrary can generate the value', () =>350 fc.assert(351 fc.property(frequencyValidInputsArb, frequencyConstraintsArbFor({}), (validInputs, constraints) => {352 // Arrange353 const warbs = fromValidInputs(validInputs);354 const depthContext = { depth: 0 };355 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');356 getDepthContextFor.mockReturnValue(depthContext);357 const value = Symbol();358 for (const input of warbs) {359 input.arbitraryMeta.canShrinkWithoutContext.mockReturnValue(false);360 }361 // Act362 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');363 // Assert364 expect(arb.canShrinkWithoutContext(value)).toBe(false);365 })366 ));367 it('should ignore arbitraries with weight of zero when maxDepth not reached', () =>368 fc.assert(369 fc.property(370 frequencyValidInputsArb,371 frequencyConstraintsArbFor({}),372 fc.nat(),373 (validInputs, constraints, position) => {374 // Arrange375 fc.pre(constraints.maxDepth !== 0);376 const warbs = fromValidInputs([377 ...validInputs.slice(0, position % validInputs.length),378 { arbitraryValue: -1, weight: 0 },379 ...validInputs.slice(position % validInputs.length),380 ]);381 const depthContext = { depth: 0 };382 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');383 getDepthContextFor.mockReturnValue(depthContext);384 const value = Symbol();385 for (const input of warbs) {386 input.arbitraryMeta.canShrinkWithoutContext.mockReturnValue(false);387 }388 warbs[position % validInputs.length].arbitraryMeta.canShrinkWithoutContext.mockReturnValue(true);389 // Act390 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');391 // Assert392 expect(arb.canShrinkWithoutContext(value)).toBe(false);393 }394 )395 ));396 it('should tell it can generate the value if one of the sub-arbitraries can generate the value (maxDepth not reached)', () =>397 fc.assert(398 fc.property(399 frequencyValidInputsArb,400 frequencyConstraintsArbFor({}),401 fc.nat(),402 (validInputs, constraints, mod) => {403 // Arrange404 fc.pre(constraints.maxDepth !== 0);405 const warbs = fromValidInputs(validInputs);406 const depthContext = { depth: 0 };407 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');408 getDepthContextFor.mockReturnValue(depthContext);409 const value = Symbol();410 const selectedIndex = mod % warbs.length;411 for (let index = 0; index !== warbs.length; ++index) {412 const input = warbs[index];413 const can = index === selectedIndex;414 input.arbitraryMeta.canShrinkWithoutContext.mockReturnValue(can);415 }416 // Act417 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');418 // Assert419 expect(arb.canShrinkWithoutContext(value)).toBe(true);420 expect(warbs[selectedIndex].arbitraryMeta.canShrinkWithoutContext).toHaveBeenCalledWith(value);421 }422 )423 ));424 it('should only consider the first arbitrary when maxDepth has been reached', () =>425 fc.assert(426 fc.property(427 frequencyValidInputsArb,428 frequencyConstraintsArbFor({ required: ['maxDepth'] }),429 fc.nat(),430 fc.boolean(),431 fc.boolean(),432 (validInputs, constraints, overflowFromMaxDepth, firstCanOrNot, allOthersCanOrNot) => {433 // Arrange434 const warbs = fromValidInputs(validInputs);435 const maxDepth = constraints.maxDepth!;436 const depthContext = { depth: maxDepth + overflowFromMaxDepth };437 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');438 getDepthContextFor.mockReturnValue(depthContext);439 const value = Symbol();440 for (let index = 0; index !== warbs.length; ++index) {441 warbs[index].arbitraryMeta.canShrinkWithoutContext.mockReturnValue(442 index === 0 ? firstCanOrNot : allOthersCanOrNot443 );444 }445 // Act446 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');447 // Assert448 expect(arb.canShrinkWithoutContext(value)).toBe(firstCanOrNot);449 expect(warbs[0].arbitraryMeta.canShrinkWithoutContext).toHaveBeenCalledWith(value);450 for (let index = 1; index < warbs.length; ++index) {451 expect(warbs[index].arbitraryMeta.canShrinkWithoutContext).not.toHaveBeenCalled();452 }453 }454 )455 ));456 });457 describe('shrink', () => {458 it('should call the right arbitrary to shrink a value generated by itself', () =>459 fc.assert(460 fc.property(461 frequencyValidInputsArb,462 frequencyConstraintsArbFor({ forbidden: ['withCrossShrink'] }),463 fc.option(fc.integer({ min: 2 }), { nil: undefined }),464 fc.nat(),465 fc.nat(),466 (validInputs, constraints, biasFactor, arbitrarySelectionSeed, generateSeed) => {467 // Arrange468 fc.pre(constraints.maxDepth !== 0);469 const warbs = fromValidInputs(validInputs);470 const selectedArbitraryIndex = arbitrarySelectionSeed % warbs.length;471 const selectedArbitrary = warbs[selectedArbitraryIndex];472 fc.pre(selectedArbitrary.weight > 0);473 const totalWeightBefore = warbs.slice(0, selectedArbitraryIndex).reduce((acc, cur) => acc + cur.weight, 0);474 const { instance: mrng, nextInt } = fakeRandom();475 nextInt.mockImplementation(() => totalWeightBefore + (generateSeed % selectedArbitrary.weight));476 selectedArbitrary.arbitraryMeta.shrink.mockReturnValue(477 Stream.of(new Value(1, undefined), new Value(42, undefined))478 );479 // Act480 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');481 const value = arb.generate(mrng, biasFactor);482 const shrinks = [...arb.shrink(value.value, value.context)];483 // Assert484 expect(shrinks.map((v) => v.value)).toEqual([1, 42]);485 expect(selectedArbitrary.arbitraryMeta.shrink).toHaveBeenCalledTimes(1);486 expect(selectedArbitrary.arbitraryMeta.shrink).toHaveBeenCalledWith(487 value.value,488 selectedArbitrary.expectedContext489 );490 }491 )492 ));493 it('should generate a new value using first arbitrary when cross-shrink enabled', () =>494 fc.assert(495 fc.property(496 frequencyValidInputsArb,497 frequencyConstraintsArbFor({ forbidden: ['withCrossShrink'] }),498 fc.option(fc.integer({ min: 2 }), { nil: undefined }),499 fc.nat(),500 fc.nat(),501 (validInputs, constraints, biasFactor, arbitrarySelectionSeed, generateSeed) => {502 // Arrange503 fc.pre(constraints.maxDepth !== 0);504 const warbs = fromValidInputs(validInputs);505 const selectedArbitraryIndex = arbitrarySelectionSeed % warbs.length;506 const selectedArbitrary = warbs[selectedArbitraryIndex];507 fc.pre(selectedArbitrary.weight > 0);508 fc.pre(warbs[0].weight > 0);509 fc.pre(selectedArbitraryIndex !== 0);510 const totalWeightBefore = warbs.slice(0, selectedArbitraryIndex).reduce((acc, cur) => acc + cur.weight, 0);511 const { instance: mrng, nextInt, clone } = fakeRandom();512 const { instance: anotherMrng } = fakeRandom();513 clone.mockReturnValue(anotherMrng);514 nextInt.mockImplementation(() => totalWeightBefore + (generateSeed % selectedArbitrary.weight));515 selectedArbitrary.arbitraryMeta.shrink.mockReturnValue(516 Stream.of(new Value(1, undefined), new Value(42, undefined))517 );518 // Act519 const arb = FrequencyArbitrary.from(warbs, { ...constraints, withCrossShrink: true }, 'test');520 const value = arb.generate(mrng, biasFactor);521 const shrinks = [...arb.shrink(value.value, value.context)];522 // Assert523 expect(shrinks.map((v) => v.value)).toEqual([warbs[0].expectedValue, 1, 42]);524 expect(warbs[0].arbitraryMeta.generate).toHaveBeenCalledWith(anotherMrng, biasFactor);525 expect(selectedArbitrary.arbitraryMeta.shrink).toHaveBeenCalledTimes(1);526 expect(selectedArbitrary.arbitraryMeta.shrink).toHaveBeenCalledWith(527 value.value,528 selectedArbitrary.expectedContext529 );530 }531 )532 ));533 it('should not call generate on first arbitrary when cross-shrink enabled and first generate already used it', () =>534 fc.assert(535 fc.property(536 frequencyValidInputsArb,537 frequencyConstraintsArbFor({ forbidden: ['withCrossShrink'] }),538 fc.option(fc.integer({ min: 2 }), { nil: undefined }),539 (validInputs, constraints, biasFactor) => {540 // Arrange541 fc.pre(constraints.maxDepth !== 0);542 const warbs = fromValidInputs(validInputs);543 fc.pre(warbs[0].weight > 0);544 const { instance: mrng, nextInt } = fakeRandom();545 nextInt.mockReturnValue(0);546 warbs[0].arbitraryMeta.shrink.mockReturnValue(Stream.of(new Value(1, undefined), new Value(42, undefined)));547 // Act548 const arb = FrequencyArbitrary.from(warbs, { ...constraints, withCrossShrink: true }, 'test');549 const value = arb.generate(mrng, biasFactor);550 const shrinks = [...arb.shrink(value.value, value.context)];551 // Assert552 expect(shrinks.map((v) => v.value)).toEqual([1, 42]);553 expect(warbs[0].arbitraryMeta.shrink).toHaveBeenCalledTimes(1);554 expect(warbs[0].arbitraryMeta.shrink).toHaveBeenCalledWith(value.value, warbs[0].expectedContext);555 }556 )557 ));558 it('should be able to shrink without context if one of the sub-arbitrary can generate the value', () =>559 fc.assert(560 fc.property(561 frequencyValidInputsArb,562 frequencyConstraintsArbFor({}),563 fc.nat(),564 (validInputs, constraints, mod) => {565 // Arrange566 fc.pre(constraints.maxDepth !== 0);567 const warbs = fromValidInputs(validInputs);568 const depthContext = { depth: 0 };569 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');570 getDepthContextFor.mockReturnValue(depthContext);571 const value = Symbol();572 const selectedIndex = mod % warbs.length;573 fc.pre(warbs[selectedIndex].weight !== 0);574 for (let index = 0; index !== warbs.length; ++index) {575 const input = warbs[index];576 const can = index === selectedIndex;577 input.arbitraryMeta.canShrinkWithoutContext.mockReturnValue(can);578 input.arbitraryMeta.shrink.mockReturnValue(579 Stream.of(new Value(42, undefined), new Value(index, undefined))580 );581 }582 // Act583 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');584 const shrinks = [...arb.shrink(value as any, undefined)];585 // Assert586 expect(shrinks.map((v) => v.value)).toEqual([42, selectedIndex]);587 expect(warbs[selectedIndex].arbitraryMeta.canShrinkWithoutContext).toHaveBeenCalledWith(value);588 expect(warbs[selectedIndex].arbitraryMeta.shrink).toHaveBeenCalledWith(value, undefined);589 }590 )591 ));592 it('should be able to shrink without context if one of the sub-arbitrary can generate the value plus prepend fallback of first (whenever possible)', () =>593 fc.assert(594 fc.property(595 frequencyValidInputsArb,596 frequencyConstraintsArbFor({}),597 fc.nat(),598 (validInputs, constraints, mod) => {599 // Arrange600 fc.pre(constraints.maxDepth !== 0);601 const warbs = fromValidInputs(validInputs);602 const depthContext = { depth: 0 };603 const getDepthContextFor = jest.spyOn(DepthContextMock, 'getDepthContextFor');604 getDepthContextFor.mockReturnValue(depthContext);605 const value = Symbol();606 const selectedIndex = mod % warbs.length;607 fc.pre(warbs[selectedIndex].weight !== 0);608 for (let index = 0; index !== warbs.length; ++index) {609 const input = warbs[index];610 const can = index === selectedIndex;611 input.arbitraryMeta.canShrinkWithoutContext.mockReturnValue(can);612 input.arbitraryMeta.shrink.mockReturnValue(613 Stream.of(new Value(42, undefined), new Value(index, undefined))614 );615 }616 warbs[0].fallbackValue = { default: 48 };617 // Act618 const arb = FrequencyArbitrary.from(warbs, constraints, 'test');619 const shrinks = [...arb.shrink(value as any, undefined)];620 // Assert621 if (warbs[0].weight !== 0 && selectedIndex !== 0 && constraints.withCrossShrink) {622 // Can only prepend when applicable ie:623 // - weight of [0] >0624 // - not shrinking a value coming from [0]625 // - cross-shrink enabled626 expect(shrinks.map((v) => v.value)).toEqual([48, 42, selectedIndex]);627 } else {628 expect(shrinks.map((v) => v.value)).toEqual([42, selectedIndex]);629 }630 expect(warbs[selectedIndex].arbitraryMeta.canShrinkWithoutContext).toHaveBeenCalledWith(value);631 expect(warbs[selectedIndex].arbitraryMeta.shrink).toHaveBeenCalledWith(value, undefined);632 }633 )634 ));635 });636});637describe('FrequencyArbitrary (integration)', () => {638 type Extra = { data: { offset: number; weight: number }[]; constraints: Partial<_Constraints> };639 const maxRangeLength = 10;640 const extraParameters: fc.Arbitrary<Extra> = fc.record(641 {642 data: fc643 .array(644 fc.record(645 {646 offset: fc.nat(),647 weight: fc.nat(),648 },649 { requiredKeys: ['offset', 'weight'] }650 ),651 { minLength: 1 }652 )653 .filter((inputs) => inputs.reduce((summedWeight, current) => summedWeight + current.weight, 0) > 0),654 constraints: fc.record(655 {656 withCrossShrink: fc.boolean(),657 depthSize: fc.oneof(fc.double({ min: 0, max: Number.MAX_VALUE, noNaN: true }), sizeArb),658 maxDepth: fc.nat(),659 },660 { requiredKeys: [] }661 ),662 },663 { requiredKeys: ['data', 'constraints'] }664 );665 const isCorrect = (value: number, extra: Extra) =>666 typeof value === 'number' && extra.data.some((m) => m.offset <= value && value <= m.offset + maxRangeLength);667 const isStrictlySmaller = (v1: number, v2: number, extra: Extra) => {668 // When withCrossShrink is toggled, the shrinker can jump from one arbitrary to the first one on shrink669 // But only if the weight associated to the first arbitrary is strictly greater than 0670 if (extra.constraints.withCrossShrink && extra.data[0].weight > 0) {671 const canBeInFirstArbitrary = extra.data[0].offset <= v1 && v1 <= extra.data[0].offset + maxRangeLength;672 if (canBeInFirstArbitrary) {673 // `v1` is possibly coming from our first arbitrary674 return true;675 }676 }677 return Math.abs(v1) < Math.abs(v2);678 };679 const frequencyBuilder = (extra: Extra) =>680 FrequencyArbitrary.from(681 extra.data.map((m) => ({ weight: m.weight, arbitrary: new FakeIntegerArbitrary(m.offset, maxRangeLength) })),682 extra.constraints,683 'test'684 );685 it('should produce the same values given the same seed', () => {686 assertProduceSameValueGivenSameSeed(frequencyBuilder, { extraParameters });687 });688 it('should only produce correct values', () => {689 assertProduceCorrectValues(frequencyBuilder, isCorrect, { extraParameters });690 });691 it('should produce values seen as shrinkable without any context', () => {692 assertProduceValuesShrinkableWithoutContext(frequencyBuilder, { extraParameters });693 });694 it('should shrink towards strictly smaller values (if underlyings do)', () => {695 assertShrinkProducesStrictlySmallerValue(frequencyBuilder, isStrictlySmaller, { extraParameters });696 });...

Full Screen

Full Screen

FrequencyArbitrary.ts

Source:FrequencyArbitrary.ts Github

copy

Full Screen

...41 depthBias: depthBiasFromSizeForArbitrary(constraints.depthSize, constraints.maxDepth !== undefined),42 maxDepth: constraints.maxDepth != undefined ? constraints.maxDepth : safePositiveInfinity,43 withCrossShrink: !!constraints.withCrossShrink,44 };45 return new FrequencyArbitrary(warbs, sanitizedConstraints, getDepthContextFor(constraints.depthIdentifier));46 }47 private constructor(48 readonly warbs: _WeightedArbitrary<T>[],49 readonly constraints: _SanitizedConstraints,50 readonly context: DepthContext51 ) {52 super();53 let currentWeight = 0;54 this.cumulatedWeights = [];55 for (let idx = 0; idx !== warbs.length; ++idx) {56 currentWeight += warbs[idx].weight;57 safePush(this.cumulatedWeights, currentWeight);58 }59 this.totalWeight = currentWeight;...

Full Screen

Full Screen

DepthContext.ts

Source:DepthContext.ts Github

copy

Full Screen

...39 * Get back the requested DepthContext40 * @remarks Since 2.25.041 * @public42 */43export function getDepthContextFor(contextMeta: DepthContext | DepthIdentifier | string | undefined): DepthContext {44 if (contextMeta === undefined) {45 return { depth: 0 };46 }47 if (typeof contextMeta !== 'string') {48 return contextMeta as DepthContext;49 }50 const cachedContext = depthContextCache.get(contextMeta);51 if (cachedContext !== undefined) {52 return cachedContext;53 }54 const context = { depth: 0 };55 depthContextCache.set(contextMeta, context);56 return context;57}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { getDepthContextFor } = require('fast-check/lib/check/arbitrary/definition/ContextArbitrary');3const ctx = getDepthContextFor(5);4fc.assert(5 fc.property(fc.nat(), fc.nat(), (a, b) => {6 return a + b === b + a;7 }),8 { seed: 42, verbose: true, path: 'test3.js', endOnFailure: true, context: ctx }9);10const fc = require('fast-check');11const { getDepthContextFor } = require('fast-check/lib/check/arbitrary/definition/ContextArbitrary');12const ctx = getDepthContextFor(5);13fc.assert(14 fc.property(fc.nat(), fc.nat(), (a, b) => {15 return a + b === b + a;16 }),17 { seed: 42, verbose: true, path: 'test4.js', endOnFailure: true, context: ctx }18);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { getDepthContextFor } = require('fast-check');2const fc = getDepthContextFor(2);3fc.check(fc.property(fc.nat(), (n) => n >= 0));4const { getDepthContextFor } = require('fast-check');5const fc = getDepthContextFor(3);6fc.check(fc.property(fc.nat(), (n) => n >= 0));7const { getDepthContextFor } = require('fast-check');8const fc = getDepthContextFor(4);9fc.check(fc.property(fc.nat(), (n) => n >= 0));10const { getDepthContextFor } = require('fast-check');11const fc = getDepthContextFor(5);12fc.check(fc.property(fc.nat(), (n) => n >= 0));13const { getDepthContextFor } = require('fast-check');14const fc = getDepthContextFor(6);15fc.check(fc.property(fc.nat(), (n) => n >= 0));16const { getDepthContextFor } = require('fast-check');17const fc = getDepthContextFor(7);18fc.check(fc.property(fc.nat(), (n) => n >= 0));19const { getDepthContextFor } = require('fast-check');20const fc = getDepthContextFor(8);21fc.check(fc.property(fc.nat(), (n) => n >= 0));22const { getDepthContextFor } = require('fast-check');23const fc = getDepthContextFor(9);24fc.check(fc.property(fc.nat(), (n) => n >= 0));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ContextTree } = require('@fast-check/arbitrary/_internals/ContextTree');2const tree = new ContextTree();3const ctx1 = tree.getDepthContextFor(1);4const ctx2 = tree.getDepthContextFor(2);5const ctx3 = tree.getDepthContextFor(3);6const ctx4 = tree.getDepthContextFor(4);7const ctx5 = tree.getDepthContextFor(5);8console.log(ctx1, ctx2, ctx3, ctx4, ctx5);9const { ContextTree } = require('@fast-check/arbitrary/_internals/ContextTree');10const tree = new ContextTree();11const ctx1 = tree.getDepthContextFor(1);12const ctx2 = tree.getDepthContextFor(2);13const ctx3 = tree.getDepthContextFor(3);14const ctx4 = tree.getDepthContextFor(4);15const ctx5 = tree.getDepthContextFor(5);16console.log(ctx1, ctx2, ctx3, ctx4, ctx5);17const { ContextTree } = require('@fast-check/arbitrary/_internals/ContextTree');18const tree1 = ContextTree.create();19const tree2 = ContextTree.create();20const ctx1 = tree1.getDepthContextFor(1);21const ctx2 = tree1.getDepthContextFor(2);22const ctx3 = tree1.getDepthContextFor(3);23const ctx4 = tree1.getDepthContextFor(4);24const ctx5 = tree1.getDepthContextFor(5);25console.log(ctx1, ctx

Full Screen

Using AI Code Generation

copy

Full Screen

1const { getDepthContextFor } = require('fast-check');2const { it } = require('mocha');3const { expect } = require('chai');4it('should get the depth context for a given depth', () => {5 const depthContext = getDepthContextFor(2);6 expect(depthContext).to.have.property('depth', 2);7});8it('should get the depth context for a given depth', () => {9 const depthContext = getDepthContextFor(3);10 expect(depthContext).to.have.property('depth', 3);11});12it('should get the depth context for a given depth', () => {13 const depthContext = getDepthContextFor(4);14 expect(depthContext).to.have.property('depth', 4);15});16it('should get the depth context for a given depth', () => {17 const depthContext = getDepthContextFor(5);18 expect(depthContext).to.have.property('depth', 5);19});20it('should get the depth context for a given depth', () => {21 const depthContext = getDepthContextFor(6);22 expect(depthContext).to.have.property('depth', 6);23});24it('should get the depth context for a given depth', () => {25 const depthContext = getDepthContextFor(7);26 expect(depthContext).to.have.property('depth', 7);27});28it('should get the depth context for a given depth', () => {29 const depthContext = getDepthContextFor(8);30 expect(depthContext).to.have.property('depth', 8);31});32it('should get the depth context for a given depth', () => {33 const depthContext = getDepthContextFor(9);34 expect(depthContext).to.have.property('depth', 9);35});36it('should get the depth context for a given depth', () => {37 const depthContext = getDepthContextFor(10);38 expect(depthContext).to.have.property('depth', 10);39});40it('should get the depth context for a given depth', () => {41 const depthContext = getDepthContextFor(11);42 expect(depthContext).to.have.property('depth', 11);43});44it('should get the depth context for a given depth', () => {45 const depthContext = getDepthContextFor(12);46 expect(depthContext).to.have.property('depth', 12);47});48it('should get

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const context = fc.getDepthContextFor(5);3const arb = fc.array(fc.integer(), 1, 10);4const arb2 = fc.array(fc.integer(), 1, 10);5const arb3 = fc.tuple(arb, arb2);6fc.assert(7 fc.property(arb3, ([a, b]) => {8 return a.length + b.length >= 2;9 }),10 { verbose: true, context }11);12const fc = require('fast-check');13const context = fc.getDepthContextFor(5);14const arb = fc.array(fc.integer(), 1, 10);15const arb2 = fc.array(fc.integer(), 1, 10);16const arb3 = fc.tuple(arb, arb2);17fc.assert(18 fc.property(arb3, ([a, b]) => {19 return a.length + b.length >= 2;20 }),21 { verbose: true, context }22);23const fc = require('fast-check');24const context = fc.getDepthContextFor(5);25const arb = fc.array(fc.integer(), 1, 10);26const arb2 = fc.array(fc.integer(), 1, 10);27const arb3 = fc.tuple(arb, arb2);28fc.assert(29 fc.property(arb3, ([a, b]) => {30 return a.length + b.length >= 2;31 }),32 { verbose: true, context }33);34const fc = require('fast-check');35const context = fc.getDepthContextFor(5);36const arb = fc.array(fc.integer(), 1, 10);37const arb2 = fc.array(fc.integer(), 1, 10);38const arb3 = fc.tuple(arb, arb2);39fc.assert(40 fc.property(arb3, ([a, b]) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1import fc from 'fast-check'2import { getDepthContextFor } from 'fast-check/lib/check/arbitrary/definition/ContextArbitrary'3const ctx = getDepthContextFor(2)4const arb = fc.array(fc.integer(), 2)5const result = fc.sample(arb, {seed: 1, numRuns: 1, verbose: true, path: 'test3.js', endOnFailure: false, context: ctx})6console.log(result)7import fc from 'fast-check'8import { getDepthContextFor } from 'fast-check/lib/check/arbitrary/definition/ContextArbitrary'9const ctx = getDepthContextFor(2)10const arb = fc.array(fc.integer(), 3)11const result = fc.sample(arb, {seed: 1, numRuns: 1, verbose: true, path: 'test4.js', endOnFailure: false, context: ctx})12console.log(result)13import fc from 'fast-check'14import { getDepthContextFor } from 'fast-check/lib/check/arbitrary/definition/ContextArbitrary'15const ctx = getDepthContextFor(2)16const arb = fc.array(fc.integer(), 4)17const result = fc.sample(arb, {seed: 1, numRuns: 1, verbose: true, path: 'test5.js', endOnFailure: false, context: ctx})18console.log(result)19import fc from 'fast-check'20import { getDepthContextFor } from 'fast-check/lib/check/arbitrary/definition/ContextArbitrary'21const ctx = getDepthContextFor(2)

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