vscode-tmgrammar-test
Version:
Test runner for VSCode textmate grammars
78 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runGrammarTestCase = exports.missingScopes_ = exports.parseGrammarTestCase = void 0;
const parsing_1 = require("./parsing");
Object.defineProperty(exports, "parseGrammarTestCase", { enumerable: true, get: function () { return parsing_1.parseGrammarTestCase; } });
async function runGrammarTestCase(registry, testCase) {
return registry.loadGrammar(testCase.metadata.scope).then((grammar) => {
if (!grammar) {
throw new Error(`Could not load scope ${testCase.metadata.scope}`);
}
const assertions = toMap((x) => x.sourceLineNumber, testCase.assertions);
let ruleStack = null;
let failures = [];
testCase.source.forEach((line, n) => {
var { tokens, ruleStack: ruleStack1 } = grammar.tokenizeLine(line, ruleStack);
ruleStack = ruleStack1;
if (assertions[n] !== undefined) {
let { testCaseLineNumber, scopeAssertions } = assertions[n];
scopeAssertions.forEach(({ from, to, scopes: requiredScopes, exclude: excludedScopes }) => {
const xs = tokens.filter((t) => from < t.endIndex && to > t.startIndex);
if (xs.length === 0 && requiredScopes.length > 0) {
failures.push({
missing: requiredScopes,
unexpected: [],
actual: [],
line: testCaseLineNumber,
srcLine: n,
start: from,
end: to
});
}
else {
xs.forEach((token) => {
const unexpected = excludedScopes.filter((s) => {
return token.scopes.includes(s);
});
const missing = missingScopes_(requiredScopes, token.scopes);
if (missing.length || unexpected.length) {
failures.push({
missing: missing,
actual: token.scopes,
unexpected: unexpected,
line: testCaseLineNumber,
srcLine: n,
start: token.startIndex,
end: token.endIndex
});
}
});
}
});
}
});
return failures;
});
}
exports.runGrammarTestCase = runGrammarTestCase;
function missingScopes_(rs, as) {
let i = 0, j = 0;
while (i < as.length && j < rs.length) {
if (as[i] === rs[j]) {
i++;
j++;
}
else {
i++;
}
}
return j === rs.length ? [] : rs.slice(j);
}
exports.missingScopes_ = missingScopes_;
function toMap(f, xs) {
return xs.reduce((m, x) => {
m[f(x)] = x;
return m;
}, {});
}
//# sourceMappingURL=index.js.map