File size: 2,029 Bytes
96af7c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { hash } from '../utils/hash.js';
import { asyncStringify, asyncToStringMethod, stringify, toStringMethod } from '../utils/stringify.js';
import { cloneMethod, hasCloneMethod } from '../check/symbols.js';
import { array } from './array.js';
import { integer } from './integer.js';
import { noShrink } from './noShrink.js';
import { tuple } from './tuple.js';
import { escapeForMultilineComments } from './_internals/helpers/TextEscaper.js';
import { safeMap, safeSort } from '../utils/globals.js';
const safeObjectDefineProperties = Object.defineProperties;
const safeObjectKeys = Object.keys;
export function func(arb) {
    return tuple(array(arb, { minLength: 1 }), noShrink(integer())).map(([outs, seed]) => {
        const producer = () => {
            const recorded = {};
            const f = (...args) => {
                const repr = stringify(args);
                const val = outs[hash(`${seed}${repr}`) % outs.length];
                recorded[repr] = val;
                return hasCloneMethod(val) ? val[cloneMethod]() : val;
            };
            function prettyPrint(stringifiedOuts) {
                const seenValues = safeMap(safeMap(safeSort(safeObjectKeys(recorded)), (k) => `${k} => ${stringify(recorded[k])}`), (line) => `/* ${escapeForMultilineComments(line)} */`);
                return `function(...args) {
  // With hash and stringify coming from fast-check${seenValues.length !== 0 ? `\n  ${seenValues.join('\n  ')}` : ''}
  const outs = ${stringifiedOuts};
  return outs[hash('${seed}' + stringify(args)) % outs.length];
}`;
            }
            return safeObjectDefineProperties(f, {
                toString: { value: () => prettyPrint(stringify(outs)) },
                [toStringMethod]: { value: () => prettyPrint(stringify(outs)) },
                [asyncToStringMethod]: { value: async () => prettyPrint(await asyncStringify(outs)) },
                [cloneMethod]: { value: producer, configurable: true },
            });
        };
        return producer();
    });
}