Skip to content

Commit

Permalink
Test runner: Allow to run tests for only some languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Oct 6, 2015
1 parent 6babaad commit 5ade8a5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"gulp-replace": "^0.5.4",
"mocha": "^2.2.5"
"mocha": "^2.2.5",
"yargs": "^3.26.0"
}
}
8 changes: 8 additions & 0 deletions test-suite.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ <h1>Running the test suite</h1>

<p>Running the test suite is simple: just call <code class="language-bash">npm test</code>.</p>
<p>All test files are run in isolation. A new prism instance is created for each test case. This will slow the test runner a bit down, but we can be sure that nothing leaks into the next test case.</p>

<section id="running-tests-for-specific-languages">
<h2>Running tests for specific languages</h2>

<p>To run the tests only for one language, you can use the <code>language</code> parameter: <code class="language-bash">npm test -- --language=markup</code>.</p>
<p>You can even specify multiple languages: <code class="language-bash">npm test -- --language=markup --language=css</code>.</p>
</section>
</section>

<section id="writing-tests">
Expand Down Expand Up @@ -139,6 +146,7 @@ <h2>Internal structure</h2>
<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
<script src="components/prism-bash.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
Expand Down
52 changes: 52 additions & 0 deletions tests/helper/test-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ module.exports = {
return testSuite;
},

/**
* Loads the list of available tests that match the given languages
*
* @param {string} rootDir
* @param {string|string[]} languages
* @returns {Object.<string, string[]>}
*/
loadSomeTests: function (rootDir, languages) {
var testSuite = {};
var self = this;

this.getSomeDirectories(rootDir, languages).forEach(
function (language) {
testSuite[language] = self.getAllFiles(path.join(rootDir, language));
}
);

return testSuite;
},


/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
Expand All @@ -41,6 +61,38 @@ module.exports = {
);
},

/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
* in the given src directory, matching the given languages
*
* @param {string} src
* @param {string|string[]} languages
* @returns {Array.<string>}
*/
getSomeDirectories: function (src, languages) {
var self = this;
return fs.readdirSync(src).filter(
function (file) {
return fs.statSync(path.join(src, file)).isDirectory() && self.directoryMatches(file, languages);
}
);
},

/**
* Returns whether a directory matches one of the given languages.
* @param {string} directory
* @param {string|string[]} languages
*/
directoryMatches: function (directory, languages) {
if (!Array.isArray(languages)) {
languages = [languages];
}
var dirLanguages = directory.split(/!?\+!?/);
return dirLanguages.some(function (lang) {
return languages.indexOf(lang) >= 0;
});
},


/**
* Returns a list of all full file paths to all files in the given src directory
Expand Down
10 changes: 8 additions & 2 deletions tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
var TestDiscovery = require("./helper/test-discovery");
var TestCase = require("./helper/test-case");
var path = require("path");
var argv = require("yargs").argv;

// load complete test suite
var testSuite = TestDiscovery.loadAllTests(__dirname + "/languages");
var testSuite;
if (argv.language) {
testSuite = TestDiscovery.loadSomeTests(__dirname + "/languages", argv.language);
} else {
// load complete test suite
testSuite = TestDiscovery.loadAllTests(__dirname + "/languages");
}

// define tests for all tests in all languages in the test suite
for (var language in testSuite) {
Expand Down

0 comments on commit 5ade8a5

Please sign in to comment.