cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
78 lines (77 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
var bracketDict = /* @__PURE__ */ ((bracketDict2) => {
bracketDict2["["] = "]";
return bracketDict2;
})(bracketDict || {});
function getNextKeyDef(text, options) {
const { type, descriptor, consumedLength } = readNextDescriptor(text);
const keyDef = options.keyboardMap.find((def) => {
var _a;
if (type === "[") {
return ((_a = def.code) == null ? void 0 : _a.toLowerCase()) === descriptor.toLowerCase();
}
return def.hex === descriptor;
}) ?? {
code: descriptor,
hex: "Unknown"
};
return {
keyDef,
consumedLength
};
}
function readNextDescriptor(text) {
let pos = 0;
const startBracket = text[pos] in bracketDict ? text[pos] : "";
pos += startBracket.length;
const startBracketRepeated = startBracket ? text.match(new RegExp(`^\\${startBracket}+`))[0].length : 0;
const isEscapedChar = startBracketRepeated === 2;
const type = isEscapedChar ? "" : startBracket;
return {
type,
...type === "" ? readPrintableChar(text, pos) : readTag(text, pos, type)
};
}
function readPrintableChar(text, pos) {
const descriptor = text[pos];
assertDescriptor(descriptor, text, pos);
pos += descriptor.length;
return {
consumedLength: pos,
descriptor,
releasePrevious: false,
releaseSelf: true,
repeat: 1
};
}
function readTag(text, pos, startBracket) {
var _a;
const descriptor = (_a = text.slice(pos).match(/^\w+/)) == null ? void 0 : _a[0];
assertDescriptor(descriptor, text, pos);
pos += descriptor.length;
const expectedEndBracket = bracketDict[startBracket];
const endBracket = text[pos] === expectedEndBracket ? expectedEndBracket : "";
if (!endBracket) {
throw new Error(
getErrorMessage(`"${expectedEndBracket}"`, text[pos], text)
);
}
pos += endBracket.length;
return {
consumedLength: pos,
descriptor
};
}
function assertDescriptor(descriptor, text, pos) {
if (!descriptor) {
throw new Error(getErrorMessage("key descriptor", text[pos], text));
}
}
function getErrorMessage(expected, found, text) {
return `Expected ${expected} but found "${found ?? ""}" in "${text}"
See https://github.com/testing-library/user-event/blob/main/README.md#keyboardtext-options
for more information about how userEvent parses your input.`;
}
exports.getNextKeyDef = getNextKeyDef;
//# sourceMappingURL=getNextKeyDef.cjs.map