fast-check
Version:
Property based testing framework for JavaScript (like QuickCheck)
41 lines (40 loc) • 1.94 kB
JavaScript
import { MaxLengthUpperBound } from '../helpers/MaxLengthFromMinLength.js';
import { safeJoin, safePop, safePush, safeSubstring } from '../../../utils/globals.js';
export function patternsToStringMapper(tab) {
return safeJoin(tab, '');
}
export 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 : 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 = safePop(stack);
for (let index = last.nextStartIndex; index <= value.length; ++index) {
const chunk = 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;
}
safePush(stack, { endIndexChunks: last.endIndexChunks, nextStartIndex: index + 1, chunks: last.chunks });
safePush(stack, { endIndexChunks: index, nextStartIndex: index + 1, chunks: newChunks });
break;
}
}
}
throw new Error('Unable to unmap received string');
};
}