fast-check
Version:
Property based testing framework for JavaScript (like QuickCheck)
45 lines (44 loc) • 2.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.patternsToStringMapper = patternsToStringMapper;
exports.patternsToStringUnmapperFor = patternsToStringUnmapperFor;
const MaxLengthFromMinLength_1 = require("../helpers/MaxLengthFromMinLength");
const globals_1 = require("../../../utils/globals");
function patternsToStringMapper(tab) {
return (0, globals_1.safeJoin)(tab, '');
}
function patternsToStringUnmapperFor(patternsArb, constraints) {
return function patternsToStringUnmapper(value) {
if (typeof value !== 'string') {
throw new Error('Unsupported value');
}
const minLength = constraints.minLength !== undefined ? constraints.minLength : 0;
const maxLength = constraints.maxLength !== undefined ? constraints.maxLength : MaxLengthFromMinLength_1.MaxLengthUpperBound;
if (value.length === 0) {
if (minLength > 0) {
throw new Error('Unable to unmap received string');
}
return [];
}
const stack = [{ endIndexChunks: 0, nextStartIndex: 1, chunks: [] }];
while (stack.length > 0) {
const last = (0, globals_1.safePop)(stack);
for (let index = last.nextStartIndex; index <= value.length; ++index) {
const chunk = (0, globals_1.safeSubstring)(value, last.endIndexChunks, index);
if (patternsArb.canShrinkWithoutContext(chunk)) {
const newChunks = [...last.chunks, chunk];
if (index === value.length) {
if (newChunks.length < minLength || newChunks.length > maxLength) {
break;
}
return newChunks;
}
(0, globals_1.safePush)(stack, { endIndexChunks: last.endIndexChunks, nextStartIndex: index + 1, chunks: last.chunks });
(0, globals_1.safePush)(stack, { endIndexChunks: index, nextStartIndex: index + 1, chunks: newChunks });
break;
}
}
}
throw new Error('Unable to unmap received string');
};
}
;