Best JavaScript code snippet using fast-check-monorepo
AnyArbitraryBuilder.ts
Source:AnyArbitraryBuilder.ts
1import { Arbitrary } from '../../../check/arbitrary/definition/Arbitrary';2import { stringify } from '../../../utils/stringify';3import { array } from '../../array';4import { oneof } from '../../oneof';5import { tuple } from '../../tuple';6import { bigInt } from '../../bigInt';7import { date } from '../../date';8import { float32Array } from '../../float32Array';9import { float64Array } from '../../float64Array';10import { int16Array } from '../../int16Array';11import { int32Array } from '../../int32Array';12import { int8Array } from '../../int8Array';13import { uint16Array } from '../../uint16Array';14import { uint32Array } from '../../uint32Array';15import { uint8Array } from '../../uint8Array';16import { uint8ClampedArray } from '../../uint8ClampedArray';17import { sparseArray } from '../../sparseArray';18import { keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper } from '../mappers/KeyValuePairsToObject';19import { QualifiedObjectConstraints } from '../helpers/QualifiedObjectConstraints';20import { arrayToMapMapper, arrayToMapUnmapper } from '../mappers/ArrayToMap';21import { arrayToSetMapper, arrayToSetUnmapper } from '../mappers/ArrayToSet';22import { objectToPrototypeLessMapper, objectToPrototypeLessUnmapper } from '../mappers/ObjectToPrototypeLess';23import { letrec } from '../../letrec';24import { SizeForArbitrary } from '../helpers/MaxLengthFromMinLength';25import { uniqueArray } from '../../uniqueArray';26import { createDepthIdentifier, DepthIdentifier } from '../helpers/DepthContext';27/** @internal */28function mapOf<T, U>(29 ka: Arbitrary<T>,30 va: Arbitrary<U>,31 maxKeys: number | undefined,32 size: SizeForArbitrary | undefined,33 depthIdentifier: DepthIdentifier34) {35 return uniqueArray(tuple(ka, va), {36 maxLength: maxKeys,37 size,38 comparator: 'SameValueZero',39 selector: (t) => t[0],40 depthIdentifier,41 }).map(arrayToMapMapper, arrayToMapUnmapper);42}43/** @internal */44function dictOf<U>(45 ka: Arbitrary<string>,46 va: Arbitrary<U>,47 maxKeys: number | undefined,48 size: SizeForArbitrary | undefined,49 depthIdentifier: DepthIdentifier50) {51 return uniqueArray(tuple(ka, va), {52 maxLength: maxKeys,53 size,54 selector: (t) => t[0],55 depthIdentifier,56 }).map(keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper);57}58/** @internal */59function setOf<U>(60 va: Arbitrary<U>,61 maxKeys: number | undefined,62 size: SizeForArbitrary | undefined,63 depthIdentifier: DepthIdentifier64) {65 return uniqueArray(va, { maxLength: maxKeys, size, comparator: 'SameValueZero', depthIdentifier }).map(66 arrayToSetMapper,67 arrayToSetUnmapper68 );69}70/** @internal */71// eslint-disable-next-line @typescript-eslint/ban-types72function prototypeLessOf(objectArb: Arbitrary<object>) {73 return objectArb.map(objectToPrototypeLessMapper, objectToPrototypeLessUnmapper);74}75/** @internal */76function typedArray(constraints: { maxLength: number | undefined; size: SizeForArbitrary }) {77 return oneof(78 int8Array(constraints),79 uint8Array(constraints),80 uint8ClampedArray(constraints),81 int16Array(constraints),82 uint16Array(constraints),83 int32Array(constraints),84 uint32Array(constraints),85 float32Array(constraints),86 float64Array(constraints)87 );88}89/** @internal */90export function anyArbitraryBuilder(constraints: QualifiedObjectConstraints): Arbitrary<unknown> {91 const arbitrariesForBase = constraints.values;92 const depthSize = constraints.depthSize;93 const depthIdentifier = createDepthIdentifier();94 const maxDepth = constraints.maxDepth;95 const maxKeys = constraints.maxKeys;96 const size = constraints.size;97 const baseArb = oneof(98 ...arbitrariesForBase,99 ...(constraints.withBigInt ? [bigInt()] : []),100 ...(constraints.withDate ? [date()] : [])101 );102 return letrec((tie) => ({103 anything: oneof(104 { maxDepth, depthSize, depthIdentifier },105 baseArb, // Final recursion case106 tie('array'),107 tie('object'),108 ...(constraints.withMap ? [tie('map')] : []),109 ...(constraints.withSet ? [tie('set')] : []),110 ...(constraints.withObjectString ? [tie('anything').map((o) => stringify(o))] : []),111 // eslint-disable-next-line @typescript-eslint/ban-types112 ...(constraints.withNullPrototype ? [prototypeLessOf(tie('object') as Arbitrary<object>)] : []),113 ...(constraints.withTypedArray ? [typedArray({ maxLength: maxKeys, size })] : []),114 ...(constraints.withSparseArray115 ? [sparseArray(tie('anything'), { maxNumElements: maxKeys, size, depthIdentifier })]116 : [])117 ),118 // String keys119 keys: constraints.withObjectString120 ? oneof(121 { arbitrary: constraints.key, weight: 10 },122 { arbitrary: tie('anything').map((o) => stringify(o)), weight: 1 }123 )124 : constraints.key,125 // anything[]126 array: array(tie('anything'), { maxLength: maxKeys, size, depthIdentifier }),127 // Set<anything>128 set: setOf(tie('anything'), maxKeys, size, depthIdentifier),129 // Map<key, anything> | Map<anything, anything>130 map: oneof(131 mapOf(tie('keys') as Arbitrary<string>, tie('anything'), maxKeys, size, depthIdentifier),132 mapOf(tie('anything'), tie('anything'), maxKeys, size, depthIdentifier)133 ),134 // {[key:string]: anything}135 object: dictOf(tie('keys') as Arbitrary<string>, tie('anything'), maxKeys, size, depthIdentifier),136 })).anything;...
array.ts
Source:array.ts
1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';2import { ArrayArbitrary } from './_internals/ArrayArbitrary';3import {4 MaxLengthUpperBound,5 SizeForArbitrary,6 maxGeneratedLengthFromSizeForArbitrary,7} from './_internals/helpers/MaxLengthFromMinLength';8import { DepthIdentifier } from './_internals/helpers/DepthContext';9/**10 * Constraints to be applied on {@link array}11 * @remarks Since 2.4.012 * @public13 */14export interface ArrayConstraints {15 /**16 * Lower bound of the generated array size17 * @remarks Since 2.4.018 */19 minLength?: number;20 /**21 * Upper bound of the generated array size22 * @remarks Since 2.4.023 */24 maxLength?: number;25 /**26 * Define how large the generated values should be (at max)27 *28 * When used in conjonction with `maxLength`, `size` will be used to define29 * the upper bound of the generated array size while `maxLength` will be used30 * to define and document the general maximal length allowed for this case.31 *32 * @remarks Since 2.22.033 */34 size?: SizeForArbitrary;35 /**36 * When receiving a depth identifier, the arbitrary will impact the depth37 * attached to it to avoid going too deep if it already generated lots of items.38 *39 * In other words, if the number of generated values within the collection is large40 * then the generated items will tend to be less deep to avoid creating structures a lot41 * larger than expected.42 *43 * For the moment, the depth is not taken into account to compute the number of items to44 * define for a precise generate call of the array. Just applied onto eligible items.45 *46 * @remarks Since 2.25.047 */48 depthIdentifier?: DepthIdentifier | string;49}50/**51 * Extra but internal constraints that can be passed to array.52 * This extra set is made of constraints mostly targets experimental and internal features not yet mature to be exposed.53 * @internal54 */55export interface ArrayConstraintsInternal<T> extends ArrayConstraints {56 /**57 * Extra user-definable and hardcoded values.58 * Each entry in the array could be used to build the final generated value outputed by the arbitrary of array on generate.59 * Each entry must have at least one element of type T into it.60 * Each T must be a value acceptable for the arbitrary passed to the array.61 */62 experimentalCustomSlices?: T[][];63}64/**65 * For arrays of values coming from `arb`66 *67 * @param arb - Arbitrary used to generate the values inside the array68 * @param constraints - Constraints to apply when building instances (since 2.4.0)69 *70 * @remarks Since 0.0.171 * @public72 */73function array<T>(arb: Arbitrary<T>, constraints: ArrayConstraints = {}): Arbitrary<T[]> {74 const size = constraints.size;75 const minLength = constraints.minLength || 0;76 const maxLengthOrUnset = constraints.maxLength;77 const depthIdentifier = constraints.depthIdentifier;78 const maxLength = maxLengthOrUnset !== undefined ? maxLengthOrUnset : MaxLengthUpperBound;79 const specifiedMaxLength = maxLengthOrUnset !== undefined;80 const maxGeneratedLength = maxGeneratedLengthFromSizeForArbitrary(size, minLength, maxLength, specifiedMaxLength);81 const customSlices = (constraints as ArrayConstraintsInternal<T>).experimentalCustomSlices || [];82 return new ArrayArbitrary<T>(arb, minLength, maxGeneratedLength, maxLength, depthIdentifier, undefined, customSlices);83}...
DepthContext.ts
Source:DepthContext.ts
1/**2 * Internal symbol used to declare an opaque type for DepthIdentifier3 * @public4 */5declare const depthIdentifierSymbol: unique symbol;6/**7 * Type used to strongly type instances of depth identifier while keeping internals8 * what they contain internally9 *10 * @remarks Since 2.25.011 * @public12 */13export type DepthIdentifier = { [depthIdentifierSymbol]: true };14/**15 * Instance of depth, can be used to alter the depth perceived by an arbitrary16 * or to bias your own arbitraries based on the current depth17 *18 * @remarks Since 2.25.019 * @public20 */21export type DepthContext = {22 /**23 * Current depth (starts at 0, continues with 1, 2...).24 * Only made of integer values superior or equal to 0.25 *26 * Remark: Whenever altering the `depth` during a `generate`, please make sure to ALWAYS27 * reset it to its original value before you leave the `generate`. Otherwise the execution28 * will imply side-effects that will potentially impact the following runs and make replay29 * of the issue barely impossible.30 */31 depth: number;32};33/**34 * Internal cache for depth contexts35 * @internal36 */37const depthContextCache = new Map<string, DepthContext>();38/**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}58/**59 * Create a new and unique instance of DepthIdentifier60 * that can be shared across multiple arbitraries if needed61 * @public62 */63export function createDepthIdentifier(): DepthIdentifier {64 const identifier: DepthContext = { depth: 0 };65 return identifier as unknown as DepthIdentifier;...
Using AI Code Generation
1const fc = require('fast-check');2const { depthIdentifier } = require('fast-check-monorepo');3fc.assert(4 fc.property(fc.integer(), fc.integer(), (a, b) => {5 return depthIdentifier(a, b);6 })7);
Using AI Code Generation
1const { depthIdentifier } = require('fast-check-monorepo');2test('depthIdentifier', () => {3 expect(depthIdentifier(1)).toBe('1');4 expect(depthIdentifier(2)).toBe('2');5 expect(depthIdentifier(3)).toBe('3');6});7const { depthIdentifier } = require('fast-check-monorepo');8test('depthIdentifier', () => {9 expect(depthIdentifier(1)).toBe('1');10 expect(depthIdentifier(2)).toBe('2');11 expect(depthIdentifier(3)).toBe('3');12});13const { depthIdentifier } = require('fast-check-monorepo');14module.exports = async function() {15 depthIdentifier(1);16};17module.exports = {18};
Using AI Code Generation
1const fc = require('fast-check');2const { depthIdentifier } = require('fast-check-monorepo');3const depthIdentifierArb = depthIdentifier(fc.string(), 2);4fc.assert(5 fc.property(depthIdentifierArb, (id) => {6 return id.length === 2;7 })8);
Using AI Code Generation
1const { depthIdentifier } = require('fast-check-monorepo');2const { assert } = require('chai');3describe('depthIdentifier', () => {4 it('should return depthIdentifier', () => {5 const depth = depthIdentifier();6 assert.equal(depth, 'depthIdentifier');7 });8});9const { depthIdentifier } = require('fast-check-monorepo');10const { assert } = require('chai');11describe('depthIdentifier', () => {12 it('should return depthIdentifier', () => {13 const depth = depthIdentifier();14 assert.equal(depth, 'depthIdentifier');15 });16});17const { depthIdentifier } = require('fast-check-monorepo');18const { assert } = require('chai');19describe('depthIdentifier', () => {20 it('should return depthIdentifier', () => {21 const depth = depthIdentifier();22 assert.equal(depth, 'depthIdentifier');23 });24});25const { depthIdentifier } = require('fast-check-monorepo');26const { assert } = require('chai');27describe('depthIdentifier', () => {28 it('should return depthIdentifier', () => {29 const depth = depthIdentifier();30 assert.equal(depth, 'depthIdentifier');31 });32});33const { depthIdentifier } = require('fast-check-monorepo');34const { assert } = require('chai');35describe('depthIdentifier', () => {36 it('should return depthIdentifier', () => {37 const depth = depthIdentifier();38 assert.equal(depth, 'depthIdentifier');39 });40});41const { depthIdentifier } = require('fast-check-monorepo');42const { assert } = require('chai');43describe('depthIdentifier', () => {44 it('should return depthIdentifier', () => {45 const depth = depthIdentifier();46 assert.equal(depth, 'depthIdentifier');47 });48});49const { depthIdentifier } =
Using AI Code Generation
1const { depthIdentifier } = require('fast-check');2const { identifier } = require('fast-check/lib/types/property/PathValue.js');3const { path } = require('fast-check/lib/types/property/PathValue.js');4const { value } = require('fast-check/lib/types/property/PathValue.js');5const id = depthIdentifier(2);6const id2 = identifier(2);7const id3 = path(2);8const id4 = value(2);9console.log(id);10console.log(id2);11console.log(id3);12console.log(id4);13depthIdentifier(depth: number): number14identifier(depth: number): number15path(depth: number): number16value(depth: number): number17depthIdentifier(depth: number): number18const depthIdentifier = (depth: number): number => depth;19const identifier = (depth: number): number => depth;20const path = (depth: number): number => depth;21const value = (depth: number): number => depth;22depthIdentifier(depth: number): number23identifier(depth: number): number
Using AI Code Generation
1const { depthIdentifier } = require('fast-check-monorepo');2console.log(depthIdentifier(3));3const { depthIdentifier } = require('fast-check-monorepo');4console.log(depthIdentifier(4));5const { depthIdentifier } = require('fast-check-monorepo');6console.log(depthIdentifier(5));7const { depthIdentifier } = require('fast-check-monorepo');8console.log(depthIdentifier(6));9const { depthIdentifier } = require('fast-check-monorepo');10console.log(depthIdentifier(7));11const { depthIdentifier } = require('fast-check-monorepo');12console.log(depthIdentifier(8));13const { depthIdentifier } = require('fast-check-monorepo');14console.log(depthIdentifier(9));15const { depthIdentifier } = require('fast-check-monorepo');16console.log(depthIdentifier(10));17const { depthIdentifier } = require('fast-check-monorepo');18console.log(depthIdentifier(11));19const { depthIdentifier } = require('fast-check-monorepo');20console.log(depthIdentifier(12));21const { depthIdentifier } = require('fast-check-monorepo');22console.log(depthIdentifier(13));23const { depthIdentifier } = require('fast-check-monorepo');24console.log(depthIdentifier(14));25const { depthIdentifier } = require('fast-check-monorepo');26console.log(depthIdentifier(15));27const { depthIdentifier } = require('fast-check-monorepo');28console.log(depthIdentifier(16));
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!