Skip to content

Commit

Permalink
suggestion: tests with aliases for better coverage of code output
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPavelec committed Oct 5, 2020
1 parent 69840ee commit a7f885c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -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}]
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
@@ -1,4 +1,4 @@
* text=auto

# Test files should not have their line endings modified by git
/tests/languages/**/*.test binary
/tests/languages/**/*.{test,test_with_alias} binary eol=crlf
41 changes: 38 additions & 3 deletions tests/helper/test-case.js
Expand Up @@ -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
Expand Down Expand Up @@ -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<string|Array<string|any[]>>}
*/
simpleTokenize(Prism, code, language) {
simpleTokenize(Prism, code, language, testWithAlias) {
const env = {
code,
grammar: Prism.languages[language],
Expand All @@ -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<string|Array<string|any[]>>} 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.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/run.js
Expand Up @@ -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));
}
Expand Down

0 comments on commit a7f885c

Please sign in to comment.