UNPKG

kuvio

Version:

Create string patterns and derive things from them, such as regexes

81 lines (79 loc) 2.65 kB
import "./chunk-QXAXOUZS.mjs"; // src/arbitrary.ts import * as fc from "fast-check"; var arbitraryFromAtom = (atom) => { switch (atom.kind) { case "anything": return fc.char(); case "character": return fc.constant(atom.char); case "characterClass": return (atom.exclude ? fc.integer({ min: 1, max: 65535 }).filter( (i) => atom.ranges.every(({ lower, upper }) => i > upper || i < lower) ) : fc.oneof( ...atom.ranges.map( ({ lower, upper }) => fc.integer({ min: lower, max: upper }) ) )).map((charCode) => String.fromCharCode(charCode)); case "subgroup": return arbitraryFromPattern(atom.subpattern); } }; var arbitraryFromQuantifiedAtom = (quantifiedAtom) => { switch (quantifiedAtom.kind) { case "star": return fc.array(arbitraryFromAtom(quantifiedAtom.atom)).map((strs) => strs.join("")); case "plus": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: 1 }).map((strs) => strs.join("")); case "question": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: 0, maxLength: 1 }).map((strs) => strs.join("")); case "exactly": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.count, maxLength: quantifiedAtom.count }).map((strs) => strs.join("")); case "between": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.min, maxLength: quantifiedAtom.max }).map((strs) => strs.join("")); case "minimum": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.min }).map((strs) => strs.join("")); } }; var arbitraryFromTerm = (term) => { switch (term.tag) { case "atom": return arbitraryFromAtom(term); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(term); } }; var chainConcatAll = (fcs) => fcs.length === 0 ? fc.constant("") : fcs[0].chain( (headStr) => chainConcatAll(fcs.slice(1)).map((tailStr) => headStr + tailStr) ); var arbitraryFromPattern = (pattern) => { switch (pattern.tag) { case "atom": return arbitraryFromAtom(pattern); case "disjunction": return fc.oneof( arbitraryFromPattern(pattern.left), arbitraryFromPattern(pattern.right) ); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(pattern); case "termSequence": return chainConcatAll(pattern.terms.map(arbitraryFromTerm)); } }; export { arbitraryFromAtom, arbitraryFromPattern, arbitraryFromQuantifiedAtom };