Skip to content

Commit

Permalink
Merge pull request #588 from apfelbox/mocha
Browse files Browse the repository at this point in the history
Implement test suite runner
  • Loading branch information
Golmote committed Aug 13, 2015
2 parents fd54995 + e8884a9 commit 956cd85
Show file tree
Hide file tree
Showing 12 changed files with 755 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js

node_js:
- "0.10"
- "0.12"

before_script:
- npm install -g gulp
- gulp

script: npm test
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Lightweight, robust, elegant syntax highlighting. A spin-off project from Dabblet.",
"main": "prism.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha tests/testrunner-tests.js && mocha tests/run.js"
},
"repository": {
"type": "git",
Expand All @@ -18,10 +18,12 @@
"license": "MIT",
"readmeFilename": "README.md",
"devDependencies": {
"chai": "^2.3.0",
"gulp": "^3.8.6",
"gulp-concat": "^2.3.4",
"gulp-header": "^1.0.5",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1"
"gulp-uglify": "^0.3.1",
"mocha": "^2.2.5"
}
}
147 changes: 147 additions & 0 deletions test-suite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8" />
<link rel="shortcut icon" href="favicon.png" />
<title>Running the test suite ▲ Prism</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
<script src="prefixfree.min.js"></script>

<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
<script src="http://www.google-analytics.com/ga.js" async></script>
</head>
<body class="language-javascript">

<header>
<div class="intro" data-src="templates/header-main.html" data-type="text/html"></div>

<h2>Running the test suite</h2>
<p>Prism has a test suite, that ensures that the correct tokens are matched.</p>
</header>

<section id="running-the-test-suite">
<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>

<section id="writing-tests">
<h1>Writing tests</h1>

<p>Thank you for writing tests! Tests are awesome! They ensure, that we can improve the codebase without breaking anything. Also, this way, we can ensure that upgrading Prism is as painless as possible for you.</p>
<p>You can add new tests by creating a new test case file (with the <code>.test</code> file extension) in the tests directory which is located at <code>/tests/languages/${language}</code>.</p>

<section id="writing-tests-directories">
<h2>Language directories</h2>
<p>All tests are sorted into directories in the <code>tests/languages</code> directory. Each directory name encodes, which language you are currently testing.</p>
<p><strong>All language names must match the names from the definition in <code>components.js</code>.</strong></p>

<h3>Example 1: testing a language in isolation (default use case)</h3>
<p>Just put your test file into the directory of the language you want to test.</p>
<p>So, if you want to test CSS, put your test file in <code>/tests/languages/css</code> to test CSS only. If you create a test case in this directory, the test runner will ensure that the <code>css</code> language definition including all required language definitions are correctly loaded.</p>

<h3>Example 2: testing language injection</h3>
<p>If you want to test language injection, you typically need to load two or more languages where one language is the “main” language that is being tested, with all other languages being injected into it.</p>
<p>You need to define multiple languages by separating them using a <code>+</code> sign: <code>markup+php</code>.</p>
<p>The languages are loaded in order, so first markup (+ dependencies) is loaded, then php (+ dependencies). The test loader ensures that no language is loaded more than once (for example if two languages have the same dependencies).</p>
<p>By default the first language is the main language: <code>markup+php</code> will have <code>markup</code> as main language. This is equal to putting your code in the following code block:</p>
<pre><code class="language-markup">...
&lt;pre>&lt;code class="language-markup">
&lt;!-- your code here -->
&lt;/code>&lt;pre>
...</code></pre>

<p>If you need to load the languages in a given order, but you don't want to use the first language as main language, you can mark the main language with an exclamation mark: <code>markup+php!</code>. This will use <code>php</code> as main language. (You can only define one main language. The test runner will fail all tests in directories with more than one main language.)</p>

