How to use letrecBuilder method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

letrec.spec.ts

Source:letrec.spec.ts Github

copy

Full Screen

1import { letrec } from '../../../src/arbitrary/letrec';2import { LazyArbitrary } from '../../../src/arbitrary/_internals/LazyArbitrary';3import { Value } from '../../../src/check/arbitrary/definition/Value';4import { Stream } from '../../../src/stream/Stream';5import { FakeIntegerArbitrary, fakeArbitrary } from './__test-helpers__/ArbitraryHelpers';6import { fakeRandom } from './__test-helpers__/RandomHelpers';7import {8 assertGenerateEquivalentTo,9 assertProduceSameValueGivenSameSeed,10 assertProduceValuesShrinkableWithoutContext,11 assertShrinkProducesSameValueWithoutInitialContext,12} from './__test-helpers__/ArbitraryAssertions';13describe('letrec', () => {14 describe('builder', () => {15 it('should be able to construct independant arbitraries', () => {16 // Arrange17 const { instance: expectedArb1 } = fakeArbitrary();18 const { instance: expectedArb2 } = fakeArbitrary();19 // Act20 const { arb1, arb2 } = letrec((_tie) => ({21 arb1: expectedArb1,22 arb2: expectedArb2,23 }));24 // Assert25 expect(arb1).toBe(expectedArb1);26 expect(arb2).toBe(expectedArb2);27 });28 it('should not produce LazyArbitrary for no-tie constructs', () => {29 // Arrange30 const { instance: expectedArb } = fakeArbitrary();31 // Act32 const { arb } = letrec((_tie) => ({33 arb: expectedArb,34 }));35 // Assert36 expect(arb).not.toBeInstanceOf(LazyArbitrary);37 expect(arb).toBe(expectedArb);38 });39 it('should not produce LazyArbitrary for indirect tie constructs', () => {40 // Arrange / Act41 const { arb } = letrec((tie) => {42 const { instance: expectedArb, generate } = fakeArbitrary();43 generate.mockImplementation((...args) => tie('arb').generate(...args));44 return {45 // arb is an arbitrary wrapping the tie value (as fc.array)46 arb: expectedArb,47 };48 });49 // Assert50 expect(arb).not.toBeInstanceOf(LazyArbitrary);51 });52 it('should produce LazyArbitrary for direct tie constructs', () => {53 // Arrange / Act54 const { arb } = letrec((tie) => ({55 arb: tie('arb'),56 }));57 // Assert58 expect(arb).toBeInstanceOf(LazyArbitrary);59 });60 it('should be able to construct mutually recursive arbitraries', () => {61 // Arrange / Act62 const { arb1, arb2 } = letrec((tie) => ({63 arb1: tie('arb2'),64 arb2: tie('arb1'),65 }));66 // Assert67 expect(arb1).toBeDefined();68 expect(arb2).toBeDefined();69 });70 it('should apply tie correctly', () => {71 // Arrange72 const { instance: expectedArb } = fakeArbitrary();73 // Act74 const { arb1, arb2, arb3 } = letrec((tie) => ({75 arb1: tie('arb2'),76 arb2: tie('arb3'),77 arb3: expectedArb,78 }));79 // Assert80 expect(arb1).toBeInstanceOf(LazyArbitrary);81 expect(arb2).toBeInstanceOf(LazyArbitrary);82 expect(arb3).not.toBeInstanceOf(LazyArbitrary);83 expect((arb1 as any as LazyArbitrary<unknown>).underlying).toBe(arb2);84 expect((arb2 as any as LazyArbitrary<unknown>).underlying).toBe(arb3);85 expect(arb3).toBe(expectedArb);86 });87 it('should apply tie the same way for a reversed declaration', () => {88 // Arrange89 const { instance: expectedArb } = fakeArbitrary();90 // Act91 const { arb1, arb2, arb3 } = letrec((tie) => ({92 // Same scenario as 'should apply tie correctly'93 // except we declared arb3 > arb2 > arb194 // instead of arb1 > arb2 > arb395 arb3: expectedArb,96 arb2: tie('arb3'),97 arb1: tie('arb2'),98 }));99 // Assert100 expect(arb1).toBeInstanceOf(LazyArbitrary);101 expect(arb2).toBeInstanceOf(LazyArbitrary);102 expect(arb3).not.toBeInstanceOf(LazyArbitrary);103 expect((arb1 as any as LazyArbitrary<unknown>).underlying).toBe(arb2);104 expect((arb2 as any as LazyArbitrary<unknown>).underlying).toBe(arb3);105 expect(arb3).toBe(expectedArb);106 });107 });108 describe('generate', () => {109 it('should be able to delay calls to tie to generate', () => {110 // Arrange111 const biasFactor = 69;112 const { instance: simpleArb, generate } = fakeArbitrary();113 generate.mockReturnValueOnce(new Value(null, undefined));114 const { instance: mrng } = fakeRandom();115 // Act116 const { arb1 } = letrec((tie) => {117 const { instance: simpleArb2, generate: generate2 } = fakeArbitrary();118 generate2.mockImplementation((...args) => tie('arb2').generate(...args));119 return {120 arb1: simpleArb2,121 arb2: simpleArb,122 };123 });124 expect(generate).not.toHaveBeenCalled();125 arb1.generate(mrng, biasFactor);126 // Assert127 expect(generate).toHaveBeenCalledTimes(1);128 expect(generate).toHaveBeenCalledWith(mrng, biasFactor);129 });130 it('should throw on generate if tie receives an invalid parameter', () => {131 // Arrange132 const biasFactor = 42;133 const { arb1 } = letrec((tie) => ({134 arb1: tie('missing'),135 }));136 const { instance: mrng } = fakeRandom();137 // Act / Assert138 expect(() => arb1.generate(mrng, biasFactor)).toThrowErrorMatchingInlineSnapshot(139 `"Lazy arbitrary "missing" not correctly initialized"`140 );141 });142 it('should throw on generate if tie receives an invalid parameter after creation', () => {143 // Arrange144 const biasFactor = 42;145 const { arb1 } = letrec((tie) => {146 const { instance: simpleArb, generate } = fakeArbitrary();147 generate.mockImplementation((...args) => tie('missing').generate(...args));148 return {149 arb1: simpleArb,150 };151 });152 const { instance: mrng } = fakeRandom();153 // Act / Assert154 expect(() => arb1.generate(mrng, biasFactor)).toThrowErrorMatchingInlineSnapshot(155 `"Lazy arbitrary "missing" not correctly initialized"`156 );157 });158 it('should accept "reserved" keys as output of builder', () => {159 // Arrange160 const biasFactor = 42;161 const { instance: simpleArb, generate } = fakeArbitrary();162 generate.mockReturnValueOnce(new Value(null, undefined));163 const { tie } = letrec((tie) => ({164 tie: tie('__proto__'),165 ['__proto__']: tie('__defineGetter__​​'),166 ['__defineGetter__​​']: tie('__defineSetter__​​'),167 ['__defineSetter__​​']: tie('__lookupGetter__​​'),168 ['__lookupGetter__​​']: tie('__lookupSetter__​​'),169 ['__lookupSetter__​​']: tie('constructor​​'),170 ['constructor​​']: tie('hasOwnProperty​​'),171 ['hasOwnProperty​​']: tie('isPrototypeOf​​'),172 ['isPrototypeOf​​']: tie('propertyIsEnumerable​​'),173 ['propertyIsEnumerable​​']: tie('toLocaleString​​'),174 ['toLocaleString​​']: tie('toSource​​'),175 ['toSource​​']: tie('toString​​'),176 ['toString​​']: tie('valueOf'),177 ['valueOf']: simpleArb,178 }));179 const { instance: mrng } = fakeRandom();180 // Act181 expect(generate).not.toHaveBeenCalled();182 tie.generate(mrng, biasFactor);183 // Assert184 expect(generate).toHaveBeenCalledTimes(1);185 expect(generate).toHaveBeenCalledWith(mrng, biasFactor);186 });187 it('should accept builders producing objects based on Object.create(null)', () => {188 // Arrange189 const biasFactor = 42;190 const { instance: simpleArb, generate } = fakeArbitrary();191 generate.mockReturnValueOnce(new Value(null, undefined));192 const { a } = letrec<{ a: unknown }>((tie) =>193 Object.assign(Object.create(null), {194 a: tie('b'),195 b: simpleArb,196 })197 );198 const { instance: mrng } = fakeRandom();199 // Act200 expect(generate).not.toHaveBeenCalled();201 a.generate(mrng, biasFactor);202 // Assert203 expect(generate).toHaveBeenCalledTimes(1);204 expect(generate).toHaveBeenCalledWith(mrng, biasFactor);205 });206 });207 describe('canShrinkWithoutContext', () => {208 it.each`209 expectedStatus210 ${false}211 ${true}212 `('should call canShrinkWithoutContext on the targets', ({ expectedStatus }) => {213 // Arrange214 const expectedValue = Symbol();215 const { instance: simpleArb, canShrinkWithoutContext } = fakeArbitrary();216 canShrinkWithoutContext.mockReturnValueOnce(expectedStatus);217 const { arb1 } = letrec((tie) => {218 return {219 arb1: tie('arb2'),220 arb2: simpleArb,221 };222 });223 // Act224 const out = arb1.canShrinkWithoutContext(expectedValue);225 // Assert226 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);227 expect(canShrinkWithoutContext).toHaveBeenCalledWith(expectedValue);228 expect(out).toBe(expectedStatus);229 });230 it('should throw on canShrinkWithoutContext if tie receives an invalid parameter', () => {231 // Arrange232 const expectedValue = Symbol();233 const { arb1 } = letrec((tie) => ({234 arb1: tie('missing'),235 }));236 // Act / Assert237 expect(() => arb1.canShrinkWithoutContext(expectedValue)).toThrowErrorMatchingInlineSnapshot(238 `"Lazy arbitrary "missing" not correctly initialized"`239 );240 });241 });242 describe('shrink', () => {243 it('should call shrink on the targets', () => {244 // Arrange245 const expectedValue = Symbol();246 const expectedContext = Symbol();247 const expectedStream = Stream.of(new Value(Symbol(), undefined));248 const { instance: simpleArb, shrink } = fakeArbitrary();249 shrink.mockReturnValueOnce(expectedStream);250 const { arb1 } = letrec((tie) => {251 return {252 arb1: tie('arb2'),253 arb2: simpleArb,254 };255 });256 // Act257 const out = arb1.shrink(expectedValue, expectedContext);258 // Assert259 expect(shrink).toHaveBeenCalledTimes(1);260 expect(shrink).toHaveBeenCalledWith(expectedValue, expectedContext);261 expect(out).toBe(expectedStream);262 });263 it('should throw on shrink if tie receives an invalid parameter', () => {264 // Arrange265 const expectedValue = Symbol();266 const expectedContext = Symbol();267 const { arb1 } = letrec((tie) => ({268 arb1: tie('missing'),269 }));270 // Act / Assert271 expect(() => arb1.shrink(expectedValue, expectedContext)).toThrowErrorMatchingInlineSnapshot(272 `"Lazy arbitrary "missing" not correctly initialized"`273 );274 });275 });276});277describe('letrec (integration)', () => {278 const letrecBuilder = () => {279 const { a } = letrec((tie) => ({280 a: tie('b'),281 b: tie('c'),282 c: new FakeIntegerArbitrary(),283 }));284 return a;285 };286 it('should generate the values as-if we directly called the target arbitrary', () => {287 assertGenerateEquivalentTo(letrecBuilder, () => new FakeIntegerArbitrary(), {288 isEqualContext: (c1, c2) => {289 expect(c2).toEqual(c1);290 },291 });292 });293 it('should produce the same values given the same seed', () => {294 assertProduceSameValueGivenSameSeed(letrecBuilder);295 });296 it('should produce values seen as shrinkable without any context (if underlyings do)', () => {297 assertProduceValuesShrinkableWithoutContext(letrecBuilder);298 });299 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {300 assertShrinkProducesSameValueWithoutInitialContext(letrecBuilder);301 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { letrecBuilder } = require('fast-check');2const { letrec } = letrecBuilder();3const list = letrec((tie) => ({4 empty: {},5 cons: { head: tie('nat'), tail: tie('list') }6}));7console.log(list);8{9 "scripts": {10 },11 "dependencies": {12 }13}14 at Object.<anonymous> (test.js:8:38)15 at Module._compile (internal/modules/cjs/loader.js:1158:30)16 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)17 at Module.load (internal/modules/cjs/loader.js:1002:32)18 at Function.Module._load (internal/modules/cjs/loader.js:901:14)19 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)20const { letrecBuilder } = require('fast-check');21const { letrec } = letrecBuilder();22const list = letrec((tie) => ({23 empty: {},24 cons: { head: 0, tail: tie('list') }25}));26console.log(list);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { letrecBuilder } = require("fast-check");2const { letrec } = letrecBuilder();3const { tuple } = require("fast-check");4const { record } = require("fast-check");5const { string } = require("fast-check");6const { number } = require("fast-check");7const { constant } = require("fast-check");8const { oneof } = require("fast-check");9const { option } = require("fast-check");10const { array } = require("fast-check");11const { map } = require("fast-check");12const { set } = require("fast-check");13const { dictionary } = require("fast-check");14const { dictionaryOf } = require("fast-check");15const { object } = require("fast-check");16const { unicode } = require("fast-check");17const { unicodeJson } = require("fast-check");18const { anyJson } = require("fast-check");19const { any } = require("fast-check");20const { bigInt } = require("fast-check");21const { float } = require("fast-check");22const { integer } = require("fast-check");23const { boolean } = require("fast-check");24const { date } = require("fast-check");25const { maxSafeInteger } = require("fast-check");26const { minSafeInteger } = require("fast-check");27const { double } = require("fast-check");28const { base64 } = require("fast-check");29const { base64String } = require("fast-check");30const { hexa } = require("fast-check");31const { hexaString } = require("fast-check");32const { char } = require("fast-check");33const { ascii } = require("fast-check");34const { asciiString } = require("fast-check");35const { fullUnicode } = require("fast-check");36const { fullUnicodeString } = require("fast-check");37const { fullUnicodeJsonObject } = require("fast-check");38const { fullUnicodeJsonString } = require("fast-check");39const { fullUnicodeJson } = require("fast-check");40const { unicodeJsonObject } = require("fast-check");41const { unicodeJsonString } = require("fast-check");42const { asciiJsonObject } = require("fast-check");43const { asciiJsonString } = require("fast-check");44const { asciiJson } = require("fast-check");45const { asciiJsonObject } = require("fast-check");46const { asciiJsonString } = require("fast-check");

Full Screen

Using AI Code Generation

copy

Full Screen

1import { letrecBuilder } from 'fast-check';2import { letrec } from 'fast-check';3import { letrec } from 'fast-check';4import { letrecBuilder } from 'fast-check';5import { letrec } from 'fast-check';6import { letrec } from 'fast-check';7import { letrecBuilder } from 'fast-check';8import { letrec } from 'fast-check';9import { letrec } from 'fast-check';10import { letrecBuilder } from 'fast-check';11import { letrec } from 'fast-check';12import { letrec } from 'fast-check';13import { letrecBuilder } from 'fast-check';14import { letrec } from 'fast-check';15import { letrec } from 'fast-check';16import { letrecBuilder } from 'fast-check';17import { letrec } from 'fast-check';18import { letrec } from 'fast-check';19import { letrecBuilder } from 'fast-check';20import { letrec } from 'fast-check';21import { letrec } from 'fast-check';22import { letrecBuilder } from 'fast-check';23import { letrec } from 'fast-check';24import { letrec } from 'fast-check';25import { letrecBuilder } from 'fast-check';26import { letrec } from 'fast-check';27import { letrec } from 'fast-check';28import { letrecBuilder } from 'fast-check';29import { letrec } from 'fast-check';30import { letrec } from 'fast-check';31import { letrecBuilder } from 'fast-check';32import { letrec } from 'fast-check';33import { letrec } from 'fast-check';

Full Screen

Using AI Code Generation

copy

Full Screen

1const { letrecBuilder } = require('fast-check');2const builder = letrecBuilder();3const { letrec } = builder;4const gen = letrec((tie) => ({5 a: tie('b'),6 b: tie('c'),7 c: tie('a'),8}));9const { letrecBuilder } = require('fast-check');10const builder = letrecBuilder();11const { letrec } = builder;12const gen = letrec((tie) => ({13 a: tie('b'),14 b: tie('c'),15 c: tie('a'),16}));17const { letrec } = builder;18const gen = letrec((tie) => ({19 a: tie('b'),20 b: tie('c'),21 c: tie('a'),22}));23const { letrec } = builder;24const gen = letrec((tie) => ({25 a: tie('b'),26 b: tie('c'),27 c: tie('a'),28}));29const { letrec } = builder;30const gen = letrec((tie) => ({31 a: tie('b'),32 b: tie('c'),33 c: tie('a'),34}));35const { letrec } = builder;36const gen = letrec((tie) => ({37 a: tie('b'),38 b: tie('c'),39 c: tie('a'),40}));41const { letrec } = builder;42const gen = letrec((tie) => ({43 a: tie('b'),44 b: tie('c'),45 c: tie('a'),46}));47const { letrec } = builder;48const gen = letrec((tie) => ({49 a: tie('b'),50 b: tie('c'),51 c: tie('a'),52}));53const { letrec } = builder;54const gen = letrec((tie) => ({55 a: tie('b'),56 b: tie('c'),57 c: tie('a'),58}));59const { letrec } = builder;60const gen = letrec((tie) => ({61 a: tie('b'),62 b: tie('c'),63 c: tie('a'),64}));65const { letrec } = builder;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { letrecBuilder } = require('fast-check');2const { letrec } = letrecBuilder();3const arb = letrec((tie) => ({4 node: tie('node', arb => arb.array(arb.node)),5}));6arb.node.generate(mrng => mrng.nextInt(0,1000));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { letrecBuilder } = require('fast-check');2const { func, string } = require('fast-check');3const letrec = letrecBuilder();4const gen = letrec('gen', () => func(string(), gen));5const testData = gen.generate(mrng);6const { letrecBuilder } = require('fast-check-monorepo');7const { func, string } = require('fast-check');8const letrec = letrecBuilder();9const gen = letrec.rec('gen', () => func(string(), gen));10const { letrecBuilder } = require('fast-check-monorepo

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