How to use keyArb method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

generators.js

Source:generators.js Github

copy

Full Screen

1import {map, take, concat, merge, identity, overEvery} from "lodash/fp";2import {3 constant,4 elements,5 random,6 array,7 dict,8 string,9 record,10 nestring,11 suchthat,12} from "jsverify";13import {data as ds, queries as qs} from "@sugarcube/core";14const trueOrFalse = () => random(0, 1) === 0;15const isNonEmpty = s => {16 if (s === "") return false;17 return true;18};19const isValidUnicode = s => {20 // Forbid unicode control characters, spaces and line tabulations.21 // eslint-disable-next-line no-control-regex22 if (/[\u0000-\u0020]/u.test(s)) return false;23 if (/[\u007F-\u00A0]/u.test(s)) return false;24 return true;25};26const isValidForm = s => {27 // Keys don't start with white space28 if (/^\s+/.test(s)) return false;29 // Keys don't end with white space30 if (/\s+$/.test(s)) return false;31 // Disallow certain keys32 if (/^-+|_+|\$+/.test(s)) return false;33 return true;34};35export const isValidKey = overEvery([isNonEmpty, isValidUnicode, isValidForm]);36export const isValidValue = overEvery([isNonEmpty, isValidUnicode]);37export const keyArb = suchthat(nestring, isValidKey);38export const objArb = suchthat(dict(nestring), o =>39 Object.keys(o).reduce((memo, k) => {40 if (!memo) return memo;41 return isValidKey(k) && isValidValue(o[k]);42 }, true),43);44const generate = (arb, len) => {45 const l = len || random(0, 5);46 let xs = [];47 while (xs.length < l) {48 xs = concat(xs, arb.generator(l));49 }50 return take(l, xs);51};52const randomSpec = () => {53 switch (random(0, 5)) {54 case 0: {55 return {[keyArb.generator(2)]: objArb};56 }57 case 1: {58 return {[keyArb.generator(2)]: array(string)};59 }60 case 2: {61 return {[keyArb.generator(2)]: record(randomSpec())};62 }63 default: {64 return {[keyArb.generator(2)]: string};65 }66 }67};68const type = elements(["type1", "type2", "type3"]);69const term = nestring;70/**71 * An arbitrary that can be used in `jsverify` based property tests.72 * It produces a single object that resembles a list.73 */74export const listArb = record(merge({type, term}, randomSpec()));75/**76 * An arbitrary that can be used in `jsverify` based property tests.77 * It produces an array of objects, where each object is a list.78 */79export const listsArb = array(listArb).smap(qs.uniq, identity);80/**81 * Randonly generate a list of lists.82 * @param {number} size The number of lists to generate.83 * @returns {Array.<Object>} A list of list objects.84 */85export const lists = size => generate(listsArb, size);86/**87 * An arbitrary that can be used in `jsverify` based property tests.88 * It produces a single object that resembles a query.89 */90export const queryArb = listArb;91/**92 * An arbitrary that can be used in `jsverify` based property tests.93 * It produces an array of objects, where each object is a list.94 */95export const queriesArb = listsArb;96/**97 * Randonly generate a list of queries.98 * @param {number} size The number of queries to generate.99 * @returns {Array.<Object>} A list of queries.100 */101export const queries = size => generate(queriesArb, size);102const dataUnitSpec = {103 field1: nestring,104 field2: nestring,105 _sc_id_fields: constant(["field1", "field2"]),106 _sc_media: listsArb,107 _sc_relations: listsArb,108 _sc_downloads: listsArb,109 _sc_queries: listsArb,110 _sc_locations: listsArb,111 _sc_annotations: listsArb,112 _sc_markers: array(string),113};114/**115 * An arbitrary that can be used in `jsverify` based property tests.116 * It produces a single object that resembles a unit of data.117 */118export const unitArb = record(merge(dataUnitSpec, randomSpec()));119/**120 * Randomly generate a single unit of data.121 * @returns {Object} A unit of data.122 */123export const unit = () => unitArb.generator(random(0, 5));124/**125 * An arbitrary that can be used in `jsverify` based property tests.126 * It produces an array of objects, where each object is a unit of data.127 */128export const dataArb = array(unitArb)129 .smap(ds.uniq, identity)130 .smap(131 map(x => (trueOrFalse() ? merge(x, {_sc_id_hash: ds.dataId(x)}) : x)),132 identity,133 );134/**135 * Randonly generate units of data..136 * @param {number} size The number of data units to generate.137 * @returns {Array.<Object>} A list of data units.138 */139export const data = size => generate(dataArb, size);140/**141 * An arbitrary that can be used in `jsverify` based property tests.142 * It produces an object that is an envelope..143 */144export const envelopeArb = record({data: dataArb, queries: queriesArb});145/**146 * Randomly generate a envelope with data and queries.147 * @param {number} sizeData The number of data units in the envelope.148 * @param {number} sizeQueries The number of queries in the envelope.149 * @returns {Object} A unit of data.150 */151export const envelope = (sizeData, sizeQueries) => ({152 data: data(sizeData || random(0, 5)),153 queries: queries(sizeQueries || random(0, 5)),154});155export default {156 objArb,157 queriesArb,158 queries,159 listArb,160 listsArb,161 lists,162 unitArb,163 unit,164 dataArb,165 data,166 envelopeArb,167 envelope,...

Full Screen

Full Screen

merge-weak-maps.js

Source:merge-weak-maps.js Github

copy

Full Screen

1/**2 * Copyright 2021 Google LLC3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16import { testProp, fc } from 'ava-fast-check'17import isWeakMap from 'is-weakmap'18import test from 'ava'19import { mergeWeakMaps } from '../src/merge-weak-maps.js'20import toCommands from './helpers/to-commands.js'21const entryArb = fc.tuple(fc.object(), fc.anything())22const arraysOfEntriesArb = fc.array(fc.array(entryArb))23testProp(24 `mergeWeakMaps returns a WeakMap`,25 [arraysOfEntriesArb],26 (t, arrays) => {27 const weakMap = mergeWeakMaps(28 ...arrays.map(entries => new WeakMap(entries))29 )30 t.true(isWeakMap(weakMap))31 }32)33testProp(34 `mergeWeakMaps throws a type error when passed a non-WeakMap`,35 [36 fc37 .array(38 fc.oneof(39 fc.array(entryArb).map(entries => new WeakMap(entries)),40 fc.anything()41 )42 )43 .filter(array => array.some(value => !isWeakMap(value)))44 ],45 (t, values) => {46 t.throws(47 () => {48 mergeWeakMaps(...values)49 },50 {51 instanceOf: TypeError,52 message: `mergeWeakMaps expects WeakMaps`53 }54 )55 }56)57testProp(58 `mergeWeakMaps returns a WeakMap with a set method that returns the same WeakMap`,59 [arraysOfEntriesArb, fc.array(entryArb, { minLength: 1 })],60 (t, arrays, entries) => {61 const weakMap = mergeWeakMaps(62 ...arrays.map(entries => new WeakMap(entries))63 )64 for (const [key, value] of entries) {65 t.is(weakMap.set(key, value), weakMap)66 }67 }68)69const { deleteCommand, getCommand, hasCommand, setCommand } = toCommands({70 delete(t, model, real, key) {71 t.is(real.delete(key), model.delete(key))72 },73 get(t, model, real, key) {74 t.is(real.get(key), model.get(key))75 },76 has(t, model, real, key) {77 t.is(real.has(key), model.has(key))78 },79 set(t, model, real, [key, value]) {80 real.set(key, value)81 model.set(key, value)82 }83})84testProp(85 `mergeWeakMaps returns a WeakMap that behaves like a non-merged WeakMap containing the same values as the merged WeakMaps`,86 [87 arraysOfEntriesArb88 .filter(89 arrays =>90 arrays.length > 0 && arrays.some(entries => entries.length > 0)91 )92 .chain(arrays => {93 const keyArb = fc.oneof(94 fc.object(),95 fc.constantFrom(96 ...arrays.flatMap(entries => entries.map(([key]) => key))97 )98 )99 return fc.tuple(100 fc.constant(arrays),101 fc.commands(102 [103 keyArb.map(deleteCommand),104 keyArb.map(getCommand),105 keyArb.map(hasCommand),106 fc107 .oneof(entryArb, fc.tuple(keyArb, fc.anything()))108 .map(setCommand)109 ],110 { maxCommands: 1000 }111 )112 )113 })114 ],115 (t, [arrays, commands]) => {116 fc.modelRun(117 () => ({118 model: { t, model: new WeakMap(arrays.flat()) },119 real: mergeWeakMaps(...arrays.map(entries => new WeakMap(entries)))120 }),121 commands122 )123 // When we only have `set` commands no assertions are run124 t.pass()125 }126)127test(`mergeWeakMaps concrete example`, t => {128 const [a, b, c, d] = [[], {}, new Set(), new Map()]129 const weakMap1 = new WeakMap([130 [a, 1],131 [b, 2]132 ])133 const weakMap2 = new WeakMap([[c, 3]])134 const mergedWeakMap = mergeWeakMaps(weakMap1, weakMap2)135 t.true(mergedWeakMap.has(a))136 t.true(mergedWeakMap.has(b))137 t.true(mergedWeakMap.has(c))138 mergedWeakMap.delete(a)139 t.true(weakMap1.has(a))140 t.false(mergedWeakMap.has(a))141 mergedWeakMap.set(d, 5)142 t.false(weakMap1.has(d))143 t.false(weakMap2.has(d))144 t.is(mergedWeakMap.get(d), 5)...

Full Screen

Full Screen

dictionary.ts

Source:dictionary.ts Github

copy

Full Screen

1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';2import { tuple } from './tuple';3import { uniqueArray } from './uniqueArray';4import { SizeForArbitrary } from './_internals/helpers/MaxLengthFromMinLength';5import { keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper } from './_internals/mappers/KeyValuePairsToObject';6/** @internal */7function dictionaryKeyExtractor(entry: [string, unknown]): string {8 return entry[0];9}10/**11 * Constraints to be applied on {@link dictionary}12 * @remarks Since 2.22.013 * @public14 */15export interface DictionaryConstraints {16 /**17 * Lower bound for the number of keys defined into the generated instance18 * @remarks Since 2.22.019 */20 minKeys?: number;21 /**22 * Lower bound for the number of keys defined into the generated instance23 * @remarks Since 2.22.024 */25 maxKeys?: number;26 /**27 * Define how large the generated values should be (at max) *28 * @remarks Since 2.22.029 */30 size?: SizeForArbitrary;31}32/**33 * For dictionaries with keys produced by `keyArb` and values from `valueArb`34 *35 * @param keyArb - Arbitrary used to generate the keys of the object36 * @param valueArb - Arbitrary used to generate the values of the object37 *38 * @remarks Since 1.0.039 * @public40 */41export function dictionary<T>(42 keyArb: Arbitrary<string>,43 valueArb: Arbitrary<T>,44 constraints: DictionaryConstraints = {}45): Arbitrary<Record<string, T>> {46 return uniqueArray(tuple(keyArb, valueArb), {47 minLength: constraints.minKeys,48 maxLength: constraints.maxKeys,49 size: constraints.size,50 selector: dictionaryKeyExtractor,51 }).map(keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { keyArb } = require('fast-check-monorepo');3const myArb = keyArb(fc.integer(), fc.string());4fc.assert(fc.property(myArb, (myObj) => {5 return myObj.key === myObj.value;6}));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const keyArb = require('fast-check-monorepo/lib/arbitraries/key-arb.js').keyArb;3const key = fc.sample(keyArb, 1)[0];4console.log(key);5{6 "scripts": {7 },8 "dependencies": {9 }10}11const fc = require('fast-check-monorepo');12const keyArb = fc.arbitraries.keyArb;13const key = fc.sample(keyArb, 1)[0];14console.log(key);15const fc = require('fast-check-monorepo');16const keyArb = fc.arbitraries.keyArb;17const key = fc.sample(keyArb, 1)[0];18console.log(key);19const fc = require('fast-check-monorepo');20const keyArb = fc.arbitraries.keyArb.keyArb;21const key = fc.sample(key

Full Screen

Using AI Code Generation

copy

Full Screen

1const {keyArb} = require('fast-check-monorepo')2const fc = require('fast-check')3const {isKey} = require('is-key')4fc.assert(5 fc.property(keyArb(), (key) => {6 return isKey(key)7 })8fc.assert(9 fc.property(keyArb(), (key) => {10 })11fc.assert(12 fc.property(keyArb(), (key) => {13 })14fc.assert(15 fc.property(keyArb(), (key) => {16 })17fc.assert(18 fc.property(keyArb(), (key) => {19 })20fc.assert(21 fc.property(keyArb(), (key) => {22 })23fc.assert(24 fc.property(keyArb(), (key) => {25 })26fc.assert(27 fc.property(keyArb(), (key) => {28 })29fc.assert(30 fc.property(keyArb(), (key) => {31 })32fc.assert(33 fc.property(keyArb(), (key) => {34 })35fc.assert(36 fc.property(keyArb(), (key) => {

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