Best JavaScript code snippet using fast-check-monorepo
arbitrary.js
Source:arbitrary.js
1/* @flow weak */2"use strict";3var arbitraryAssert = require("./arbitraryAssert.js");4var arbitraryBless = require("./arbitraryBless.js");5var array = require("./array.js");6var assert = require("assert");7var dict = require("./dict.js");8var generator = require("./generator.js");9var json = require("./json.js");10var pair = require("./pair.js");11var show = require("./show.js");12var shrink = require("./shrink.js");13var utils = require("./utils.js");14/**15 ### Arbitrary combinators16*/17/**18 - `nonshrink(arb: arbitrary a): arbitrary a`19 Non shrinkable version of arbitrary `arb`.20*/21function nonshrink(arb) {22 arb = utils.force(arb);23 return arbitraryBless({24 generator: arb.generator,25 shrink: shrink.noop,26 show: arb.show,27 });28}29/**30 - `unit: arbitrary ()`31*/32var unit = arbitraryBless({33 generator: generator.unit,34 shrink: shrink.noop,35 show: show.def,36});37/**38 - `either(arbA: arbitrary a, arbB : arbitrary b): arbitrary (either a b)`39*/40function either(a, b) {41 a = utils.force(a || json.json);42 b = utils.force(b || json.json);43 arbitraryAssert(a);44 arbitraryAssert(b);45 return arbitraryBless({46 generator: generator.either(a.generator, b.generator),47 shrink: shrink.either(a.shrink, b.shrink),48 show: show.either(a.show, b.show),49 });50}51/**52 - `pair(arbA: arbitrary a, arbB : arbitrary b): arbitrary (pair a b)`53 If not specified `a` and `b` are equal to `value()`.54*/55function pairArb(a, b) {56 return pair.pair(a || json.json, b || json.json);57}58/**59 - `tuple(arbs: (arbitrary a, arbitrary b...)): arbitrary (a, b...)`60*/61function tuple(arbs) {62 arbs = arbs.map(utils.force);63 return arbitraryBless({64 generator: generator.tuple(utils.pluck(arbs, "generator")),65 shrink: shrink.tuple(utils.pluck(arbs, "shrink")),66 show: show.tuple(utils.pluck(arbs, "show")),67 });68}69/**70 - `sum(arbs: (arbitrary a, arbitrary b...)): arbitrary (a | b ...)`71*/72function sum(arbs) {73 arbs = arbs.map(utils.force);74 return arbitraryBless({75 generator: generator.sum(utils.pluck(arbs, "generator")),76 shrink: shrink.sum(utils.pluck(arbs, "shrink")),77 show: show.sum(utils.pluck(arbs, "show")),78 });79}80/**81 - `dict(arb: arbitrary a): arbitrary (dict a)`82 Generates a JavaScript object with properties of type `A`.83*/84function dictArb(arb) {85 return dict.arbitrary(arb || json.json);86}87/**88 - `array(arb: arbitrary a): arbitrary (array a)`89*/90function arrayArb(arb) {91 return array.array(arb || json.json);92}93/**94 - `nearray(arb: arbitrary a): arbitrary (array a)`95*/96function nearrayArb(arb) {97 return array.nearray(arb || json.json);98}99/**100 - `json: arbitrary json`101 JavaScript Objects: boolean, number, string, null, array of `json` values or object with `json` values.102*/103var jsonArb = json.json;104/**105 - `oneof(gs : array (arbitrary a)...) : arbitrary a`106 Randomly uses one of the given arbitraries.107*/108function oneof() {109 assert(arguments.length !== 0, "oneof: at least one parameter expected");110 // TODO: write this in more functional way111 var generators = [];112 var append = function (a) {113 generators.push(utils.force(a).generator);114 };115 for (var i = 0; i < arguments.length; i++) {116 var arg = arguments[i];117 if (utils.isArray(arg)) {118 arg.forEach(append);119 } else {120 append(arg);121 }122 }123 return arbitraryBless({124 generator: generator.oneof(generators),125 // TODO: make shrink126 shrink: shrink.noop,127 show: show.def,128 });129}130// Return a lazy arbitrary that delegates to another arbitrary at its131// 'strict' property. An arbitrary must be assigned to that property before132// this arbitrary can generate anything.133function lazyArbitrary() {134 var arb = {};135 // This function must be pure because it will not be called with136 // meaningful context.137 arb.generator = generator.bless(function (size) {138 return arb.strict.generator(size);139 });140 arb.shrink = shrink.noop;141 arb.show = show.def;142 arb = arbitraryBless(arb);143 return arb;144}145/**146 - ```js147 letrec(148 (tie: key -> (arbitrary a | arbitrary b | ...))149 -> { key: arbitrary a, key: arbitrary b, ... }):150 { key: arbitrary a, key: arbitrary b, ... }151 ```152 Mutually recursive definitions. Every reference to a sibling arbitrary153 should go through the `tie` function.154 ```js155 { arb1, arb2 } = jsc.letrec(function (tie) {156 return {157 arb1: jsc.tuple(jsc.int, jsc.oneof(jsc.const(null), tie("arb2"))),158 arb2: jsc.tuple(jsc.bool, jsc.oneof(jsc.const(null), tie("arb1"))),159 }160 });161 ```162*/163function letrec(definition) {164 // We must use a lazy dictionary because we do not know the key set165 // before calling the definition.166 var lazyArbs = {};167 function tie(name) {168 if (!lazyArbs.hasOwnProperty(name)) {169 lazyArbs[name] = lazyArbitrary();170 }171 return lazyArbs[name];172 }173 var strictArbs = definition(tie);174 Object.keys(lazyArbs).forEach(function (key) {175 var strictArb = strictArbs[key];176 if (!strictArb) {177 throw new Error("undefined lazy arbitrary: " + key);178 }179 lazyArbs[key].strict = strictArb;180 });181 return strictArbs;182}183function recursive(arbZ, arbS) {184 var genZ = arbZ.generator;185 var genS = function (recGen) {186 var recArb = arbitraryBless({187 generator: recGen,188 shrink: shrink.noop,189 show: show.def,190 });191 return arbS(recArb).generator;192 };193 var gen = generator.recursive(genZ, genS);194 return arbitraryBless({195 generator: gen,196 shrink: shrink.noop,197 show: show.def,198 });199}200module.exports = {201 nonshrink: nonshrink,202 pair: pairArb,203 either: either,204 unit: unit,205 dict: dictArb,206 json: jsonArb,207 nearray: nearrayArb,208 array: arrayArb,209 tuple: tuple,210 sum: sum,211 oneof: oneof,212 recursive: recursive,213 letrec: letrec,...
letrec.ts
Source:letrec.ts
1import { LazyArbitrary } from './_internals/LazyArbitrary';2import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';3import { safeHasOwnProperty } from '../utils/globals';4const safeObjectCreate = Object.create;5/**6 * Type of the value produced by {@link letrec}7 * @remarks Since 3.0.08 * @public9 */10export type LetrecValue<T> = {11 [K in keyof T]: Arbitrary<T[K]>;12};13/**14 * Strongly typed type for the `tie` function passed by {@link letrec} to the `builder` function we pass to it.15 * You may want also want to use its loosely typed version {@link LetrecLooselyTypedTie}.16 *17 * @remarks Since 3.0.018 * @public19 */20export interface LetrecTypedTie<T> {21 <K extends keyof T>(key: K): Arbitrary<T[K]>;22 (key: string): Arbitrary<unknown>;23}24/**25 * Strongly typed type for the `builder` function passed to {@link letrec}.26 * You may want also want to use its loosely typed version {@link LetrecLooselyTypedBuilder}.27 *28 * @remarks Since 3.0.029 * @public30 */31export type LetrecTypedBuilder<T> = (tie: LetrecTypedTie<T>) => LetrecValue<T>;32/**33 * Loosely typed type for the `tie` function passed by {@link letrec} to the `builder` function we pass to it.34 * You may want also want to use its strongly typed version {@link LetrecTypedTie}.35 *36 * @remarks Since 3.0.037 * @public38 */39export type LetrecLooselyTypedTie = (key: string) => Arbitrary<unknown>;40/**41 * Loosely typed type for the `builder` function passed to {@link letrec}.42 * You may want also want to use its strongly typed version {@link LetrecTypedBuilder}.43 *44 * @remarks Since 3.0.045 * @public46 */47export type LetrecLooselyTypedBuilder<T> = (tie: LetrecLooselyTypedTie) => LetrecValue<T>;48/**49 * For mutually recursive types50 *51 * @example52 * ```typescript53 * type Leaf = number;54 * type Node = [Tree, Tree];55 * type Tree = Node | Leaf;56 * const { tree } = fc.letrec<{ tree: Tree, node: Node, leaf: Leaf }>(tie => ({57 * tree: fc.oneof({depthSize: 'small'}, tie('leaf'), tie('node')),58 * node: fc.tuple(tie('tree'), tie('tree')),59 * leaf: fc.nat()60 * }));61 * // tree is 50% of node, 50% of leaf62 * // the ratio goes in favor of leaves as we go deeper in the tree (thanks to depthSize)63 * ```64 *65 * @param builder - Arbitraries builder based on themselves (through `tie`)66 *67 * @remarks Since 1.16.068 * @public69 */70export function letrec<T>(builder: T extends Record<string, unknown> ? LetrecTypedBuilder<T> : never): LetrecValue<T>;71/**72 * For mutually recursive types73 *74 * @example75 * ```typescript76 * const { tree } = fc.letrec(tie => ({77 * tree: fc.oneof({depthSize: 'small'}, tie('leaf'), tie('node')),78 * node: fc.tuple(tie('tree'), tie('tree')),79 * leaf: fc.nat()80 * }));81 * // tree is 50% of node, 50% of leaf82 * // the ratio goes in favor of leaves as we go deeper in the tree (thanks to depthSize)83 * ```84 *85 * @param builder - Arbitraries builder based on themselves (through `tie`)86 *87 * @remarks Since 1.16.088 * @public89 */90export function letrec<T>(builder: LetrecLooselyTypedBuilder<T>): LetrecValue<T>;91// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types92export function letrec<T>(builder: LetrecLooselyTypedBuilder<T> | LetrecTypedBuilder<T>): LetrecValue<T> {93 const lazyArbs: { [K in keyof T]?: LazyArbitrary<unknown> } = safeObjectCreate(null);94 const tie = (key: keyof T): Arbitrary<any> => {95 if (!safeHasOwnProperty(lazyArbs, key)) {96 // Call to hasOwnProperty ensures that the property key will be defined97 lazyArbs[key] = new LazyArbitrary(String(key));98 }99 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion100 return lazyArbs[key]!;101 };102 const strictArbs = builder(tie as any);103 for (const key in strictArbs) {104 if (!safeHasOwnProperty(strictArbs, key)) {105 // Prevents accidental iteration over properties inherited from an objectâs prototype106 continue;107 }108 const lazyAtKey: LazyArbitrary<unknown> | undefined = lazyArbs[key];109 const lazyArb = lazyAtKey !== undefined ? lazyAtKey : new LazyArbitrary(key);110 lazyArb.underlying = strictArbs[key];111 lazyArbs[key] = lazyArb;112 }113 return strictArbs;...
Using AI Code Generation
1const { strictArbs } = require('fast-check');2const { strictArbs } = require('fast-check');3const { strictArbs } = require('fast-check');4const { strictArbs } = require('fast-check');5const { strictArbs } = require('fast-check');6const { strictArbs } = require('fast-check');7const { strictArbs } = require('fast-check');8const { strictArbs } = require('fast-check');9const { strictArbs } = require('fast-check');10const { strictArbs } = require('fast-check');11const { strictArbs } = require('fast-check');12const { strictArbs } = require('fast-check');13const { strictArbs } = require('fast-check');14const { strictArbs } = require('fast-check');15const { strictArbs } = require('fast-check');
Using AI Code Generation
1const { strictArbs } = require("fast-check");2const { string } = strictArbs;3const fc = require("fast-check");4const { string } = fc;5const { string } = require("fast-check-monorepo");6const { string } = require("fast-check-monorepo");7const fc = require("fast-check");8const { string } = fc;9const { string } = require("fast-check-monorepo");10const fc = require("fast-check");11const { string } = fc;12const { string } = require("fast-check-monorepo");13const fc = require("fast-check");14const { string } = fc;15const { string } = require("fast-check-monorepo");16const fc = require("fast-check");17const { string } = fc;18const { string } = require("fast-check-monorepo");19const fc = require("fast-check");20const { string } = fc;21const { string } = require("fast-check-monorepo");22const fc = require("fast-check");23const { string } = fc;24const { string } = require("fast-check-monorepo");25const fc = require("fast-check");26const { string } = fc;27const { string } = require("fast-check-monorepo");28const fc = require("fast-check");29const { string } = fc;30const { string } = require("fast-check-monorepo");31const fc = require("fast-check");32const { string } = fc;33const { string } = require("fast-check-monorepo");
Using AI Code Generation
1const { strictArbs } = require('fast-check')2const { oneof } = strictArbs3const arb = oneof(4 strictArbs.string(),5 strictArbs.number(),6 strictArbs.boolean(),7 strictArbs.constantFrom('a', 'b', 'c'),8 strictArbs.constantFrom(1, 2, 3),9 strictArbs.constantFrom(true, false),10 strictArbs.constantFrom('a', 1, true),11 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true),12 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false),13 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false, 'e', 5, true),14 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false, 'e', 5, true, 'f', 6, false),15 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false, 'e', 5, true, 'f', 6, false, 'g', 7, true),16 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false, 'e', 5, true, 'f', 6, false, 'g', 7, true, 'h', 8, false),17 strictArbs.constantFrom('a', 1, true, 'b', 2, false, 'c', 3, true, 'd', 4, false, 'e', 5, true, 'f', 6, false, 'g', 7, true, 'h', 8, false, 'i', 9, true),18 strictArbs.constantFrom('a', 1, true, 'b', 2,
Using AI Code Generation
1const { strictArbs } = require("fast-check-monorepo");2const { string, number, record, array } = strictArbs;3const arb = record({ foo: string(), bar: number(), baz: array(string()) });4const sample = arb.sample();5console.log(sample);6const { strictArbs } = require("fast-check");7const { string, number, record, array } = strictArbs;8const arb = record({ foo: string(), bar: number(), baz: array(string()) });9const sample = arb.sample();10console.log(sample);
Using AI Code Generation
1const fc = require('fast-check');2const { strictArbs } = require('fast-check-monorepo');3const arb = strictArbs(10, 1, 100);4fc.assert(fc.property(arb, (arr) => {5 return arr.length === 10;6}));
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!!