module.exports = function setup() {
const fs = require('node:fs');
const { apgLib } = require('apg-js');
const GrammarCtor = require('./odata-grammar');
const { parser: ParserCtor, ids, utils, trace: TraceCtor } = apgLib;
const fileName = './src/odata/odata-abnf-testcases.json';
const universalCallback = function universalCallback(result, chars, phraseIndex, userData) {
if (result.state === ids.MATCH) {
userData.rulesTouched[result.ruleIndex] = true;
const value = userData.map.get(result.ruleIndex);
if (value) {
const phrase = utils.charsToString(chars, phraseIndex, result.phraseLength);
const found = value.find((element) => element === phrase);
if (!found) {
if (userData.trace) {
console.log(
`=> universalCallback[${result.ruleIndex}]: phrase "${phrase}" not found in mapped list of phrases for this rule`
);
}
result.state = ids.NOMATCH;
result.phraseLength = 0;
}
}
}
};
try {
const data = fs.readFileSync(fileName);
const tests = JSON.parse(data.toString());
const odataParser = new ParserCtor();
const odataGrammar = new GrammarCtor();
const odataTrace = new TraceCtor();
odataTrace.filter.rules['<ALL>'] = true;
odataTrace.filter.operators['<ALL>'] = true;
odataGrammar.rules.forEach((rule) => {
odataParser.callbacks[rule.lower] = universalCallback;
});
const map = new Map();
tests.constraints.forEach((c) => {
const key = parseInt(c.ruleId, 10);
const value = [];
c.match.forEach((s) => {
value.push(s);
});
map.set(key, value);
});
return { odataParser, odataTrace, odataGrammar, utils, tests, map };
} catch (e) {
console.log('setup error');
console.log(e.message);
return {};
}
};