Best JavaScript code snippet using fast-check-monorepo
CloneArbitrary.spec.ts
Source:CloneArbitrary.spec.ts
1import fc from 'fast-check';2import { CloneArbitrary } from '../../../../src/arbitrary/_internals/CloneArbitrary';3import { Arbitrary } from '../../../../src/check/arbitrary/definition/Arbitrary';4import { Value } from '../../../../src/check/arbitrary/definition/Value';5import { cloneMethod, hasCloneMethod } from '../../../../src/check/symbols';6import { Random } from '../../../../src/random/generator/Random';7import { Stream } from '../../../../src/stream/Stream';8import {9 assertProduceValuesShrinkableWithoutContext,10 assertProduceCorrectValues,11 assertShrinkProducesSameValueWithoutInitialContext,12 assertShrinkProducesStrictlySmallerValue,13 assertProduceSameValueGivenSameSeed,14} from '../__test-helpers__/ArbitraryAssertions';15import { FakeIntegerArbitrary, fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';16import { fakeRandom } from '../__test-helpers__/RandomHelpers';17import { buildShrinkTree, renderTree, walkTree } from '../__test-helpers__/ShrinkTree';18describe('CloneArbitrary', () => {19 describe('generate', () => {20 it('should call generate numValues times on the passed arbitrary', () => {21 // Arrange22 const numValues = 3;23 const biasFactor = 48;24 const producedValue = Symbol();25 const { instance: mrng, clone } = fakeRandom();26 const { instance: mrngClone1 } = fakeRandom();27 const { instance: mrngClone2 } = fakeRandom();28 clone.mockReturnValueOnce(mrngClone1).mockReturnValueOnce(mrngClone2);29 const { instance: sourceArb, generate } = fakeArbitrary<symbol>();30 generate.mockReturnValue(new Value(producedValue, undefined));31 // Act32 const arb = new CloneArbitrary(sourceArb, numValues);33 const out = arb.generate(mrng, biasFactor);34 // Assert35 expect(out.value).toEqual([producedValue, producedValue, producedValue]);36 expect(generate).toHaveBeenCalledTimes(3);37 expect(generate).toHaveBeenCalledWith(mrngClone1, biasFactor);38 expect(generate).toHaveBeenCalledWith(mrngClone2, biasFactor);39 expect(generate).toHaveBeenLastCalledWith(mrng, biasFactor);40 });41 it.each`42 type | cloneable43 ${'non-cloneable'} | ${false}44 ${'cloneable'} | ${true}45 `('should produce a $type tuple when sub-value is $type', ({ cloneable }) => {46 // Arrange47 const numValues = 1;48 const { instance: mrng } = fakeRandom();49 const { instance: sourceArb, generate } = fakeArbitrary<unknown>();50 if (cloneable) generate.mockReturnValue(new Value({ [cloneMethod]: jest.fn() }, undefined));51 else generate.mockReturnValue(new Value({ m: jest.fn() }, undefined));52 // Act53 const arb = new CloneArbitrary(sourceArb, numValues);54 const out = arb.generate(mrng, numValues);55 // Assert56 expect(out.hasToBeCloned).toBe(cloneable);57 expect(hasCloneMethod(out.value)).toBe(cloneable);58 });59 });60 describe('canShrinkWithoutContext', () => {61 it('should return false if passed value does not have the right length', () =>62 fc.assert(63 fc.property(fc.nat({ max: 1000 }), fc.nat({ max: 1000 }), (numValues, numRequestedValues) => {64 // Arrange65 fc.pre(numValues !== numRequestedValues);66 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();67 // Act68 const arb = new CloneArbitrary(sourceArb, numValues);69 const out = arb.canShrinkWithoutContext([...Array(numRequestedValues)]);70 // Assert71 expect(out).toBe(false);72 expect(canShrinkWithoutContext).not.toHaveBeenCalled();73 })74 ));75 it('should return false if values are not equal regarding Object.is', () => {76 // Arrange77 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();78 // Act79 const arb = new CloneArbitrary(sourceArb, 2);80 const out = arb.canShrinkWithoutContext([{}, {}]);81 // Assert82 expect(out).toBe(false);83 expect(canShrinkWithoutContext).not.toHaveBeenCalled();84 });85 it.each`86 canShrinkWithoutContextValue87 ${true}88 ${false}89 `(90 'should ask sub-arbitrary whenever length is correct and children are equal regarding Object.is',91 ({ canShrinkWithoutContextValue }) => {92 // Arrange93 const value = {};94 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();95 canShrinkWithoutContext.mockReturnValue(canShrinkWithoutContextValue);96 // Act97 const arb = new CloneArbitrary(sourceArb, 2);98 const out = arb.canShrinkWithoutContext([value, value]);99 // Assert100 expect(out).toBe(canShrinkWithoutContextValue);101 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);102 expect(canShrinkWithoutContext).toHaveBeenCalledWith(value);103 }104 );105 });106 describe('shrink', () => {107 it('should shrink numValues times the value and zip the outputs together', () => {108 // Arrange109 const value = Symbol();110 const s1 = Symbol();111 const s2 = Symbol();112 const numValues = 3;113 const { instance: sourceArb, shrink } = fakeArbitrary<symbol>();114 shrink115 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)))116 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)))117 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)));118 // Act119 const arb = new CloneArbitrary(sourceArb, numValues);120 const shrinks = [...arb.shrink([value, value, value])];121 // Assert122 expect(shrinks.map((v) => v.value)).toEqual([123 [s1, s1, s1],124 [s2, s2, s2],125 ]);126 expect(shrink).toHaveBeenCalledTimes(3);127 expect(shrink).toHaveBeenCalledWith(value, undefined);128 });129 });130});131describe('CloneArbitrary (integration)', () => {132 type Extra = number;133 const extraParameters: fc.Arbitrary<Extra> = fc.nat({ max: 100 });134 const isCorrect = (value: number[], extra: Extra) =>135 Array.isArray(value) && value.length === extra && new Set(value).size <= 1;136 // Should never be called for extra = 0137 const isStrictlySmaller = (t1: number[], t2: number[]) => t1[0] < t2[0];138 const cloneBuilder = (extra: Extra) => new CloneArbitrary(new FakeIntegerArbitrary(), extra);139 it('should produce the same values given the same seed', () => {140 assertProduceSameValueGivenSameSeed(cloneBuilder, { extraParameters });141 });142 it('should only produce correct values', () => {143 assertProduceCorrectValues(cloneBuilder, isCorrect, { extraParameters });144 });145 it('should produce values seen as shrinkable without any context', () => {146 // Only when equal regarding Object.is147 assertProduceValuesShrinkableWithoutContext(cloneBuilder, { extraParameters });148 });149 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {150 assertShrinkProducesSameValueWithoutInitialContext(cloneBuilder, { extraParameters });151 });152 it('should preserve strictly smaller ordering in shrink (if underlyings do)', () => {153 assertShrinkProducesStrictlySmallerValue(cloneBuilder, isStrictlySmaller, { extraParameters });154 });155 it('should produce the right shrinking tree', () => {156 // Arrange157 const arb = new CloneArbitrary(new FirstArbitrary(), 5);158 const { instance: mrng } = fakeRandom();159 // Act160 const g = arb.generate(mrng, undefined);161 const renderedTree = renderTree(buildShrinkTree(arb, g)).join('\n');162 // Assert163 expect(renderedTree).toMatchInlineSnapshot(`164 "[4,4,4,4,4]165 â> [2,2,2,2,2]166 | â> [0,0,0,0,0]167 â> [3,3,3,3,3]168 â> [0,0,0,0,0]169 â> [1,1,1,1,1]"170 `);171 });172 it('should not re-use twice the same instance of cloneable', () => {173 // Arrange174 const alreadySeenCloneable = new Set<unknown>();175 const arb = new CloneArbitrary(new CloneableArbitrary(), 5);176 const { instance: mrng } = fakeRandom();177 // Act178 const g = arb.generate(mrng, undefined);179 const treeA = buildShrinkTree(arb, g);180 const treeB = buildShrinkTree(arb, g);181 // Assert182 walkTree(treeA, ([_first, cloneable, _second]) => {183 expect(alreadySeenCloneable.has(cloneable)).toBe(false);184 alreadySeenCloneable.add(cloneable);185 });186 walkTree(treeB, ([_first, cloneable, _second]) => {187 expect(alreadySeenCloneable.has(cloneable)).toBe(false);188 alreadySeenCloneable.add(cloneable);189 });190 });191});192// Helpers193const expectedFirst = 4;194class FirstArbitrary extends Arbitrary<number> {195 generate(_mrng: Random): Value<number> {196 return new Value(expectedFirst, { step: 2 });197 }198 canShrinkWithoutContext(_value: unknown): _value is number {199 throw new Error('No call expected in that scenario');200 }201 shrink(value: number, context?: unknown): Stream<Value<number>> {202 if (typeof context !== 'object' || context === null || !('step' in context)) {203 throw new Error('Invalid context for FirstArbitrary');204 }205 if (value <= 0) {206 return Stream.nil();207 }208 const currentStep = (context as { step: number }).step;209 const nextStep = currentStep + 1;210 return Stream.of(211 ...(value - currentStep >= 0 ? [new Value(value - currentStep, { step: nextStep })] : []),212 ...(value - currentStep + 1 >= 0 ? [new Value(value - currentStep + 1, { step: nextStep })] : [])213 );214 }215}216class CloneableArbitrary extends Arbitrary<number[]> {217 private instance() {218 return Object.defineProperty([], cloneMethod, { value: () => this.instance() });219 }220 generate(_mrng: Random): Value<number[]> {221 return new Value(this.instance(), { shrunkOnce: false });222 }223 canShrinkWithoutContext(_value: unknown): _value is number[] {224 throw new Error('No call expected in that scenario');225 }226 shrink(value: number[], context?: unknown): Stream<Value<number[]>> {227 if (typeof context !== 'object' || context === null || !('shrunkOnce' in context)) {228 throw new Error('Invalid context for CloneableArbitrary');229 }230 const safeContext = context as { shrunkOnce: boolean };231 if (safeContext.shrunkOnce) {232 return Stream.nil();233 }234 return Stream.of(new Value(this.instance(), { shrunkOnce: true }));235 }...
fluent-query.ts
Source:fluent-query.ts
...26 }27 }28 //region IQueryable29 public select<TValue extends ColumnType>(column: DataSetColumn<TEntity, TValue>): IQueryable<TEntity> {30 const newBuilder = this.cloneBuilder().select(Column.of(column));31 return this.updateQuery(newBuilder);32 }33 public where<TValue extends ColumnType>(column: DataSetColumn<TEntity, TValue>): IWhereFilter<TEntity, TValue> {34 return new BasicWhereFilter(this, Column.of(column));35 }36 // TODO: If TypeScript ever allows type guards on generics, create an overload where() instead37 public location(column: (type: TEntity) => Location): ILocationFilter<TEntity> {38 return new LocationFilter(this, Column.of(column));39 }40 // TODO: If TypeScript ever allows type guards on generics, create an overload where() instead41 public geometry(column: (type: TEntity) => Geometry): IGeometryFilter<TEntity> {42 return new GeometryFilter(this, Column.of(column));43 }44 public limit(records: number): IQueryable<TEntity> {45 const newBuilder = this.cloneBuilder().limit(records);46 return this.updateQuery(newBuilder);47 }48 public offset(records: number): IQueryable<TEntity> {49 const newBuilder = this.cloneBuilder().offset(records);50 return this.updateQuery(newBuilder);51 }52 public orderBy<TValue extends ColumnType>(column: DataSetColumn<TEntity, TValue>, descending?: boolean): IQueryable<TEntity> {53 const newBuilder = this.cloneBuilder().orderBy(new OrderColumn(Column.of(column).Name, descending));54 return this.updateQuery(newBuilder);55 }56 public observable(): Observable<TEntity[]> {57 return this.sodaResource.Context.Client.getResource(this.sodaResource, this);58 }59 public toString(): string {60 return this.queryBuilder.getQuery().toString();61 }62 //endregion63 //region IFilteredQueryable64 public and<TValue extends ColumnType>(column: DataSetColumn<TEntity, TValue>): IWhereFilter<TEntity, TValue> {65 return new BasicWhereFilter(this, Column.of(column), new WhereOperator(Operator.And));66 }67 public or<TValue extends ColumnType>(column: DataSetColumn<TEntity, TValue>): IWhereFilter<TEntity, TValue> {68 return new BasicWhereFilter(this, Column.of(column), new WhereOperator(Operator.Or));69 }70 //endregion71 //region IInternalQuery72 public addFilter(...filters: WhereFilterType[]): IFilteredQueryable<TEntity> {73 const newBuilder = this.cloneBuilder().filter(...filters);74 return new FluentQuery<TEntity>(this.sodaResource, newBuilder);75 }76 //endregion77 private updateQuery(newBuilder: SoqlQueryBuilder): IQueryable<TEntity> {78 return new FluentQuery<TEntity>(this.sodaResource, newBuilder);79 }80 private cloneBuilder() {81 return this.queryBuilder.clone();82 }...
strip.analyzer.ts
Source:strip.analyzer.ts
...14 const patternWidth = pattern.__bounds.width;15 const count = model.count;16 const spacing = (helper.width() - patternWidth * count) / (count + 1);17 // Create clones18 const cloneBuilder = helper.cloneBuilder(pattern);19 const x0 = spacing;20 for (let i = 0; i < count; i++) {21 const xPosition = x0 + (spacing + patternWidth) * i;22 cloneBuilder.addClone(xPosition);23 }24 return cloneBuilder.build();25}26function createClonePattern(27 model: ChargeStrip,28 helper: StripHelper29): StripItem {30 const bounds = helper.rotation.rotatedBounds;31 const stripByGroup = getStripByGroup(model);32 // n part by strip + (n+1) empty part...
Using AI Code Generation
1const fc = require('fast-check');2const { cloneBuilder } = require('fast-check/lib/check/arbitrary/definition/CloneArbitraryBuilder');3const { cloneMethod } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');4const { cloneMethod: cloneMethod2 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');5const { cloneMethod: cloneMethod3 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');6const { cloneMethod: cloneMethod4 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');7const { cloneMethod: cloneMethod5 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');8const { cloneMethod: cloneMethod6 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');9const { cloneMethod: cloneMethod7 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');10const { cloneMethod: cloneMethod8 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');11const { cloneMethod: cloneMethod9 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');12const { cloneMethod: cloneMethod10 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');13const { cloneMethod: cloneMethod11 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');14const { cloneMethod: cloneMethod12 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');15const { cloneMethod: cloneMethod13 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');16const { cloneMethod: cloneMethod14 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');17const { cloneMethod: cloneMethod15 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');18const { cloneMethod: cloneMethod16 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');19const { cloneMethod: cloneMethod17 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');20const { cloneMethod: cloneMethod18 } = require('fast-check/lib/check/arbitrary/definition/CloneArbitrary');21const { cloneMethod: cloneMethod19 } = require('fast-check/lib/check/arbitrary/definition/Clone
Using AI Code Generation
1var fc = require('fast-check');2var cloneBuilder = require('fast-check-monorepo').cloneBuilder;3var cloneBuilder2 = require('fast-check-monorepo').cloneBuilder;4var arb = fc.integer(0, 1000);5var arb2 = cloneBuilder(arb);6var arb3 = cloneBuilder2(arb);7fc.assert(8 fc.property(arb2, arb3, function (v1, v2) {9 return v1 == v2;10 })11);12var fc = require('fast-check');13var cloneBuilder = require('fast-check-monorepo').cloneBuilder;14var arb = fc.integer(0, 1000);15var arb2 = cloneBuilder(arb);16var arb3 = cloneBuilder(arb);17fc.assert(18 fc.property(arb2, arb3, function (v1, v2) {19 return v1 == v2;20 })21);22var arb = fc.integer(0, 1000);23var builder = fc.integer;24var arb2 = cloneBuilder(builder, 0, 1000);25var arb3 = cloneBuilder(builder, 0, 1000);26fc.assert(27 fc.property(arb2, arb3, function (v1, v2) {28 return v1 == v2;29 })30);
Using AI Code Generation
1const fc = require('fast-check');2const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;3const arb = fc.integer(1, 100);4const cloneArb = cloneBuilder(arb);5console.log('cloneArb: ', cloneArb);6const fc = require('fast-check');7const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;8const arb = fc.integer(1, 100);9const cloneArb = cloneBuilder(arb);10console.log('cloneArb: ', cloneArb);11const fc = require('fast-check');12const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;13const arb = fc.integer(1, 100);14const cloneArb = cloneBuilder(arb);15console.log('cloneArb: ', cloneArb);16const fc = require('fast-check');17const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;18const arb = fc.integer(1, 100);19const cloneArb = cloneBuilder(arb);20console.log('cloneArb: ', cloneArb);21const fc = require('fast-check');22const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;23const arb = fc.integer(1, 100);24const cloneArb = cloneBuilder(arb);25console.log('cloneArb: ', cloneArb);26const fc = require('fast-check');27const cloneBuilder = require('fast-check/lib/arbitrary/_internals/CloneArbitraryBuilder.js').cloneBuilder;28const arb = fc.integer(1, 100);29const cloneArb = cloneBuilder(arb);30console.log('cloneArb: ', cloneArb);
Using AI Code Generation
1const cloneBuilder = require('fast-check-monorepo').cloneBuilder;2const clone = cloneBuilder();3 .withArbitrary(() => 1)4 .withArbitrary(() => 2)5 .build();6const cloneBuilder = require('fast-check-monorepo').cloneBuilder;7const clone = cloneBuilder();8 .withArbitrary(() => 1)9 .withArbitrary(() => 2)10 .build();11const cloneBuilder = require('fast-check-monorepo').cloneBuilder;12const clone = cloneBuilder();13 .withArbitrary(() => 1)14 .withArbitrary(() => 2)15 .build();16const cloneBuilder = require('fast-check-monorepo').cloneBuilder;17const clone = cloneBuilder();18 .withArbitrary(() => 1)19 .withArbitrary(() => 2)20 .build();21const cloneBuilder = require('fast-check-monorepo').cloneBuilder;22const clone = cloneBuilder();23 .withArbitrary(() => 1)24 .withArbitrary(() => 2)25 .build();26const cloneBuilder = require('fast-check-monorepo').cloneBuilder;27const clone = cloneBuilder();28 .withArbitrary(() => 1)29 .withArbitrary(() => 2)30 .build();31const cloneBuilder = require('fast-check-monorepo').cloneBuilder;32const clone = cloneBuilder();33 .withArbitrary(() => 1)
Using AI Code Generation
1import { cloneBuilder } from 'fast-check';2import { clone } from 'ramda';3const arb = cloneBuilder(clone);4import { cloneBuilder } from 'fast-check';5import { clone } from 'ramda';6const arb = cloneBuilder(clone);7import { cloneBuilder } from 'fast-check';8import { clone } from 'ramda';9const arb = cloneBuilder(clone);10import { cloneBuilder } from 'fast-check';11import { clone } from 'ramda';12const arb = cloneBuilder(clone);13import { cloneBuilder } from 'fast-check';14import { clone } from 'ramda';15const arb = cloneBuilder(clone);16import { cloneBuilder } from 'fast-check';17import { clone } from 'ramda';18const arb = cloneBuilder(clone);19import { cloneBuilder } from 'fast-check';20import { clone } from 'ramda';21const arb = cloneBuilder(clone);22import { cloneBuilder } from 'fast-check';23import { clone } from 'ramda';24const arb = cloneBuilder(clone);
Using AI Code Generation
1const { cloneBuilder } = require('fast-check');2const { builder } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');3const builder2 = cloneBuilder(builder);4const ArrayArbitrary = (0, _genericTuple.GenericTupleArbitrary)((0, _genericTuple.genericTuple)([...builders]));5TypeError: (0 , _genericTuple.genericTuple) is not a function6 at Object.<anonymous> (/home/.../fast-check-monorepo/node_modules/fast-check/lib/check/arbitrary/ArrayArbitrary.js:7:66)7 at Module._compile (internal/modules/cjs/loader.js:956:30)8 at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)9 at Module.load (internal/modules/cjs/loader.js:812:32)10 at Function.Module._load (internal/modules/cjs/loader.js:724:14)11 at Module.require (internal/modules/cjs/loader.js:849:19)12 at require (internal/modules/cjs/helpers.js:74:18)13 at Object.<anonymous> (/home/.../fast-check-monorepo/node_modules/fast-check/lib/check/arbitrary/ArrayArbitrary.js:1:1)14 at Module._compile (internal/modules/cjs/loader.js:956:30)15 at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)16I'm not sure to understand the use case. You want to clone the builder (and so the Arbitrary) to use it in another file?
Using AI Code Generation
1const { cloneBuilder } = require("fast-check");2const { build } = require("fast-check");3const builder = build(4 (s) => s.length < 5,5 (s) => s + "!"6);7const builder2 = cloneBuilder(builder);8builder2.seed = 123;9builder2.path = "test2";10console.log(builder2.generate());11console.log(builder.generate());12const { cloneBuilder } = require("fast-check");13const { build } = require("fast-check");14const builder = build(15 (s) => s.length < 5,16 (s) => s + "!"17);18const builder2 = cloneBuilder(builder);19builder2.seed = 123;20builder2.path = "test2";21console.log(builder2.generate());22console.log(builder.generate());23const { cloneBuilder } = require("fast-check");24const { build } = require("fast-check");25const builder = build(26 (s) => s.length <
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!