Best JavaScript code snippet using fast-check-monorepo
SubarrayArbitrary.spec.ts
Source:SubarrayArbitrary.spec.ts
1import * as fc from 'fast-check';2import { SubarrayArbitrary } from '../../../../src/arbitrary/_internals/SubarrayArbitrary';3import {4 assertProduceSameValueGivenSameSeed,5 assertProduceCorrectValues,6 assertProduceValuesShrinkableWithoutContext,7 assertShrinkProducesSameValueWithoutInitialContext,8 assertShrinkProducesStrictlySmallerValue,9} from '../__test-helpers__/ArbitraryAssertions';10function beforeEachHook() {11 jest.resetModules();12 jest.restoreAllMocks();13 fc.configureGlobal({ beforeEach: beforeEachHook });14}15beforeEach(beforeEachHook);16describe('SubarrayArbitrary', () => {17 describe('constructor', () => {18 it('should raise an error whenever minLength is below zero', () => {19 fc.assert(20 fc.property(21 fc.array(fc.integer()),22 fc.nat(),23 fc.nat(),24 fc.boolean(),25 (originalArray, minLengthSeed, maxLengthSeed, isOrdered) => {26 // Arrange27 const minLength = -minLengthSeed - 1;28 const maxLength = maxLengthSeed % (originalArray.length + 1);29 // Act / Assert30 expect(() => {31 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);32 }).toThrowError(/minimal length to be between 0/);33 }34 )35 );36 });37 it('should raise an error whenever minLength is greater than array size', () => {38 fc.assert(39 fc.property(40 fc.array(fc.integer()),41 fc.nat(),42 fc.nat(),43 fc.boolean(),44 (originalArray, minLengthOffsetToLength, maxLengthSeed, isOrdered) => {45 // Arrange46 const minLength = originalArray.length + minLengthOffsetToLength + 1;47 const maxLength = maxLengthSeed % (originalArray.length + 1);48 // Act / Assert49 expect(() => {50 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);51 }).toThrowError(/minimal length to be between 0/);52 }53 )54 );55 });56 it('should raise an error whenever maxLength is below zero', () => {57 fc.assert(58 fc.property(59 fc.array(fc.integer()),60 fc.nat(),61 fc.nat(),62 fc.boolean(),63 (originalArray, maxLengthSeed, minLengthSeed, isOrdered) => {64 // Arrange65 const minLength = minLengthSeed % (originalArray.length + 1);66 const maxLength = -maxLengthSeed - 1;67 // Act / Assert68 expect(() => {69 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);70 }).toThrowError(/maximal length to be between 0/);71 }72 )73 );74 });75 it('should raise an error whenever maxLength is greater than array size', () => {76 fc.assert(77 fc.property(78 fc.array(fc.integer()),79 fc.nat(),80 fc.nat(),81 fc.boolean(),82 (originalArray, maxLengthOffsetToLength, minLengthSeed, isOrdered) => {83 // Arrange84 const minLength = minLengthSeed % (originalArray.length + 1);85 const maxLength = originalArray.length + maxLengthOffsetToLength + 1;86 // Act / Assert87 expect(() => {88 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);89 }).toThrowError(/maximal length to be between 0/);90 }91 )92 );93 });94 it('should raise an error whenever minLength is greater than maxLength', () => {95 fc.assert(96 fc.property(97 fc98 .tuple(fc.nat(100), fc.nat(100))99 .map(([a, b]) => (a < b ? [a, b] : [b, a]))100 .filter(([a, b]) => a !== b),101 fc.nat(100),102 fc.boolean(),103 (minMax, offset, isOrdered) => {104 // Arrange105 const [maxLength, minLength] = minMax;106 const originalArray = [...Array(minMax[1] + offset)].map((_) => 0);107 // Act / Assert108 expect(() => {109 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);110 }).toThrowError(/minimal length to be inferior or equal to the maximal length/);111 }112 )113 );114 });115 it('should accept any valid combination of inputs', () => {116 fc.assert(117 fc.property(118 fc119 .tuple(fc.nat(100), fc.nat(100))120 .map(([a, b]) => (a < b ? [a, b] : [b, a]))121 .filter(([a, b]) => a !== b),122 fc.nat(100),123 fc.boolean(),124 (minMax, offset, isOrdered) => {125 // Arrange126 const [minLength, maxLength] = minMax;127 const originalArray = [...Array(minMax[1] + offset)].map((_) => 0);128 // Act / Assert129 expect(() => {130 new SubarrayArbitrary(originalArray, isOrdered, minLength, maxLength);131 }).not.toThrow();132 }133 )134 );135 });136 });137});138describe('SubarrayArbitrary (integration)', () => {139 type Extra = {140 data: number[];141 isOrdered: boolean;142 minLength: number;143 maxLength: number;144 };145 const extraParameters: fc.Arbitrary<Extra> = fc146 .record({147 data: fc.array(fc.integer()),148 isOrdered: fc.boolean(),149 lengthA: fc.nat(),150 lengthB: fc.nat(),151 })152 .map((ct) => {153 const rescaledLengthA = ct.lengthA % (ct.data.length + 1);154 const rescaledLengthB = ct.lengthB % (ct.data.length + 1);155 return {156 data: ct.data,157 isOrdered: ct.isOrdered,158 minLength: Math.min(rescaledLengthA, rescaledLengthB),159 maxLength: Math.max(rescaledLengthA, rescaledLengthB),160 };161 });162 const isCorrect = (arr: number[], ct: Extra) => {163 expect(arr.length).toBeGreaterThanOrEqual(ct.minLength);164 expect(arr.length).toBeLessThanOrEqual(ct.maxLength);165 if (ct.isOrdered) expect(isOrderedSubarray(ct.data, arr)).toBe(true);166 else expect(isSubarray(ct.data, arr)).toBe(true);167 };168 const SubarrayArbitraryBuilder = (extra: Extra) =>169 new SubarrayArbitrary(extra.data, extra.isOrdered, extra.minLength, extra.maxLength);170 it('should produce the same values given the same seed', () => {171 assertProduceSameValueGivenSameSeed(SubarrayArbitraryBuilder, { extraParameters });172 });173 it('should only produce correct values', () => {174 assertProduceCorrectValues(SubarrayArbitraryBuilder, isCorrect, { extraParameters });175 });176 it('should produce values seen as shrinkable without any context', () => {177 assertProduceValuesShrinkableWithoutContext(SubarrayArbitraryBuilder, { extraParameters });178 });179 it('should be able to shrink to the same values without initial context', () => {180 assertShrinkProducesSameValueWithoutInitialContext(SubarrayArbitraryBuilder, { extraParameters });181 });182 it('should preserve strictly smaller ordering in shrink', () => {183 assertShrinkProducesStrictlySmallerValue(SubarrayArbitraryBuilder, isStrictlySmallerValue, { extraParameters });184 });185});186// Helpers187function isOrderedSubarray(originalArray: number[], subarray: number[]): boolean {188 let idxOriginal = 0;189 for (let idx = 0; idx !== subarray.length; ++idx) {190 while (originalArray[idxOriginal] !== subarray[idx]) {191 ++idxOriginal;192 if (idxOriginal >= originalArray.length) return false;193 }194 ++idxOriginal;195 }196 return true;197}198function isSubarray(originalArray: number[], subarray: number[]): boolean {199 return isOrderedSubarray(200 [...originalArray].sort((a, b) => a - b),201 [...subarray].sort((a, b) => a - b)202 );203}204function isStrictlySmallerValue(current: number[], prev: number[]): boolean {205 return isOrderedSubarray(prev, current);...
Using AI Code Generation
1import { SubarrayArbitraryBuilder } from 'fast-check';2const arb = new SubarrayArbitraryBuilder([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);3console.log(arb.build());4import { SubarrayArbitraryBuilder } from 'fast-check/lib/check/arbitrary/SubarrayArbitraryBuilder';5const arb = new SubarrayArbitraryBuilder([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);6console.log(arb.build());7import { SubarrayArbitraryBuilder } from 'fast-check/lib/arbitrary/SubarrayArbitraryBuilder';8import { SubarrayArbitraryBuilder } from 'fast-check/lib/arbitrary/SubarrayArbitraryBuilder';9import { SubarrayArbitraryBuilder } from 'fast-check/lib/arbitrary/SubarrayArbitraryBuilder';10import { SubarrayArbitraryBuilder } from 'fast-check/lib/arbitrary
Using AI Code Generation
1const fc = require('fast-check');2const { SubarrayArbitraryBuilder } = require('fast-check/lib/arbitrary/SubarrayArbitraryBuilder.js');3const { array } = require('fast-check/lib/arbitrary/array.js');4const { string } = require('fast-check/lib/arbitrary/string.js');5const { set } = require('fast-check/lib/arbitrary/set.js');6const subarrayArbitraryBuilder = new SubarrayArbitraryBuilder();7 .array(array(string()))8 .subarray(set(string()))9 .build();10fc.assert(11 fc.property(subarrayArbitrary, ([subarray, array]) => {12 return subarray.every((v) => array.includes(v));13 })14);15 at SubarrayArbitraryBuilder.build (C:\Users\user\Documents\fast-check-monorepo\fast-check\lib\arbitrary\SubarrayArbitraryBuilder.js:34:38)16 at Object.<anonymous> (C:\Users\user\Documents\fast-check-monorepo\test.js:10:45)17 at Module._compile (internal/modules/cjs/loader.js:1063:30)18 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)19 at Module.load (internal/modules/cjs/loader.js:928:32)20 at Function.Module._load (internal/modules/cjs/loader.js:769:14)21 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Using AI Code Generation
1const { SubarrayArbitraryBuilder } = require('fast-check');2const arb = SubarrayArbitraryBuilder()3 .withMinSize(0)4 .withMaxSize(5)5 .withUnique(true)6 .withValues([1, 2, 3, 4, 5])7 .build();8arb.generate(1).then((v) => console.log(v));9const { SubarrayArbitraryBuilder } = require('fast-check');10const arb = SubarrayArbitraryBuilder()11 .withMinSize(0)12 .withMaxSize(5)13 .withUnique(false)14 .withValues([1, 2, 3, 4, 5])15 .build();16arb.generate(1).then((v) => console.log(v));17const { SubarrayArbitraryBuilder } = require('fast-check');18const arb = SubarrayArbitraryBuilder()19 .withMinSize(0)20 .withMaxSize(5)21 .withUnique(true)22 .withValues([1, 2, 3, 4, 5])23 .build();24arb.generate(1).then((v) => console.log(v));25const { SubarrayArbitraryBuilder } = require('fast-check');26const arb = SubarrayArbitraryBuilder()27 .withMinSize(0)28 .withMaxSize(5)29 .withUnique(false)30 .withValues([1, 2, 3, 4, 5])31 .build();32arb.generate(1).then((v) => console.log(v));33const { SubarrayArbitrary
Using AI Code Generation
1const {SubarrayArbitraryBuilder} = require('fast-check');2const {array} = require('fast-check');3const {string} = require('fast-check');4const {option} = require('fast-check');5const subarray = (arr) => {6 const builder = new SubarrayArbitraryBuilder();7 return builder.from(arr).build();8};9const testSubarray = () => {10 const testArray = array(string(), 1, 10);11 const testSubarray = subarray(testArray);12 const testOption = option(testSubarray);13 return testOption;14};15const testSubarray2 = () => {16 const testArray = array(string(), 1, 10);17 const testSubarray = subarray(testArray);18 const testOption = option(testSubarray);19 return testOption;20};21const testSubarray3 = () => {22 const testArray = array(string(), 1, 10);23 const testSubarray = subarray(testArray);24 const testOption = option(testSubarray);25 return testOption;26};27const testSubarray4 = () => {28 const testArray = array(string(), 1, 10);29 const testSubarray = subarray(testArray);30 const testOption = option(testSubarray);31 return testOption;32};33const testSubarray5 = () => {34 const testArray = array(string(), 1, 10);35 const testSubarray = subarray(testArray);36 const testOption = option(testSubarray);37 return testOption;38};39const testSubarray6 = () => {40 const testArray = array(string(), 1, 10);41 const testSubarray = subarray(testArray);42 const testOption = option(testSubarray);43 return testOption;44};45const testSubarray7 = () => {46 const testArray = array(string(), 1, 10);
Using AI Code Generation
1import { SubarrayArbitraryBuilder } from 'fast-check';2const builder = new SubarrayArbitraryBuilder();3 .withSubarrayLength(5)4 .withSubarrayLength(10)5 .withSubarrayLength(15)6 .withSubarrayLength(20)7 .build();8 .withSubarrayLength(5)9 .withSubarrayLength(10)10 .withSubarrayLength(15)11 .withSubarrayLength(20)12 .build();13 .withSubarrayLength(5)14 .withSubarrayLength(10)15 .withSubarrayLength(15)16 .withSubarrayLength(20)17 .build();18 .withSubarrayLength(5)19 .withSubarrayLength(10)20 .withSubarrayLength(15)21 .withSubarrayLength(20)22 .build();23 .withSubarrayLength(5)24 .withSubarrayLength(10)25 .withSubarrayLength(15)26 .withSubarrayLength(20)27 .build();28 .withSubarrayLength(5)29 .withSubarrayLength(10)30 .withSubarrayLength(15)31 .withSubarrayLength(20)32 .build();33 .withSubarrayLength(5)34 .withSubarrayLength(10)35 .withSubarrayLength(15)36 .withSubarrayLength(20)37 .build();38 .withSubarrayLength(5)39 .withSubarrayLength(10)40 .withSubarrayLength(15)41 .withSubarrayLength(20)42 .build();43 .withSubarrayLength(5)44 .withSubarrayLength(10)45 .withSubarrayLength(15)46 .withSubarrayLength(20)47 .build();48 .withSubarrayLength(5)49 .withSubarrayLength(10)50 .withSubarrayLength(15)51 .withSubarrayLength(20
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!!