Skip to content

Commit 2f0a715

Browse files
committed
add test case using mocha.
1 parent dd99e93 commit 2f0a715

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea/
2-
.node_modules/
2+
node_modules/
33
.DS_Store

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
* @returns {boolean}
1717
*/
1818
isEmail: function (str) {
19-
return /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*$/i.test(str);
19+
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(str);
2020
},
2121

2222
/**

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"url": "https://github.com/SFantasy/node-validator/issues"
2323
},
2424
"homepage": "https://github.com/SFantasy/node-validator",
25-
"dependencies": {}
25+
"dependencies": {},
26+
"devDependencies": {
27+
"mocha": "^1.21.4"
28+
}
2629
}

test/test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
*
3+
* test
4+
*
5+
* @description
6+
* @author Fantasy <fantasyshao@icloud.com>
7+
* @create 2014-09-10
8+
* @update 2014-09-10
9+
*/
10+
11+
var validator = require('../index');
12+
var assert = require('assert');
13+
var format = require('util').format;
14+
15+
function test(options) {
16+
var args = options.args || [];
17+
args.unshift(null);
18+
if (options.valid) {
19+
options.valid.forEach(function (valid) {
20+
args[0] = valid;
21+
if (validator[options.validator].apply(validator, args) !== true) {
22+
var warning = format('validator.%s(%s) failed but should have passed',
23+
options.validator, args.join(', '));
24+
throw new Error(warning);
25+
}
26+
});
27+
}
28+
if (options.invalid) {
29+
options.invalid.forEach(function (invalid) {
30+
args[0] = invalid;
31+
if (validator[options.validator].apply(validator, args) !== false) {
32+
var warning = format('validator.%s(%s) passed but should have failed',
33+
options.validator, args.join(', '));
34+
throw new Error(warning);
35+
}
36+
});
37+
}
38+
}
39+
40+
describe('Validate', function () {
41+
42+
describe('#isEmail', function () {
43+
it('should validate email address', function (done) {
44+
test({
45+
validator: 'isEmail',
46+
valid: [
47+
'abc@test.com',
48+
'a0gg@gmail.com',
49+
'test123@abc.com.cn',
50+
'test.123@gmail.com'
51+
],
52+
invalid: [
53+
'abcd@',
54+
'@test.com'
55+
]
56+
});
57+
58+
done();
59+
});
60+
});
61+
62+
});

0 commit comments

Comments
 (0)