From c1e6a7fdaddd4bb289f38d34df010efda70b323f Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 23 Apr 2019 00:30:37 +0200 Subject: [PATCH] Tests: Added test for empty regexes (#1847) This adds a new test which checks all regexes to not match the empty string. --- package.json | 3 ++- tests/regex-tests.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/regex-tests.js diff --git a/package.json b/package.json index 6eb6937af9..1a3fcb6637 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 0000000000..0b30ad82e6 --- /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 = ''; + + 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.`); + } + }); + + }); + }); +}