Best JavaScript code snippet using fast-check-monorepo
sparseArray.spec.ts
Source:sparseArray.spec.ts
...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: [] }230 )231 .map((ct) => {232 // We use map there in order not to filter on generated values233 if (ct.minNumElements !== undefined && ct.maxNumElements !== undefined && ct.minNumElements > ct.maxNumElements) {234 return { ...ct, minNumElements: ct.maxNumElements, maxNumElements: ct.minNumElements };235 }236 return ct;237 })238 .map((ct) => {239 // We use map there in order not to filter on generated values240 if (ct.minNumElements !== undefined && ct.maxLength !== undefined && ct.minNumElements > ct.maxLength) {241 return { ...ct, minNumElements: ct.maxLength, maxLength: ct.minNumElements };242 }243 return ct;244 });245}246function isLimitNoTrailingCase(ct?: SparseArrayConstraints): boolean {247 // In that precise case the only solution is a simple constant equal to []248 return ct !== undefined && !!ct.noTrailingHole && ct.maxLength === 0;...
Using AI Code Generation
1import { RunnerParameters } from "fast-check";2const runnerParameters = new RunnerParameters();3const isLimitNoTrailingCase = runnerParameters.isLimitNoTrailingCase();4console.log(isLimitNoTrailingCase);5const runnerParameters = new RunnerParameters();6const isLimitNoTrailingCase = (runnerParameters as any).isLimitNoTrailingCase();7console.log(isLimitNoTrailingCase);
Using AI Code Generation
1const { isLimitNoTrailingCase } = require('fast-check-monorepo');2console.log(isLimitNoTrailingCase('abc'));3console.log(isLimitNoTrailingCase('abC'));4console.log(isLimitNoTrailingCase('abC1'));5const { isLimitNoTrailingCase } = require('fast-check-monorepo');6console.log(isLimitNoTrailingCase('abc'));7console.log(isLimitNoTrailingCase('abC'));8console.log(isLimitNoTrailingCase('abC1'));9const { isLimitNoTrailingCase } = require('fast-check-monorepo');10console.log(isLimitNoTrailingCase('abc'));11console.log(isLimitNoTrailingCase('abC'));12console.log(isLimitNoTrailingCase('abC1'));13const { isLimitNoTrailingCase } = require('fast-check-monorepo');14console.log(isLimitNoTrailingCase('abc'));15console.log(isLimitNoTrailingCase('abC'));16console.log(isLimitNoTrailingCase('abC1'));17const { isLimitNoTrailingCase } = require('fast-check-monorepo');18console.log(isLimitNoTrailingCase('abc'));19console.log(isLimitNoTrailingCase('abC'));20console.log(isLimitNoTrailingCase('abC1'));21const { isLimitNoTrailingCase } = require('fast-check-monorepo');22console.log(isLimitNoTrailingCase('abc'));23console.log(isLimitNoTrailingCase('abC'));24console.log(isLimitNoTrailingCase('abC1'));
Using AI Code Generation
1const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");2const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");3const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");4const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");5const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");6const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");7const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary/CharacterArbitrary.ts");8const { isLimitNoTrailingCase } = require("fast-check-monorepo/packages/fast-check/src/check/arbitrary
Using AI Code Generation
1const {isLimitNoTrailingCase} = require('fast-check/lib/check/arbitrary/CharacterArbitrary')2console.log(isLimitNoTrailingCase(0x10000, 0x10FFFF))3{4 "scripts": {5 },6 "dependencies": {7 }8}
Using AI Code Generation
1import {isLimitNoTrailingCase} from 'fast-check';2console.log(isLimitNoTrailingCase(0x0));3console.log(isLimitNoTrailingCase(0x10FFFF));4console.log(isLimitNoTrailingCase(0x110000));5import { isLimitNoTrailingCase } from 'fast-check';6console.log(isLimitNoTrailingCase(0x0));7console.log(isLimitNoTrailingCase(0x10FFFF));8console.log(isLimitNoTrailingCase(0x110000));9 table_name;10LINE 1: SELECT date::text FROM table_name;11 table_name;12LINE 1: SELECT date::text FROM table_name;13 table_name;14LINE 1: SELECT date::text FROM table_name;
Using AI Code Generation
1const isLimitNoTrailingCase = require('fast-check-monorepo');2const result = isLimitNoTrailingCase(1, 2, 3);3console.log(result);4const path = require('path');5module.exports = {6 output: {7 path: path.resolve(__dirname, 'dist')8 }9};
Using AI Code Generation
1const { isLimitNoTrailingCase } = require('fast-check');2const { isLimitNoTrailingCase } = require('fast-check/lib/utils/limit');3const { isLimitNoTrailingCase } = require('fast-check');4const { isLimitNoTrailingCase } = require('fast-check/lib/utils/limit');5const { isLimitNoTrailingCase } = require('fast-check-monorepo');6const { isLimitNoTrailingCase } = require('fast-check-monorepo/lib/utils/limit');7const { isLimitNoTrailingCase } = require('fast-check');8const { isLimitNoTrailingCase } = require('fast-check/lib/utils/limit');9const { isLimitNoTrailingCase } = require('fast-check-monorepo');10const { isLimitNoTrailingCase } = require('fast-check-monorepo/lib/utils/limit');11const { isLimitNoTrailingCase } = require('fast-check');12const { isLimitNoTrailingCase } = require('fast-check/lib/utils/limit');13const { isLimitNoTrailingCase } = require('fast-check-monorepo');14const { isLimitNoTrailingCase } = require('fast-check-monorepo/lib/utils/limit');15const { isLimitNoTrailingCase } = require('fast-check');16const { isLimitNoTrailingCase } = require('fast-check/lib/utils/limit');17const { isLimitNoTrailingCase } = require('fast-check-monorepo');18const { isLimitNoTrailingCase } = require('fast-check-monorepo/lib/utils/limit');19const { isLimitNoTrailingCase } = require('fast-check');20const { isLimitNoTr
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!!