@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
31 lines (30 loc) • 1.16 kB
JavaScript
import { BuiltinParameterExpressionAlreadyExistsError, CustomParameterExpressionAlreadyExistsError, } from '../../errors/errors';
import { builtInExpressionRegEx } from './ExpressionStep';
import { ExpressionRegex } from './regexes';
class CustomExpressionRegex extends ExpressionRegex {
args;
constructor(args) {
super({
keyword: `{${args.name}}`,
groupName: args.name,
keywordRegex: new RegExp(`{${args.name}}`, `g`),
});
this.args = args;
}
getRegex(index) {
return `(?<${this.groupName}${index}>${this.args.regexp.source})`;
}
getValue(str) {
return this.args.transformer(str);
}
}
export const customExpressionRegEx = [];
export const defineParameterExpression = (args) => {
if (customExpressionRegEx.some((r) => r.groupName === args.name)) {
throw new CustomParameterExpressionAlreadyExistsError(args.name);
}
if (builtInExpressionRegEx.some((r) => r.groupName === args.name)) {
throw new BuiltinParameterExpressionAlreadyExistsError(args.name);
}
customExpressionRegEx.push(new CustomExpressionRegex(args));
};