How to use manyArbsIncludingCommandsOne method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

commands.spec.ts

Source:commands.spec.ts Github

copy

Full Screen

1import * as fc from 'fast-check';2import { commands } from '../../../src/arbitrary/commands';3import prand from 'pure-rand';4import { Command } from '../../../src/check/model/command/Command';5import { Random } from '../../../src/random/generator/Random';6import { Arbitrary } from '../../../src/check/arbitrary/definition/Arbitrary';7import { Value } from '../../../src/check/arbitrary/definition/Value';8import { Stream } from '../../../src/stream/Stream';9import { tuple } from '../../../src/arbitrary/tuple';10import { nat } from '../../../src/arbitrary/nat';11import { isStrictlySmallerArray } from './__test-helpers__/ArrayHelpers';12type Model = Record<string, unknown>;13type Real = unknown;14type Cmd = Command<Model, Real>;15const model: Model = Object.freeze({});16const real: Real = Object.freeze({});17describe('commands (integration)', () => {18 function simulateCommands(cmds: Iterable<Cmd>): void {19 for (const c of cmds) {20 if (!c.check(model)) continue;21 try {22 c.run(model, real);23 } catch (err) {24 return;25 }26 }27 }28 it('should generate a cloneable instance', () => {29 fc.assert(30 fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {31 // Arrange32 const mrng = new Random(prand.xorshift128plus(seed));33 const logOnCheck: { data: string[] } = { data: [] };34 // Act35 const commandsArb = commands([36 new FakeConstant(new SuccessCommand(logOnCheck)),37 new FakeConstant(new SkippedCommand(logOnCheck)),38 new FakeConstant(new FailureCommand(logOnCheck)),39 ]);40 const baseCommands = commandsArb.generate(mrng, biasFactor);41 // Assert42 return baseCommands.hasToBeCloned;43 })44 );45 });46 it('should skip skipped commands on shrink', () => {47 fc.assert(48 fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {49 // Arrange50 const mrng = new Random(prand.xorshift128plus(seed));51 const logOnCheck: { data: string[] } = { data: [] };52 // Act53 const commandsArb = commands([54 new FakeConstant(new SuccessCommand(logOnCheck)),55 new FakeConstant(new SkippedCommand(logOnCheck)),56 new FakeConstant(new FailureCommand(logOnCheck)),57 ]);58 const baseCommands = commandsArb.generate(mrng, biasFactor);59 simulateCommands(baseCommands.value);60 // Assert61 const shrinks = commandsArb.shrink(baseCommands.value_, baseCommands.context);62 for (const shrunkCmds of shrinks) {63 logOnCheck.data = [];64 [...shrunkCmds.value].forEach((c) => c.check(model));65 expect(logOnCheck.data.every((e) => e !== 'skipped')).toBe(true);66 }67 })68 );69 });70 it('should shrink with failure at the end', () => {71 fc.assert(72 fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {73 // Arrange74 const mrng = new Random(prand.xorshift128plus(seed));75 const logOnCheck: { data: string[] } = { data: [] };76 // Act77 const commandsArb = commands([78 new FakeConstant(new SuccessCommand(logOnCheck)),79 new FakeConstant(new SkippedCommand(logOnCheck)),80 new FakeConstant(new FailureCommand(logOnCheck)),81 ]);82 const baseCommands = commandsArb.generate(mrng, biasFactor);83 simulateCommands(baseCommands.value);84 fc.pre(logOnCheck.data[logOnCheck.data.length - 1] === 'failure');85 // Assert86 const shrinks = commandsArb.shrink(baseCommands.value_, baseCommands.context);87 for (const shrunkCmds of shrinks) {88 logOnCheck.data = [];89 [...shrunkCmds.value].forEach((c) => c.check(model));90 if (logOnCheck.data.length > 0) {91 // either empty or ending by the failure92 expect(logOnCheck.data[logOnCheck.data.length - 1]).toEqual('failure');93 }94 }95 })96 );97 });98 it('should shrink with at most one failure and all successes', () => {99 fc.assert(100 fc.property(fc.integer(), fc.option(fc.integer({ min: 2 }), { nil: undefined }), (seed, biasFactor) => {101 // Arrange102 const mrng = new Random(prand.xorshift128plus(seed));103 const logOnCheck: { data: string[] } = { data: [] };104 // Act105 const commandsArb = commands([106 new FakeConstant(new SuccessCommand(logOnCheck)),107 new FakeConstant(new SkippedCommand(logOnCheck)),108 new FakeConstant(new FailureCommand(logOnCheck)),109 ]);110 const baseCommands = commandsArb.generate(mrng, biasFactor);111 simulateCommands(baseCommands.value);112 // Assert113 const shrinks = commandsArb.shrink(baseCommands.value_, baseCommands.context);114 for (const shrunkCmds of shrinks) {115 logOnCheck.data = [];116 [...shrunkCmds.value].forEach((c) => c.check(model));117 expect(logOnCheck.data.every((e) => e === 'failure' || e === 'success')).toBe(true);118 expect(logOnCheck.data.filter((e) => e === 'failure').length <= 1).toBe(true);119 }120 })121 );122 });123 it('should provide commands which have never run', () => {124 const commandsArb = commands([new FakeConstant(new SuccessCommand({ data: [] }))], {125 disableReplayLog: true,126 });127 const manyArbsIncludingCommandsOne = tuple(nat(16), commandsArb, nat(16));128 const assertCommandsNotStarted = (value: Value<[number, Iterable<Cmd>, number]>) => {129 // Check the commands have never been executed130 // by checking the toString of the iterable is empty131 expect(String(value.value_[1])).toEqual('');132 };133 const startCommands = (value: Value<[number, Iterable<Cmd>, number]>) => {134 // Iterate over all the generated commands to run them all135 let ranOneCommand = false;136 for (const cmd of value.value_[1]) {137 ranOneCommand = true;138 cmd.run({}, {});139 }140 // Confirming that executing at least one command will make the toString of the iterable141 // not empty142 if (ranOneCommand) {143 expect(String(value.value_[1])).not.toEqual('');144 }145 };146 fc.assert(147 fc.property(148 fc.integer().noShrink(),149 fc.infiniteStream(fc.nat()),150 fc.option(fc.integer({ min: 2 }), { nil: undefined }),151 (seed, shrinkPath, biasFactor) => {152 // Generate the first Value153 const it = shrinkPath[Symbol.iterator]();154 const mrng = new Random(prand.xorshift128plus(seed));155 let currentValue: Value<[number, Iterable<Cmd>, number]> | null = manyArbsIncludingCommandsOne.generate(156 mrng,157 biasFactor158 );159 // Check status and update first Value160 assertCommandsNotStarted(currentValue);161 startCommands(currentValue);162 // Traverse the shrink tree in order to detect already seen ids163 while (currentValue !== null) {164 currentValue = manyArbsIncludingCommandsOne165 .shrink(currentValue.value_, currentValue.context)166 .map((nextValue) => {167 // Check nothing starting for the next one168 assertCommandsNotStarted(nextValue);169 // Start everything: not supposed to impact any other shrinkable170 startCommands(nextValue);171 return nextValue;172 })173 .getNthOrLast(it.next().value);174 }175 }176 )177 );178 });179 it('should shrink to smaller values', () => {180 const commandsArb = commands([nat(3).map((id) => new SuccessIdCommand(id))]);181 fc.assert(182 fc.property(183 fc.integer().noShrink(),184 fc.infiniteStream(fc.nat()),185 fc.option(fc.integer({ min: 2 }), { nil: undefined }),186 (seed, shrinkPath, biasFactor) => {187 // Generate the first Value188 const it = shrinkPath[Symbol.iterator]();189 const mrng = new Random(prand.xorshift128plus(seed));190 let currentValue: Value<Iterable<Cmd>> | null = commandsArb.generate(mrng, biasFactor);191 // Run all commands of first Value192 simulateCommands(currentValue.value_);193 // Traverse the shrink tree in order to detect already seen ids194 const extractIdRegex = /^custom\((\d+)\)$/;195 while (currentValue !== null) {196 const currentItems = [...currentValue.value_].map((c) => +extractIdRegex.exec(c.toString())![1]);197 currentValue = commandsArb198 .shrink(currentValue.value_, currentValue.context)199 .map((nextValue) => {200 // Run all commands of nextShrinkable201 simulateCommands(nextValue.value_);202 // Check nextShrinkable is strictly smaller than current one203 const nextItems = [...nextValue.value_].map((c) => +extractIdRegex.exec(c.toString())![1]);204 expect(isStrictlySmallerArray(nextItems, currentItems)).toBe(true);205 // Next is eligible for shrinking206 return nextValue;207 })208 .getNthOrLast(it.next().value);209 }210 }211 )212 );213 });214 it('should shrink the same way when based on replay data', () => {215 fc.assert(216 fc.property(217 fc.integer().noShrink(),218 fc.nat(100),219 fc.option(fc.integer({ min: 2 }), { nil: undefined }),220 (seed, numValues, biasFactor) => {221 // create unused logOnCheck222 const logOnCheck: { data: string[] } = { data: [] };223 // generate scenario and simulate execution224 const rng = prand.xorshift128plus(seed);225 const refArbitrary = commands([226 new FakeConstant(new SuccessCommand(logOnCheck)),227 new FakeConstant(new SkippedCommand(logOnCheck)),228 new FakeConstant(new FailureCommand(logOnCheck)),229 nat().map((v) => new SuccessIdCommand(v)),230 ]);231 const refValue: Value<Iterable<Cmd>> = refArbitrary.generate(new Random(rng), biasFactor);232 simulateCommands(refValue.value_);233 // trigger computation of replayPath234 // and extract shrinks for ref235 const refShrinks = [236 ...refArbitrary237 .shrink(refValue.value_, refValue.context)238 .take(numValues)239 .map((s) => [...s.value_].map((c) => c.toString())),240 ];241 // extract replayPath242 const replayPath = /\/\*replayPath=['"](.*)['"]\*\//.exec(refValue.value_.toString())![1];243 // generate scenario but do not simulate execution244 const noExecArbitrary = commands(245 [246 new FakeConstant(new SuccessCommand(logOnCheck)),247 new FakeConstant(new SkippedCommand(logOnCheck)),248 new FakeConstant(new FailureCommand(logOnCheck)),249 nat().map((v) => new SuccessIdCommand(v)),250 ],251 { replayPath }252 );253 const noExecValue: Value<Iterable<Cmd>> = noExecArbitrary.generate(new Random(rng), biasFactor);254 // check shrink values are identical255 const noExecShrinks = [256 ...noExecArbitrary257 .shrink(noExecValue.value_, noExecValue.context)258 .take(numValues)259 .map((s) => [...s.value_].map((c) => c.toString())),260 ];261 expect(noExecShrinks).toEqual(refShrinks);262 }263 )264 );265 });266});267// Helpers268class FakeConstant extends Arbitrary<Cmd> {269 constructor(private readonly cmd: Cmd) {270 super();271 }272 generate(): Value<Cmd> {273 return new Value(this.cmd, undefined);274 }275 canShrinkWithoutContext(value: unknown): value is Cmd {276 return false;277 }278 shrink(): Stream<Value<Cmd>> {279 return Stream.nil();280 }281}282class SuccessIdCommand implements Cmd {283 constructor(readonly id: number) {}284 check = () => true;285 run = () => {};286 toString = () => `custom(${this.id})`;287}288class SuccessCommand implements Cmd {289 constructor(readonly log: { data: string[] }) {}290 check = () => {291 this.log.data.push(this.toString());292 return true;293 };294 run = () => {};295 toString = () => 'success';296}297class SkippedCommand implements Cmd {298 constructor(readonly log: { data: string[] }) {}299 check = () => {300 this.log.data.push(this.toString());301 return false;302 };303 run = () => {};304 toString = () => 'skipped';305}306class FailureCommand implements Cmd {307 constructor(readonly log: { data: string[] }) {}308 check = () => {309 this.log.data.push(this.toString());310 return true;311 };312 run = () => {313 throw 'error';314 };315 toString = () => 'failure';...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { manyArbsIncludingCommandsOne } from 'fast-check'2const fc = require('fast-check')3const { manyArbsIncludingCommandsOne } = require('fast-check-monorepo')4const arb1 = fc.integer()5const arb2 = fc.string()6const arb3 = fc.object()7const arb4 = fc.tuple(arb1, arb2, arb3)8const arb5 = fc.record({ a: arb1, b: arb2, c: arb3 })9const arb6 = fc.array(arb1)10const arb7 = fc.tuple(arb1, arb2, arb3, arb4, arb5, arb6)11const arb8 = fc.record({ a: arb1, b: arb2, c: arb3, d: arb4, e: arb5, f: arb6 })12const arb9 = fc.array(arb1)13const arb10 = fc.tuple(arb1, arb2, arb3, arb4, arb5, arb6, arb7, arb8, arb9)14const arb11 = fc.record({15})16const arb12 = fc.array(arb1)17const arb13 = fc.tuple(18const arb14 = fc.record({19})20const arb15 = fc.array(arb1)21const arb16 = fc.tuple(

Full Screen

Using AI Code Generation

copy

Full Screen

1import {manyArbsIncludingCommandsOne} from 'fast-check-monorepo';2import {commandOne} from './commandOne';3import {commandTwo} from './commandTwo';4import {commandThree} from './commandThree';5import {manyArbsIncludingCommandsTwo} from 'fast-check-monorepo';6import {commandFour} from './commandFour';7import {commandFive} from './commandFive';8import {commandSix} from './commandSix';9describe('test', () => {10 it('should pass', () => {11 const result = manyArbsIncludingCommandsOne([commandOne, commandTwo, commandThree]);12 const result = manyArbsIncludingCommandsTwo([commandFour, commandFive, commandSix]);13 });14});15import {commandOne} from 'fast-check-monorepo';16import {arbitraryOne} from './arbitraryOne';17import {arbitraryTwo} from './arbitraryTwo';18import {arbitraryThree} from './arbitraryThree';19export const commandOne = commandOne(arbitraryOne, arbitraryTwo, arbitraryThree);20import {commandTwo} from 'fast-check-monorepo';21import {arbitraryFour} from './arbitraryFour';22import {arbitraryFive} from './arbitraryFive';23import {arbitrarySix} from './arbitrarySix';24export const commandTwo = commandTwo(arbitraryFour, arbitraryFive, arbitrarySix);25import {commandThree} from 'fast-check-monorepo';26import {arbitrarySeven} from './arbitrarySeven';27import {arbitraryEight} from './arbitraryEight';28import {arbitraryNine} from './arbitraryNine';29export const commandThree = commandThree(arbitrarySeven, arbitraryEight, arbitraryNine);30import {commandFour} from 'fast-check-monorepo';31import {arbitraryTen} from './arbitraryTen';32import

Full Screen

Using AI Code Generation

copy

Full Screen

1const { manyArbsIncludingCommandsOne } = require('fast-check-monorepo');2const { command } = require('./command');3describe('test', () => {4 it('should pass', () => {5 manyArbsIncludingCommandsOne([command], 100, 1000);6 });7});8const { command } = require('fast-check-monorepo');9const { arb } = require('./arb');10exports.command = command(arb, (model, arb) => {11 model.push(arb);12});13const { arb } = require('fast-check-monorepo');14exports.arb = arb({15 generate: () => 'a',16 shrink: () => [],17 show: () => 'a',18});19exports.model = [];20const { property } = require('fast-check-monorepo');21exports.property = property((model, arb) => {22 return model.length === arb.length;23});24module.exports = {25};

Full Screen

Using AI Code Generation

copy

Full Screen

1import {manyArbsIncludingCommandsOne} from 'fast-check-monorepo';2import {Arbitrary, command} from 'fast-check';3import {Command, CommandContext} from 'fast-check/lib/check/model/Command';4const command1 = command(5 (context: CommandContext) => {6 },7 [Arbitrary.string()]8);9const command2 = command(10 (context: CommandContext) => {11 },12 [Arbitrary.string()]13);14const command3 = command(15 (context: CommandContext) => {16 },17 [Arbitrary.string()]18);19const command4 = command(20 (context: CommandContext) => {21 },22 [Arbitrary.string()]23);24const command5 = command(25 (context: CommandContext) => {26 },27 [Arbitrary.string()]28);29const command6 = command(30 (context: CommandContext) => {31 },32 [Arbitrary.string()]33);34const command7 = command(35 (context: CommandContext) => {36 },37 [Arbitrary.string()]38);39const command8 = command(40 (context: CommandContext) => {41 },42 [Arbitrary.string()]43);44const command9 = command(

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { manyArbsIncludingCommandsOne } = require('fast-check-monorepo');3const arb1 = fc.integer();4const arb2 = fc.string();5const arb3 = fc.boolean();6const arb4 = fc.object();7const arb5 = fc.array(fc.integer());8const arbs = [arb1, arb2, arb3, arb4, arb5];9 {10 run: (arb1, arb2, arb3, arb4, arb5) => {11 console.log(arb1);12 },13 },14 {15 run: (arb1, arb2, arb3, arb4, arb5) => {16 console.log(arb2);17 },18 },19 {20 run: (arb1, arb2, arb3, arb4, arb5) => {21 console.log(arb3);22 },23 },24 {25 run: (arb1, arb2, arb3, arb4, arb5) => {26 console.log(arb4);27 },28 },29 {30 run: (arb1, arb2, arb3, arb4, arb5) => {31 console.log(arb5);32 },33 },34];35const manyArbsIncludingCommandsOneResult = manyArbsIncludingCommandsOne(arbs, commands);36manyArbsIncludingCommandsOneResult.property.check();

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