-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Additional checks for Prism functions (#1803)
This adds additional checks for `extend` and `insertBefore` under `Prism.languages`.
- Loading branch information
1 parent
29a30c6
commit c3e74ea
Showing
4 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { testFunction } = require('./../helper/check-functionality'); | ||
|
||
function extendTest(id, redef) { | ||
const details = `\nextend("${id}", ${redef})`; | ||
|
||
// type checks | ||
if (typeof id !== 'string') { | ||
throw new TypeError(`The id argument has to be a 'string'.` + details); | ||
} | ||
if (typeof redef !== 'object') { | ||
throw new TypeError(`The redef argument has to be an 'object'.` + details); | ||
} | ||
|
||
|
||
if (!(id in Prism.languages)) { | ||
throw new Error(`Cannot extend '${id}' because it is not defined in Prism.languages.`); | ||
} | ||
} | ||
|
||
testFunction('extend', Prism.languages, extendTest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { testFunction } = require('./../helper/check-functionality'); | ||
|
||
function insertBeforeTest(inside, before, insert, root) { | ||
const details = `\ninsertBefore("${inside}", "${before}", ${insert}, ${root})`; | ||
|
||
// type checks | ||
if (typeof inside !== 'string') { | ||
throw new TypeError(`The inside argument has to be a 'string'.` + details); | ||
} | ||
if (typeof before !== 'string') { | ||
throw new TypeError(`The before argument has to be a 'string'.` + details); | ||
} | ||
if (typeof insert !== 'object') { | ||
throw new TypeError(`The insert argument has to be an 'object'.` + details); | ||
} | ||
if (root && typeof root !== 'object') { | ||
throw new TypeError(`The root argument has to be an 'object' if defined.` + details); | ||
} | ||
|
||
|
||
root = root || Prism.languages; | ||
var grammar = root[inside]; | ||
|
||
if (typeof grammar !== 'object') { | ||
throw new Error(`The grammar "${inside}" has to be an 'object' not '${typeof grammar}'.`); | ||
} | ||
if (!(before in grammar)) { | ||
throw new Error(`"${before}" has to be a key of the grammar "${inside}".`); | ||
} | ||
} | ||
|
||
testFunction('insertBefore', Prism.languages, insertBeforeTest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
testFunction(name, object, tester) { | ||
const func = object[name]; | ||
|
||
object[name] = function () { | ||
tester.apply(this, arguments); | ||
return func.apply(this, arguments); | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters