-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
59 lines (51 loc) · 2.11 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
require('mocha');
var assert = require('assert');
var not = require('./');
describe('regex-not', function() {
it('should export a function', function() {
assert.equal(typeof not, 'function');
});
it('should create a negation regex', function() {
var re = not('foo');
assert.deepEqual(re, /^(?:(?!^(?:foo)$).)+$/);
assert.equal(re.test('foo'), false);
assert.equal(re.test('bar'), true);
assert.equal(re.test('foobar'), true);
assert.equal(re.test('barfoo'), true);
});
it('should create a loose negation regex when `options.contains` is true', function() {
assert.deepEqual(not('foo', {contains: true}), /^(?:(?!(?:foo)).)+$/);
assert.equal(not('foo', {contains: true}).test('foo'), false);
assert.equal(not('foo', {contains: true}).test('bar'), true);
assert.equal(not('foo', {contains: true}).test('foobar'), false);
assert.equal(not('foo', {contains: true}).test('barfoo'), false);
});
it('should create a loose negation regex when `options.strictNegate` is false', function() {
var opts = {strictNegate: false};
assert.deepEqual(not('foo', opts), /^(?:(?!(?:foo)).)+$/);
assert.equal(not('foo', opts).test('foo'), false);
assert.equal(not('foo', opts).test('bar'), true);
assert.equal(not('foo', opts).test('foobar'), false);
assert.equal(not('foo', opts).test('barfoo'), false);
});
it('should support `options.endChar`', function() {
var opts = {endChar: '*'};
assert.deepEqual(not('foo', opts), /^(?:(?!^(?:foo)$).)*$/);
assert.deepEqual(not('foo', opts).exec('foo'), null);
assert.equal(not('foo', opts).test('foo'), false);
assert.equal(not('foo', opts).test('bar'), true);
assert.equal(not('foo', opts).test('foobar'), true);
assert.equal(not('foo', opts).test('barfoo'), true);
});
it('should throw when a potentially unsafe regex is passed', function() {
assert.throws(function() {
not('(x+x+)+y', { safe: true });
}, /potentially unsafe/);
});
it('should throw an error when invalid args are passed', function() {
assert.throws(function() {
not();
}, /expected/);
});
});