Skip to content

Commit 35b1fc1

Browse files
authored
refactor: change test files to prepare for #1874 (#2091)
1 parent 7bef722 commit 35b1fc1

7 files changed

Lines changed: 67 additions & 61 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"build:node": "babel src -d .",
6767
"build": "run-p build:*",
6868
"pretest": "npm run build && npm run lint",
69-
"test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot"
69+
"test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive"
7070
},
7171
"engines": {
7272
"node": ">= 0.10"
File renamed without changes.

test/testFunctions.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import assert from 'assert';
2+
import { format } from 'util';
3+
import validator from '../src/index';
4+
5+
export default function test(options) {
6+
const args = options.args || [];
7+
8+
args.unshift(null);
9+
10+
if (options.error) {
11+
options.error.forEach((error) => {
12+
args[0] = error;
13+
14+
try {
15+
assert.throws(() => validator[options.validator](...args));
16+
} catch (err) {
17+
const warning = format(
18+
'validator.%s(%s) passed but should error',
19+
options.validator, args.join(', ')
20+
);
21+
22+
throw new Error(warning);
23+
}
24+
});
25+
}
26+
27+
if (options.valid) {
28+
options.valid.forEach((valid) => {
29+
args[0] = valid;
30+
31+
if (validator[options.validator](...args) !== true) {
32+
const warning = format(
33+
'validator.%s(%s) failed but should have passed',
34+
options.validator, args.join(', ')
35+
);
36+
37+
throw new Error(warning);
38+
}
39+
});
40+
}
41+
42+
if (options.invalid) {
43+
options.invalid.forEach((invalid) => {
44+
args[0] = invalid;
45+
46+
if (validator[options.validator](...args) !== false) {
47+
const warning = format(
48+
'validator.%s(%s) passed but should have failed',
49+
options.validator, args.join(', ')
50+
);
51+
52+
throw new Error(warning);
53+
}
54+
});
55+
}
56+
}
File renamed without changes.
Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,10 @@ import fs from 'fs';
33
import { format } from 'util';
44
import vm from 'vm';
55
import validator from '../src/index';
6+
import test from './testFunctions';
67

78
let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString();
89

9-
function test(options) {
10-
let args = options.args || [];
11-
args.unshift(null);
12-
if (options.error) {
13-
options.error.forEach((error) => {
14-
args[0] = error;
15-
try {
16-
assert.throws(() => validator[options.validator](...args));
17-
} catch (err) {
18-
let warning = format(
19-
'validator.%s(%s) passed but should error',
20-
options.validator, args.join(', ')
21-
);
22-
throw new Error(warning);
23-
}
24-
});
25-
}
26-
if (options.valid) {
27-
options.valid.forEach((valid) => {
28-
args[0] = valid;
29-
if (validator[options.validator](...args) !== true) {
30-
let warning = format(
31-
'validator.%s(%s) failed but should have passed',
32-
options.validator, args.join(', ')
33-
);
34-
throw new Error(warning);
35-
}
36-
});
37-
}
38-
if (options.invalid) {
39-
options.invalid.forEach((invalid) => {
40-
args[0] = invalid;
41-
if (validator[options.validator](...args) !== false) {
42-
let warning = format(
43-
'validator.%s(%s) passed but should have failed',
44-
options.validator, args.join(', ')
45-
);
46-
throw new Error(warning);
47-
}
48-
});
49-
}
50-
}
51-
52-
function repeat(str, count) {
53-
let result = '';
54-
for (; count; count--) {
55-
result += str;
56-
}
57-
return result;
58-
}
59-
6010
describe('Validators', () => {
6111
it('should validate email addresses', () => {
6212
test({
@@ -74,9 +24,9 @@ describe('Validators', () => {
7424
'"foobar"@example.com',
7525
'" foo m端ller "@example.com',
7626
'"foo\\@bar"@example.com',
77-
`${repeat('a', 64)}@${repeat('a', 63)}.com`,
78-
`${repeat('a', 64)}@${repeat('a', 63)}.com`,
79-
`${repeat('a', 31)}@gmail.com`,
27+
`${'a'.repeat(64)}@${'a'.repeat(63)}.com`,
28+
`${'a'.repeat(64)}@${'a'.repeat(63)}.com`,
29+
`${'a'.repeat(31)}@gmail.com`,
8030
'test@gmail.com',
8131
'test.1@gmail.com',
8232
'test@1337.com',
@@ -90,10 +40,10 @@ describe('Validators', () => {
9040
'foo@bar.co.uk.',
9141
'z@co.c',
9242
'gmailgmailgmailgmailgmail@gmail.com',
93-
`${repeat('a', 64)}@${repeat('a', 251)}.com`,
94-
`${repeat('a', 65)}@${repeat('a', 250)}.com`,
95-
`${repeat('a', 64)}@${repeat('a', 64)}.com`,
96-
`${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`,
43+
`${'a'.repeat(64)}@${'a'.repeat(251)}.com`,
44+
`${'a'.repeat(65)}@${'a'.repeat(250)}.com`,
45+
`${'a'.repeat(64)}@${'a'.repeat(64)}.com`,
46+
`${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`,
9747
'test1@invalid.co m',
9848
'test2@invalid.co m',
9949
'test3@invalid.co m',
@@ -128,10 +78,10 @@ describe('Validators', () => {
12878
'foobar@gmail.com',
12979
'foo.bar@gmail.com',
13080
'foo.bar@googlemail.com',
131-
`${repeat('a', 30)}@gmail.com`,
81+
`${'a'.repeat(30)}@gmail.com`,
13282
],
13383
invalid: [
134-
`${repeat('a', 31)}@gmail.com`,
84+
`${'a'.repeat(31)}@gmail.com`,
13585
'test@gmail.com',
13686
'test.1@gmail.com',
13787
'.foobar@gmail.com',

0 commit comments

Comments
 (0)