How to use noBiasArbitrary method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

Arbitrary.utest.spec.ts

Source:Arbitrary.utest.spec.ts Github

copy

Full Screen

1import { Arbitrary } from '../../../../../src/check/arbitrary/definition/Arbitrary';2import { Value } from '../../../../../src/check/arbitrary/definition/Value';3import { Stream } from '../../../../../src/stream/Stream';4import * as stubRng from '../../../stubs/generators';5import { cloneMethod, hasCloneMethod } from '../../../../../src/check/symbols';6import { Random } from '../../../../../src/random/generator/Random';7const mrngNoCall = stubRng.mutable.nocall();8describe('NextArbitrary', () => {9 describe('filter', () => {10 it('should filter the values produced by the original arbitrary on generate', () => {11 // Arrange12 const expectedBiasFactor = 48;13 const generate = jest.fn();14 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;15 const shrink = jest.fn();16 const choice1 = new Value(1, Symbol());17 const choice2 = new Value(2, Symbol());18 const choice3 = new Value(3, Symbol());19 const choice4 = new Value(4, Symbol());20 generate21 .mockReturnValueOnce(choice1)22 .mockReturnValueOnce(choice2)23 .mockReturnValueOnce(choice3)24 .mockReturnValueOnce(choice4);25 class MyNextArbitrary extends Arbitrary<any> {26 generate = generate;27 canShrinkWithoutContext = canShrinkWithoutContext;28 shrink = shrink;29 }30 // Act31 const arb = new MyNextArbitrary().filter((v) => v % 3 === 0);32 const g = arb.generate(mrngNoCall, expectedBiasFactor);33 // Assert34 expect(g).toBe(choice3); // just returning the first Value that fits35 expect(generate).toHaveBeenNthCalledWith(3, mrngNoCall, expectedBiasFactor); // same Random not cloned36 });37 it('should filter the values produced by the original arbitrary on shrink', () => {38 // Arrange39 const generate = jest.fn();40 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;41 const shrink = jest.fn();42 const valueToShrink = 5;43 const contextToShrink = Symbol();44 const choice1 = new Value(1, Symbol());45 const choice2 = new Value(2, Symbol());46 const choice3 = new Value(3, Symbol());47 const choice4 = new Value(4, Symbol());48 const choice5 = new Value(6, Symbol());49 shrink.mockReturnValueOnce(Stream.of(choice1, choice2, choice3, choice4, choice5));50 class MyNextArbitrary extends Arbitrary<any> {51 generate = generate;52 canShrinkWithoutContext = canShrinkWithoutContext;53 shrink = shrink;54 }55 // Act56 const arb = new MyNextArbitrary().filter((v) => v % 3 === 0);57 const shrinks = arb.shrink(valueToShrink, contextToShrink);58 // Assert59 expect([...shrinks]).toEqual([choice3, choice5]); // just keeping values fitting the predicate60 expect(shrink).toHaveBeenCalledWith(valueToShrink, contextToShrink);61 });62 it.each`63 canShrinkWithoutContextOutput | predicateOutput | expected64 ${false} | ${false} | ${false}65 ${false} | ${true} | ${false}66 ${false} | ${false} | ${false}67 ${true} | ${true} | ${true}68 `(69 'should check underlying arbitrary then predicate to know if the value could have been generated',70 ({ canShrinkWithoutContextOutput, predicateOutput, expected }) => {71 // Arrange72 const requestedValue = Symbol();73 const generate = jest.fn();74 const canShrinkWithoutContext = jest.fn();75 const shrink = jest.fn();76 const predicate = jest.fn();77 class MyNextArbitrary extends Arbitrary<any> {78 generate = generate;79 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;80 shrink = shrink;81 }82 canShrinkWithoutContext.mockReturnValueOnce(canShrinkWithoutContextOutput);83 predicate.mockReturnValueOnce(predicateOutput);84 // Act85 const arb = new MyNextArbitrary().filter(predicate);86 const out = arb.canShrinkWithoutContext(requestedValue);87 // Assert88 expect(out).toBe(expected);89 expect(canShrinkWithoutContext).toHaveBeenCalledWith(requestedValue);90 if (canShrinkWithoutContextOutput) {91 expect(predicate).toHaveBeenCalledWith(requestedValue);92 } else {93 expect(predicate).not.toHaveBeenCalledWith(requestedValue);94 expect(predicate).not.toHaveBeenCalled();95 }96 }97 );98 });99 describe('map', () => {100 it('should map the values produced by the original arbitrary on generate', () => {101 // Arrange102 const expectedBiasFactor = 48;103 const generate = jest.fn();104 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;105 const shrink = jest.fn();106 const choice = new Value(1, Symbol());107 generate.mockReturnValueOnce(choice);108 class MyNextArbitrary extends Arbitrary<any> {109 generate = generate;110 canShrinkWithoutContext = canShrinkWithoutContext;111 shrink = shrink;112 }113 // Act114 const arb = new MyNextArbitrary().map((v) => String(v));115 const g = arb.generate(mrngNoCall, expectedBiasFactor);116 // Assert117 expect(g.value).toBe(String(choice.value)); // value has been mapped118 expect(generate).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor);119 });120 it('should preserve cloneable capabilities for mapped values on generate', () => {121 // Arrange122 const expectedBiasFactor = 48;123 const generate = jest.fn();124 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;125 const shrink = jest.fn();126 const choice = new Value({ source: 1, [cloneMethod]: () => choice.value_ }, Symbol());127 generate.mockReturnValueOnce(choice);128 class MyNextArbitrary extends Arbitrary<any> {129 generate = generate;130 canShrinkWithoutContext = canShrinkWithoutContext;131 shrink = shrink;132 }133 // Act134 const arb = new MyNextArbitrary().map((v) => ({ stringValue: String(v.source) }));135 const g = arb.generate(mrngNoCall, expectedBiasFactor);136 // Assert137 expect(g.value.stringValue).toBe(String(choice.value.source)); // value has been mapped138 expect(g.hasToBeCloned).toBe(true); // clone has been preserved on Value139 expect(hasCloneMethod(g.value)).toBe(true); // clone has been preserved on the instance140 expect(generate).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor);141 });142 it('should not alter the mapped value if already cloneable on generate', () => {143 // Arrange144 const expectedBiasFactor = 48;145 const generate = jest.fn();146 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;147 const shrink = jest.fn();148 const choice = new Value({ source: 1, [cloneMethod]: () => choice.value_ }, Symbol());149 const mappedClone = jest.fn();150 const mapped = { source: 42, [cloneMethod]: mappedClone };151 generate.mockReturnValueOnce(choice);152 class MyNextArbitrary extends Arbitrary<any> {153 generate = generate;154 canShrinkWithoutContext = canShrinkWithoutContext;155 shrink = shrink;156 }157 // Act158 const arb = new MyNextArbitrary().map((_v) => mapped); // mapped already comes with clone capacities159 const g = arb.generate(mrngNoCall, expectedBiasFactor);160 // Assert161 expect(g.value_).toBe(mapped); // value has been mapped162 expect(g.value_.source).toBe(mapped.source); // value has been mapped and value not altered163 expect(g.value_[cloneMethod]).toBe(mapped[cloneMethod]); // value has been mapped and clone method has been preserved164 expect(g.hasToBeCloned).toBe(true); // clone has been preserved on Value165 expect(hasCloneMethod(g.value_)).toBe(true); // clone has been preserved on the instance166 expect(generate).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor);167 });168 it('should properly shrink output of generate by calling back shrink with the right context', () => {169 // Arrange170 const expectedBiasFactor = 42;171 const generate = jest.fn();172 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;173 const shrink = jest.fn();174 const source = new Value(69, Symbol());175 generate.mockReturnValueOnce(source);176 const choice1 = new Value(1, Symbol());177 const choice2 = new Value(2, Symbol());178 const choice3 = new Value(3, Symbol());179 shrink.mockReturnValueOnce(Stream.of(choice1, choice2, choice3));180 class MyNextArbitrary extends Arbitrary<any> {181 generate = generate;182 canShrinkWithoutContext = canShrinkWithoutContext;183 shrink = shrink;184 }185 // Act186 const arb = new MyNextArbitrary().map((v) => String(v));187 const g = arb.generate(mrngNoCall, expectedBiasFactor);188 const shrinks = arb.shrink(g.value, g.context);189 // Assert190 expect([...shrinks].map((s) => s.value)).toEqual(['1', '2', '3']); // just mapping values191 expect(shrink).toHaveBeenCalledWith(source.value, source.context);192 });193 it('should properly shrink output of shrink by calling back shrink with the right context', () => {194 // Arrange195 const expectedBiasFactor = 42;196 const generate = jest.fn();197 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;198 const shrink = jest.fn();199 const source = new Value(69, Symbol());200 generate.mockReturnValueOnce(source);201 const choice1 = new Value(1, Symbol());202 const choice2 = new Value(2, Symbol());203 const choice3 = new Value(3, Symbol());204 shrink.mockReturnValueOnce(Stream.of(choice1, choice2, choice3));205 const choice21 = new Value(21, Symbol());206 const choice22 = new Value(22, Symbol());207 shrink.mockReturnValueOnce(Stream.of(choice21, choice22));208 class MyNextArbitrary extends Arbitrary<any> {209 generate = generate;210 canShrinkWithoutContext = canShrinkWithoutContext;211 shrink = shrink;212 }213 // Act214 const arb = new MyNextArbitrary().map((v) => String(v));215 const g = arb.generate(mrngNoCall, expectedBiasFactor);216 const shrinksGen1 = arb.shrink(g.value, g.context);217 const mappedChoice2 = shrinksGen1.getNthOrLast(1)!;218 const shrinksGen2 = arb.shrink(mappedChoice2.value, mappedChoice2.context);219 // Assert220 expect([...shrinksGen2].map((s) => s.value)).toEqual(['21', '22']); // just mapping values221 expect(shrink).toHaveBeenCalledWith(source.value, source.context);222 expect(shrink).toHaveBeenCalledWith(choice2.value, choice2.context);223 });224 it('should preserve cloneable capabilities for mapped values on shrink', () => {225 // Arrange226 const expectedBiasFactor = 48;227 const generate = jest.fn();228 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;229 const shrink = jest.fn();230 const source = new Value({ source: 1, [cloneMethod]: () => source.value_ }, Symbol());231 generate.mockReturnValueOnce(source);232 const choice1 = new Value({ source: 2, [cloneMethod]: () => choice1.value_ }, Symbol());233 const choice2 = new Value({ source: 2, [cloneMethod]: () => choice2.value_ }, Symbol());234 shrink.mockReturnValueOnce(Stream.of(choice1, choice2));235 class MyNextArbitrary extends Arbitrary<any> {236 generate = generate;237 canShrinkWithoutContext = canShrinkWithoutContext;238 shrink = shrink;239 }240 // Act241 const arb = new MyNextArbitrary().map((v) => ({ stringValue: String(v.source) }));242 const g = arb.generate(mrngNoCall, expectedBiasFactor);243 const shrinks = arb.shrink(g.value, g.context);244 const shrinksValues = [...shrinks];245 // Assert246 expect(shrinksValues).toHaveLength(2);247 expect(shrinksValues[0].hasToBeCloned).toBe(true); // clone has been preserved on Value248 expect(shrinksValues[1].hasToBeCloned).toBe(true);249 expect(hasCloneMethod(shrinksValues[0].value)).toBe(true); // clone has been preserved on the instance250 expect(hasCloneMethod(shrinksValues[1].value)).toBe(true);251 });252 it('should always return false for canShrinkWithoutContext when not provided any unmapper function', () => {253 // Arrange254 const generate = jest.fn();255 const canShrinkWithoutContext = jest.fn();256 const shrink = jest.fn();257 class MyNextArbitrary extends Arbitrary<any> {258 generate = generate;259 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;260 shrink = shrink;261 }262 // Act263 const arb = new MyNextArbitrary().map(() => '');264 const out = arb.canShrinkWithoutContext('');265 // Assert266 expect(out).toBe(false);267 expect(canShrinkWithoutContext).not.toHaveBeenCalled();268 });269 it('should return empty stream when shrinking without any context and not provided any unmapper function', () => {270 // Arrange271 const generate = jest.fn();272 const canShrinkWithoutContext = jest.fn();273 const shrink = jest.fn();274 class MyNextArbitrary extends Arbitrary<any> {275 generate = generate;276 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;277 shrink = shrink;278 }279 // Act280 const arb = new MyNextArbitrary().map(() => '');281 const shrinks = arb.shrink('', undefined);282 // Assert283 expect([...shrinks]).toHaveLength(0);284 expect(shrink).not.toHaveBeenCalled();285 expect(canShrinkWithoutContext).not.toHaveBeenCalled();286 });287 it.each`288 outputCanGenerate289 ${false}290 ${true}291 `(292 'should try to unmap the value then call source arbitrary on canShrinkWithoutContext when provided a successful unmapper function',293 ({ outputCanGenerate }) => {294 // Arrange295 const generate = jest.fn();296 const canShrinkWithoutContext = jest.fn().mockReturnValue(outputCanGenerate);297 const shrink = jest.fn();298 const originalValue = Symbol();299 const unmapperOutput = Symbol();300 const unmapper = jest.fn().mockReturnValue(unmapperOutput);301 class MyNextArbitrary extends Arbitrary<any> {302 generate = generate;303 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;304 shrink = shrink;305 }306 // Act307 const arb = new MyNextArbitrary().map(() => Symbol(), unmapper);308 const out = arb.canShrinkWithoutContext(originalValue);309 // Assert310 expect(out).toBe(outputCanGenerate);311 expect(unmapper).toHaveBeenCalledTimes(1);312 expect(unmapper).toHaveBeenCalledWith(originalValue);313 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);314 expect(canShrinkWithoutContext).toHaveBeenCalledWith(unmapperOutput);315 }316 );317 it('should try to unmap the value and stop on error in case of failing unmapper function', () => {318 // Arrange319 const generate = jest.fn();320 const canShrinkWithoutContext = jest.fn();321 const shrink = jest.fn();322 const originalValue = Symbol();323 const unmapper = jest.fn().mockImplementation(() => {324 throw new Error('Unable to unmap such value');325 });326 class MyNextArbitrary extends Arbitrary<any> {327 generate = generate;328 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;329 shrink = shrink;330 }331 // Act332 const arb = new MyNextArbitrary().map(() => Symbol(), unmapper);333 const out = arb.canShrinkWithoutContext(originalValue);334 // Assert335 expect(out).toBe(false);336 expect(unmapper).toHaveBeenCalledTimes(1);337 expect(unmapper).toHaveBeenCalledWith(originalValue);338 expect(canShrinkWithoutContext).not.toHaveBeenCalled();339 });340 it('should return a mapped version of the stream produced by the source arbitrary for the unmapped value when provided an unmapper function', () => {341 // Arrange342 const expectedStreamValuesFromSource = Stream.of(343 new Value('titi', undefined),344 new Value('toto', undefined),345 new Value('tutu', undefined)346 );347 const generate = jest.fn();348 const canShrinkWithoutContext = jest.fn();349 const shrink = jest.fn().mockReturnValueOnce(expectedStreamValuesFromSource);350 const originalValue = Symbol();351 const unmapperOutput = 'tata';352 const unmapper = jest.fn().mockReturnValue('tata');353 class MyNextArbitrary extends Arbitrary<any> {354 generate = generate;355 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;356 shrink = shrink;357 }358 // Act359 const arb = new MyNextArbitrary().map((tag) => Symbol.for(tag), unmapper);360 const shrinks = [...arb.shrink(originalValue, undefined)];361 // Assert362 expect(shrinks.map((s) => s.value)).toEqual([Symbol.for('titi'), Symbol.for('toto'), Symbol.for('tutu')]);363 expect(unmapper).toHaveBeenCalledTimes(1);364 expect(unmapper).toHaveBeenCalledWith(originalValue);365 expect(shrink).toHaveBeenCalledTimes(1);366 expect(shrink).toHaveBeenCalledWith(unmapperOutput, undefined);367 });368 });369 describe('chain', () => {370 it('should chain the values produced by the original arbitrary on generate', () => {371 // Arrange372 const expectedBiasFactor = 48;373 const generate = jest.fn();374 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;375 const shrink = jest.fn();376 const choiceRoot = new Value(1, Symbol());377 generate.mockReturnValueOnce(choiceRoot);378 const generateChained = jest.fn();379 const canShrinkWithoutContextChained = jest.fn() as any as (value: unknown) => value is any;380 const shrinkChained = jest.fn();381 const choiceChained = new Value(50, Symbol());382 generateChained.mockReturnValueOnce(choiceChained);383 class MyNextArbitrary extends Arbitrary<any> {384 generate = generate;385 canShrinkWithoutContext = canShrinkWithoutContext;386 shrink = shrink;387 }388 class MyNextChainedArbitrary extends Arbitrary<any> {389 generate = generateChained;390 canShrinkWithoutContext = canShrinkWithoutContextChained;391 shrink = shrinkChained;392 }393 const chainer = jest.fn();394 chainer.mockReturnValueOnce(new MyNextChainedArbitrary());395 // Act396 const arb = new MyNextArbitrary().chain(chainer);397 const g = arb.generate(mrngNoCall, expectedBiasFactor);398 // Assert399 expect(g.value).toBe(choiceChained.value); // value has been chained400 expect(generate).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor); // the two calls to generate401 expect(generateChained).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor); // they share the same Random402 expect(chainer).toHaveBeenCalledWith(choiceRoot.value); // chainer was called with output of generate403 });404 it('should properly shrink output of generate by calling back shrink with the right context', () => {405 // Arrange406 const expectedBiasFactor = 48;407 const generate = jest.fn();408 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;409 const shrink = jest.fn();410 const choiceRoot = new Value(1, Symbol());411 generate.mockReturnValueOnce(choiceRoot);412 const shrinkRoot1 = new Value(10, Symbol());413 const shrinkRoot2 = new Value(11, Symbol());414 const shrinkRoot3 = new Value(15, Symbol());415 shrink.mockReturnValueOnce(Stream.of(shrinkRoot1, shrinkRoot2, shrinkRoot3));416 const generateChained = jest.fn();417 const canShrinkWithoutContextChained = jest.fn() as any as (value: unknown) => value is any;418 const shrinkChained = jest.fn();419 const choiceChained = new Value(50, Symbol());420 const choiceShrink1Chained = new Value(58, Symbol()); // chain will be called for each sub-shrink of root421 const choiceShrink2Chained = new Value(57, Symbol());422 const choiceShrink3Chained = new Value(16, Symbol());423 generateChained424 .mockReturnValueOnce(choiceChained)425 .mockReturnValueOnce(choiceShrink1Chained)426 .mockReturnValueOnce(choiceShrink2Chained)427 .mockReturnValueOnce(choiceShrink3Chained);428 const shrinkChained1 = new Value(25, Symbol());429 const shrinkChained2 = new Value(51, Symbol());430 shrinkChained.mockReturnValueOnce(Stream.of(shrinkChained1, shrinkChained2));431 class MyNextArbitrary extends Arbitrary<any> {432 generate = generate;433 canShrinkWithoutContext = canShrinkWithoutContext;434 shrink = shrink;435 }436 class MyNextChainedArbitrary extends Arbitrary<any> {437 generate = generateChained;438 canShrinkWithoutContext = canShrinkWithoutContextChained;439 shrink = shrinkChained;440 }441 const chainer = jest.fn();442 chainer.mockReturnValue(new MyNextChainedArbitrary());443 // Act444 const arb = new MyNextArbitrary().chain(chainer);445 const g = arb.generate(mrngNoCall, expectedBiasFactor);446 const shrinks = arb.shrink(g.value, g.context);447 // Assert448 expect([...shrinks].map((v) => v.value)).toEqual([449 choiceShrink1Chained.value,450 choiceShrink2Chained.value,451 choiceShrink3Chained.value,452 shrinkChained1.value,453 shrinkChained2.value,454 ]); // shrink of source chained, then the one of original chained455 expect(generateChained).toHaveBeenNthCalledWith(4, expect.any(Random), expectedBiasFactor); // sub-sequent calls re-use the original bias456 expect(chainer).toHaveBeenCalledWith(choiceRoot.value); // original call457 expect(chainer).toHaveBeenCalledWith(shrinkRoot1.value); // chained during shrink458 expect(chainer).toHaveBeenCalledWith(shrinkRoot2.value); // chained during shrink459 expect(chainer).toHaveBeenCalledWith(shrinkRoot3.value); // chained during shrink460 });461 it('should properly shrink output of shrink by calling back shrink with the right context', () => {462 // Arrange463 const expectedBiasFactor = 48;464 const generate = jest.fn();465 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;466 const shrink = jest.fn();467 const choiceRoot = new Value(1, Symbol());468 generate.mockReturnValueOnce(choiceRoot);469 const shrinkRoot1 = new Value(10, Symbol());470 const shrinkRoot2 = new Value(11, Symbol()); // will not be iterated (getNthOrLast(0))471 const shrinkRoot3 = new Value(15, Symbol()); // will not be iterated (getNthOrLast(0))472 shrink.mockReturnValueOnce(Stream.of(shrinkRoot1, shrinkRoot2, shrinkRoot3));473 const shrinkRoot11 = new Value(310, Symbol());474 shrink.mockReturnValueOnce(Stream.of(shrinkRoot11));475 const generateChained = jest.fn();476 const canShrinkWithoutContextChained = jest.fn() as any as (value: unknown) => value is any;477 const shrinkChained = jest.fn();478 const choiceChained = new Value(50, Symbol());479 const choiceShrink1Chained = new Value(58, Symbol()); // chain will be called for each iterated sub-shrink of root (->10)480 const choiceShrink2Chained = new Value(57, Symbol()); // ->310 - 11 and 15 will not be retrieved (getNthOrLast(0))481 generateChained482 .mockReturnValueOnce(choiceChained)483 .mockReturnValueOnce(choiceShrink1Chained)484 .mockReturnValueOnce(choiceShrink2Chained);485 const shrinkChained1 = new Value(25, Symbol());486 const shrinkChained2 = new Value(51, Symbol());487 shrinkChained.mockReturnValueOnce(Stream.of(shrinkChained1, shrinkChained2));488 const shrinkChained11 = new Value(125, Symbol());489 const shrinkChained12 = new Value(151, Symbol());490 shrinkChained.mockReturnValueOnce(Stream.of(shrinkChained11, shrinkChained12));491 class MyNextArbitrary extends Arbitrary<any> {492 generate = generate;493 canShrinkWithoutContext = canShrinkWithoutContext;494 shrink = shrink;495 }496 class MyNextChainedArbitrary extends Arbitrary<any> {497 generate = generateChained;498 canShrinkWithoutContext = canShrinkWithoutContextChained;499 shrink = shrinkChained;500 }501 const chainer = jest.fn();502 chainer.mockReturnValue(new MyNextChainedArbitrary());503 // Act504 const arb = new MyNextArbitrary().chain(chainer);505 const g = arb.generate(mrngNoCall, expectedBiasFactor);506 const firstShrunkValue = arb.shrink(g.value, g.context).getNthOrLast(0)!;507 const shrinks = arb.shrink(firstShrunkValue.value, firstShrunkValue.context);508 // Assert509 expect([...shrinks].map((v) => v.value)).toEqual([510 choiceShrink2Chained.value,511 shrinkChained11.value,512 shrinkChained12.value,513 ]);514 expect(generateChained).toHaveBeenNthCalledWith(2, expect.any(Random), expectedBiasFactor); // sub-sequent calls re-use the original bias515 expect(chainer).toHaveBeenCalledWith(choiceRoot.value); // original call516 expect(chainer).toHaveBeenCalledWith(shrinkRoot1.value); // chained during shrink517 expect(chainer).not.toHaveBeenCalledWith(shrinkRoot2.value); // chained during shrink (skipped due to getNthOrLast(0)518 expect(chainer).not.toHaveBeenCalledWith(shrinkRoot3.value); // chained during shrink (skipped due to getNthOrLast(0)519 expect(chainer).toHaveBeenCalledWith(shrinkRoot11.value); // chained during second shrink (returned choiceShrink2Chained)520 });521 it('should stop shrink on source if it exhausted it once', () => {522 // Arrange523 const expectedBiasFactor = 48;524 const generate = jest.fn();525 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;526 const shrink = jest.fn();527 const choiceRoot = new Value(1, Symbol());528 generate.mockReturnValueOnce(choiceRoot);529 const shrinkRoot1 = new Value(10, Symbol());530 const shrinkRoot2 = new Value(11, Symbol());531 shrink.mockReturnValueOnce(Stream.of(shrinkRoot1, shrinkRoot2));532 const generateChained = jest.fn();533 const canShrinkWithoutContextChained = jest.fn() as any as (value: unknown) => value is any;534 const shrinkChained = jest.fn();535 const choiceChained = new Value(50, Symbol());536 const choiceShrink1Chained = new Value(58, Symbol());537 const choiceShrink2Chained = new Value(57, Symbol());538 generateChained539 .mockReturnValueOnce(choiceChained)540 .mockReturnValueOnce(choiceShrink1Chained)541 .mockReturnValueOnce(choiceShrink2Chained);542 const shrinkChained1 = new Value(25, Symbol());543 const shrinkChained2 = new Value(51, Symbol());544 shrinkChained.mockReturnValueOnce(Stream.of(shrinkChained1, shrinkChained2));545 const shrinkChained11 = new Value(125, Symbol());546 const shrinkChained12 = new Value(151, Symbol());547 shrinkChained.mockReturnValueOnce(Stream.of(shrinkChained11, shrinkChained12));548 class MyNextArbitrary extends Arbitrary<any> {549 generate = generate;550 canShrinkWithoutContext = canShrinkWithoutContext;551 shrink = shrink;552 }553 class MyNextChainedArbitrary extends Arbitrary<any> {554 generate = generateChained;555 canShrinkWithoutContext = canShrinkWithoutContextChained;556 shrink = shrinkChained;557 }558 const chainer = jest.fn();559 chainer.mockReturnValue(new MyNextChainedArbitrary());560 // Act561 const arb = new MyNextArbitrary().chain(chainer);562 const g = arb.generate(mrngNoCall, expectedBiasFactor);563 const shrunkValue = arb.shrink(g.value, g.context).getNthOrLast(2)!; // source will be exhausted it only declares two shrunk values564 const shrinks = arb.shrink(shrunkValue.value, shrunkValue.context);565 // Assert566 expect([...shrinks].map((v) => v.value)).toEqual([shrinkChained11.value, shrinkChained12.value]);567 expect(shrink).toHaveBeenCalledTimes(1); // not called back on second call to shrink568 });569 it('should always return false for canShrinkWithoutContext when not provided any unchain function', () => {570 // Arrange571 const generate = jest.fn();572 const canShrinkWithoutContext = jest.fn();573 const shrink = jest.fn();574 class MyNextArbitrary extends Arbitrary<any> {575 generate = generate;576 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;577 shrink = shrink;578 }579 // Act580 const arb = new MyNextArbitrary().chain(() => new MyNextArbitrary());581 const out = arb.canShrinkWithoutContext('');582 // Assert583 expect(out).toBe(false);584 expect(canShrinkWithoutContext).not.toHaveBeenCalled();585 });586 it('should return empty stream when shrinking without any context and not provided any unchainer function', () => {587 // Arrange588 const generate = jest.fn();589 const canShrinkWithoutContext = jest.fn();590 const shrink = jest.fn();591 class MyNextArbitrary extends Arbitrary<any> {592 generate = generate;593 canShrinkWithoutContext = canShrinkWithoutContext as any as (value: unknown) => value is any;594 shrink = shrink;595 }596 // Act597 const arb = new MyNextArbitrary().chain(() => new MyNextArbitrary());598 const shrinks = arb.shrink('', undefined);599 // Assert600 expect([...shrinks]).toHaveLength(0);601 expect(shrink).not.toHaveBeenCalled();602 });603 });604 describe('noShrink', () => {605 it('should simply return the original instance of Value on generate', () => {606 // Arrange607 const expectedBiasFactor = 48;608 const generate = jest.fn();609 const canShrinkWithoutContext = jest.fn() as any as (value: unknown) => value is any;610 const shrink = jest.fn();611 const choice = new Value(1, Symbol());612 generate.mockReturnValueOnce(choice);613 class MyNextArbitrary extends Arbitrary<any> {614 generate = generate;615 canShrinkWithoutContext = canShrinkWithoutContext;616 shrink = shrink;617 }618 // Act619 const arb = new MyNextArbitrary().noShrink();620 const g = arb.generate(mrngNoCall, expectedBiasFactor);621 // Assert622 expect(g).toBe(choice); // just returning the instance of the source arbitrary (including its context)623 expect(generate).toHaveBeenCalledWith(mrngNoCall, expectedBiasFactor);624 });625 it('should override default shrink with function returning an empty Stream', () => {626 // Arrange627 const shrink = jest.fn();628 class MyNextArbitrary extends Arbitrary<any> {629 generate(): Value<any> {630 throw new Error('Not implemented.');631 }632 canShrinkWithoutContext(value: unknown): value is any {633 throw new Error('Not implemented.');634 }635 shrink = shrink;636 }637 const fakeArbitrary: Arbitrary<any> = new MyNextArbitrary();638 const noShrinkArbitrary = fakeArbitrary.noShrink();639 // Act640 const out = noShrinkArbitrary.shrink(5, Symbol());641 // Assert642 expect([...out]).toHaveLength(0);643 expect(shrink).not.toHaveBeenCalled();644 });645 it('should return itself when called twice', () => {646 // Arrange647 class MyNextArbitrary extends Arbitrary<any> {648 generate(): Value<any> {649 throw new Error('Not implemented.');650 }651 canShrinkWithoutContext(value: unknown): value is any {652 throw new Error('Not implemented.');653 }654 shrink(): Stream<Value<any>> {655 throw new Error('Not implemented.');656 }657 }658 const fakeArbitrary: Arbitrary<any> = new MyNextArbitrary();659 // Act660 const firstNoShrink = fakeArbitrary.noShrink();661 const secondNoShrink = firstNoShrink.noShrink();662 // Assert663 expect(secondNoShrink).toBe(firstNoShrink);664 });665 });666 describe('noBias', () => {667 it('should override passed bias with undefined', () => {668 // Arrange669 const generate = jest.fn();670 class MyNextArbitrary extends Arbitrary<any> {671 generate = generate;672 canShrinkWithoutContext(value: unknown): value is any {673 throw new Error('Not implemented.');674 }675 shrink(): Stream<Value<any>> {676 throw new Error('Not implemented.');677 }678 }679 const fakeArbitrary: Arbitrary<any> = new MyNextArbitrary();680 const noBiasArbitrary = fakeArbitrary.noBias();681 // Act682 noBiasArbitrary.generate(mrngNoCall, 42);683 // Assert684 expect(generate).toHaveBeenCalledTimes(1);685 expect(generate).toHaveBeenCalledWith(mrngNoCall, undefined);686 });687 it('should return itself when called twice', () => {688 // Arrange689 class MyNextArbitrary extends Arbitrary<any> {690 generate(): Value<any> {691 throw new Error('Not implemented.');692 }693 canShrinkWithoutContext(value: unknown): value is any {694 throw new Error('Not implemented.');695 }696 shrink(): Stream<Value<any>> {697 throw new Error('Not implemented.');698 }699 }700 const fakeArbitrary: Arbitrary<any> = new MyNextArbitrary();701 // Act702 const firstNoBias = fakeArbitrary.noBias();703 const secondNoBias = firstNoBias.noBias();704 // Assert705 expect(secondNoBias).toBe(firstNoBias);706 });707 });...

Full Screen

Full Screen

Arbitrary.ts

Source:Arbitrary.ts Github

copy

Full Screen

1import { Random } from '../../../random/generator/Random';2import { Stream } from '../../../stream/Stream';3import { cloneMethod, hasCloneMethod } from '../../symbols';4import { Value } from './Value';5const safeObjectAssign = Object.assign;6/**7 * Abstract class able to generate values on type `T`8 *9 * The values generated by an instance of Arbitrary can be previewed - with {@link sample} - or classified - with {@link statistics}.10 *11 * @remarks Since 0.0.712 * @public13 */14export abstract class Arbitrary<T> {15 /**16 * Generate a value of type `T` along with its context (if any)17 * based on the provided random number generator18 *19 * @param mrng - Random number generator20 * @param biasFactor - If taken into account 1 value over biasFactor must be biased. Either integer value greater or equal to 2 (bias) or undefined (no bias)21 * @returns Random value of type `T` and its context22 *23 * @remarks Since 0.0.1 (return type changed in 3.0.0)24 */25 abstract generate(mrng: Random, biasFactor: number | undefined): Value<T>;26 /**27 * Check if a given value could be pass to `shrink` without providing any context.28 *29 * In general, `canShrinkWithoutContext` is not designed to be called for each `shrink` but rather on very special cases.30 * Its usage must be restricted to `canShrinkWithoutContext` or in the rare* contexts of a `shrink` method being called without31 * any context. In this ill-formed case of `shrink`, `canShrinkWithoutContext` could be used or called if needed.32 *33 * *we fall in that case when fast-check is asked to shrink a value that has been provided manually by the user,34 * in other words: a value not coming from a call to `generate` or a normal `shrink` with context.35 *36 * @param value - Value to be assessed37 * @returns `true` if and only if the value could have been generated by this instance38 *39 * @remarks Since 3.0.040 */41 abstract canShrinkWithoutContext(value: unknown): value is T;42 /**43 * Shrink a value of type `T`, may rely on the context previously provided to shrink efficiently44 *45 * Must never be called with possibly invalid values and no context without ensuring that such call is legal46 * by calling `canShrinkWithoutContext` first on the value.47 *48 * @param value - The value to shrink49 * @param context - Its associated context (the one returned by generate) or `undefined` if no context but `canShrinkWithoutContext(value) === true`50 * @returns Stream of shrinks for value based on context (if provided)51 *52 * @remarks Since 3.0.053 */54 abstract shrink(value: T, context: unknown | undefined): Stream<Value<T>>;55 /**56 * Create another arbitrary by filtering values against `predicate`57 *58 * All the values produced by the resulting arbitrary59 * satisfy `predicate(value) == true`60 *61 * Be aware that using filter may highly impact the time required to generate a valid entry62 *63 * @example64 * ```typescript65 * const integerGenerator: Arbitrary<number> = ...;66 * const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);67 * // new Arbitrary only keeps even values68 * ```69 *70 * @param refinement - Predicate, to test each produced element. Return true to keep the element, false otherwise71 * @returns New arbitrary filtered using predicate72 *73 * @remarks Since 1.23.074 */75 filter<U extends T>(refinement: (t: T) => t is U): Arbitrary<U>;76 /**77 * Create another arbitrary by filtering values against `predicate`78 *79 * All the values produced by the resulting arbitrary80 * satisfy `predicate(value) == true`81 *82 * Be aware that using filter may highly impact the time required to generate a valid entry83 *84 * @example85 * ```typescript86 * const integerGenerator: Arbitrary<number> = ...;87 * const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);88 * // new Arbitrary only keeps even values89 * ```90 *91 * @param predicate - Predicate, to test each produced element. Return true to keep the element, false otherwise92 * @returns New arbitrary filtered using predicate93 *94 * @remarks Since 0.0.195 */96 filter(predicate: (t: T) => boolean): Arbitrary<T>;97 filter<U extends T>(refinement: (t: T) => t is U): Arbitrary<U> {98 // eslint-disable-next-line @typescript-eslint/no-use-before-define99 return new FilterArbitrary(this, refinement);100 }101 /**102 * Create another arbitrary by mapping all produced values using the provided `mapper`103 * Values produced by the new arbitrary are the result of applying `mapper` value by value104 *105 * @example106 * ```typescript107 * const rgbChannels: Arbitrary<{r:number,g:number,b:number}> = ...;108 * const color: Arbitrary<string> = rgbChannels.map(ch => `#${(ch.r*65536 + ch.g*256 + ch.b).toString(16).padStart(6, '0')}`);109 * // transform an Arbitrary producing {r,g,b} integers into an Arbitrary of '#rrggbb'110 * ```111 *112 * @param mapper - Map function, to produce a new element based on an old one113 * @param unmapper - Optional unmap function, it will never be used except when shrinking user defined values. Must throw if value is not compatible (since 3.0.0)114 * @returns New arbitrary with mapped elements115 *116 * @remarks Since 0.0.1117 */118 map<U>(mapper: (t: T) => U, unmapper?: (possiblyU: unknown) => T): Arbitrary<U> {119 // eslint-disable-next-line @typescript-eslint/no-use-before-define120 return new MapArbitrary(this, mapper, unmapper);121 }122 /**123 * Create another arbitrary by mapping a value from a base Arbirary using the provided `fmapper`124 * Values produced by the new arbitrary are the result of the arbitrary generated by applying `fmapper` to a value125 * @example126 * ```typescript127 * const arrayAndLimitArbitrary = fc.nat().chain((c: number) => fc.tuple( fc.array(fc.nat(c)), fc.constant(c)));128 * ```129 *130 * @param chainer - Chain function, to produce a new Arbitrary using a value from another Arbitrary131 * @returns New arbitrary of new type132 *133 * @remarks Since 1.2.0134 */135 chain<U>(chainer: (t: T) => Arbitrary<U>): Arbitrary<U> {136 // eslint-disable-next-line @typescript-eslint/no-use-before-define137 return new ChainArbitrary(this, chainer);138 }139 /**140 * Create another Arbitrary with no shrink values141 *142 * @example143 * ```typescript144 * const dataGenerator: Arbitrary<string> = ...;145 * const unshrinkableDataGenerator: Arbitrary<string> = dataGenerator.noShrink();146 * // same values no shrink147 * ```148 *149 * @returns Create another arbitrary with no shrink values150 * @remarks Since 0.0.9151 */152 noShrink(): Arbitrary<T> {153 // eslint-disable-next-line @typescript-eslint/no-use-before-define154 return new NoShrinkArbitrary(this);155 }156 /**157 * Create another Arbitrary that cannot be biased158 *159 * @param freq - The biased version will be used one time over freq - if it exists160 * @remarks Since 1.1.0161 */162 noBias(): Arbitrary<T> {163 // eslint-disable-next-line @typescript-eslint/no-use-before-define164 return new NoBiasArbitrary(this);165 }166}167/** @internal */168type ChainArbitraryContext<T, U> = {169 originalBias: number | undefined;170 originalValue: T;171 originalContext: unknown;172 stoppedForOriginal: boolean;173 chainedArbitrary: Arbitrary<U>;174 chainedContext: unknown;175 clonedMrng: Random;176};177/** @internal */178class ChainArbitrary<T, U> extends Arbitrary<U> {179 constructor(readonly arb: Arbitrary<T>, readonly chainer: (t: T) => Arbitrary<U>) {180 super();181 }182 generate(mrng: Random, biasFactor: number | undefined): Value<U> {183 const clonedMrng = mrng.clone();184 const src = this.arb.generate(mrng, biasFactor);185 return this.valueChainer(src, mrng, clonedMrng, biasFactor);186 }187 canShrinkWithoutContext(value: unknown): value is U {188 // TODO Need unchainer189 return false;190 }191 shrink(value: U, context?: unknown): Stream<Value<U>> {192 if (this.isSafeContext(context)) {193 return (194 !context.stoppedForOriginal195 ? this.arb196 .shrink(context.originalValue, context.originalContext)197 .map((v) => this.valueChainer(v, context.clonedMrng.clone(), context.clonedMrng, context.originalBias))198 : Stream.nil<Value<U>>()199 ).join(200 context.chainedArbitrary.shrink(value, context.chainedContext).map((dst) => {201 // TODO - Move back to object spreading as soon as we bump support from es2017 to es2018+202 const newContext: ChainArbitraryContext<T, U> = safeObjectAssign(safeObjectAssign({}, context), {203 chainedContext: dst.context,204 stoppedForOriginal: true,205 });206 return new Value(dst.value_, newContext);207 })208 );209 }210 // TODO Need unchainer211 return Stream.nil();212 }213 private valueChainer(214 v: Value<T>,215 generateMrng: Random,216 clonedMrng: Random,217 biasFactor: number | undefined218 ): Value<U> {219 const chainedArbitrary = this.chainer(v.value_);220 const dst = chainedArbitrary.generate(generateMrng, biasFactor);221 const context: ChainArbitraryContext<T, U> = {222 originalBias: biasFactor,223 originalValue: v.value_,224 originalContext: v.context,225 stoppedForOriginal: false,226 chainedArbitrary,227 chainedContext: dst.context,228 clonedMrng,229 };230 return new Value(dst.value_, context);231 }232 private isSafeContext(context: unknown): context is ChainArbitraryContext<T, U> {233 return (234 context != null &&235 typeof context === 'object' &&236 'originalBias' in (context as any) &&237 'originalValue' in (context as any) &&238 'originalContext' in (context as any) &&239 'stoppedForOriginal' in (context as any) &&240 'chainedArbitrary' in (context as any) &&241 'chainedContext' in (context as any) &&242 'clonedMrng' in (context as any)243 );244 }245}246/** @internal */247type MapArbitraryContext<T> = {248 originalValue: T;249 originalContext: unknown;250};251/** @internal */252class MapArbitrary<T, U> extends Arbitrary<U> {253 readonly bindValueMapper: (v: Value<T>) => Value<U>;254 constructor(readonly arb: Arbitrary<T>, readonly mapper: (t: T) => U, readonly unmapper?: (possiblyU: unknown) => T) {255 super();256 this.bindValueMapper = (v: Value<T>): Value<U> => this.valueMapper(v);257 }258 generate(mrng: Random, biasFactor: number | undefined): Value<U> {259 const g = this.arb.generate(mrng, biasFactor);260 return this.valueMapper(g);261 }262 canShrinkWithoutContext(value: unknown): value is U {263 if (this.unmapper !== undefined) {264 try {265 const unmapped = this.unmapper(value);266 return this.arb.canShrinkWithoutContext(unmapped);267 } catch (_err) {268 return false;269 }270 }271 return false;272 }273 shrink(value: U, context?: unknown): Stream<Value<U>> {274 if (this.isSafeContext(context)) {275 return this.arb.shrink(context.originalValue, context.originalContext).map(this.bindValueMapper);276 }277 if (this.unmapper !== undefined) {278 const unmapped = this.unmapper(value);279 // As `shrink` should never be called without a valid context280 // except if `canShrinkWithoutContext` tells that the value was compatible with a shrink without any context281 // we can safely consider `this.arb.canShrinkWithoutContext(unmapped)` to be true at that point.282 return this.arb.shrink(unmapped, undefined).map(this.bindValueMapper);283 }284 return Stream.nil();285 }286 private mapperWithCloneIfNeeded(v: Value<T>): [U, T] {287 const sourceValue = v.value;288 const mappedValue = this.mapper(sourceValue);289 if (290 v.hasToBeCloned &&291 ((typeof mappedValue === 'object' && mappedValue !== null) || typeof mappedValue === 'function') &&292 Object.isExtensible(mappedValue) &&293 !hasCloneMethod(mappedValue)294 ) {295 // WARNING: In case the mapped value is not extensible it will not be extended296 Object.defineProperty(mappedValue, cloneMethod, { get: () => () => this.mapperWithCloneIfNeeded(v)[0] });297 }298 return [mappedValue, sourceValue];299 }300 private valueMapper(v: Value<T>): Value<U> {301 const [mappedValue, sourceValue] = this.mapperWithCloneIfNeeded(v);302 const context: MapArbitraryContext<T> = { originalValue: sourceValue, originalContext: v.context };303 return new Value(mappedValue, context);304 }305 private isSafeContext(context: unknown): context is MapArbitraryContext<T> {306 return (307 context != null &&308 typeof context === 'object' &&309 'originalValue' in (context as any) &&310 'originalContext' in (context as any)311 );312 }313}314/** @internal */315class FilterArbitrary<T, U extends T> extends Arbitrary<U> {316 readonly bindRefinementOnValue: (v: Value<T>) => v is Value<U>;317 constructor(readonly arb: Arbitrary<T>, readonly refinement: (t: T) => t is U) {318 super();319 this.bindRefinementOnValue = (v: Value<T>): v is Value<U> => this.refinementOnValue(v);320 }321 generate(mrng: Random, biasFactor: number | undefined): Value<U> {322 // eslint-disable-next-line no-constant-condition323 while (true) {324 const g = this.arb.generate(mrng, biasFactor);325 if (this.refinementOnValue(g)) {326 return g;327 }328 }329 }330 canShrinkWithoutContext(value: unknown): value is U {331 return this.arb.canShrinkWithoutContext(value) && this.refinement(value);332 }333 shrink(value: U, context?: unknown): Stream<Value<U>> {334 return this.arb.shrink(value, context).filter(this.bindRefinementOnValue);335 }336 private refinementOnValue(v: Value<T>): v is Value<U> {337 return this.refinement(v.value);338 }339}340/** @internal */341class NoShrinkArbitrary<T> extends Arbitrary<T> {342 constructor(readonly arb: Arbitrary<T>) {343 super();344 }345 generate(mrng: Random, biasFactor: number | undefined): Value<T> {346 return this.arb.generate(mrng, biasFactor);347 }348 canShrinkWithoutContext(value: unknown): value is T {349 return this.arb.canShrinkWithoutContext(value);350 }351 shrink(_value: T, _context?: unknown): Stream<Value<T>> {352 return Stream.nil();353 }354 noShrink() {355 return this;356 }357}358/** @internal */359class NoBiasArbitrary<T> extends Arbitrary<T> {360 constructor(readonly arb: Arbitrary<T>) {361 super();362 }363 generate(mrng: Random, _biasFactor: number | undefined): Value<T> {364 return this.arb.generate(mrng, undefined);365 }366 canShrinkWithoutContext(value: unknown): value is T {367 return this.arb.canShrinkWithoutContext(value);368 }369 shrink(value: T, context?: unknown): Stream<Value<T>> {370 return this.arb.shrink(value, context);371 }372 noBias() {373 return this;374 }375}376/**377 * Ensure an instance is an instance of Arbitrary378 * @param instance - The instance to be checked379 * @internal380 */381export function isArbitrary(instance: unknown): instance is Arbitrary<unknown> {382 return (383 typeof instance === 'object' &&384 instance !== null &&385 'generate' in instance &&386 'shrink' in instance &&387 'canShrinkWithoutContext' in instance388 );389}390/**391 * Ensure an instance is an instance of Arbitrary392 * @param instance - The instance to be checked393 * @internal394 */395export function assertIsArbitrary(instance: unknown): asserts instance is Arbitrary<unknown> {396 if (!isArbitrary(instance)) {397 throw new Error('Unexpected value received: not an instance of Arbitrary');398 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { noBiasArbitrary } = require('fast-check');2const arb = noBiasArbitrary(3 fc.integer({ min: 0, max: 1000 }),4 fc.integer({ min: 0, max: 1000 }),5 (a, b) => a + b6);7fc.assert(8 fc.property(arb, ([a, b]) => {9 return a + b >= 0;10 })11);12const { noBiasArbitrary } = require('fast-check/lib/arbitrary/noBiasArbitrary');13const arb = noBiasArbitrary(14 fc.integer({ min: 0, max: 1000 }),15 fc.integer({ min: 0, max: 1000 }),16 (a, b) => a + b17);18fc.assert(19 fc.property(arb, ([a, b]) => {20 return a + b >= 0;21 })22);23const { noBiasArbitrary } = require('fast-check/lib/arbitrary/noBiasArbitrary');24const arb = noBiasArbitrary(25 fc.integer({ min: 0, max: 1000 }),26 fc.integer({ min: 0, max: 1000 }),27 (a, b) => a + b28);29fc.assert(30 fc.property(arb, ([a, b]) => {31 return a + b >= 0;32 })33);34const { noBiasArbitrary } = require('fast-check/lib/arbitrary/noBiasArbitrary');35const arb = noBiasArbitrary(36 fc.integer({ min: 0, max: 1000 }),37 fc.integer({ min: 0, max: 1000 }),38 (a, b) => a + b39);40fc.assert(41 fc.property(arb, ([a, b]) => {42 return a + b >= 0;43 })44);45const { noBiasArbitrary } = require('fast-check/lib/arbitrary/noBiasArbitrary');46const arb = noBiasArbitrary(47 fc.integer({ min: 0, max: 1000 }),48 fc.integer({ min: 0, max: 1000 }),49 (a

Full Screen

Using AI Code Generation

copy

Full Screen

1var fc = require("fast-check");2var arb = fc.nat().noBiasArbitrary();3var gen = arb.generator;4console.log(gen.next());5console.log(gen.next());6console.log(gen.next());7console.log(gen.next());8console.log(gen.next());9var fc = require("fast-check");10var arb = fc.nat().noBiasArbitrary();11var gen = arb.generator;12console.log(gen.next());13console.log(gen.next());14console.log(gen.next());15console.log(gen.next());16console.log(gen.next());17var fc = require("fast-check");18var arb = fc.nat().noBiasArbitrary();19var gen = arb.generator;20console.log(gen.next());21console.log(gen.next());22console.log(gen.next());23console.log(gen.next());24console.log(gen.next());25var fc = require("fast-check");26var arb = fc.nat().noBiasArbitrary();27var gen = arb.generator;28console.log(gen.next());29console.log(gen.next());30console.log(gen.next());31console.log(gen.next());32console.log(gen.next());

Full Screen

Using AI Code Generation

copy

Full Screen

1const { noBiasArbitrary } = require('fast-check');2const { array } = require('fast-check/lib/arbitrary/array.js');3const { integer } = require('fast-check/lib/arbitrary/integer.js');4const myArbitrary = noBiasArbitrary(array(integer(), 1, 10));5myArbitrary.sample(10, 1000).then(console.log);6{7 "dependencies": {8 }9}

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { noBiasArbitrary } = require('fast-check-monorepo');3 .tuple(fc.integer(), fc.integer())4 .map(([x, y]) => x + y);5const myNoBiasArbitrary = noBiasArbitrary(myArbitrary);6fc.assert(7 fc.property(myNoBiasArbitrary, (x) => {8 console.log(x);9 return true;10 })11);12{13 "scripts": {14 },15 "dependencies": {16 }17}

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { noBiasArbitrary } = require('fast-check-monorepo');3const { myArbitrary } = require('./myArbitrary');4const myArbitraryWithNoBias = noBiasArbitrary(myArbitrary);5fc.assert(6 fc.property(myArbitraryWithNoBias, (x) => {7 console.log(x);8 return true;9 })10);11const fc = require('fast-check');12const myArbitrary = fc.oneof(13 fc.constant('a'),14 fc.constant('b'),15 fc.constant('c'),16 fc.constant('d'),17 fc.constant('e'),18 fc.constant('f'),19 fc.constant('g'),20 fc.constant('h'),21 fc.constant('i'),22 fc.constant('j'),23 fc.constant('k'),24 fc.constant('l'),25 fc.constant('m'),26 fc.constant('n'),27 fc.constant('o'),28 fc.constant('p'),29 fc.constant('q'),30 fc.constant('r'),31 fc.constant('s'),32 fc.constant('t'),33 fc.constant('u'),34 fc.constant('v'),35 fc.constant('w'),36 fc.constant('x'),37 fc.constant('y'),38 fc.constant('z')39);40module.exports = {41};42{43 "scripts": {44 },45 "dependencies": {46 }47}48 at Object.<anonymous> (C:\Users\...\my-project\test.js:7:3)49 at Module._compile (internal/modules/cjs/loader.js:1138:30)50 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)51 at Module.load (internal/modules/cjs/loader.js:985:

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check')2const { noBiasArbitrary } = require('fast-check-monorepo')3const { array } = fc4const arb = array(fc.integer(), 1, 10)5const arb2 = noBiasArbitrary(arb)6fc.assert(7 fc.property(arb2, (a) => {8 })

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