How to use rawShrinkValue method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

FrequencyArbitrary.ts

Source:FrequencyArbitrary.ts Github

copy

Full Screen

1import { Random } from '../../random/generator/Random';2import { Stream } from '../../stream/Stream';3import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';4import { Value } from '../../check/arbitrary/definition/Value';5import { DepthContext, DepthIdentifier, getDepthContextFor } from './helpers/DepthContext';6import { depthBiasFromSizeForArbitrary, DepthSize } from './helpers/MaxLengthFromMinLength';7import { safePush } from '../../utils/globals';8const safePositiveInfinity = Number.POSITIVE_INFINITY;9const safeMaxSafeInteger = Number.MAX_SAFE_INTEGER;10const safeNumberIsInteger = Number.isInteger;11const safeMathFloor = Math.floor;12const safeMathPow = Math.pow;13const safeMathMin = Math.min;14/** @internal */15export class FrequencyArbitrary<T> extends Arbitrary<T> {16 readonly cumulatedWeights: number[];17 readonly totalWeight: number;18 static from<T>(warbs: _WeightedArbitrary<T>[], constraints: _Constraints, label: string): Arbitrary<T> {19 if (warbs.length === 0) {20 throw new Error(`${label} expects at least one weighted arbitrary`);21 }22 let totalWeight = 0;23 for (let idx = 0; idx !== warbs.length; ++idx) {24 const currentArbitrary = warbs[idx].arbitrary;25 if (currentArbitrary === undefined) {26 throw new Error(`${label} expects arbitraries to be specified`);27 }28 const currentWeight = warbs[idx].weight;29 totalWeight += currentWeight;30 if (!safeNumberIsInteger(currentWeight)) {31 throw new Error(`${label} expects weights to be integer values`);32 }33 if (currentWeight < 0) {34 throw new Error(`${label} expects weights to be superior or equal to 0`);35 }36 }37 if (totalWeight <= 0) {38 throw new Error(`${label} expects the sum of weights to be strictly superior to 0`);39 }40 const sanitizedConstraints: _SanitizedConstraints = {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;60 }61 generate(mrng: Random, biasFactor: number | undefined): Value<T> {62 if (this.mustGenerateFirst()) {63 // index=0 can be selected even if it has a weight equal to zero64 return this.safeGenerateForIndex(mrng, 0, biasFactor);65 }66 const selected = mrng.nextInt(this.computeNegDepthBenefit(), this.totalWeight - 1);67 for (let idx = 0; idx !== this.cumulatedWeights.length; ++idx) {68 if (selected < this.cumulatedWeights[idx]) {69 return this.safeGenerateForIndex(mrng, idx, biasFactor);70 }71 }72 throw new Error(`Unable to generate from fc.frequency`);73 }74 canShrinkWithoutContext(value: unknown): value is T {75 return this.canShrinkWithoutContextIndex(value) !== -1;76 }77 shrink(value: T, context?: unknown): Stream<Value<T>> {78 if (context !== undefined) {79 const safeContext = context as _FrequencyArbitraryContext<T>;80 const selectedIndex = safeContext.selectedIndex;81 const originalBias = safeContext.originalBias;82 const originalArbitrary = this.warbs[selectedIndex].arbitrary;83 const originalShrinks = originalArbitrary84 .shrink(value, safeContext.originalContext)85 .map((v) => this.mapIntoValue(selectedIndex, v, null, originalBias));86 if (safeContext.clonedMrngForFallbackFirst !== null) {87 if (safeContext.cachedGeneratedForFirst === undefined) {88 safeContext.cachedGeneratedForFirst = this.safeGenerateForIndex(89 safeContext.clonedMrngForFallbackFirst,90 0,91 originalBias92 );93 }94 const valueFromFirst = safeContext.cachedGeneratedForFirst;95 return Stream.of(valueFromFirst).join(originalShrinks);96 }97 return originalShrinks;98 }99 const potentialSelectedIndex = this.canShrinkWithoutContextIndex(value);100 if (potentialSelectedIndex === -1) {101 return Stream.nil(); // No arbitrary found to accept this value102 }103 return this.defaultShrinkForFirst(potentialSelectedIndex).join(104 this.warbs[potentialSelectedIndex].arbitrary105 .shrink(value, undefined) // re-checked by canShrinkWithoutContextIndex106 .map((v) => this.mapIntoValue(potentialSelectedIndex, v, null, undefined))107 );108 }109 /** Generate shrink values for first arbitrary when no context and no value was provided */110 private defaultShrinkForFirst(selectedIndex: number): Stream<Value<T>> {111 ++this.context.depth; // increase depth112 try {113 if (!this.mustFallbackToFirstInShrink(selectedIndex) || this.warbs[0].fallbackValue === undefined) {114 // Not applicable: no fallback to first arbitrary on shrink OR no hint to shrink without an initial value and context115 return Stream.nil();116 }117 } finally {118 --this.context.depth; // decrease depth (reset depth)119 }120 // The arbitrary at [0] accepts to shrink fallbackValue.default without any context (context=undefined)121 const rawShrinkValue = new Value(this.warbs[0].fallbackValue.default, undefined);122 return Stream.of(this.mapIntoValue(0, rawShrinkValue, null, undefined));123 }124 /** Extract the index of the generator that would have been able to gennrate the value */125 private canShrinkWithoutContextIndex(value: unknown): number {126 if (this.mustGenerateFirst()) {127 return this.warbs[0].arbitrary.canShrinkWithoutContext(value) ? 0 : -1;128 }129 try {130 ++this.context.depth; // increase depth131 for (let idx = 0; idx !== this.warbs.length; ++idx) {132 const warb = this.warbs[idx];133 if (warb.weight !== 0 && warb.arbitrary.canShrinkWithoutContext(value)) {134 return idx;135 }136 }137 return -1;138 } finally {139 --this.context.depth; // decrease depth (reset depth)140 }141 }142 /** Map the output of one of the children with the context of frequency */143 private mapIntoValue(144 idx: number,145 value: Value<T>,146 clonedMrngForFallbackFirst: Random | null,147 biasFactor: number | undefined148 ): Value<T> {149 const context: _FrequencyArbitraryContext<T> = {150 selectedIndex: idx,151 originalBias: biasFactor,152 originalContext: value.context,153 clonedMrngForFallbackFirst,154 };155 return new Value(value.value, context);156 }157 /** Generate using Arbitrary at index idx and safely handle depth context */158 private safeGenerateForIndex(mrng: Random, idx: number, biasFactor: number | undefined): Value<T> {159 ++this.context.depth; // increase depth160 try {161 const value = this.warbs[idx].arbitrary.generate(mrng, biasFactor);162 const clonedMrngForFallbackFirst = this.mustFallbackToFirstInShrink(idx) ? mrng.clone() : null;163 return this.mapIntoValue(idx, value, clonedMrngForFallbackFirst, biasFactor);164 } finally {165 --this.context.depth; // decrease depth (reset depth)166 }167 }168 /** Check if generating a value based on the first arbitrary is compulsory */169 private mustGenerateFirst(): boolean {170 return this.constraints.maxDepth <= this.context.depth;171 }172 /** Check if fallback on first arbitrary during shrinking is required */173 private mustFallbackToFirstInShrink(idx: number): boolean {174 return idx !== 0 && this.constraints.withCrossShrink && this.warbs[0].weight !== 0;175 }176 /** Compute the benefit for the current depth */177 private computeNegDepthBenefit(): number {178 const depthBias = this.constraints.depthBias;179 if (depthBias <= 0 || this.warbs[0].weight === 0) {180 return 0;181 }182 // We use a pow-based biased benefit as the deeper we go the more chance we have183 // to encounter thousands of instances of the current arbitrary.184 const depthBenefit = safeMathFloor(safeMathPow(1 + depthBias, this.context.depth)) - 1;185 // -0 has to be converted into 0 thus we call ||0186 return -safeMathMin(this.totalWeight * depthBenefit, safeMaxSafeInteger) || 0;187 }188}189/** @internal */190export type _Constraints = {191 withCrossShrink?: boolean;192 depthSize?: DepthSize;193 maxDepth?: number;194 depthIdentifier?: DepthIdentifier | string;195};196/** @internal */197type _SanitizedConstraints = {198 withCrossShrink: boolean;199 depthBias: number;200 maxDepth: number;201};202/** @internal */203interface _WeightedArbitrary<T> {204 weight: number;205 arbitrary: Arbitrary<T>;206 // If specified, the arbitrary must accept to shrink fallbackValue.default without any context207 fallbackValue?: { default: T };208}209/** @internal */210type _FrequencyArbitraryContext<T> = {211 selectedIndex: number;212 originalBias: number | undefined;213 originalContext: unknown;214 clonedMrngForFallbackFirst: Random | null;215 cachedGeneratedForFirst?: Value<T>;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');3const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');4const arb = integer(0, 1000);5const value = 500;6const shrinks = rawShrinkValue(arb, value);7console.log(shrinks);8const fc = require('fast-check');9const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');10const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');11const arb = integer(0, 1000);12const value = 500;13const shrinks = rawShrinkValue(arb, value);14console.log(shrinks);15const fc = require('fast-check');16const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');17const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');18const arb = integer(0, 1000);19const value = 500;20const shrinks = rawShrinkValue(arb, value);21console.log(shrinks);22const fc = require('fast-check');23const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');24const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');25const arb = integer(0, 1000);26const value = 500;27const shrinks = rawShrinkValue(arb, value);28console.log(shrinks);29const fc = require('fast-check');30const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink');31const { integer } = require('fast-check/lib/check/arbitrary/IntegerArbitrary');32const arb = integer(0, 1000);33const value = 500;34const shrinks = rawShrinkValue(arb, value);35console.log(shrinks

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { rawShrinkValue } = require("fast-check/lib/check/arbitrary/definition/Shrinkable");3const { nat } = require("fast-check/lib/check/arbitrary/NatArbitrary");4const test = () => {5 const shrunk = rawShrinkValue(nat(), 10);6 console.log(shrunk);7}8test();9Shrinkable {10 context: Context {11 },12 shrink: [Function (anonymous)]13}14const fc = require("fast-check");15const {nat} = require("fast-check/lib/check/arbitrary/NatArbitrary");16const test = () => {17 const shrunk = fc.shrink(nat(), 10);18 console.log(shrunk);19}20test();21Shrinkable {22 context: Context {23 },24 shrink: [Function (anonymous)]25}

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const {rawShrinkValue} = require('fast-check/lib/arbitrary/Shrinkable');3const {shrinkNumber} = require('fast-check/lib/arbitrary/number/NumberArbitrary');4const {shrinkString} = require('fast-check/lib/arbitrary/string/CharacterArbitrary');5const {shrinkObject} = require('fast-check/lib/arbitrary/object/ObjectArbitrary');6const shrinker = (value) => {7 if (typeof value === 'number') {8 return shrinkNumber(value);9 }10 if (typeof value === 'string') {11 return shrinkString(value);12 }13 if (typeof value === 'object') {14 return shrinkObject(value);15 }16 return [];17};18const value = {a: 1, b: 2};19const shrinkable = rawShrinkValue(value, shrinker);20shrinkable.shrink().forEach((v) => console.log(v.value));21I am using fast-check version 2.16.0. I am using the following code to import the rawShrinkValue method:22const {rawShrinkValue} = require('fast-check/lib/arbitrary/Shrinkable');23const rawShrinkValue = require('fast-check/lib/arbitrary/Shrinkable').rawShrinkValue;24const rawShrinkValue = require('fast-check/lib/arbitrary/Shrinkable').default.rawShrinkValue;25const rawShrinkValue = require('fast-check/lib/arbitrary/Shrinkable').rawShrinkValue.default;26None of these work. How can I import the rawShrinkValue method from the Shrinkable.js file?

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { rawShrinkValue } = require('fast-check/lib/check/arbitrary/definition/Shrinkable.js');3const { isValid } = require('fast-check/lib/check/runner/RunnerParameters.js');4const { stringify } = require('fast-check/lib/utils/Json.js');5const { stringifyValue } = require('fast-check/lib/check/runner/Value.js');6const { stringifyValue_ } = require('fast-check/lib/check/runner/Value.js');7const { PathReporter } = require('io-ts/lib/PathReporter');8const { report } = require('io-ts/lib/PathReporter');9const { pipe } = require('fp-ts/lib/pipeable');10const { Either } = require('fp-ts/lib/Either');11const { map } = require('fp-ts/lib/Either');12const { identity } = require('fp-ts/lib/function');13const { fromEither } = require('fp-ts/lib/Either');14const { fromPredicate } = require('fp-ts/lib/Either');15const { fold } = require('fp-ts/lib/Either');16const { left } = require('fp-ts/lib/Either');17const { right } = require('fp-ts/lib/Either');18const { string } = require('io-ts/lib/Codec');19const { number } = require('io-ts/lib/Codec');20const { array } = require('io-ts/lib/Codec');21const { type } = require('io-ts/lib/Codec');22const { Codec } = require('io-ts/lib/Codec');23const { is } = require('io-ts/lib/Codec');24const { identity } = require('io-ts/lib/Codec');25const { literal } = require('io-ts/lib/Codec');26const { boolean } = require('io-ts/lib/Codec');27const { nullType } = require('io-ts/lib/Codec');28const { undefinedType } = require('io-ts/lib/Codec');29const { mixed } = require('io-ts/lib/Codec');30const { UnknownArray } = require('io-ts/lib/Codec');31const { UnknownRecord } = require('io-ts/lib/Codec');32const { UnknownC } = require('io-ts/lib/Codec');33const { union } = require('io-ts/lib/Codec');34const { intersection } = require('io-ts/lib/Codec');35const { sum } = require('io-ts/lib/Codec');

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const prop = (x) => x === 42;3const run = () => {4 const { counterexample } = fc.check(prop, { seed: 42 });5 console.log(counterexample);6 const shrinked = fc.rawShrinkValue(counterexample, prop);7 console.log(shrinked);8};9run();10const fc = require('fast-check');11const prop = (x) => x === 42;12const run = () => {13 const { counterexample } = fc.check(prop, { seed: 42 });14 console.log(counterexample);15 const shrinked = fc.rawShrinkValue(counterexample, prop);16 console.log(shrinked);17};18run();19const fc = require('fast-check');20const prop = (x) => x === 42;21const run = () => {22 const { counterexample } = fc.check(prop, { seed: 42 });23 console.log(counterexample);24 const shrinked = fc.rawShrinkValue(counterexample, prop);25 console.log(shrinked);26};27run();28const fc = require('fast-check');29const prop = (x) => x === 42;30const run = () => {31 const { counterexample } = fc.check(prop, { seed: 42 });32 console.log(counterexample);33 const shrinked = fc.rawShrinkValue(counterexample, prop);34 console.log(shrinked);35};36run();37const fc = require('fast-check');38const prop = (x) => x === 42;39const run = () => {40 const { counterexample } = fc.check(prop, { seed: 42 });

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