File size: 7,135 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Arbitrary = void 0;
exports.isArbitrary = isArbitrary;
exports.assertIsArbitrary = assertIsArbitrary;
const Stream_1 = require("../../../stream/Stream");
const symbols_1 = require("../../symbols");
const Value_1 = require("./Value");
const safeObjectAssign = Object.assign;
class Arbitrary {
filter(refinement) {
return new FilterArbitrary(this, refinement);
}
map(mapper, unmapper) {
return new MapArbitrary(this, mapper, unmapper);
}
chain(chainer) {
return new ChainArbitrary(this, chainer);
}
noShrink() {
return new NoShrinkArbitrary(this);
}
noBias() {
return new NoBiasArbitrary(this);
}
}
exports.Arbitrary = Arbitrary;
class ChainArbitrary extends Arbitrary {
constructor(arb, chainer) {
super();
this.arb = arb;
this.chainer = chainer;
}
generate(mrng, biasFactor) {
const clonedMrng = mrng.clone();
const src = this.arb.generate(mrng, biasFactor);
return this.valueChainer(src, mrng, clonedMrng, biasFactor);
}
canShrinkWithoutContext(value) {
return false;
}
shrink(value, context) {
if (this.isSafeContext(context)) {
return (!context.stoppedForOriginal
? this.arb
.shrink(context.originalValue, context.originalContext)
.map((v) => this.valueChainer(v, context.clonedMrng.clone(), context.clonedMrng, context.originalBias))
: Stream_1.Stream.nil()).join(context.chainedArbitrary.shrink(value, context.chainedContext).map((dst) => {
const newContext = safeObjectAssign(safeObjectAssign({}, context), {
chainedContext: dst.context,
stoppedForOriginal: true,
});
return new Value_1.Value(dst.value_, newContext);
}));
}
return Stream_1.Stream.nil();
}
valueChainer(v, generateMrng, clonedMrng, biasFactor) {
const chainedArbitrary = this.chainer(v.value_);
const dst = chainedArbitrary.generate(generateMrng, biasFactor);
const context = {
originalBias: biasFactor,
originalValue: v.value_,
originalContext: v.context,
stoppedForOriginal: false,
chainedArbitrary,
chainedContext: dst.context,
clonedMrng,
};
return new Value_1.Value(dst.value_, context);
}
isSafeContext(context) {
return (context != null &&
typeof context === 'object' &&
'originalBias' in context &&
'originalValue' in context &&
'originalContext' in context &&
'stoppedForOriginal' in context &&
'chainedArbitrary' in context &&
'chainedContext' in context &&
'clonedMrng' in context);
}
}
class MapArbitrary extends Arbitrary {
constructor(arb, mapper, unmapper) {
super();
this.arb = arb;
this.mapper = mapper;
this.unmapper = unmapper;
this.bindValueMapper = (v) => this.valueMapper(v);
}
generate(mrng, biasFactor) {
const g = this.arb.generate(mrng, biasFactor);
return this.valueMapper(g);
}
canShrinkWithoutContext(value) {
if (this.unmapper !== undefined) {
try {
const unmapped = this.unmapper(value);
return this.arb.canShrinkWithoutContext(unmapped);
}
catch (_err) {
return false;
}
}
return false;
}
shrink(value, context) {
if (this.isSafeContext(context)) {
return this.arb.shrink(context.originalValue, context.originalContext).map(this.bindValueMapper);
}
if (this.unmapper !== undefined) {
const unmapped = this.unmapper(value);
return this.arb.shrink(unmapped, undefined).map(this.bindValueMapper);
}
return Stream_1.Stream.nil();
}
mapperWithCloneIfNeeded(v) {
const sourceValue = v.value;
const mappedValue = this.mapper(sourceValue);
if (v.hasToBeCloned &&
((typeof mappedValue === 'object' && mappedValue !== null) || typeof mappedValue === 'function') &&
Object.isExtensible(mappedValue) &&
!(0, symbols_1.hasCloneMethod)(mappedValue)) {
Object.defineProperty(mappedValue, symbols_1.cloneMethod, { get: () => () => this.mapperWithCloneIfNeeded(v)[0] });
}
return [mappedValue, sourceValue];
}
valueMapper(v) {
const [mappedValue, sourceValue] = this.mapperWithCloneIfNeeded(v);
const context = { originalValue: sourceValue, originalContext: v.context };
return new Value_1.Value(mappedValue, context);
}
isSafeContext(context) {
return (context != null &&
typeof context === 'object' &&
'originalValue' in context &&
'originalContext' in context);
}
}
class FilterArbitrary extends Arbitrary {
constructor(arb, refinement) {
super();
this.arb = arb;
this.refinement = refinement;
this.bindRefinementOnValue = (v) => this.refinementOnValue(v);
}
generate(mrng, biasFactor) {
while (true) {
const g = this.arb.generate(mrng, biasFactor);
if (this.refinementOnValue(g)) {
return g;
}
}
}
canShrinkWithoutContext(value) {
return this.arb.canShrinkWithoutContext(value) && this.refinement(value);
}
shrink(value, context) {
return this.arb.shrink(value, context).filter(this.bindRefinementOnValue);
}
refinementOnValue(v) {
return this.refinement(v.value);
}
}
class NoShrinkArbitrary extends Arbitrary {
constructor(arb) {
super();
this.arb = arb;
}
generate(mrng, biasFactor) {
return this.arb.generate(mrng, biasFactor);
}
canShrinkWithoutContext(value) {
return this.arb.canShrinkWithoutContext(value);
}
shrink(_value, _context) {
return Stream_1.Stream.nil();
}
noShrink() {
return this;
}
}
class NoBiasArbitrary extends Arbitrary {
constructor(arb) {
super();
this.arb = arb;
}
generate(mrng, _biasFactor) {
return this.arb.generate(mrng, undefined);
}
canShrinkWithoutContext(value) {
return this.arb.canShrinkWithoutContext(value);
}
shrink(value, context) {
return this.arb.shrink(value, context);
}
noBias() {
return this;
}
}
function isArbitrary(instance) {
return (typeof instance === 'object' &&
instance !== null &&
'generate' in instance &&
'shrink' in instance &&
'canShrinkWithoutContext' in instance);
}
function assertIsArbitrary(instance) {
if (!isArbitrary(instance)) {
throw new Error('Unexpected value received: not an instance of Arbitrary');
}
}
|