forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_case.js
60 lines (53 loc) · 1.93 KB
/
test_case.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const {promisify} = require('util');
const { JSDOM } = require('jsdom');
const utility = require('../utility');
const glob = promisify(require('glob'));
const fs = require('fs');
// default to the minified library if it was built, otherwise fallback to
// the non-minified
async function findLibrary() {
const files = ['highlight.min.js', 'highlight.js'];
for (let file of files) {
try {
let path = utility.buildPath('..', 'build', file);
await fs.promises.stat(path);
return path;
} catch {}
}
throw new Error("could not find library in `build`");
}
function newTestCase(opts) {
var test = {};
test.code = opts.code;
test.html = opts.html;
test.expect = opts.expect;
test.language = opts.language;
test.html = test.html || `<pre><code class='${test.language}'>${test.code}</code></pre>`;
test.runner = async function() {
await buildFakeDOM.bind(this, test)();
this.hljs.highlightElement(this.block);
const actual = this.block.innerHTML;
actual.should.equal(test.expect);
}
return test;
}
const buildFakeDOM = async function(data) {
const filePath = await findLibrary();
const hljsPath = await glob(filePath);
const hljsFiles = await hljsPath.map(path => fs.readFileSync(path, 'utf8'));
const hljsScript = await hljsFiles.map(file => `<script>${file}</script>`).join("");
const { window} = await new JSDOM(hljsScript + data.html, { runScripts: "dangerously" });
this.block = window.document.querySelector('pre code');
this.hljs = window.hljs;
};
// quotes are not encoded because we're testing the value
// returned by innerHTML where the browser helpfully reencodes
// " to " for us...
const defaultCase = newTestCase({
code: 'var say = "Hello";',
language: "javascript",
expect: '<span class="hljs-keyword">' +
'var</span> say = <span class="hljs-string">' +
'"Hello"</span>;'
});
module.exports = { newTestCase, defaultCase, buildFakeDOM, findLibrary };