How to use shrunkValue 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

04.js

Source:04.js Github

copy

Full Screen

1/* Question 04 (stretch)2This is a STRETCH QUESTION.3Given a size in bits convert it to relevant size in B/KB/MB/GB/TB. Round your answers to two decimal places at most. Use base 10 for sizes.4- 1 B5- 1 kB == 1000 B6- 1 MB == 1000 kB7- 1 GB == 1000 MB8- 1 TB == 1000 GB9More info on these in case you are curious:10https://en.wikipedia.org/wiki/Byte#Unit_symbol11Examples:12- filesize(1) => "1B"13- filesize(1000) => "1kB"14- filesize(1000000) => "1MB"15- filesize(1500000) => "1.5MB"16- filesize(1250000000) => "1.25GB"17- filesize(9000000000000) => "9TB"18*/19// 1 B20// 10 B21// 100 B22// 1000 kB23const units = ["B", "B", "B", "kB", "kB", "kB", "MB", "MB", "MB"];24const someSize = 123456;25const length = String(someSize).length;26const chosenUnit = units[length - 1];27const shrunkSize = someSize / 10 ** (length - 1);28const roundedShrunkSize = Math.round(shrunkSize * 100) / 100;29console.log(length, chosenUnit, shrunkSize, roundedShrunkSize);30const shrinkAndRound = (value) => {31 const length = String(value).length;32 const shrunkValue = value / 10 ** (length - 1);33 return Math.round(shrunkValue * 100) / 100;34};35const fileSize = (size) => {36 if (size < 1000) {37 const unit = "B";38 return shrinkAndRound(size) + unit;39 }40 if (size < 1000000) {41 const unit = "kB";42 return shrinkAndRound(size) + unit;43 }44 if (size < 1000000000) {45 const unit = "MB";46 return shrinkAndRound(size) + unit;47 }48 if (size < 1000000000000) {49 const unit = "GB";50 return shrinkAndRound(size) + unit;51 }52 if (size < 1000000000000000) {53 const unit = "TB";54 return shrinkAndRound(size) + unit;55 }56};57const betterFileSizeMaybeQuestionMark = (originalSize) => {58 const unitList = [];59 for (unit of ["B", "kB", "MB", "GB", "TB"]) {60 unitList.push(unit);61 unitList.push(unit);62 unitList.push(unit);63 }64 let size = originalSize;65 let howManyTimes = 0;66 while (size > 1) {67 size = size / 10;68 howManyTimes++;69 }70 console.log(size, howManyTimes, originalSize, unitList);71 return size * 10 + unitList[howManyTimes - 1];72};73const result = betterFileSizeMaybeQuestionMark(123456);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { shrunkValue } from 'fast-check';2import { shrunkValue } from 'fast-check-monorepo';3import { shrunkValue } from 'fast-check-monorepo/lib/types';4import { shrunkValue } from 'fast-check-monorepo/types';5import { shrunkValue } from 'fast-check-monorepo/types/lib';6import { shrunkValue } from 'fast-check-monorepo/types/lib/types';7import { shrunkValue } from 'fast-check-monorepo/types/types';8import { shrunkValue } from 'fast-check-monorepo/types/types/lib';9import { shrunkValue } from 'fast-check-monorepo/types/types/lib/types';10import { shrunkValue } from 'fast-check-monorepo/types/types/types';11import { shrunkValue } from 'fast-check-monorepo/types/types/types/lib';12import { shrunkValue } from 'fast-check-monorepo/types/types/types/lib/types';13import { shrunkValue } from 'fast-check-monorepo/types/types/types/types';14import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/lib';15import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/lib/types';16import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/types';17import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/types/lib';18import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/types/lib/types';19import { shrunkValue } from 'fast-check-monorepo/types/types/types/types/types/types';20shrunkValue(42

Full Screen

Using AI Code Generation

copy

Full Screen

1import { shrunkValue } from 'fast-check';2const shrunkValue = require('fast-check').shrunkValue;3import { fc } from 'fast-check';4const fc = require('fast-check');5import { fc } from 'fast-check';6const fc = require('fast-check');

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check')2const value = fc.character().shrinkableFor('a').value3console.log(value)4const fc = require('fast-check')5const value = fc.character().shrinkableFor('a').shrink().value6console.log(value)7const fc = require('fast-check')8const value = fc.character().shrinkableFor('a').shrink().shrink().value9console.log(value)10const fc = require('fast-check')11const value = fc.character().shrinkableFor('a').shrink().shrink().shrink().value12console.log(value)13const fc = require('fast-check')14const value = fc.character().shrinkableFor('a').shrink().shrink().shrink().shrink().value15console.log(value)16const fc = require('fast-check')17const value = fc.character().shrinkableFor('a').shrink().shrink().shrink().shrink().shrink().value18console.log(value)19const fc = require('fast-check')20const value = fc.character().shrinkableFor('a').shrink().shrink().shrink().shrink().shrink().shrink().value21console.log(value)

Full Screen

Using AI Code Generation

copy

Full Screen

1const {shrunkValue} = require('fast-check');2const value = shrunkValue(10, 0.5);3console.log(value);4const {shrunkValue} = require('fast-check/lib/check/arbitrary/definition/ShrunkValue');5const value = shrunkValue(10, 0.5);6console.log(value);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { shrunkValue } = require('fast-check')2const { create } = require('fast-check-monorepo')3const arb = create({4})5console.log(shrunkValue(arb))6{7 "dependencies": {8 }9}

Full Screen

Using AI Code Generation

copy

Full Screen

1import * as fc from 'fast-check';2const unicodeChar = '🚀';3const unicodeCharString = unicodeChar.repeat(10);4const unicodeCharArb = fc.stringOf(fc.char(unicodeChar));5fc.assert(6 fc.property(unicodeCharArb, (unicodeCharString) => {7 const shrunkValue = fc.shrink(unicodeCharArb, unicodeCharString)8 return shrunkValue.includes(unicodeCharString);9 })10);

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