harmonyc
Version:
Harmony Code - model-driven BDD for Vitest
51 lines (50 loc) • 1.69 kB
JavaScript
import { basename } from 'node:path';
import { VitestGenerator } from "../code_generator/VitestGenerator.js";
import { OutFile } from "../code_generator/outFile.js";
import { base, testFileName } from "../filenames/filenames.js";
import { Feature } from "../model/model.js";
import { autoLabel } from "../optimizations/autoLabel/autoLabel.js";
import { parse } from "../parser/parser.js";
const X = 'X'.codePointAt(0);
const A = 'A'.codePointAt(0);
const x = 'x'.codePointAt(0);
const a = 'a'.codePointAt(0);
export function XYZAB(index) {
return String.fromCodePoint(A + ((X - A + index) % 26));
}
export function xyzab(index) {
return String.fromCodePoint(a + ((x - a + index) % 26));
}
export const DEFAULT_COMPILER_OPTIONS = {
argumentPlaceholder: XYZAB,
};
export function compileFeature(fileName, src, opts = {}) {
const feature = new Feature(basename(base(fileName)));
try {
feature.root = parse(src);
}
catch (e) {
if (e.pos && e.errorMessage) {
e.message =
e.stack = `Error in ${fileName}:${e.pos.rowBegin}:${e.pos.columnBegin}: ${e.errorMessage}`;
}
else {
e.stack = `Error in ${fileName}: ${e.stack}`;
}
throw e;
}
feature.root.setFeature(feature);
autoLabel(feature.root);
const testFn = testFileName(fileName);
const testFile = new OutFile(testFn, fileName);
const cg = new VitestGenerator(testFile, fileName, {
...DEFAULT_COMPILER_OPTIONS,
...opts,
});
feature.toCode(cg);
return {
outFile: testFile,
phraseMethods: cg.phraseMethods,
featureClassName: cg.featureClassName,
};
}