File size: 2,051 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimitedShrinkArbitrary = void 0;
const Arbitrary_1 = require("../../check/arbitrary/definition/Arbitrary");
const Value_1 = require("../../check/arbitrary/definition/Value");
const Stream_1 = require("../../stream/Stream");
const ZipIterableIterators_1 = require("./helpers/ZipIterableIterators");
function* iotaFrom(startValue) {
    let value = startValue;
    while (true) {
        yield value;
        ++value;
    }
}
class LimitedShrinkArbitrary extends Arbitrary_1.Arbitrary {
    constructor(arb, maxShrinks) {
        super();
        this.arb = arb;
        this.maxShrinks = maxShrinks;
    }
    generate(mrng, biasFactor) {
        const value = this.arb.generate(mrng, biasFactor);
        return this.valueMapper(value, 0);
    }
    canShrinkWithoutContext(value) {
        return this.arb.canShrinkWithoutContext(value);
    }
    shrink(value, context) {
        if (this.isSafeContext(context)) {
            return this.safeShrink(value, context.originalContext, context.length);
        }
        return this.safeShrink(value, undefined, 0);
    }
    safeShrink(value, originalContext, currentLength) {
        const remaining = this.maxShrinks - currentLength;
        if (remaining <= 0) {
            return Stream_1.Stream.nil();
        }
        return new Stream_1.Stream((0, ZipIterableIterators_1.zipIterableIterators)(this.arb.shrink(value, originalContext), iotaFrom(currentLength + 1)))
            .take(remaining)
            .map((valueAndLength) => this.valueMapper(valueAndLength[0], valueAndLength[1]));
    }
    valueMapper(v, newLength) {
        const context = { originalContext: v.context, length: newLength };
        return new Value_1.Value(v.value, context);
    }
    isSafeContext(context) {
        return (context != null &&
            typeof context === 'object' &&
            'originalContext' in context &&
            'length' in context);
    }
}
exports.LimitedShrinkArbitrary = LimitedShrinkArbitrary;