From a7f885c69d0d361fc1cefb022c2953e60835ff04 Mon Sep 17 00:00:00 2001 From: Tom Pavelec Date: Mon, 5 Oct 2020 11:10:32 +0200 Subject: [PATCH] suggestion: tests with aliases for better coverage of code output --- .editorconfig | 2 +- .gitattributes | 2 +- tests/helper/test-case.js | 41 ++++++++++++++++++++++++++++++++++++--- tests/run.js | 4 +++- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/.editorconfig b/.editorconfig index b2e4603bf0..899cfda98b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,7 +6,7 @@ charset = utf-8 indent_style = tab indent_size = 4 -[tests/languages/**.test] +[tests/languages/**.{test,test_with_alias}] end_of_line = crlf [{package.json,.travis.yml}] diff --git a/.gitattributes b/.gitattributes index 1d598bdc92..f41d3ad777 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ * text=auto # Test files should not have their line endings modified by git -/tests/languages/**/*.test binary \ No newline at end of file +/tests/languages/**/*.{test,test_with_alias} binary eol=crlf \ No newline at end of file diff --git a/tests/helper/test-case.js b/tests/helper/test-case.js index a00ceda1aa..52797f1da5 100644 --- a/tests/helper/test-case.js +++ b/tests/helper/test-case.js @@ -50,15 +50,16 @@ module.exports = { * @param {string} languageIdentifier * @param {string} filePath * @param {boolean} acceptEmpty + * @param {boolean} testWithAlias */ - runTestCase(languageIdentifier, filePath, acceptEmpty) { + runTestCase(languageIdentifier, filePath, acceptEmpty, testWithAlias) { const testCase = this.parseTestCaseFile(filePath); const usedLanguages = this.parseLanguageNames(languageIdentifier); const Prism = PrismLoader.createInstance(usedLanguages.languages); // the first language is the main language to highlight - const simplifiedTokenStream = this.simpleTokenize(Prism, testCase.code, usedLanguages.mainLanguage); + const simplifiedTokenStream = this.simpleTokenize(Prism, testCase.code, usedLanguages.mainLanguage, testWithAlias); if (testCase.expectedTokenStream === null) { // the test case doesn't have an expected value @@ -119,9 +120,10 @@ module.exports = { * @param {import('../../components/prism-core')} Prism The Prism instance which will tokenize `code`. * @param {string} code The code to tokenize. * @param {string} language The language id. + * @param {boolean} testWithAlias * @returns {Array>} */ - simpleTokenize(Prism, code, language) { + simpleTokenize(Prism, code, language, testWithAlias) { const env = { code, grammar: Prism.languages[language], @@ -132,10 +134,43 @@ module.exports = { env.tokens = Prism.tokenize(env.code, env.grammar); Prism.hooks.run('after-tokenize', env); + if (testWithAlias === true) { + env.tokens = this.addAlias(env.tokens); + } + return TokenStreamTransformer.simplify(env.tokens); }, + /** + * @param {Array>} tokens + */ + addAlias(tokens) { + return tokens.map(function (token) { + if (typeof token === 'string') { + return token; + } + + if (Array.isArray(token.content)) { + token.content = this.addAlias(token.content); + } + + if (typeof token.alias === 'undefined') { + return token; + } + + var alias = token.alias; + if (Array.isArray(alias)) { + alias = alias.join(' '); + } + + token.type += ' ' + alias; + + return token; + }, this) + }, + + /** * Parses the language names and finds the main language. * diff --git a/tests/run.js b/tests/run.js index 4803dde72b..b3270e7a11 100644 --- a/tests/run.js +++ b/tests/run.js @@ -29,7 +29,9 @@ for (const language in testSuite) { it("– should pass test case '" + fileName + "'", function () { if (path.extname(filePath) === '.test') { - TestCase.runTestCase(language, filePath, accept); + TestCase.runTestCase(language, filePath, accept, false); + } else if (path.extname(filePath) === '.test_with_alias') { + TestCase.runTestCase(language, filePath, accept, true); } else { TestCase.runTestsWithHooks(language, require(filePath)); }