Skip to content

Commit

Permalink
Tests: Pretty-printed token stream (#1801)
Browse files Browse the repository at this point in the history
This adds support for pretty-printed token streams in the output of error messages.
  • Loading branch information
RunDevelopment committed Mar 10, 2019
1 parent f246748 commit 9ea6d60
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions test-suite.html
Expand Up @@ -120,6 +120,8 @@ <h2>Explaining the simplified token stream</h2>
<li>All strings that are either empty or only contain whitespace, are removed from the token stream.</li>
<li>All empty structures are removed.</li>
</ul>
<p>To get a pretty-printed version of the simplified token stream of a failed test, add the <code>--pretty</code> modifier. Keep in mind that the pretty-printed token stream is indented using spaces, you may need to convert these to tabs. (Most editors today have an option which handles the conversion for you.)<br>
E.g. <code class="language-bash">npm test -- --pretty</code>.</p>
<p>For further information: reading the tests of the test runner (<code>tests/testrunner-tests.js</code>) will help you understand the transformation.</p>
</section>
</section>
Expand Down
14 changes: 8 additions & 6 deletions tests/helper/test-case.js
Expand Up @@ -50,8 +50,9 @@ module.exports = {
*
* @param {string} languageIdentifier
* @param {string} filePath
* @param {boolean} [pretty=false]
*/
runTestCase: function (languageIdentifier, filePath) {
runTestCase: function (languageIdentifier, filePath, pretty) {
var testCase = this.parseTestCaseFile(filePath);
var usedLanguages = this.parseLanguageNames(languageIdentifier);

Expand All @@ -74,12 +75,13 @@ module.exports = {

var simplifiedTokenStream = TokenStreamTransformer.simplify(compiledTokenStream);

var tzd = JSON.stringify( simplifiedTokenStream ); var exp = JSON.stringify( testCase.expectedTokenStream );
var tzd = JSON.stringify(simplifiedTokenStream);
var exp = JSON.stringify(testCase.expectedTokenStream);
var i = 0; var j = 0; var diff = "";
while ( j < tzd.length ){ if (exp[i] != tzd[j] || i == exp.length) diff += tzd[j]; else i++; j++; }
while (j < tzd.length) { if (exp[i] != tzd[j] || i == exp.length) diff += tzd[j]; else i++; j++; }

// var message = "\nToken Stream: \n" + JSON.stringify( simplifiedTokenStream, null, " " ) +
var message = "\nToken Stream: \n" + tzd +
const tokenStreamStr = pretty ? TokenStreamTransformer.prettyprint(simplifiedTokenStream) : tzd;
var message = "\nToken Stream: \n" + tokenStreamStr +
"\n-----------------------------------------\n" +
"Expected Token Stream: \n" + exp +
"\n-----------------------------------------\n" + diff;
Expand Down Expand Up @@ -120,7 +122,7 @@ module.exports = {
);

if (!mainLanguage) {
mainLanguage = languages[languages.length-1];
mainLanguage = languages[languages.length - 1];
}

return {
Expand Down
45 changes: 42 additions & 3 deletions tests/helper/token-stream-transformer.js
Expand Up @@ -11,22 +11,61 @@ module.exports = {
*
*
* @param {Array} tokenStream
* @returns {Array.<string[]|Array>}
* @returns {Array<string|[string, string|any[]]>}
*/
simplify: function (tokenStream) {
if (Array.isArray(tokenStream)) {
return tokenStream
.map(this.simplify.bind(this))
.filter(function (value) {
return !(Array.isArray(value) && !value.length) && !(typeof value === "string" && !value.trim().length);
}
);
});
}
else if (typeof tokenStream === "object") {
return [tokenStream.type, this.simplify(tokenStream.content)];
}
else {
return tokenStream;
}
},

/**
*
* @param {ReadonlyArray<string|[string, string|ReadonlyArray]>} tokenStream
* @param {number} [indentationLevel=0]
*/
prettyprint(tokenStream, indentationLevel = 1) {
const indentChar = ' ';

// can't use tabs because the console will convert one tab to four spaces
const indentation = new Array(indentationLevel + 1).join(indentChar);

let out = "";
out += "[\n"
tokenStream.forEach((item, i) => {
out += indentation;

if (typeof item === 'string') {
out += JSON.stringify(item);
} else {
const name = item[0];
const content = item[1];

out += '[' + JSON.stringify(name) + ', ';

if (typeof content === 'string') {
out += JSON.stringify(content);
} else {
out += this.prettyprint(content, indentationLevel + 1);
}

out += ']';
}

const lineEnd = (i === tokenStream.length - 1) ? '\n' : ',\n';
out += lineEnd;
})
out += indentation.substr(indentChar.length) + ']'
return out;
}
};
3 changes: 2 additions & 1 deletion tests/run.js
Expand Up @@ -12,6 +12,7 @@ if (argv.language) {
// load complete test suite
testSuite = TestDiscovery.loadAllTests(__dirname + "/languages");
}
const pretty = 'pretty' in argv;

// define tests for all tests in all languages in the test suite
for (var language in testSuite) {
Expand All @@ -31,7 +32,7 @@ for (var language in testSuite) {
function () {

if (path.extname(filePath) === '.test') {
TestCase.runTestCase(language, filePath);
TestCase.runTestCase(language, filePath, pretty);
} else {
TestCase.runTestsWithHooks(language, require(filePath));
}
Expand Down

0 comments on commit 9ea6d60

Please sign in to comment.