File: test.js

package info (click to toggle)
node-regex-not 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 108 kB
  • ctags: 5
  • sloc: makefile: 4; sh: 2
file content (58 lines) | stat: -rw-r--r-- 2,094 bytes parent folder | download
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
'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 an error when invalid args are passed', function(cb) {
    try {
      not();
      cb(new Error('expected an error'));
    } catch (err) {
      assert(err);
      assert.equal(err.message, 'expected a string');
      cb();
    }
  });
});