Commit c1e6a7fdaddd4bb289f38d34df010efda70b323f

Michael Schmidt 2019-04-23T00:30:37

Tests: Added test for empty regexes (#1847) This adds a new test which checks all regexes to not match the empty string.

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.`);
+				}
+			});
+
+		});
+	});
+}