<p><em>Note: by loading multiple languages you can do integration tests (ensure that loading two or more languages together won't break anything).</em></p>
</section>

<section id="writing-tests-creating-your-test-case-file">
<h2>Creating your test case file</h2>
<p>At first you need to create a new file in the language directory, you want to test.</p>
<p><strong>Use a proper name for your test case.</strong> Please use one case of the following conventions:</p>
<ul>
<li><code>issue{issueid}</code>: reference a github issue id (example: <code>issue588.test</code>).</li>
<li><code>{featurename}_feature</code>: group all tests to one feature in one file (example: <code>string_interpolation_feature.test</code>).</li>
<li><code>{language}_inclusion</code>: test inclusion of one language into the other (example: <code>markup/php_inclusion.test</code> will test php inclusion into markup).</li>
</ul>
<p>You can use all conventions as a prefix, so <code>string_interpolation_feature_inline.test</code> is possible. <strong>But please take a minute or two to think of a proper name of your test case file. You are writing code not only for the computers, but also for your fellow developers.</strong></p>
</section>

<section id="writing-tests-writing-your-test">
<h2>Writing your test</h2>
<p>The structure of a test case file is as follows:</p>
<pre><code>
... language snippet...
----
... the simplified token stream you expect ...</code></pre>

<p>Your file is built up of two or three sections, separated by three or more dashes <code>-</code>, starting at the begin of the line:</p>
<ol>
<li>Your language snippet. The code you want to compile using Prism. (<strong>required</strong>)</li>
<li>The simplified token stream you expect. Needs to be valid JSON. (<strong>required</strong>)</li>
<li>A comment explaining the test case. (<em>optional</em>)</li>
</ol>
<p>The easiest way would be to look at an existing test file:</p>
<pre><code>var a = 5;

----------------------------------------------------

[
["keyword", "var"],
" a ",
["operator", "="],
["number", "5"],
["punctuation", ";"]
]

----------------------------------------------------

This is a comment explaining this test case.</code></pre>
</section>

<section>
<h2>Explaining the simplified token stream</h2>
<p>While compiling, Prism transforms your source code into a token stream. This is basically a tree of nested tokens (or arrays, or strings).</p>
<p>As these trees are hard to write by hand, the test runner uses a simplified version of it.</p>
<p>It uses the following rules:</p>
<ul>
<li><code>Token</code> objects are transformed into an array: <code>[token.type, token.content]</code> (whereas <code>token.content</code> can be a nested structure).</li>
<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>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>


<section id="test-runner-tests">
<h2>Test runner tests</h2>
<p>The test runner itself is tested in a separate test case. You can find all “test core” related tests in <code>tests/testrunner-tests.js</code>.</p>
<p>You shouldn't need to touch this file ever, except you modify the test runner code.</p>
</section>

<section id="internal-structure">
<h2>Internal structure</h2>
<p>The global test flow is at follows:</p>
<ol>
<li>Run all internal tests (test the test runner).</li>
<li>Find all language tests.</li>
<li>Run all language tests individually.</li>
<li>Report the results.</li>
</ol>
</section>


<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>

</body>
</html>
10 changes: 10 additions & 0 deletions tests/helper/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

var fs = require("fs");
var vm = require("vm");

var fileContent = fs.readFileSync(__dirname + "/../../components.js", "utf8");
var context = {};
vm.runInNewContext(fileContent, context);

module.exports = context.components;
113 changes: 113 additions & 0 deletions tests/helper/prism-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use strict";

var fs = require("fs");
var vm = require("vm");
var components = require("./components");
var languagesCatalog = components.languages;


module.exports = {

/**
* Creates a new Prism instance with the given language loaded
*
* @param {string|string[]} languages
* @returns {Prism}
*/
createInstance: function (languages) {
var context = {
loadedLanguages: [],
Prism: this.createEmptyPrism()
};
languages = Array.isArray(languages) ? languages : [languages];

for (var i = 0, l = languages.length; i < l; i++) {
context = this.loadLanguage(languages[i], context);
}

return context.Prism;
},


/**
* Loads the given language (including recursively loading the dependencies) and
* appends the config to the given Prism object
*
* @private
* @param {string} language
* @param {{loadedLanguages: string[], Prism: Prism}} context
* @returns {{loadedLanguages: string[], Prism: Prism}}
*/
loadLanguage: function (language, context) {
if (!languagesCatalog[language]) {
throw new Error("Language '" + language + "' not found.");
}

// the given language was already loaded
if (-1 < context.loadedLanguages.indexOf(language)) {
return context;
}

// if the language has a dependency -> load it first
if (languagesCatalog[language].require) {
context = this.loadLanguage(languagesCatalog[language].require, context);
}

// load the language itself
var languageSource = this.loadFileSource(language);
context.Prism = this.runFileWithContext(languageSource, {Prism: context.Prism}).Prism;
context.loadedLanguages.push(language);

return context;
},


/**
* Creates a new empty prism instance
*
* @private
* @returns {Prism}
*/
createEmptyPrism: function () {
var coreSource = this.loadFileSource("core");
var context = this.runFileWithContext(coreSource);
return context.Prism;
},


/**
* Cached file sources, to prevent massive HDD work
*
* @private
* @type {Object.<string, string>}
*/
fileSourceCache: {},


/**
* Loads the given file source as string
*
* @private
* @param {string} name
* @returns {string}
*/
loadFileSource: function (name) {
return this.fileSourceCache[name] = this.fileSourceCache[name] || fs.readFileSync(__dirname + "/../../components/prism-" + name + ".js", "utf8");
},


/**
* Runs a VM for a given file source with the given context
*
* @private
* @param {string} fileSource
* @param {*} [context]
*
* @returns {*}
*/
runFileWithContext: function (fileSource, context) {
context = context || {};
vm.runInNewContext(fileSource, context);
return context;
}
};
Loading

0 comments on commit 956cd85

Please sign in to comment.