-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add assert helper for supporting node 10.x
- Loading branch information
Showing
3 changed files
with
50 additions
and
2 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,48 @@ | ||
'use strict'; | ||
// support for older node versions (<12) | ||
const assert = require('assert'); | ||
|
||
const internalMatch = (string, regexp, message, fn) => { | ||
if (!regexp.test) { | ||
throw new Error('regexp parameter is not a RegExp'); | ||
} | ||
if (typeof string !== 'string') { | ||
throw new Error('string parameter is not a string'); | ||
} | ||
const match = fn.name === 'match'; | ||
|
||
const result = string.match(regexp); | ||
if (match && !result) { | ||
if (message) { | ||
throw message; | ||
} else { | ||
throw new Error(`${string} does not match regex ${regexp}`); | ||
} | ||
} | ||
if (!match && result) { | ||
if (message) { | ||
throw message; | ||
} else { | ||
throw new Error(`${string} does match regex ${regexp}`); | ||
} | ||
} | ||
}; | ||
|
||
|
||
if (!assert.match) { | ||
const match = (string, regexp, message) => { | ||
internalMatch(string, regexp, message, match); | ||
}; | ||
assert.match = match; | ||
} | ||
if (!assert.strict.match) assert.strict.match = assert.match; | ||
|
||
if (!assert.doesNotMatch) { | ||
const doesNotMatch = (string, regexp, message) => { | ||
internalMatch(string, regexp, message, doesNotMatch); | ||
}; | ||
assert.doesNotMatch = doesNotMatch; | ||
} | ||
if (!assert.strict.doesNotMatch) assert.strict.doesNotMatch = assert.doesNotMatch; | ||
|
||
module.exports = assert; |
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
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