Debian Codegraph @

test-suite.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4
5 <meta charset="utf-8" />
6 <link rel="icon" href="assets/favicon.png" />
7 <title>Running the test suite ▲ Prism</title>
8 <link rel="stylesheet" href="assets/style.css" />
9 <link rel="stylesheet" href="themes/prism.css" data-noprefix />
10 <script src="assets/vendor/prefixfree.min.js"></script>
11
12 <script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
13 <script src="https://www.google-analytics.com/ga.js" async></script>
14 </head>
15 <body class="language-javascript">
16
17 <header>
18 <div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div>
19
20 <h2>Running the test suite</h2>
21
22 <p>Prism has a test suite, that ensures that the correct tokens are matched.</p>
23 </header>
24
25 <section id="running-the-test-suite">
26 <h1>Running the test suite</h1>
27
28 <p>Running the test suite is simple: just call <code class="language-bash">npm test</code>.</p>
29 <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>
30
31
32 <h2 id="running-tests-for-specific-languages">Running tests for specific languages</h2>
33
34 <p>To run the tests only for one language, you can use the <code>language</code> parameter:</p>
35
36 <pre><code class="language-bash">npm run test:languages -- --language=markup</code></pre>
37
38 <p>You can even specify multiple languages:</p>
39
40 <pre><code class="language-bash">npm run test:languages -- --language=markup --language=css</code></pre>
41 </section>
42
43 <section id="writing-tests">
44 <h1>Writing tests</h1>
45
46 <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>
47 <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>
48
49
50 <h2 id="writing-tests-directories">Language directories</h2>
51
52 <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>
53 <p><strong>All language names must match the names from the definition in <code>components.json</code>.</strong></p>
54
55 <h3>Example 1: testing a language in isolation (default use case)</h3>
56 <p>Just put your test file into the directory of the language you want to test.</p>
57 <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>
58
59 <h3>Example 2: testing language injection</h3>
60 <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>
61 <p>You need to define multiple languages by separating them using a <code>+</code> sign: <code>markup+php</code>.</p>
62 <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>
63 <p>By default the last language is the main language: <code>php+markup</code> will have <code>markup</code> as main language. This is equal to putting your code in the following code block:</p>
64 <pre><code class="language-markup">...
65 &lt;pre>&lt;code class="language-markup">
66 &lt;!-- your code here -->
67 &lt;/code>&lt;pre>
68 ...</code></pre>
69
70 <p>If you need to load the languages in a given order, but you don't want to use the last language as main language, you can mark the main language with an exclamation mark: <code>php!+markup</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>
71
72 <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>
73
74
75 <h2 id="writing-tests-creating-your-test-case-file">Creating your test case file</h2>
76
77 <p>At first you need to create a new file in the language directory, you want to test.</p>
78 <p><strong>Use a proper name for your test case.</strong> Please use one case of the following conventions:</p>
79 <ul>
80 <li><code>issue{issueid}</code>: reference a github issue id (example: <code>issue588.test</code>).</li>
81 <li><code>{featurename}_feature</code>: group all tests to one feature in one file (example: <code>string_interpolation_feature.test</code>).</li>
82 <li><code>{language}_inclusion</code>: test inclusion of one language into the other (example: <code>markup!+css/css_inclusion.test</code> will test CSS inclusion into markup).</li>
83 </ul>
84 <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>
85
86
87 <h2 id="writing-tests-writing-your-test">Writing your test</h2>
88
89 <p>A test case file is built up of two or three sections separated by ten or more dashes <code>-</code> starting at the begin of the line. The sections are the following:</p>
90
91 <ol>
92 <li>Your language snippet. The code you want to tokenize using Prism. (<strong>required</strong>)</li>
93 <li>
94 The simplified token stream you expect. Needs to be valid JSON. (<em>optional</em>) <br>
95 The test runner will automatically insert this if not present. <strong>Carefully check</strong> that the inserted token stream is what you expected. <br>
96 If the test case fails because the JSON is present but incorrect, then you can use the <a href="#writing-tests-update"><code>--update</code> flag</a> to overwrite it.
97 </li>
98 <li>A brief comment explaining the test case. (<em>optional</em>)</li>
99 </ol>
100
101 <p>Here is an example:</p>
102 <pre><code>var a = 5;
103
104 ----------------------------------------------------
105
106 [
107 ["keyword", "var"],
108 " a ",
109 ["operator", "="],
110 ["number", "5"],
111 ["punctuation", ";"]
112 ]
113
114 ----------------------------------------------------
115
116 This is a comment explaining this test case.</code></pre>
117
118 <h2 id="writing-tests-the-easy-way">The easy way to write tests</h2>
119 <p>The easy way to create one or multiple new test case(s) is this:</p>
120
121 <ol>
122 <li>Create a new test case file <code class="language-none">tests/languages/{language}/{test-case}.test</code>.</li>
123 <li>Insert the code you want to test (and nothing more).</li>
124 <li>Repeat the first two steps for as many test cases as you want.</li>
125 <li>Run <code class="language-bash">npm run test:languages</code>.</li>
126 <li>Done.</li>
127 </ol>
128
129 <p>Updating existing test case files is easy too!</p>
130
131 <ol>
132 <li>Run <code class="language-bash">npm run test:languages -- --update</code>.</li>
133 <li>Done.</li>
134 </ol>
135
136 <p>This works by making the test runner insert the actual token stream of you test code as the expected token stream. <strong>Carefully check that the inserted token stream is actually what you expect or else the test is meaningless!</strong></p>
137
138
139 <h2 id="writing-tests-update">Updating tests</h2>
140
141 <p>When creating and changing languages, their test files have to be updated to properly test the language. The rather tedious task of updating test files can be automated using the following command:</p>
142
143 <pre><code class="language-bash">npm run test:languages -- --update</code></pre>
144
145 <p>Updates (overwrites) the expected token stream of all failing test files. The language tests are guaranteed to pass after running this command.</p>
146
147 <p><em>Keep in mind:</em> This command makes it easy to create/update test files but this doesn't mean that the tests will be correct. <strong>Always carefully check the inserted/updated token streams!</strong></p>
148
149 <h2 id="writing-tests-explaining-the-simplified-token-stream">Explaining the simplified token stream</h2>
150
151 <p>While highlighting, Prism transforms your source code into a token stream. This is basically a tree of nested tokens (or arrays, or strings).</p>
152 <p>As these trees are hard to write by hand, the test runner uses a simplified version of it.</p>
153 <p>It uses the following rules:</p>
154 <ul>
155 <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>
156 <li>All strings that are either empty or only contain whitespace, are removed from the token stream.</li>
157 <li>All empty structures are removed.</li>
158 </ul>
159
160 <p>The simplified token stream does not contain the aliases of a token.</p>
161
162 <p>For further information: reading the tests of the test runner (<code>tests/testrunner-tests.js</code>) will help you understand the transformation.</p>
163 </section>
164
165 <section id="writing-specific-tests">
166 <h1>Writing specific tests</h1>
167
168 <p>Sometimes, using the token stream tests is not powerful enough. By creating a test file with the file extension <code>.html.test</code> instead of <code>.test</code>, you can make Prism highlight arbitrary pieces of code and check their HTML results.</p>
169 <p>The language is determined by the folder containing the test file lies, as explained in the previous section.</p>
170 <p>The structure of your test file will look like this, for example:</p>
171 <pre><code class="language-html">&amp;amp;
172 &amp;#x41;
173
174 ----------------------------------------------------
175
176 &lt;span class="token entity named-entity" title="&amp;amp;">&amp;amp;amp;&lt;/span>
177 &lt;span class="token entity" title="&amp;#x41;">&amp;amp;#x41;&lt;/span>
178
179 ----------------------------------------------------
180
181 This is a comment explaining this test case.
182 </code></pre>
183 </section>
184
185 <section id="test-runner-tests">
186 <h1>Test runner tests</h1>
187 <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>
188 <p>You shouldn't need to touch this file ever, except you modify the test runner code.</p>
189 </section>
190
191 <section id="internal-structure">
192 <h1>Internal structure</h1>
193 <p>The global test flow is at follows:</p>
194 <ol>
195 <li>Run all internal tests (test the test runner).</li>
196 <li>Find all language tests.</li>
197 <li>Run all language tests individually.</li>
198 <li>Report the results.</li>
199 </ol>
200 </section>
201
202
203 <footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
204
205 <script src="assets/vendor/utopia.js"></script>
206 <script src="prism.js"></script>
207 <script src="components/prism-bash.js"></script>
208 <script src="components.js"></script>
209 <script src="assets/code.js"></script>
210
211 </body>
212 </html>