Best JavaScript code snippet using fast-check-monorepo
TypedIntArrayArbitraryBuilder.spec.ts
Source:TypedIntArrayArbitraryBuilder.spec.ts
1import * as fc from 'fast-check';2import { typedIntArrayArbitraryArbitraryBuilder } from '../../../../../src/arbitrary/_internals/builders/TypedIntArrayArbitraryBuilder';3import { FakeIntegerArbitrary, fakeArbitrary, fakeArbitraryStaticValue } from '../../__test-helpers__/ArbitraryHelpers';4import * as ArrayMock from '../../../../../src/arbitrary/array';5import {6 assertProduceCorrectValues,7 assertProduceSameValueGivenSameSeed,8 assertProduceValuesShrinkableWithoutContext,9 assertShrinkProducesSameValueWithoutInitialContext,10} from '../../__test-helpers__/ArbitraryAssertions';11function beforeEachHook() {12 jest.resetModules();13 jest.restoreAllMocks();14 fc.configureGlobal({ beforeEach: beforeEachHook });15}16beforeEach(beforeEachHook);17describe('typedIntArrayArbitraryArbitraryBuilder', () => {18 it('should default constraints for arbitraryBuilder to defaultMin/Max when not specified', () => {19 fc.assert(20 fc.property(21 defaultsMinMaxTypedInt8Arb(),22 validArrayConstraintsArb(),23 ({ defaultMin, defaultMax, TypedArrayClass }, arrayConstraints) => {24 // Arrange25 const array = jest.spyOn(ArrayMock, 'array');26 const { instance: arrayInstance } = fakeArbitraryStaticValue<unknown[]>(() => []);27 array.mockReturnValue(arrayInstance);28 const constraints = { ...arrayConstraints };29 const arbitraryBuilder = jest.fn();30 const { instance: arbitraryInstance } = fakeArbitrary<number>();31 arbitraryBuilder.mockReturnValue(arbitraryInstance);32 // Act33 typedIntArrayArbitraryArbitraryBuilder(34 constraints,35 defaultMin,36 defaultMax,37 TypedArrayClass,38 arbitraryBuilder39 );40 // Assert41 expect(arbitraryBuilder).toHaveBeenLastCalledWith({ min: defaultMin, max: defaultMax });42 }43 )44 );45 });46 it('should properly distribute constraints accross arbitraries when receiving valid ones', () => {47 fc.assert(48 fc.property(49 validArrayConstraintsArb(),50 validIntegerConstraintsArb(-128, 127),51 (arrayConstraints, integerConstraints) => {52 // Arrange53 const array = jest.spyOn(ArrayMock, 'array');54 const { instance: arrayInstance } = fakeArbitraryStaticValue<unknown[]>(() => []);55 array.mockReturnValue(arrayInstance);56 const constraints = { ...arrayConstraints, ...integerConstraints };57 const defaultMin = -128;58 const defaultMax = 127;59 const TypedArrayClass = Int8Array;60 const arbitraryBuilder = jest.fn();61 const { instance: arbitraryInstance } = fakeArbitrary<number>();62 arbitraryBuilder.mockReturnValue(arbitraryInstance);63 // Act64 typedIntArrayArbitraryArbitraryBuilder(65 constraints,66 defaultMin,67 defaultMax,68 TypedArrayClass,69 arbitraryBuilder70 );71 // Assert72 expect(arbitraryBuilder).toHaveBeenLastCalledWith({73 min: defaultMin,74 max: defaultMax,75 ...integerConstraints,76 });77 }78 )79 );80 });81 it('should reject invalid integer ranges', () => {82 fc.assert(83 fc.property(84 validArrayConstraintsArb(),85 invalidIntegerConstraintsArb(-128, 127),86 (arrayConstraints, integerConstraints) => {87 // Arrange88 const array = jest.spyOn(ArrayMock, 'array');89 const { instance: arrayInstance } = fakeArbitraryStaticValue<unknown[]>(() => []);90 array.mockReturnValue(arrayInstance);91 const constraints = { ...arrayConstraints, ...integerConstraints };92 const defaultMin = -128;93 const defaultMax = 127;94 const TypedArrayClass = Int8Array;95 const arbitraryBuilder = jest.fn();96 const { instance: arbitraryInstance } = fakeArbitrary<number>();97 arbitraryBuilder.mockReturnValue(arbitraryInstance);98 // Act / Assert99 expect(() =>100 typedIntArrayArbitraryArbitraryBuilder(101 constraints,102 defaultMin,103 defaultMax,104 TypedArrayClass,105 arbitraryBuilder106 )107 ).toThrowError();108 }109 )110 );111 });112});113describe('typedIntArrayArbitraryArbitraryBuilder (integration)', () => {114 type Extra = { minLength?: number; maxLength?: number; min?: number; max?: number };115 const extraParameters: fc.Arbitrary<Extra> = fc116 .record(117 {118 minLength: fc.nat({ max: 5 }),119 maxLength: fc.nat({ max: 25 }),120 min: fc.integer({ min: -128, max: 127 }),121 max: fc.integer({ min: -128, max: 127 }),122 },123 { requiredKeys: [] }124 )125 .map((rawConstraints) => {126 const constraints = { ...rawConstraints };127 if ('minLength' in constraints && 'maxLength' in constraints && constraints.minLength! > constraints.maxLength!) {128 [constraints.minLength, constraints.maxLength] = [constraints.maxLength, constraints.minLength];129 }130 if ('min' in constraints && 'max' in constraints && constraints.min! > constraints.max!) {131 [constraints.min, constraints.max] = [constraints.max, constraints.min];132 }133 return constraints;134 });135 const isCorrect = (value: Int8Array, extra: Extra) => {136 expect(value).toBeInstanceOf(Int8Array);137 if ('minLength' in extra) {138 expect(value.length).toBeGreaterThanOrEqual(extra.minLength!);139 }140 if ('maxLength' in extra) {141 expect(value.length).toBeLessThanOrEqual(extra.maxLength!);142 }143 };144 const typedIntArrayArbitraryArbitraryBuilderBuilder = (extra: Extra) =>145 typedIntArrayArbitraryArbitraryBuilder(146 extra,147 -128,148 127,149 Int8Array,150 ({ min = 0, max = min }) => new FakeIntegerArbitrary(min, max - min)151 );152 it('should produce the same values given the same seed', () => {153 assertProduceSameValueGivenSameSeed(typedIntArrayArbitraryArbitraryBuilderBuilder, { extraParameters });154 });155 it('should only produce correct values', () => {156 assertProduceCorrectValues(typedIntArrayArbitraryArbitraryBuilderBuilder, isCorrect, { extraParameters });157 });158 it('should produce values seen as shrinkable without any context', () => {159 assertProduceValuesShrinkableWithoutContext(typedIntArrayArbitraryArbitraryBuilderBuilder, { extraParameters });160 });161 it('should be able to shrink to the same values without initial context', () => {162 assertShrinkProducesSameValueWithoutInitialContext(typedIntArrayArbitraryArbitraryBuilderBuilder, {163 extraParameters,164 });165 });166});167// Helpers168function defaultsMinMaxTypedInt8Arb() {169 return fc170 .tuple(fc.integer({ min: -128, max: 127 }), fc.integer({ min: -128, max: 127 }))171 .map(([defaultA, defaultB]) => ({172 defaultMin: Math.min(defaultA, defaultB),173 defaultMax: Math.min(defaultA, defaultB),174 TypedArrayClass: Int8Array,175 }));176}177function validArrayConstraintsArb() {178 return fc.record({ minLength: fc.nat(), maxLength: fc.nat() }, { withDeletedKeys: true }).map((ct) => {179 if (ct.minLength !== undefined && ct.maxLength !== undefined && ct.minLength > ct.maxLength) {180 return { minLength: ct.maxLength, maxLength: ct.minLength };181 }182 return ct;183 });184}185function validIntegerConstraintsArb(min: number, max: number) {186 return fc187 .record({ min: fc.integer({ min, max }), max: fc.integer({ min, max }) }, { withDeletedKeys: true })188 .map((ct) => {189 if (ct.min !== undefined && ct.max !== undefined && ct.min > ct.max) {190 return { min: ct.max, max: ct.min };191 }192 return ct;193 });194}195function invalidIntegerConstraintsArb(min: number, max: number) {196 return fc.oneof(197 // min > max198 fc199 .record({ min: fc.integer({ min, max }), max: fc.integer({ min, max }) })200 .filter(({ min, max }) => min !== max)201 .map((ct) => (ct.min < ct.max ? { min: ct.max, max: ct.min } : ct)),202 // min < lowest203 fc.record({ min: fc.integer({ min: Number.MIN_SAFE_INTEGER, max: min - 1 }) }),204 fc.record({ min: fc.integer({ min: Number.MIN_SAFE_INTEGER, max: min - 1 }), max: fc.integer({ min, max }) }),205 // max > highest206 fc.record({ min: fc.integer({ min, max }), max: fc.integer({ min: max + 1, max: Number.MAX_SAFE_INTEGER }) }),207 fc.record({ max: fc.integer({ min: max + 1, max: Number.MAX_SAFE_INTEGER }) })208 );...
Using AI Code Generation
1const fc = require('fast-check');2const { typedIntArrayArbitraryArbitraryBuilderBuilder } = require('fast-check-monorepo');3const intArrayArbitraryBuilder = typedIntArrayArbitraryArbitraryBuilderBuilder(Int32Array);4const intArrayArbitrary = intArrayArbitraryBuilder().noBias().noShrink().filter((array) => array.length > 0);5fc.assert(6 fc.property(intArrayArbitrary, (array) => {7 return array.length > 0;8 }),9);10const fc = require('fast-check');11const { typedIntArrayArbitraryArbitraryBuilderBuilder } = require('fast-check-monorepo');12const intArrayArbitraryBuilder = typedIntArrayArbitraryArbitraryBuilderBuilder(Int32Array);13const intArrayArbitrary = intArrayArbitraryBuilder().noBias().noShrink().filter((array) => array.length > 0);14fc.assert(15 fc.property(intArrayArbitrary, (array) => {16 return array.length > 0;17 }),18);
Using AI Code Generation
1import { typedIntArrayArbitraryArbitraryBuilderBuilder } from 'fast-check-monorepo';2typedIntArrayArbitraryArbitraryBuilderBuilder().int8ArrayArbitraryBuilder().build().generate();3typedIntArrayArbitraryArbitraryBuilderBuilder().int16ArrayArbitraryBuilder().build().generate();4typedIntArrayArbitraryArbitraryBuilderBuilder().int32ArrayArbitraryBuilder().build().generate();5typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ArrayArbitraryBuilder().build().generate();6typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ClampedArrayArbitraryBuilder().build().generate();7typedIntArrayArbitraryArbitraryBuilderBuilder().uint16ArrayArbitraryBuilder().build().generate();8typedIntArrayArbitraryArbitraryBuilderBuilder().uint32ArrayArbitraryBuilder().build().generate();9typedIntArrayArbitraryArbitraryBuilderBuilder().int8ArrayArbitraryBuilder().min(0).build().generate();10typedIntArrayArbitraryArbitraryBuilderBuilder().int16ArrayArbitraryBuilder().max(100).build().generate();11typedIntArrayArbitraryArbitraryBuilderBuilder().int32ArrayArbitraryBuilder().min(0).max(100).build().generate();12typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ArrayArbitraryBuilder().min(0).max(100).build().generate();13typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ClampedArrayArbitraryBuilder().min(0).max(100).build().generate();14typedIntArrayArbitraryArbitraryBuilderBuilder().uint16ArrayArbitraryBuilder().min(0).max(100).build().generate();15typedIntArrayArbitraryArbitraryBuilderBuilder().uint32ArrayArbitraryBuilder().min(0).max(100).build().generate();16typedIntArrayArbitraryArbitraryBuilderBuilder().int8ArrayArbitraryBuilder().min(0).build().generate();17typedIntArrayArbitraryArbitraryBuilderBuilder().int16ArrayArbitraryBuilder().max(100).build().generate();18typedIntArrayArbitraryArbitraryBuilderBuilder().int32ArrayArbitraryBuilder().min(0).max(100).build().generate();19typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ArrayArbitraryBuilder().min(0).max(100).build().generate();20typedIntArrayArbitraryArbitraryBuilderBuilder().uint8ClampedArrayArbitraryBuilder().min(0).max(100).build().generate();21typedIntArrayArbitraryArbitraryBuilderBuilder().uint16ArrayAr
Using AI Code Generation
1import { typedIntArrayArbitraryArbitraryBuilderBuilder } from "fast-check-monorepo";2const arb = typedIntArrayArbitraryArbitraryBuilderBuilder().integer().build();3const arb2 = typedIntArrayArbitraryArbitraryBuilderBuilder().integer().build();4- [typedIntArrayArbitraryArbitraryBuilderBuilder](#typedintarrayarbitraryarbitrarybuilderbuilder)5 - [Parameters](#parameters)6 - [Examples](#examples)7 - [integer](#integer)8 - [Parameters](#parameters-1)9 - [Examples](#examples-1)10 - [build](#build)11 - [Examples](#examples-2)12- [TypedIntArrayArbitrary](#typedintarrayarbitrary)13 - [Parameters](#parameters-2)14 - [Examples](#examples-3)15- [TypedIntArrayArbitraryBuilder](#typedintarrayarbitrarybuilder)16 - [Parameters](#parameters-3)17 - [Examples](#examples-4)18 - [integer](#integer-1)19 - [Parameters](#parameters-4)20 - [Examples](#examples-5)21 - [build](#build-1)22 - [Examples](#examples-6)23import { typedIntArrayArbitraryArbitraryBuilderBuilder } from "fast-check-monorepo";24const arb = typedIntArrayArbitraryArbitraryBuilderBuilder().integer().build();25const arb2 = typedIntArrayArbitraryArbitraryBuilderBuilder().integer().build();26Returns **[TypedIntArrayArbitraryBuilder](#typedintarrayarbitrarybuilder)**
Using AI Code Generation
1import { typedIntArrayArbitraryBuilder } from '@rimiti/fast-check-monorepo'2const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Int8Array)3const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Uint8Array)4const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Uint8ClampedArray)5const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Int16Array)6const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Uint16Array)7const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Int32Array)8const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Uint32Array)9const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Float32Array)10const typedIntArrayArbitrary = typedIntArrayArbitraryBuilder(0, 10, 10, Float64Array)11typedIntArrayArbitrary.sample()12typedIntArrayArbitrary.sampleOne()13typedIntArrayArbitrary.generate()14typedIntArrayArbitrary.generateOne()15typedIntArrayArbitrary.shrink()16typedIntArrayArbitrary.shrinkOne()17typedIntArrayArbitrary.withBias()18typedIntArrayArbitrary.withBias().generate()19typedIntArrayArbitrary.withBias().generateOne()20typedIntArrayArbitrary.withBias().sample()21typedIntArrayArbitrary.withBias().sampleOne()22typedIntArrayArbitrary.withBias().shrink()23typedIntArrayArbitrary.withBias().shrinkOne()24typedIntArrayArbitrary.withBias(0).generate()25typedIntArrayArbitrary.withBias(0).generateOne()26typedIntArrayArbitrary.withBias(0).sample()27typedIntArrayArbitrary.withBias(0).sampleOne()28typedIntArrayArbitrary.withBias(0).shrink()29typedIntArrayArbitrary.withBias(0).shrinkOne()30typedIntArrayArbitrary.withBias(0).withBias()31typedIntArrayArbitrary.withBias(0).withBias().generate()32typedIntArrayArbitrary.withBias(0).withBias().generateOne()33typedIntArrayArbitrary.withBias(0).withBias().sample()34typedIntArrayArbitrary.withBias(0).withBias().sampleOne()35typedIntArrayArbitrary.withBias(0).withBias().sh
Using AI Code Generation
1const { typedIntArrayArbitraryArbitraryBuilderBuilder } = require('@dubzzz/fast-check');2const { typedIntArrayArbitraryBuilder } = typedIntArrayArbitraryArbitraryBuilderBuilder(Int32Array);3const { typedIntArrayArbitrary } = typedIntArrayArbitraryBuilder();4const { sample } = require('fast-check');5sample(typedIntArrayArbitrary, { seed: 42, numRuns: 5 });6const { typedIntArrayArbitraryArbitraryBuilderBuilder } = require('@dubzzz/fast-check');7const { typedIntArrayArbitraryBuilder } = typedIntArrayArbitraryArbitraryBuilderBuilder(Int32Array);8const { typedIntArrayArbitrary } = typedIntArrayArbitraryBuilder();9const { sample } = require('fast-check');10sample(typedIntArrayArbitrary, { seed: 42, numRuns: 5 });
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!!