Tests: Added test for empty regexes (#1847) This adds a new test which checks all regexes to not match the empty string.
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
diff --git a/package.json b/package.json
index 6eb6937..1a3fcb6 100755
--- a/package.json
+++ b/package.json
@@ -8,8 +8,9 @@
"test:aliases": "mocha tests/aliases-test.js",
"test:languages": "mocha tests/run.js",
"test:plugins": "mocha tests/plugins/**/*.js",
+ "test:regex": "mocha tests/regex-tests.js",
"test:runner": "mocha tests/testrunner-tests.js",
- "test": "npm run test:runner && npm run test:languages && npm run test:plugins && npm run test:aliases"
+ "test": "npm run test:runner && npm run test:languages && npm run test:plugins && npm run test:aliases && npm run test:regex"
},
"repository": {
"type": "git",
diff --git a/tests/regex-tests.js b/tests/regex-tests.js
new file mode 100644
index 0000000..0b30ad8
--- /dev/null
+++ b/tests/regex-tests.js
@@ -0,0 +1,31 @@
+"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);
+
+ it('- should not match the empty string', function () {
+ let lastToken = '<unknown>';
+
+ Prism.languages.DFS(Prism.languages, function (name, value) {
+ if (typeof this === 'object' && !Array.isArray(this) && name !== 'pattern') {
+ lastToken = name;
+ }
+
+ if (Prism.util.type(value) === 'RegExp') {
+ assert.notMatch('', value, `Token '${lastToken}': ${value} should not match the empty string.`);
+ }
+ });
+
+ });
+ });
+}