OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 var fs = require("fs"); |
| 4 var assert = require("chai").assert; |
| 5 var PrismLoader = require("./prism-loader"); |
| 6 var TokenStreamTransformer = require("./token-stream-transformer"); |
| 7 |
| 8 /** |
| 9 * Handles parsing of a test case file. |
| 10 * |
| 11 * |
| 12 * A test case file consists of at least two parts, separated by a line of dashe
s. |
| 13 * This separation line must start at the beginning of the line and consist of a
t least three dashes. |
| 14 * |
| 15 * The test case file can either consist of two parts: |
| 16 * |
| 17 * {source code} |
| 18 * ---- |
| 19 * {expected token stream} |
| 20 * |
| 21 * |
| 22 * or of three parts: |
| 23 * |
| 24 * {source code} |
| 25 * ---- |
| 26 * {expected token stream} |
| 27 * ---- |
| 28 * {text comment explaining the test case} |
| 29 * |
| 30 * If the file contains more than three parts, the remaining parts are just igno
red. |
| 31 * If the file however does not contain at least two parts (so no expected token
stream), |
| 32 * the test case will later be marked as failed. |
| 33 * |
| 34 * |
| 35 * @type {{runTestCase: Function, transformCompiledTokenStream: Function, parseT
estCaseFile: Function}} |
| 36 */ |
| 37 module.exports = { |
| 38 |
| 39 /** |
| 40 * Runs the given test case file and asserts the result |
| 41 * |
| 42 * The passed language identifier can either be a language like "css" or
a composed language |
| 43 * identifier like "css+markup". Composed identifiers can be used for te
sting language inclusion. |
| 44 * |
| 45 * When testing language inclusion, the first given language is the main
language which will be passed |
| 46 * to Prism for highlighting ("css+markup" will result in a call to Pris
m to highlight with the "css" grammar). |
| 47 * But it will be ensured, that the additional passed languages will be
loaded too. |
| 48 * |
| 49 * The languages will be loaded in the order they were provided. |
| 50 * |
| 51 * @param {string} languageIdentifier |
| 52 * @param {string} filePath |
| 53 */ |
| 54 runTestCase: function (languageIdentifier, filePath) { |
| 55 var testCase = this.parseTestCaseFile(filePath); |
| 56 var usedLanguages = this.parseLanguageNames(languageIdentifier); |
| 57 |
| 58 if (null === testCase) { |
| 59 throw new Error("Test case file has invalid format (or t
he provided token stream is invalid JSON), please read the docs."); |
| 60 } |
| 61 |
| 62 var Prism = PrismLoader.createInstance(usedLanguages.languages); |
| 63 // the first language is the main language to highlight |
| 64 var mainLanguageGrammar = Prism.languages[usedLanguages.mainLang
uage]; |
| 65 var compiledTokenStream = Prism.tokenize(testCase.testSource, ma
inLanguageGrammar); |
| 66 var simplifiedTokenStream = TokenStreamTransformer.simplify(comp
iledTokenStream); |
| 67 |
| 68 assert.deepEqual(simplifiedTokenStream, testCase.expectedTokenSt
ream, testCase.comment); |
| 69 }, |
| 70 |
| 71 |
| 72 /** |
| 73 * Parses the language names and finds the main language. |
| 74 * |
| 75 * It is either the first language or the language followed by a exclama
tion mark “!”. |
| 76 * There should only be one language with an exclamation mark. |
| 77 * |
| 78 * @param {string} languageIdentifier |
| 79 * |
| 80 * @returns {{languages: string[], mainLanguage: string}} |
| 81 */ |
| 82 parseLanguageNames: function (languageIdentifier) { |
| 83 var languages = languageIdentifier.split("+"); |
| 84 var mainLanguage = null; |
| 85 |
| 86 languages = languages.map( |
| 87 function (language) { |
| 88 var pos = language.indexOf("!"); |
| 89 |
| 90 if (-1 < pos) { |
| 91 if (mainLanguage) { |
| 92 throw "There are multiple main l
anguages defined."; |
| 93 } |
| 94 |
| 95 mainLanguage = language.replace("!", "")
; |
| 96 return mainLanguage; |
| 97 } |
| 98 |
| 99 return language; |
| 100 } |
| 101 ); |
| 102 |
| 103 if (!mainLanguage) { |
| 104 mainLanguage = languages[languages.length-1]; |
| 105 } |
| 106 |
| 107 return { |
| 108 languages: languages, |
| 109 mainLanguage: mainLanguage |
| 110 }; |
| 111 }, |
| 112 |
| 113 |
| 114 /** |
| 115 * Parses the test case from the given test case file |
| 116 * |
| 117 * @private |
| 118 * @param {string} filePath |
| 119 * @returns {{testSource: string, expectedTokenStream: Array.<Array.<str
ing>>, comment:string?}|null} |
| 120 */ |
| 121 parseTestCaseFile: function (filePath) { |
| 122 var testCaseSource = fs.readFileSync(filePath, "utf8"); |
| 123 var testCaseParts = testCaseSource.split(/^-{10,}\w*$/m); |
| 124 |
| 125 try { |
| 126 var testCase = { |
| 127 testSource: testCaseParts[0].trim(), |
| 128 expectedTokenStream: JSON.parse(testCaseParts[1]
), |
| 129 comment: null |
| 130 }; |
| 131 |
| 132 // if there are three parts, the third one is the commen
t |
| 133 // explaining the test case |
| 134 if (testCaseParts[2]) { |
| 135 testCase.comment = testCaseParts[2].trim(); |
| 136 } |
| 137 |
| 138 return testCase; |
| 139 } |
| 140 catch (e) { |
| 141 // the JSON can't be parsed (e.g. it could be empty) |
| 142 return null; |
| 143 } |
| 144 } |
| 145 }; |
OLD | NEW |