|
7 | 7 |
|
8 | 8 | 'use strict';
|
9 | 9 |
|
10 |
| -var mentionsRegex = require('./index'); |
11 |
| -var str = str = '@first git @tunnckoCore and @face some @al.so [email protected] glob @last' |
12 |
| -var res = str.match(mentionsRegex({flags: 'g', dot: true, startSpace: false, endSpace: false})) |
13 |
| -console.log(res) |
| 10 | +var assert = require('assert'); |
| 11 | +var regexp = require('./index'); |
| 12 | +var fixtures = require('./fixtures'); |
| 13 | + |
| 14 | +function match(str, dot) { |
| 15 | + var ex = regexp(dot).exec(str); |
| 16 | + return ex && ex[1] || null; |
| 17 | +} |
| 18 | + |
| 19 | +function test(str, dot) { |
| 20 | + return regexp(dot).test(str); |
| 21 | +} |
| 22 | + |
| 23 | +describe('mentions-regex:', function() { |
| 24 | + describe('when `dot` is true', function() { |
| 25 | + it('should match `google.com` from `foo @google.com bar`', function(done) { |
| 26 | + assert.strictEqual(match('foo @google.com bar', true), 'google.com'); |
| 27 | + assert.strictEqual(test('foo @google.com bar', true), true); |
| 28 | + done(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should not match when `[email protected] qux`', function(done) { |
| 32 | + assert.strictEqual(match('[email protected] qux', true), null); |
| 33 | + assert.strictEqual(test('[email protected] qux', true), false); |
| 34 | + done(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('when `dot` is falsey value', function() { |
| 39 | + it('should match `google` from `foo @google.com bar`', function(done) { |
| 40 | + assert.strictEqual(match('foo @google.com bar', false), 'google'); |
| 41 | + assert.strictEqual(test('foo @google.com bar', false), true); |
| 42 | + done(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not match when `[email protected] qux`', function(done) { |
| 46 | + assert.strictEqual(match('[email protected] qux', false), null); |
| 47 | + assert.strictEqual(test('[email protected] qux', false), false); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('special cases', function() { |
| 53 | + it('should match `bar_baz` from `foo @bar_baz qux`', function(done) { |
| 54 | + assert.strictEqual(match('foo @bar_baz qux'), 'bar_baz'); |
| 55 | + assert.strictEqual(test('foo @bar_baz qux'), true); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should match `_bar_baz` from `foo @_bar_baz qux`', function(done) { |
| 60 | + assert.strictEqual(match('foo @_bar_baz qux'), '_bar_baz'); |
| 61 | + assert.strictEqual(test('foo @_bar_baz qux'), true); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should match `_bar` from `foo @_bar baz qux`', function(done) { |
| 66 | + assert.strictEqual(match('foo @_bar baz qux'), '_bar'); |
| 67 | + assert.strictEqual(test('foo @_bar baz qux'), true); |
| 68 | + done(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should match `bar/baz` from `foo @bar/baz qux`', function(done) { |
| 72 | + assert.strictEqual(match('foo @bar/baz qux'), 'bar/baz'); |
| 73 | + assert.strictEqual(test('foo @bar/baz qux'), true); |
| 74 | + done(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should match `bar/baz` from `foo /@bar/baz qux`', function(done) { |
| 78 | + assert.strictEqual(match('foo /@bar/baz qux'), 'bar/baz'); |
| 79 | + assert.strictEqual(test('foo /@bar/baz qux'), true); |
| 80 | + done(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should match `bar` from `foo /@bar baz qux`', function(done) { |
| 84 | + assert.strictEqual(match('foo /@bar baz qux'), 'bar'); |
| 85 | + assert.strictEqual(test('foo /@bar baz qux'), true); |
| 86 | + done(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should match `222` from `foo @222 baz qux`', function(done) { |
| 90 | + assert.strictEqual(match('foo @222 baz qux'), '222'); |
| 91 | + assert.strictEqual(test('foo @222 baz qux'), true); |
| 92 | + done(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not match from `foo @/bar/baz qux`', function(done) { |
| 96 | + assert.strictEqual(match('foo @/bar/baz qux'), null); |
| 97 | + assert.strictEqual(test('foo @/bar/baz qux'), false); |
| 98 | + done(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should not match from `foo @/bar baz qux`', function(done) { |
| 102 | + assert.strictEqual(match('foo @/bar baz qux'), null); |
| 103 | + assert.strictEqual(test('foo @/bar baz qux'), false); |
| 104 | + done(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should not match email addresses `[email protected]`', function(done) { |
| 108 | + assert.strictEqual(match('[email protected]'), null); |
| 109 | + assert.strictEqual(test('[email protected]'), false); |
| 110 | + done(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('should match `bar` only', function() { |
| 115 | + fixtures.shouldMatchBarOnly.forEach(function _each(item) { |
| 116 | + it('from `' + item + '` string given', function(done) { |
| 117 | + assert.strictEqual(match(item), 'bar'); |
| 118 | + assert.strictEqual(test(item), true); |
| 119 | + done(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('should not match', function() { |
| 125 | + fixtures.shouldNotMatch.forEach(function _each(item) { |
| 126 | + it('from `' + item + '` string given', function(done) { |
| 127 | + assert.strictEqual(match(item), null); |
| 128 | + assert.strictEqual(test(item), false); |
| 129 | + done(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments