Hash :
3ba786cd
Author :
Date :
2019-06-02T15:08:24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
"use strict";
const { assert } = require('chai');
const PrismLoader = require('./helper/prism-loader');
const { languages } = require('../components');
for (const lang in languages) {
if (lang === 'meta') {
continue;
}
describe(`Testing regular expressions of '${lang}'`, function () {
const Prism = PrismLoader.createInstance(lang);
/**
* Invokes the given function on every pattern in `Prism.languages`.
*
* @param {(values: { pattern: RegExp, tokenName: string, name: string, parent: any }) => void} callback
*/
function forEachPattern(callback) {
/** @type {Map<string, string>} */
const nameMap = new Map();
Prism.languages.DFS(Prism.languages, function (name, value) {
let path = nameMap.get(this) || '<languages>';
if (/^\d+$/.test(name)) {
path += `[${name}]`;
} else if (/^[a-z]\w*$/i.test(name)) {
path += `.${name}`;
} else {
path += `[${JSON.stringify(name)}]`;
}
if (Array.isArray(value) || Prism.util.type(value) === 'Object') {
nameMap.set(value, path);
}
if (Prism.util.type(value) === 'RegExp') {
callback({
pattern: value,
tokenName: path,
name,
parent: this,
});
}
});
}
it('- should not match the empty string', function () {
forEachPattern(({ pattern, tokenName }) => {
// test for empty string
assert.notMatch('', pattern, `Token ${tokenName}: ${pattern} should not match the empty string.`);
});
});
it('- should have a capturing group if lookbehind is set to true', function () {
forEachPattern(({ pattern, tokenName, name, parent }) => {
if (name === 'pattern' && parent.lookbehind) {
const simplifiedSource = pattern.source.replace(/\\\D/g, '_').replace(/\[[^\]]*\]/g, '_');
if (!/\((?!\?)/.test(simplifiedSource)) {
assert.fail(`Token ${tokenName}: The pattern is set to 'lookbehind: true' but does not have a capturing group.`);
}
}
});
});
});
}