How to use schemeArb method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

arbitraries.ts

Source:arbitraries.ts Github

copy

Full Screen

1const { curried2 } = require("jsverify/lib/utils");2import { Record } from "../generated/registry/api";3const lazyseq = require("lazy-seq");4import uuid = require("uuid/v4");5import * as _ from "lodash";6import jsc from "./jsverify";7function fromCode(code: number) {8 return String.fromCharCode(code);9}10function toCode(c: string) {11 return c.charCodeAt(0);12}13const charArb = jsc.integer(32, 0xff).smap(fromCode, toCode);14/**15 * Generates a random string with characters greater than code 32 (so no random16 * TTY crap that can't be matched by any regex17 */18export const stringArb = jsc19 .array(charArb)20 .smap(chars => chars.join(""), string => string.split(""));21export const lowerCaseAlphaCharArb = jsc22 .integer(97, 122)23 .smap(fromCode, toCode);24export const numArb = jsc.integer(48, 57).smap(fromCode, toCode);25export const lcAlphaNumCharArb = jsc.oneof([numArb, lowerCaseAlphaCharArb]);26export const lcAlphaNumStringArb = jsc27 .array(lcAlphaNumCharArb)28 .smap((arr: any) => arr.join(""), (string: string) => string.split(""));29export const lcAlphaNumStringArbNe = jsc30 .nearray(lcAlphaNumCharArb)31 .smap((arr: any) => arr.join(""), (string: string) => string.split(""));32export const lcAlphaStringArb = jsc33 .array(lowerCaseAlphaCharArb)34 .smap(chars => chars.join(""), string => string.split(""));35export const lcAlphaStringArbNe = jsc36 .nearray(lowerCaseAlphaCharArb)37 .smap(chars => chars.join(""), string => string.split(""));38export const peopleNameArb = jsc39 .tuple([lcAlphaStringArbNe, lcAlphaStringArbNe])40 .smap(strArr => strArr.join(" "), string => string.split(" "));41const uuidArb: jsc.Arbitrary<string> = jsc.bless({42 generator: jsc.generator.bless(x => uuid()),43 shrink: jsc.shrink.bless(x => []),44 show: (x: string) => x45});46export const recordArb = jsc.record<Record>({47 id: uuidArb,48 name: stringArb,49 aspects: jsc.suchthat(jsc.array(jsc.json), arr => arr.length <= 10),50 sourceTag: jsc.constant(undefined)51});52export const specificRecordArb = (aspectArbs: {53 [key: string]: jsc.Arbitrary<any>;54}) =>55 jsc.record<Record>({56 id: uuidArb,57 name: stringArb,58 aspects: jsc.record(aspectArbs),59 sourceTag: jsc.constant(undefined)60 });61const defaultSchemeArb = jsc.oneof([62 jsc.constant("http"),63 jsc.constant("https"),64 jsc.constant("ftp"),65 lcAlphaNumStringArbNe66]);67type UrlArbOptions = {68 schemeArb?: jsc.Arbitrary<string>;69 hostArb?: jsc.Arbitrary<string>;70};71function shrinkArrayWithMinimumSize<T>(72 size: number73): (x: jsc.Shrink<T>) => jsc.Shrink<T[]> {74 function shrinkArrayImpl(...args: any[]) {75 const shrink: (x: any) => Array<any> = args[0];76 const result: any = jsc.shrink.bless(function(arr: Array<any>) {77 if (arr.length <= size) {78 return lazyseq.nil;79 } else {80 var x = arr[0];81 var xs = arr.slice(1);82 return lazyseq83 .cons(xs, lazyseq.nil)84 .append(85 shrink(x).map(function(xp: any) {86 return [xp].concat(xs);87 })88 )89 .append(90 shrinkArrayImpl(shrink, xs).map(function(xsp: any) {91 return [x].concat(xsp);92 })93 );94 }95 });96 return curried2(result, args);97 }98 return shrinkArrayImpl;99}100export type MonadicArb<T> = jsc.Arbitrary<T> & {101 flatMap<U>(102 arbForward: (t: T) => jsc.Arbitrary<U>,103 backwards: (u: U) => T104 ): MonadicArb<U>;105};106/** Sometimes jsc returns a lazy-seq when it should return an array, this makes sure it's an array. */107function unSeq<T>(maybeArray: any): T[] {108 return maybeArray.toArray ? maybeArray.toArray() : maybeArray;109}110export function arbFlatMap<T, U>(111 arb: jsc.Arbitrary<T>,112 arbForward: (t: T) => jsc.Arbitrary<U>,113 backwards: (u: U) => T,114 show: (u: U) => string | undefined = u => JSON.stringify(u)115): MonadicArb<U> {116 const x = jsc.bless<U>({117 generator: arb.generator.flatmap((t: T) => {118 return arbForward(t).generator;119 }),120 show,121 shrink: jsc.shrink.bless((u: U) => {122 const t = backwards(u);123 if (_.isUndefined(t)) {124 return [];125 }126 const ts = unSeq<T>(arb.shrink(t));127 const us = _(ts)128 .flatMap(thisT => {129 const newArb = arbForward(thisT);130 const newU = (jsc.sampler(newArb, ts.length)(1) as any)[0];131 return _.take(unSeq<U>(newArb.shrink(newU)), 3);132 })133 .value();134 return us;135 })136 });137 return {138 ...x,139 flatMap<V>(140 arbForward: (u: U) => jsc.Arbitrary<V>,141 backwards: (v: V) => U142 ): MonadicArb<V> {143 return arbFlatMap<U, V>(x, arbForward, backwards);144 }145 };146}147function generateArrayOfSize<T>(148 arrSize: number,149 gen: jsc.Generator<T>150): jsc.Generator<T[]> {151 var result = jsc.generator.bless(function(size: number) {152 var arr = new Array(arrSize);153 for (var i = 0; i < arrSize; i++) {154 arr[i] = gen(size);155 }156 return arr;157 });158 return result;159}160/** Generates an array that is guaranteed to be of the supplied size */161export function arrayOfSizeArb<T>(162 size: number,163 arb: jsc.Arbitrary<T>164): jsc.Arbitrary<T[]> {165 return jsc.bless<T[]>({166 generator: generateArrayOfSize(size, arb.generator),167 shrink: shrinkArrayWithMinimumSize<T>(size)(arb.shrink),168 show: x => x.toString()169 });170}171/**172 * Randomly generates a scheme, host, port and path for a URL.173 */174const urlPartsArb = (options: UrlArbOptions) =>175 jsc.record({176 scheme: options.schemeArb,177 host: jsc.suchthat(178 options.hostArb,179 (string: string) => !string.startsWith("ftp")180 ),181 port: jsc.oneof([182 jsc.integer(1, 65000),183 jsc.constant(80),184 jsc.constant(21)185 ]),186 path: jsc.array(lcAlphaNumStringArbNe)187 });188/** Generates a URL for a distribution - this could be ftp, http or https */189export const distUrlArb = ({190 schemeArb = defaultSchemeArb,191 hostArb = lcAlphaNumStringArbNe192}: UrlArbOptions = {}) =>193 urlPartsArb({ schemeArb, hostArb }).smap(194 urlParts => {195 const port =196 (urlParts.scheme === "http" && urlParts.port === 80) ||197 (urlParts.scheme === "ftp" && urlParts.port === 21)198 ? ""199 : ":" + urlParts.port;200 return `${urlParts.scheme}://${urlParts.host}.com${port}/${(201 urlParts.path || []202 ).join("/")}` as string;203 },204 (url: string) => {205 const splitAfterScheme = url.split("://");206 const scheme = splitAfterScheme[0];207 const splitOnDotCom = splitAfterScheme[1].split(/\.com:?/);208 const host = splitOnDotCom[0];209 const pathSegments = splitOnDotCom[1].split("/");210 const port: number = parseInt(pathSegments[0]);211 const path = pathSegments.slice(1);212 return { scheme, host, port, path };213 }214 );215/**216 * Can be passed into distStringsArb to override the default arbitraries.217 */218export type DistStringsOverrideArbs = {219 url?: jsc.Arbitrary<string>;220 license?: jsc.Arbitrary<string>;221 format?: jsc.Arbitrary<string>;222};223/**224 * Can be passed into sourceLinkArb to override the default arbitraries.225 */226export type SourceLinkOverrideArbs = {227 status?: jsc.Arbitrary<string | undefined>;228};229/**230 * Can be passed into datasetFormatArb to override the default arbitaries.231 */232export type DatasetFormatOverrideArbs = {233 format?: jsc.Arbitrary<string>;234};235/**236 * Generates the content of a distribution's dcat-distribution-strings aspect.237 */238export const distStringsArb = ({239 url = jsc.oneof([distUrlArb(), jsc.constant(undefined)]),240 license = stringArb,241 format = stringArb242}: DistStringsOverrideArbs) =>243 jsc.record({244 downloadURL: url,245 accessURL: url,246 license,247 format248 });249/**250 * Generates the content of source link status aspect251 */252export const sourceLinkArb = ({253 status = jsc.constant("active")254}: SourceLinkOverrideArbs) =>255 jsc.record({256 status: status257 });258/**259 *260 */261export const datasetFormatArb = ({262 format = stringArb263}: DatasetFormatOverrideArbs) =>264 jsc.record({265 format266 });267/**268 * Generates records, allowing specific arbs to be defined for the distribution.269 */270export const recordArbWithDistArbs = (271 distStringsOverrides: DistStringsOverrideArbs = {},272 sourceLinkOverrides: SourceLinkOverrideArbs = {},273 datasetFormatOverrides: DatasetFormatOverrideArbs = {}274) =>275 specificRecordArb({276 "dataset-distributions": jsc.record({277 distributions: jsc.array(278 specificRecordArb({279 "dcat-distribution-strings": distStringsArb(280 distStringsOverrides281 ),282 "dataset-format": datasetFormatArb(datasetFormatOverrides),283 "source-link-status": sourceLinkArb(sourceLinkOverrides)284 })285 )286 })287 });288/**289 * Randomly generates a record with the passed objects under its290 * distributions' dcat-distribution-strings aspect.291 */292export const recordArbWithDists = (dists: object[]) =>293 specificRecordArb({294 "dataset-distributions": jsc.record({295 distributions: jsc.tuple(296 dists.map(dist =>297 specificRecordArb({298 "dcat-distribution-strings": jsc.constant(dist)299 })300 )301 )302 })303 });304/**305 * Randomly returns a subset of the array, in-order.306 */307export function someOf<T>(array: T[]): jsc.Arbitrary<T[]> {308 return jsc309 .record({310 length: jsc.integer(0, array.length),311 start: jsc.integer(0, array.length - 1)312 })313 .smap(314 ({ length, start }) =>315 _(array)316 .drop(start)317 .take(length)318 .value(),319 (newArray: T[]) => {320 const start = _.indexOf(array, newArray[0]);321 const length = newArray.length - start;322 return { start, length };323 }324 );325}326/**327 * Fuzzes a string, changing the case and putting random strings around it328 * and in spaces.329 */330export function fuzzStringArb(331 string: string,332 fuzzArb: jsc.Arbitrary<string> = stringArb333): jsc.Arbitrary<string> {334 const words = string.split(/[^a-zA-Z\d]+/);335 const aroundArbs = arrayOfSizeArb(words.length + 1, fuzzArb);336 return arbFlatMap(337 aroundArbs,338 around => {339 const string = _(around)340 .zip(words)341 .flatten()342 .join("");343 const caseArb = arrayOfSizeArb(string.length, jsc.bool);344 return jsc.record({ string: jsc.constant(string), cases: caseArb });345 },346 ({ string, cases }) => {347 const wordsRegex = new RegExp(words.join("|"));348 return string.split(wordsRegex);349 }350 ).smap(351 ({ string, cases }) =>352 _353 .map(string, (char, index) => {354 if (cases[index]) {355 return char.toUpperCase();356 } else {357 return char.toLowerCase();358 }359 })360 .join(""),361 string => ({362 cases: _.map(string, (char, index) => char === char.toUpperCase()),363 string364 })365 );366}367/**368 * Gets the result of the passed string arbitrary and fuzzes it, changing369 * the case and putting random strings around it and in spaces.370 */371export function fuzzStringArbResult(372 stringArb: jsc.Arbitrary<string>,373 fuzzArb?: jsc.Arbitrary<string>374) {375 return arbFlatMap(376 stringArb,377 string => fuzzStringArb(string, fuzzArb),378 fuzzedString => undefined379 );380}381export const dateStringArb = jsc.datetime.smap(382 date => date.toString(),383 string => new Date(Date.parse(string))...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { schemeArb } = require("fast-check-monorepo");3const scheme = schemeArb().generate(fc.random());4console.log(scheme);5const fc = require("fast-check");6const { schemeArb } = require("fast-check-monorepo");7const scheme = schemeArb().generate(fc.random());8console.log(scheme);9const fc = require("fast-check");10const { schemeArb } = require("fast-check-monorepo");11const scheme = schemeArb().generate(fc.random());12console.log(scheme);13const fc = require("fast-check");14const { schemeArb } = require("fast-check-monorepo");15const scheme = schemeArb().generate(fc.random());16console.log(scheme);17const fc = require("fast-check");18const { schemeArb } = require("fast-check-monorepo");19const scheme = schemeArb().generate(fc.random());20console.log(scheme);21const fc = require("fast-check");22const { schemeArb } = require("fast-check-monorepo");23const scheme = schemeArb().generate(fc.random());24console.log(scheme);25const fc = require("fast-check");26const { schemeArb } = require("fast-check-monorepo");27const scheme = schemeArb().generate(fc.random());28console.log(scheme);29const fc = require("fast-check");30const { schemeArb } = require("fast-check-monorepo");31const scheme = schemeArb().generate(fc.random());32console.log(scheme);33const fc = require("fast-check");34const {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { schemeArb } from 'fast-check-monorepo';2import { assert } from 'chai';3describe('schemeArb', () => {4 it('should generate a scheme', () => {5 const scheme = schemeArb().generate();6 });7});

Full Screen

Using AI Code Generation

copy

Full Screen

1import schemeArb from 'fast-check-monorepo/lib/Arbitrary/schemeArb'2const scheme = {type: 'string'};3const arb = schemeArb(scheme);4fc.assert(fc.property(arb, value => {5 assert(typeof value === 'string');6}));7const scheme = {type: 'array', items: {type: 'string'}};8const arb = schemeArb(scheme);9fc.assert(fc.property(arb, value => {10 assert(Array.isArray(value));11 assert(value.every(v => typeof v === 'string'));12}));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { schemeArb } = require('fast-check-monorepo');3const isScheme = (s) => {4};5fc.assert(6 fc.property(schemeArb(), (s) => {7 return isScheme(s);8 })9);10const fc = require('fast-check');11const { schemeArb } = require('fast-check-monorepo');12const isScheme = (s) => {13};14fc.assert(15 fc.property(schemeArb(), (s) => {16 return isScheme(s);17 })18);19const fc = require('fast-check');20const { schemeArb } = require('fast-check-monorepo');21const isScheme = (s) => {22};23fc.assert(24 fc.property(schemeArb(), (s) => {25 return isScheme(s);26 })27);28const fc = require('fast-check');29const { schemeArb } = require('fast-check-monorepo');30const isScheme = (s) => {31};32fc.assert(33 fc.property(schemeArb(), (s) => {34 return isScheme(s);35 })36);37const fc = require('fast-check');38const { schemeArb } = require('fast-check-monorepo');39const isScheme = (s) => {40};41fc.assert(42 fc.property(schemeArb(), (s) => {43 return isScheme(s);44 })45);

Full Screen

Using AI Code Generation

copy

Full Screen

1const {schemeArb} = require("fast-check-monorepo")2const fc = require("fast-check")3const scheme = schemeArb().generate(fc.random(0))4console.log(scheme)5const {schemeArb} = require("fast-check-monorepo")6const fc = require("fast-check")7const scheme = schemeArb().generate(fc.random(0))8console.log(scheme)9const {schemeArb} = require("fast-check-monorepo")10const fc = require("fast-check")11const scheme = schemeArb().generate(fc.random(0))12console.log(scheme)13const {schemeArb} = require("fast-check-monorepo")14const fc = require("fast-check")15const scheme = schemeArb().generate(fc.random(0))16console.log(scheme)17const {schemeArb} = require("fast-check-monorepo")18const fc = require("fast-check")19const scheme = schemeArb().generate(fc.random(0))20console.log(scheme)21const {schemeArb} = require("fast-check-monorepo")22const fc = require("fast-check")23const scheme = schemeArb().generate(fc.random(0))24console.log(scheme)25const {schemeArb} = require("fast-check-monorepo")26const fc = require("fast-check")27const scheme = schemeArb().generate(fc.random(0))28console.log(scheme)29const {schemeArb} = require("fast-check-monorepo")30const fc = require("fast-check")31const scheme = schemeArb().generate(fc.random(0))32console.log(scheme)

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const schemeArb = require('fast-check-monorepo');3fc.assert(fc.property(arb, (url) => {4 console.log(url);5 return true;6}));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { schemeArb } = require('fast-check-monorepo');3const scheme = {4 properties: {5 id: { type: 'number' },6 name: { type: 'string' },7 address: {8 properties: {9 street: { type: 'string' },10 city: { type: 'string' },11 state: { type: 'string' },12 zip: { type: 'string' },13 },14 },15 phones: {16 items: {17 properties: {18 type: { type: 'string' },19 number: { type: 'string' },20 },21 },22 },23 },24};25const arb = schemeArb(scheme);26fc.assert(27 fc.property(arb, (obj) => {28 console.log(obj);29 return true;30 })31);

Full Screen

Using AI Code Generation

copy

Full Screen

1var arb = require('fast-check').schemeArb;2var scheme = {3 items: {4 }5};6var schemeArb = arb(scheme);7var value = schemeArb.sampleOne();8console.log(value);

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