diff --git a/src/check/arbitrary/FunctionArbitrary.ts b/src/check/arbitrary/FunctionArbitrary.ts index 7d5cd40f13d..d98a3ee3b71 100644 --- a/src/check/arbitrary/FunctionArbitrary.ts +++ b/src/check/arbitrary/FunctionArbitrary.ts @@ -1,5 +1,6 @@ import { hash } from '../../utils/hash'; import { stringify } from '../../utils/stringify'; +import { cloneMethod, hasCloneMethod } from '../symbols'; import { array } from './ArrayArbitrary'; import { Arbitrary } from './definition/Arbitrary'; import { integer } from './IntegerArbitrary'; @@ -17,7 +18,7 @@ export function func(arb: Arbitrary): Arbitrary const repr = stringify(args); const val = outs[hash(`${seed}${repr}`) % outs.length]; recorded[repr] = val; - return val; + return hasCloneMethod(val) ? val[cloneMethod]() : val; }; const toString = () => ' { return va1 === va2 && vb1 === vb2; }) )); + it('Should clone produced values if they implement [fc.cloneMethod]', () => { + const mrng = stubRng.mutable.fastincrease(0); + const f = func(context()).generate(mrng).value; + const ctx1 = f(0); + ctx1.log('Logging some stuff'); + const ctx2 = f(0); + assert.equal(ctx1.size(), 1); + assert.equal(ctx2.size(), 0); + }); describe('Is valid arbitrary', () => { genericHelper.isValidArbitrary(() => func<[number, number], number>(integer()), { isEqual: (f1, f2) => f1(0, 42) === f2(0, 42),