8000 Tests: Additional checks for Prism functions (#1803) · PrismJS/prism@c3e74ea · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Dismiss alert

Commit c3e74ea

Browse files
Tests: Additional checks for Prism functions (#1803)
This adds additional checks for `extend` and `insertBefore` under `Prism.languages`.
1 parent 29a30c6 commit c3e74ea

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

tests/checks/extend.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { testFunction } = require('./../helper/check-functionality');
2+
3+
function extendTest(id, redef) {
4+
const details = `\nextend("${id}", ${redef})`;
5+
6+
// type checks
7+
if (typeof id !== 'string') {
8+
throw new TypeError(`The id argument has to be a 'string'.` + details);
9+
}
10+
if (typeof redef !== 'object') {
11+
throw new TypeError(`The redef argument has to be an 'object'.` + details);
12+
}
13+
14+
15+
if (!(id in Prism.languages)) {
16+
throw new Error(`Cannot extend '${id}' because it is not defined in Prism.languages.`);
17+
}
18+
}
19+
20+
testFunction('extend', Prism.languages, extendTest);

tests/checks/insert-before.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { testFunction } = require('./../helper/check-functionality');
2+
3+
function insertBeforeTest(inside, before, insert, root) {
4+
const details = `\ninsertBefore("${inside}", "${before}", ${insert}, ${root})`;
5+
6+
// type checks
7+
if (typeof inside !== 'string') {
8+
throw new TypeError(`The inside argument has to be a 'string'.` + details);
9+
}
10+
if (typeof before !== 'string') {
11+
throw new TypeError(`The before argument has to be a 'string'.` + details);
12+
}
13+
if (typeof insert !== 'object') {
14+
throw new TypeError(`The insert argument has to be an 'object'.` + details);
15+
}
16+
if (root && typeof root !== 'object') {
17+
throw new TypeError(`The root argument has to be an 'object' if defined.` + details);
18+
}
19+
20+
21+
root = root || Prism.languages;
22+
var grammar = root[inside];
23+
24+
if (typeof grammar !== 'object') {
25+
throw new Error(`The grammar "${inside}" has to be an 'object' not '${typeof grammar}'.`);
26+
}
27+
if (!(before in grammar)) {
28+
throw new Error(`"${before}" has to be a key of the grammar "${inside}".`);
29+
}
30+
}
31+
32+
testFunction('insertBefore', Prism.languages, insertBeforeTest);

tests/helper/check-functionality.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
module.exports = {
4+
testFunction(name, object, tester) {
5+
const func = object[name];
6+
7+
object[name] = function () {
8+
tester.apply(this, arguments);
9+
return func.apply(this, arguments);
10+
};
11+
}
12+
13+
}

tests/helper/prism-loader.js

+53-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require("fs");
44
const vm = require("vm");
5+
const { getAllFiles } = require("./test-discovery");
56
const components = require("../../components");
67
const languagesCatalog = components.languages;
78

@@ -70,7 +71,7 @@ module.exports = {
7071
}
7172

7273
// load the language itself
73-
const languageSource = this.loadFileSource(language);
74+
const languageSource = this.loadComponentSource(language);
7475
context.Prism = this.runFileWithContext(languageSource, { Prism: context.Prism }).Prism;
7576
context.loadedLanguages.push(language);
7677

@@ -85,8 +86,32 @@ module.exports = {
8586
* @returns {Prism}
8687
*/
8788
createEmptyPrism() {
88-
const coreSource = this.loadFileSource("core");
89+
const coreSource = this.loadComponentSource("core");
8990
const context = this.runFileWithContext(coreSource);
91+
92+
for (const testSource of this.getChecks().map(src => this.loadFileSource(src))) {
93+
context.Prism = this.runFileWithContext(testSource, {
94+
Prism: context.Prism,
95+
/**
96+
* A pseudo require function for the checks.
97+
*
98+
* This function will behave like the regular `require` in real modules when called form a check file.
99+
*
100+
* @param {string} id The id of relative path to require.
101+
*/
102+
require(id) {
103+
if (id.startsWith('./')) {
104+
// We have to rewrite relative paths starting with './'
105+
return require('./../checks/' + id.substr(2));
106+
} else {
107+
// This might be an id like 'mocha' or 'fs' or a relative path starting with '../'.
108+
// In both cases we don't have to change anything.
109+
return require(id);
110+
}
111+
}
112+
}).Prism;
113+
}
114+
90115
return context.Prism;
91116
},
92117

@@ -101,14 +126,37 @@ module.exports = {
101126

102127

103128
/**
104-
* Loads the given file source as string
129+
* Loads the given component's file source as string
105130
*
106131
* @private
107132
* @param {string} name
108133
* @returns {string}
109134
*/
110-
loadFileSource(name) {
111-
return this.fileSourceCache[name] = this.fileSourceCache[name] || fs.readFileSync(__dirname + "/../../components/prism-" + name + ".js", "utf8");
135+
loadComponentSource(name) {
136+
return this.loadFileSource(__dirname + "/../../components/prism-" + name + ".js");
137+
},
138+
139+
/**
140+
* Loads the given file source as string
141+
*
142+
* @private
143+
* @param {string} src
144+
* @returns {string}
145+
*/
146+
loadFileSource(src) {
147+
return this.fileSourceCache[src] = this.fileSourceCache[src] || fs.readFileSync(src, "utf8");
148+
},
149+
150+
151+
checkCache: null,
152+
153+
/**
154+
* Returns a list of files which add additional checks to Prism functions.
155+
*
156+
* @returns {ReadonlyArray<string>}
157+
*/
158+
getChecks() {
159+
return this.checkCache = this.checkCache || getAllFiles(__dirname + "/../checks");
112160
},
113161

114162

0 commit comments

Comments
 (0)
0