Skip to content

Commit

Permalink
Merge caf77a2 into 307e418
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Nov 3, 2018
2 parents 307e418 + caf77a2 commit b19aa1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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';
Expand All @@ -17,7 +18,7 @@ export function func<TArgs extends any[], TOut>(arb: Arbitrary<TOut>): 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 = () =>
'<function :: ' +
Expand Down
11 changes: 11 additions & 0 deletions test/unit/check/arbitrary/FunctionArbitrary.spec.ts
@@ -1,6 +1,8 @@
import * as assert from 'assert';
import * as fc from '../../../../lib/fast-check';

import { func, compareFunc, compareBooleanFunc } from '../../../../src/check/arbitrary/FunctionArbitrary';
import { context } from '../../../../src/check/arbitrary/ContextArbitrary';
import { integer } from '../../../../src/check/arbitrary/IntegerArbitrary';

import * as genericHelper from './generic/GenericArbitraryHelper';
Expand Down Expand Up @@ -31,6 +33,15 @@ describe('FunctionArbitrary', () => {
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),
Expand Down

0 comments on commit b19aa1a

Please sign in to comment.