-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e45a707
commit bcef888
Showing
3 changed files
with
130 additions
and
113 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,107 @@ | ||
import { toRegExp } from '../../src/helpers'; | ||
import { toRegExp, inferValue } from '../../src/helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'scriptlets-redirects helpers'; | ||
|
||
module(name); | ||
|
||
test('Test toRegExp for valid inputs', (assert) => { | ||
const DEFAULT_VALUE = '.?'; | ||
const defaultRegexp = new RegExp(DEFAULT_VALUE); | ||
let inputStr; | ||
let expRegex; | ||
// test('Test toRegExp for valid inputs', (assert) => { | ||
// const DEFAULT_VALUE = '.?'; | ||
// const defaultRegexp = new RegExp(DEFAULT_VALUE); | ||
// let inputStr; | ||
// let expRegex; | ||
|
||
inputStr = '/abc/'; | ||
expRegex = /abc/; | ||
assert.deepEqual(toRegExp(inputStr), expRegex); | ||
// inputStr = '/abc/'; | ||
// expRegex = /abc/; | ||
// assert.deepEqual(toRegExp(inputStr), expRegex); | ||
|
||
inputStr = '/[a-z]{1,9}/'; | ||
expRegex = /[a-z]{1,9}/; | ||
assert.deepEqual(toRegExp(inputStr), expRegex); | ||
// inputStr = '/[a-z]{1,9}/'; | ||
// expRegex = /[a-z]{1,9}/; | ||
// assert.deepEqual(toRegExp(inputStr), expRegex); | ||
|
||
inputStr = ''; | ||
assert.deepEqual(toRegExp(inputStr), defaultRegexp); | ||
}); | ||
// inputStr = ''; | ||
// assert.deepEqual(toRegExp(inputStr), defaultRegexp); | ||
// }); | ||
|
||
// test('Test toRegExp for invalid inputs', (assert) => { | ||
// let inputStr; | ||
|
||
// assert.throws(() => { | ||
// inputStr = '/\\/'; | ||
// toRegExp(inputStr); | ||
// }); | ||
|
||
// assert.throws(() => { | ||
// inputStr = '/[/'; | ||
// toRegExp(inputStr); | ||
// }); | ||
|
||
// assert.throws(() => { | ||
// inputStr = '/*/'; | ||
// toRegExp(inputStr); | ||
// }); | ||
|
||
// assert.throws(() => { | ||
// inputStr = '/[0-9]++/'; | ||
// toRegExp(inputStr); | ||
// }); | ||
// }); | ||
|
||
test('inferValue works as expected with valid args', (assert) => { | ||
// convert to number | ||
let rawString = '1234'; | ||
let expected = 1234; | ||
let result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to number ok'); | ||
|
||
// convert to float | ||
rawString = '-12.34'; | ||
expected = -12.34; | ||
result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to float ok'); | ||
|
||
// convert to boolean | ||
rawString = 'false'; | ||
expected = false; | ||
result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to false ok'); | ||
|
||
rawString = 'true'; | ||
expected = true; | ||
result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to true ok'); | ||
|
||
test('Test toRegExp for invalid inputs', (assert) => { | ||
let inputStr; | ||
// convert to undefined | ||
rawString = 'undefined'; | ||
expected = undefined; | ||
result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to undefined ok'); | ||
|
||
assert.throws(() => { | ||
inputStr = '/\\/'; | ||
toRegExp(inputStr); | ||
}); | ||
// convert to null | ||
rawString = 'null'; | ||
expected = null; | ||
result = inferValue(rawString); | ||
assert.strictEqual(result, expected, 'value to null ok'); | ||
|
||
assert.throws(() => { | ||
inputStr = '/[/'; | ||
toRegExp(inputStr); | ||
}); | ||
// convert to NaN | ||
rawString = 'NaN'; | ||
result = inferValue(rawString); | ||
assert.ok(Number.isNaN(result), 'value to NaN ok'); | ||
|
||
assert.throws(() => { | ||
inputStr = '/*/'; | ||
toRegExp(inputStr); | ||
}); | ||
// convert to object | ||
rawString = '{"aaa":123,"bbb":{"ccc":"string"}}'; | ||
expected = { | ||
aaa: 123, | ||
bbb: { | ||
ccc: 'string', | ||
}, | ||
}; | ||
result = inferValue(rawString); | ||
assert.deepEqual(result, expected, 'value to object ok'); | ||
|
||
assert.throws(() => { | ||
inputStr = '/[0-9]++/'; | ||
toRegExp(inputStr); | ||
}); | ||
// convert to array | ||
rawString = '[1,2,"string"]'; | ||
expected = [1, 2, 'string']; | ||
result = inferValue(rawString); | ||
assert.deepEqual(result, expected, 'value to array ok'); | ||
}); |