How to use biasFactorArbitrary method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

ArbitraryAssertions.ts

Source:ArbitraryAssertions.ts Github

copy

Full Screen

...36 fc.assert(37 fc38 .property(39 fc.integer().noShrink(),40 biasFactorArbitrary(),41 fc.infiniteStream(fc.nat({ max: 20 })),42 extra,43 (seed, biasFactor, shrinkPath, extraParameters) => {44 // Arrange45 const arb = arbitraryBuilder(extraParameters);46 // Act / Assert47 let g1: Value<T> | null = arb.generate(randomFromSeed(seed), biasFactor);48 let g2: Value<T> | null = arb.generate(randomFromSeed(seed), biasFactor);49 if (noInitialContext) {50 const originalG2 = g2!;51 g2 = new Value(originalG2.value_, undefined, () => originalG2.value);52 }53 while (g1 !== null && g2 !== null) {54 assertEquality(isEqual, g1.value, g2.value, extraParameters);55 const pos = shrinkPath.next().value;56 g1 = arb.shrink(g1.value_, g1.context).getNthOrLast(pos);57 g2 = arb.shrink(g2.value_, g2.context).getNthOrLast(pos);58 }59 expect(g1).toBe(null);60 expect(g2).toBe(null);61 }62 )63 .afterEach(poisoningAfterEach),64 assertParameters65 );66}67// Optional requirements68// > The following requirements are optional as they do not break the design of fast-check when they are not totally ensured69// > But some of them are really recommended to build valid arbitraries that can be used.70export function assertProduceCorrectValues<T, U = never>(71 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,72 isCorrect: (v: T, extraParameters: U, arb: Arbitrary<T>) => void | boolean,73 options: {74 extraParameters?: fc.Arbitrary<U>;75 assertParameters?: fc.Parameters<unknown>;76 } = {}77): void {78 const { extraParameters: extra = fc.constant(undefined as unknown as U) as fc.Arbitrary<U>, assertParameters } =79 options;80 fc.assert(81 fc82 .property(83 fc.integer().noShrink(),84 biasFactorArbitrary(),85 fc.infiniteStream(fc.nat({ max: 20 })),86 extra,87 (seed, biasFactor, shrinkPath, extraParameters) => {88 // Arrange89 const arb = arbitraryBuilder(extraParameters);90 // Act / Assert91 let g: Value<T> | null = arb.generate(randomFromSeed(seed), biasFactor);92 while (g !== null) {93 assertCorrectness(isCorrect, g.value, extraParameters, arb);94 const pos = shrinkPath.next().value;95 g = arb.shrink(g.value, g.context).getNthOrLast(pos);96 }97 expect(g).toBe(null);98 }99 )100 .afterEach(poisoningAfterEach),101 assertParameters102 );103}104export function assertGenerateEquivalentTo<T, U = never>(105 arbitraryBuilderA: (extraParameters: U) => Arbitrary<T>,106 arbitraryBuilderB: (extraParameters: U) => Arbitrary<T>,107 options: {108 isEqual?: (v1: T, v2: T, extraParameters: U) => void | boolean;109 isEqualContext?: (c1: unknown, c2: unknown, extraParameters: U) => void | boolean;110 extraParameters?: fc.Arbitrary<U>;111 assertParameters?: fc.Parameters<unknown>;112 } = {}113): void {114 const {115 isEqual,116 isEqualContext,117 extraParameters: extra = fc.constant(undefined as unknown as U) as fc.Arbitrary<U>,118 assertParameters,119 } = options;120 fc.assert(121 fc122 .property(fc.integer().noShrink(), biasFactorArbitrary(), extra, (seed, biasFactor, extraParameters) => {123 // Arrange124 const arbA = arbitraryBuilderA(extraParameters);125 const arbB = arbitraryBuilderB(extraParameters);126 // Act127 const gA = arbA.generate(randomFromSeed(seed), biasFactor);128 const gB = arbB.generate(randomFromSeed(seed), biasFactor);129 // Assert130 assertEquality(isEqual, gA.value, gB.value, extraParameters);131 if (isEqualContext) {132 assertEquality(isEqualContext, gA.context, gB.context, extraParameters);133 }134 })135 .afterEach(poisoningAfterEach),136 assertParameters137 );138}139// Extra requirements140// The assertions above can be configured to push generators even further. They ensure more complex invariants.141// Following assertions are mostly derived from the one above.142// > assertShrinkProducesSameValueGivenSameSeed with option {noInitialContext:true}143// > assertGenerateProducesCorrectValues with option isCorrect: (v, _, arb) => arb.canShrinkWithoutContext(v)144// > assertShrinkProducesCorrectValues with option (v, _, arb) => arb.canShrinkWithoutContext(v)145export function assertShrinkProducesSameValueWithoutInitialContext<T, U = never>(146 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,147 options: {148 isEqual?: (v1: T, v2: T, extraParameters: U) => void | boolean;149 extraParameters?: fc.Arbitrary<U>;150 assertParameters?: fc.Parameters<unknown>;151 } = {}152): void {153 return assertProduceSameValueGivenSameSeed(arbitraryBuilder, { ...options, noInitialContext: true });154}155export function assertProduceValuesShrinkableWithoutContext<T, U = never>(156 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,157 options: {158 extraParameters?: fc.Arbitrary<U>;159 assertParameters?: fc.Parameters<unknown>;160 } = {}161): void {162 return assertProduceCorrectValues(arbitraryBuilder, (v, _, arb) => arb.canShrinkWithoutContext(v), options);163}164export function assertShrinkProducesStrictlySmallerValue<T, U = never>(165 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,166 isStrictlySmaller: (vNew: T, vOld: T, extraParameters: U) => void | boolean,167 options: {168 extraParameters?: fc.Arbitrary<U>;169 assertParameters?: fc.Parameters<unknown>;170 } = {}171): void {172 const previousValue: { value?: T } = {};173 function arbitraryBuilderInternal(...args: Parameters<typeof arbitraryBuilder>) {174 delete previousValue.value;175 return arbitraryBuilder(...args);176 }177 function isStrictlySmallerInternal(v: T, extraParameters: U) {178 try {179 if (!('value' in previousValue)) {180 return true;181 }182 const vNew = v;183 const vOld = previousValue.value!;184 try {185 const out = isStrictlySmaller(vNew, vOld, extraParameters);186 expect(out).not.toBe(false);187 } catch (err) {188 throw new Error(189 `Expect: ${fc.stringify(vNew)} to be strictly smaller than ${fc.stringify(vOld)}\n\nGot error: ${err}`190 );191 }192 } finally {193 previousValue.value = v;194 }195 }196 return assertProduceCorrectValues(arbitraryBuilderInternal, isStrictlySmallerInternal, options);197}198export function assertProduceSomeSpecificValues<T, U = never>(199 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,200 isSpecificValue: (value: T) => boolean,201 options: {202 extraParameters?: fc.Arbitrary<U>;203 assertParameters?: fc.Parameters<unknown>;204 } = {}205): void {206 let foundOne = false;207 function detectSpecificValue(value: T): boolean {208 if (isSpecificValue(value)) {209 foundOne = true;210 return false; // failure of the property211 }212 return true; // success of the property213 }214 try {215 assertProduceCorrectValues(arbitraryBuilder, detectSpecificValue, {216 ...options,217 // We default numRuns to 1000, but let user override it whenever needed218 assertParameters: { numRuns: 1000, ...options.assertParameters, endOnFailure: true },219 });220 } catch (err) {221 // no-op222 }223 expect(foundOne).toBe(true);224}225export function assertGenerateIndependentOfSize<T, U = never>(226 arbitraryBuilder: (extraParameters: U) => Arbitrary<T>,227 options: {228 isEqual?: (v1: T, v2: T, extraParameters: U) => void | boolean;229 isEqualContext?: (c1: unknown, c2: unknown, extraParameters: U) => void | boolean;230 extraParameters?: fc.Arbitrary<U>;231 assertParameters?: fc.Parameters<unknown>;232 } = {}233): void {234 const {235 extraParameters = fc.constant(undefined as unknown as U) as fc.Arbitrary<U>,236 isEqual,237 isEqualContext,238 assertParameters,239 } = options;240 assertGenerateEquivalentTo(241 (extra) => arbitraryBuilder(extra.requested),242 (extra) => withConfiguredGlobal(extra.global, () => arbitraryBuilder(extra.requested)),243 {244 extraParameters: fc.record({245 requested: extraParameters,246 global: fc.record({ defaultSizeToMaxWhenMaxSpecified: fc.boolean(), baseSize: sizeArb }, { requiredKeys: [] }),247 }),248 isEqual: isEqual !== undefined ? (a, b, extra) => isEqual(a, b, extra.requested) : undefined,249 isEqualContext: isEqualContext !== undefined ? (a, b, extra) => isEqualContext(a, b, extra.requested) : undefined,250 assertParameters,251 }252 );253}254// Various helpers255function randomFromSeed(seed: number): Random {256 return new Random(prand.xorshift128plus(seed));257}258function biasFactorArbitrary() {259 return fc.option(fc.integer({ min: 2 }), { freq: 2, nil: undefined });260}261function assertEquality<T, U>(262 isEqual: ((v1: T, v2: T, extraParameters: U) => void | boolean) | undefined,263 v1: T,264 v2: T,265 extraParameters: U266): void {267 try {268 if (isEqual) {269 const out = isEqual(v1, v2, extraParameters);270 expect(out).not.toBe(false);271 } else {272 expect(v1).toStrictEqual(v2);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const {biasFactorArbitrary} = require('fast-check-monorepo');3const arb = biasFactorArbitrary(fc.nat(), 0.5);4fc.assert(fc.property(arb, (n) => {5 console.log(n);6 return true;7}));8const arb = biasFactorArbitrary(fc.double(), 0.5);9const arb = biasFactorArbitrary(fc.double(), 0.5);10const arb = biasFactorArbitrary(fc.double(), 0.5);11const arb = biasFactorArbitrary(fc.double(), 0.5);12const arb = biasFactorArbitrary(fc.double(), 0.5);13const arb = biasFactorArbitrary(fc.double(), 0.5);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { biasFactorArbitrary } = require('fast-check/lib/check/runner/ArbitraryRunner');3const arb = fc.integer(0, 100);4const biasedArb = biasFactorArbitrary(arb, 0.5);5const biasedArb2 = biasFactorArbitrary(arb, 0.1);6const biasedArb3 = biasFactorArbitrary(arb, 0.9);7const biasedArb4 = biasFactorArbitrary(arb, 1.0);8console.log(biasedArb);9console.log(biasedArb2);10console.log(biasedArb3);11console.log(biasedArb4);12const fc = require('fast-check');13const { biasFactorArbitrary } = require('fast-check/lib/check/runner/ArbitraryRunner');14const arb = fc.integer(0, 100);15const biasedArb = biasFactorArbitrary(arb, 0.5);16const biasedArb2 = biasFactorArbitrary(arb, 0.1);17const biasedArb3 = biasFactorArbitrary(arb, 0.9);18const biasedArb4 = biasFactorArbitrary(arb, 1.0);19console.log(biasedArb);20console.log(biasedArb2);21console.log(biasedArb3);22console.log(biasedArb4);

Full Screen

Using AI Code Generation

copy

Full Screen

1import * as fc from 'fast-check';2import { biasFactorArbitrary } from 'fast-check/lib/arbitrary/biasFactorArbitrary';3import { BiasFactor } from 'fast-check/lib/types';4import { biasArbitrary } from 'fast-check/lib/arbitrary/biasArbitrary';5import { Bias } from 'fast-check/lib/types';6import { Arbitrary } from 'fast-check/lib/types';7import { ArbitraryWithShrink } from 'fast-check/lib/types';8import { Shrinkable } from 'fast-check/lib/types';9import { Stream } from 'fast-check/lib/types';10type biasFactor = BiasFactor;11type bias = Bias;12type arbitrary = Arbitrary;13type arbitraryWithShrink = ArbitraryWithShrink;14type shrinkable = Shrinkable;15type stream = Stream;16type biasFactorArbitrary = (arg0: arbitrary, arg1: biasFactor, arg2: bias) => arbitraryWithShrink;17type biasArbitrary = (arg0: arbitrary, arg1: bias) => arbitraryWithShrink;18type shrinkableToStream = (arg0: shrinkable) => stream;19type streamToArray = (arg0: stream) => shrinkable[];20const biasFactorArbitrary: biasFactorArbitrary = (arbitrary: arbitrary, biasFactor: biasFactor, bias: bias) =>

Full Screen

Using AI Code Generation

copy

Full Screen

1const { biasFactorArbitrary } = require("@dubzzz/fast-check");2const { biasFactorArbitrary } = require("@dubzzz/fast-check");3const { biasFactorArbitrary } = require("@dubzzz/fast-check");4const { biasFactorArbitrary } = require("@dubzzz/fast-check");5const { biasFactorArbitrary } = require("@dubzzz/fast-check");6const { biasFactorArbitrary } = require("@dubzzz/fast-check");7const { biasFactorArbitrary } = require("@dubzzz/fast-check");8const { biasFactorArbitrary } = require("@dubzzz/fast-check");

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