How to use validSparseArrayConstraints method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

sparseArray.spec.ts

Source:sparseArray.spec.ts Github

copy

Full Screen

...24});25describe('sparseArray', () => {26 it('should always specify a minLength and maxLength on the underlying set', () => {27 fc.assert(28 fc.property(fc.option(validSparseArrayConstraints(), { nil: undefined }), (ct) => {29 // Arrange30 fc.pre(!isLimitNoTrailingCase(ct));31 const tuple = jest.spyOn(TupleMock, 'tuple');32 const uniqueArray = jest.spyOn(UniqueMock, 'uniqueArray');33 const { instance: tupleInstance } = fakeArbitraryStaticValue(() => []);34 const { instance: uniqueInstance } = fakeArbitraryStaticValue(() => []);35 tuple.mockReturnValueOnce(tupleInstance);36 uniqueArray.mockReturnValueOnce(uniqueInstance);37 const { instance: arb } = fakeArbitrary();38 // Act39 sparseArray(arb, ct);40 // Assert41 expect(uniqueArray).toHaveBeenCalledTimes(1);42 expect(uniqueArray).toHaveBeenCalledWith(43 expect.anything(),44 expect.objectContaining({ minLength: expect.any(Number), maxLength: expect.any(Number) })45 );46 })47 );48 });49 it('should always pass a not too large maxLength or with a size to set given the length we expect at the end', () => {50 fc.assert(51 fc.property(fc.option(validSparseArrayConstraints(), { nil: undefined }), (ct) => {52 // Arrange53 fc.pre(!isLimitNoTrailingCase(ct));54 const tuple = jest.spyOn(TupleMock, 'tuple');55 const uniqueArray = jest.spyOn(UniqueMock, 'uniqueArray');56 const restrictedIntegerArbitraryBuilder = jest.spyOn(57 RestrictedIntegerArbitraryBuilderMock,58 'restrictedIntegerArbitraryBuilder'59 ); // called to build indexes60 const { instance: tupleInstance } = fakeArbitraryStaticValue(() => []);61 const { instance: uniqueArrayInstance } = fakeArbitraryStaticValue(() => []);62 tuple.mockReturnValueOnce(tupleInstance);63 uniqueArray.mockReturnValueOnce(uniqueArrayInstance);64 const { instance: arb } = fakeArbitrary();65 // Act66 sparseArray(arb, ct);67 // Assert68 expect(restrictedIntegerArbitraryBuilder).toHaveBeenCalled(); // at least once69 expect(uniqueArray).toHaveBeenCalledTimes(1);70 // First call is to configure items coming with data71 const maxGeneratedIndexes = restrictedIntegerArbitraryBuilder.mock.calls[0][1]; // ie maxGenerated72 const maxRequestedIndexes = restrictedIntegerArbitraryBuilder.mock.calls[0][2]; // ie max73 expect(maxGeneratedIndexes).toBeLessThanOrEqual(maxRequestedIndexes);74 const maxRequestedLength = uniqueArray.mock.calls[0][1].maxLength!;75 if (ct !== undefined && ct.noTrailingHole) {76 // maxRequestedIndexes is the maximal index we may have for the current instance (+1 is the length)77 // maxRequestedLength is the maximal number of elements we ask to the set78 const maxElementsToGenerateForArray = maxRequestedLength;79 const maxResultingArrayLength = maxRequestedIndexes + 1;80 expect(maxElementsToGenerateForArray).toBeLessThanOrEqual(maxResultingArrayLength);81 } else {82 // In the default case, for falsy noTrailingHole:83 // - nat will be called with the maximal length allowed for the requested array84 // - set with the maximal number of elements +185 const maxElementsToGenerateForArray = maxRequestedLength - 1;86 const maxResultingArrayLength = maxRequestedIndexes;87 expect(maxElementsToGenerateForArray).toBeLessThanOrEqual(maxResultingArrayLength);88 }89 // Second call is only to handle the length computation in case we allow trailing holes90 const resultedMinNumElements = ct !== undefined ? ct.minNumElements || 0 : 0;91 const resultedMaxLength = ct !== undefined && ct.maxLength !== undefined ? ct.maxLength : MaxLengthUpperBound;92 if (ct === undefined || (!ct.noTrailingHole && resultedMaxLength > resultedMinNumElements)) {93 expect(restrictedIntegerArbitraryBuilder).toHaveBeenCalledTimes(2);94 const [min, maxGenerated, max] = restrictedIntegerArbitraryBuilder.mock.calls[1];95 expect(min).toBe(resultedMinNumElements);96 expect(maxGenerated).toBe(maxGeneratedIndexes + 1);97 expect(max).toBe(maxRequestedIndexes + 1);98 }99 })100 );101 });102 it('should reject constraints having minNumElements > maxLength', () => {103 fc.assert(104 fc.property(105 validSparseArrayConstraints(['minNumElements', 'maxLength']),106 fc.nat({ max: 4294967295 }),107 fc.nat({ max: 4294967295 }),108 (draftCt, a, b) => {109 // Arrange110 fc.pre(a !== b);111 const ct = { ...draftCt, minNumElements: a > b ? a : b, maxLength: a > b ? b : a };112 const { instance: arb } = fakeArbitrary();113 // Act / Assert114 expect(() => sparseArray(arb, ct)).toThrowError(/non-hole/);115 }116 )117 );118 });119 it('should reject constraints having minNumElements > maxNumElements', () => {120 fc.assert(121 fc.property(122 validSparseArrayConstraints(['minNumElements', 'maxNumElements']),123 fc.nat({ max: 4294967295 }),124 fc.nat({ max: 4294967295 }),125 (draftCt, a, b) => {126 // Arrange127 fc.pre(a !== b);128 const ct = { ...draftCt, minNumElements: a > b ? a : b, maxNumElements: a > b ? b : a };129 const { instance: arb } = fakeArbitrary();130 // Act / Assert131 expect(() => sparseArray(arb, ct)).toThrowError(/non-hole/);132 }133 )134 );135 });136});137describe('sparseArray (integration)', () => {138 type Extra = SparseArrayConstraints | undefined;139 // Even if full of holes, they still are memory intensive we explicitely140 // limit num elements in order to avoid running our tests for too long141 const extraParameters: fc.Arbitrary<Extra> = fc.option(validSparseArrayConstraints([], 100), { nil: undefined });142 const isEqual = (v1: number[], v2: number[]): boolean => {143 // WARNING: Very long loops in Jest when comparing two very large sparse arrays144 expect(v1.length).toBe(v2.length);145 expect(Object.entries(v1)).toEqual(Object.entries(v2));146 return true;147 };148 const isCorrect = (v: number[], extra: Extra = {}) => {149 // Should be an array150 if (!Array.isArray(v)) return false;151 // Should not have a length greater than the requested one (if any)152 if (extra.maxLength !== undefined && v.length > extra.maxLength) return false;153 // Should contain at least the minimal number of requested items (if specified)154 if (extra.minNumElements !== undefined && Object.keys(v).length < extra.minNumElements) return false;155 // Should contain at most the maxiaml number of requested items (if specified)156 if (extra.maxNumElements !== undefined && Object.keys(v).length > extra.maxNumElements) return false;157 // Should only contain valid keys: numbers within 0 and length-1158 for (const k of Object.keys(v)) {159 const i = Number(k);160 if (Number.isNaN(i) || i < 0 || i >= v.length) return false;161 }162 // Should never end by a hole if user activated noTrailingHole163 if (extra.noTrailingHole && v.length > 0 && !(v.length - 1 in v)) return false;164 // If all the previous checks passed, then array should be ok165 return true;166 };167 const sparseArrayBuilder = (extra: Extra) => sparseArray(new FakeIntegerArbitrary(), extra);168 it('should produce the same values given the same seed', () => {169 assertProduceSameValueGivenSameSeed(sparseArrayBuilder, { extraParameters, isEqual });170 });171 it('should only produce correct values', () => {172 assertProduceCorrectValues(sparseArrayBuilder, isCorrect, { extraParameters });173 });174 it('should produce values seen as shrinkable without any context', () => {175 // Remark: It will not shrink towards the exact same values for various reasons,176 // - when noTrailingHole=false, there is no real way to buid back the targetLength177 // - the key-value pairs will most of the time not be in the same ordered as the build order,178 // thus it will lead to a different shrink order179 assertProduceValuesShrinkableWithoutContext(sparseArrayBuilder, { extraParameters });180 });181 it.each`182 source | constraints183 ${['1'] /* unsupported value */} | ${{}}184 ${[1, , , , ,] /* ending by a hole not allowed */} | ${{ noTrailingHole: true }}185 ${[, , , , , 3, , , , , , , , , , 6] /* not enough non-holey items */} | ${{ minNumElements: 3 }}186 ${[, , , , , 3, 4, , , 5, , , , , , 6] /* too many non-holey items */} | ${{ maxNumElements: 3 }}187 ${[, , , 4] /* too long (length is 4) */} | ${{ maxLength: 3 }}188 `('should not be able to generate $source with fc.sparseArray(..., $constraints)', ({ source, constraints }) => {189 // Arrange / Act190 const arb = sparseArray(new FakeIntegerArbitrary(), constraints);191 const out = arb.canShrinkWithoutContext(source);192 // Assert193 expect(out).toBe(false);194 });195 it.each`196 rawValue | constraints197 ${[1, , , , ,]} | ${{ noTrailingHole: false }}198 ${[, , , , , 3, , , , , , , , , , 6]} | ${{ minNumElements: 2 }}199 ${[, , , , , 3, 4, , , 5, , , , , , 6]} | ${{ maxNumElements: 4 }}200 ${[, , , 4]} | ${{ maxLength: 4 }}201 ${Object.assign(Array(200), { 1: 7 }) /* length longer than default maxGeneratedLength but ok for shrink */} | ${{}}202 ${[...Array(50)].map((_, i) => i) /* non-holey items higher than default maxGeneratedLength but ok for shrink */} | ${{}}203 `('should be able to shrink $rawValue with fc.sparseArray(..., $constraints)', ({ rawValue, constraints }) => {204 // Arrange205 const arb = sparseArray(new FakeIntegerArbitrary(), constraints);206 const value = new Value(rawValue, undefined);207 // Act208 const renderedTree = renderTree(buildShrinkTree(arb, value, { numItems: 100 })).join('\n');209 // Assert210 expect(arb.canShrinkWithoutContext(rawValue)).toBe(true);211 expect(renderedTree).toMatchSnapshot();212 });213});214// Helpers215function validSparseArrayConstraints(216 removedKeys: (keyof SparseArrayConstraints)[] = [],217 max: number | undefined = undefined218) {219 return fc220 .record(221 {222 maxLength: removedKeys.includes('maxLength') ? fc.constant(undefined) : fc.nat({ max }),223 minNumElements: removedKeys.includes('minNumElements')224 ? fc.constant(undefined)225 : fc.nat({ max: max !== undefined ? Math.min(5, max) : undefined }),226 maxNumElements: removedKeys.includes('maxNumElements') ? fc.constant(undefined) : fc.nat({ max }),227 noTrailingHole: removedKeys.includes('noTrailingHole') ? fc.constant(undefined) : fc.boolean(),228 },229 { requiredKeys: [] }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { validSparseArrayConstraints } from 'fast-check-monorepo';2const { validSparseArrayConstraints } = require('fast-check-monorepo');3const { validSparseArrayConstraints } = require('fast-check-monorepo').default;4const { validSparseArrayConstraints } = require('fast-check-monorepo').validSparseArrayConstraints;5const validSparseArrayConstraints = require('fast-check-monorepo').validSparseArrayConstraints;6const validSparseArrayConstraints = require('fast-check-monorepo').default.validSparseArrayConstraints;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { validSparseArrayConstraints } = require('fast-check');2const constraints = { minLength: 3, maxLength: 7, noUndefined: true };3const isValid = validSparseArrayConstraints(constraints);4const constraints2 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: true };5const isValid2 = validSparseArrayConstraints(constraints2);6const constraints3 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false };7const isValid3 = validSparseArrayConstraints(constraints3);8const constraints4 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: true };9const isValid4 = validSparseArrayConstraints(constraints4);10const constraints5 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: false };11const isValid5 = validSparseArrayConstraints(constraints5);12const constraints6 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: false, noBigSparse: true };13const isValid6 = validSparseArrayConstraints(constraints6);14const constraints7 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: false, noBigSparse: false };15const isValid7 = validSparseArrayConstraints(constraints7);16const constraints8 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: false, noBigSparse: false, noTrailingHoles: true };17const isValid8 = validSparseArrayConstraints(constraints8);18const constraints9 = { minLength: 3, maxLength: 7, noUndefined: true, noEmpty: false, noSparse: false, noBigSparse: false, noTrailingHoles: false };19const isValid9 = validSparseArrayConstraints(constraints9);20const constraints10 = { minLength: 3,

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { validSparseArrayConstraints } = require('fast-check-monorepo');3const validConstraints = validSparseArrayConstraints(0, 100);4fc.assert(5 fc.property(fc.sparseArray(fc.integer(), validConstraints), (arr) => {6 return arr.length <= 100 && arr.length >= 0;7 })8);9const fc = require('fast-check');10const { validSparseArrayConstraints } = require('fast-check-monorepo');11const validConstraints = validSparseArrayConstraints(0, 100);12fc.assert(13 fc.property(fc.sparseArray(fc.integer(), validConstraints), (arr) => {14 return arr.length <= 100 && arr.length >= 0;15 })16);17const fc = require('fast-check');18const { validSparseArrayConstraints } = require('fast-check-monorepo');19const validConstraints = validSparseArrayConstraints(0, 100);20fc.assert(21 fc.property(fc.sparseArray(fc.integer(), validConstraints), (arr) => {22 return arr.length <= 100 && arr.length >= 0;23 })24);25const fc = require('fast-check');26const { validSparseArrayConstraints } = require('fast-check-monorepo');27const validConstraints = validSparseArrayConstraints(0, 100);28fc.assert(29 fc.property(fc.sparseArray(fc.integer(), validConstraints), (arr) => {30 return arr.length <= 100 && arr.length >= 0;31 })32);33const fc = require('fast-check');34const { validSparseArrayConstraints } = require('fast-check-monorepo');35const validConstraints = validSparseArrayConstraints(0, 100);36fc.assert(37 fc.property(fc.sparseArray(fc.integer(), validConstraints), (arr) => {38 return arr.length <= 100 && arr.length >= 0;39 })40);41const fc = require('fast-check');42const { validSparseArray

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check')2const { validSparseArrayConstraints } = require('fast-check/lib/check/arbitrary/ArrayArbitrary')3const constraints = validSparseArrayConstraints(10, 5, 10)4console.log(constraints)5{ maxLength: 5,6 minNumOfValues: 10 }7interface SparseArrayConstraints {8 minLength: number;9 maxLength: number;10 sparse: boolean;11 minNumOfValues: number;12 maxNumOfValues: number;13}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { validSparseArrayConstraints } from 'fast-check-monorepo';2const constraints = validSparseArrayConstraints(0.5, 0.5, 5, 10);3console.log(constraints);4import { validSparseArray } from 'fast-check-monorepo';5const arr = validSparseArray(constraints);6console.log(arr);7{8 "scripts": {9 },10 "dependencies": {11 }12}

Full Screen

Using AI Code Generation

copy

Full Screen

1import * as fc from 'fast-check';2const constraints = fc.validSparseArrayConstraints({ maxLength: 20 });3const gen = fc.array(constraints);4const sample = fc.sample(gen, 5, 10);5console.log(sample);6const constraints = fc.validSparseArrayConstraints({ maxLength: 20 });7const gen = fc.array(constraints);8const constraints = fc.validSparseArrayConstraints({ maxLength: 2 });9const gen = fc.tuple(constraints);10const constraints = fc.validSparseArrayConstraints({ maxLength: 2 });11const gen = fc.object(constraints);12const constraints = fc.validSparseArrayConstraints({ maxLength: 2 });13const gen = fc.dictionary(constraints);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { validSparseArrayConstraints } from 'fast-check-monorepo';2const constraints = validSparseArrayConstraints(5, 10, 5, 100, 5);3console.log(constraints);4import { validSparseArrayConstraints } from 'fast-check-monorepo';5const constraints = validSparseArrayConstraints(5, 10, 5, 100, 5);6console.log(constraints);7import { validSparseArrayConstraints } from 'fast-check';8const constraints = validSparseArrayConstraints(5, 10, 5, 100, 5);9console.log(constraints);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { validSparseArrayConstraints } = require('fast-check');2const constraints = {3}4const testArray = [1, 2, , 3, , , , , , 4];5const test = validSparseArrayConstraints(testArray, constraints);6console.log(test);7console.log(testArray);8console.log(constraints);9const { validSparseArrayConstraints } = require('fast-check');10const constraints = {11}12const testArray = [1, 2, , 3, , , , , , 4];13const test = validSparseArrayConstraints(testArray, constraints);14console.log(test);15console.log(testArray);16console.log(constraints);

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