forked from validatorjs/validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change test files to prepare for validatorjs#1874
- Loading branch information
Showing
7 changed files
with
67 additions
and
61 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,56 @@ | ||
import assert from 'assert'; | ||
import { format } from 'util'; | ||
import validator from '../src/index'; | ||
|
||
export default function test(options) { | ||
const args = options.args || []; | ||
|
||
args.unshift(null); | ||
|
||
if (options.error) { | ||
options.error.forEach((error) => { | ||
args[0] = error; | ||
|
||
try { | ||
assert.throws(() => validator[options.validator](...args)); | ||
} catch (err) { | ||
const warning = format( | ||
'validator.%s(%s) passed but should error', | ||
options.validator, args.join(', ') | ||
); | ||
|
||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
|
||
if (options.valid) { | ||
options.valid.forEach((valid) => { | ||
args[0] = valid; | ||
|
||
if (validator[options.validator](...args) !== true) { | ||
const warning = format( | ||
'validator.%s(%s) failed but should have passed', | ||
options.validator, args.join(', ') | ||
); | ||
|
||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
|
||
if (options.invalid) { | ||
options.invalid.forEach((invalid) => { | ||
args[0] = invalid; | ||
|
||
if (validator[options.validator](...args) !== false) { | ||
const warning = format( | ||
'validator.%s(%s) passed but should have failed', | ||
options.validator, args.join(', ') | ||
); | ||
|
||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
} |
File renamed without changes.
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 |
---|---|---|
|
@@ -3,60 +3,10 @@ import fs from 'fs'; | |
import { format } from 'util'; | ||
import vm from 'vm'; | ||
import validator from '../src/index'; | ||
import test from './testFunctions'; | ||
|
||
let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); | ||
|
||
function test(options) { | ||
let args = options.args || []; | ||
args.unshift(null); | ||
if (options.error) { | ||
options.error.forEach((error) => { | ||
args[0] = error; | ||
try { | ||
assert.throws(() => validator[options.validator](...args)); | ||
} catch (err) { | ||
let warning = format( | ||
'validator.%s(%s) passed but should error', | ||
options.validator, args.join(', ') | ||
); | ||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
if (options.valid) { | ||
options.valid.forEach((valid) => { | ||
args[0] = valid; | ||
if (validator[options.validator](...args) !== true) { | ||
let warning = format( | ||
'validator.%s(%s) failed but should have passed', | ||
options.validator, args.join(', ') | ||
); | ||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
if (options.invalid) { | ||
options.invalid.forEach((invalid) => { | ||
args[0] = invalid; | ||
if (validator[options.validator](...args) !== false) { | ||
let warning = format( | ||
'validator.%s(%s) passed but should have failed', | ||
options.validator, args.join(', ') | ||
); | ||
throw new Error(warning); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function repeat(str, count) { | ||
let result = ''; | ||
for (; count; count--) { | ||
result += str; | ||
} | ||
return result; | ||
} | ||
|
||
describe('Validators', () => { | ||
it('should validate email addresses', () => { | ||
test({ | ||
|
@@ -74,9 +24,9 @@ describe('Validators', () => { | |
'"foobar"@example.com', | ||
'" foo m端ller "@example.com', | ||
'"foo\\@bar"@example.com', | ||
`${repeat('a', 64)}@${repeat('a', 63)}.com`, | ||
`${repeat('a', 64)}@${repeat('a', 63)}.com`, | ||
`${repeat('a', 31)}@gmail.com`, | ||
`${'a'.repeat(64)}@${'a'.repeat(63)}.com`, | ||
`${'a'.repeat(64)}@${'a'.repeat(63)}.com`, | ||
`${'a'.repeat(31)}@gmail.com`, | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
|
@@ -90,10 +40,10 @@ describe('Validators', () => { | |
'[email protected].', | ||
'[email protected]', | ||
'gmailgmailgmailgmailgmail@gmail.com', | ||
`${repeat('a', 64)}@${repeat('a', 251)}.com`, | ||
`${repeat('a', 65)}@${repeat('a', 250)}.com`, | ||
`${repeat('a', 64)}@${repeat('a', 64)}.com`, | ||
`${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, | ||
`${'a'.repeat(64)}@${'a'.repeat(251)}.com`, | ||
`${'a'.repeat(65)}@${'a'.repeat(250)}.com`, | ||
`${'a'.repeat(64)}@${'a'.repeat(64)}.com`, | ||
`${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`, | ||
'[email protected] m', | ||
'[email protected] m', | ||
'[email protected] m', | ||
|
@@ -128,10 +78,10 @@ describe('Validators', () => { | |
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
`${repeat('a', 30)}@gmail.com`, | ||
`${'a'.repeat(30)}@gmail.com`, | ||
], | ||
invalid: [ | ||
`${repeat('a', 31)}@gmail.com`, | ||
`${'a'.repeat(31)}@gmail.com`, | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
|