|
| 1 | +/*eslint quotes: [0]*/ |
1 | 2 | 'use strict';
|
2 | 3 |
|
3 | 4 | require('mocha');
|
4 | 5 | var assert = require('assert');
|
5 | 6 | var regex = require('./');
|
6 | 7 |
|
7 | 8 | describe('regex', function() {
|
8 |
| - it('should return a function:', function() { |
9 |
| - assert(typeof regex === 'function'); |
| 9 | + describe('single quotes', function() { |
| 10 | + it('should match single-quoted strings', function() { |
| 11 | + var match = `var foo = 'bar';`.match(regex()); |
| 12 | + assert.equal(match[0], `'bar'`); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should match empty single-quoted strings', function() { |
| 16 | + var match = `var foo = '';`.match(regex()); |
| 17 | + assert.equal(match[0], `''`); |
| 18 | + }); |
10 | 19 | });
|
11 | 20 |
|
12 |
| - it('should match single-quoted strings:', function() { |
13 |
| - var re = regex(); |
14 |
| - var str = 'var foo = \'bar\';'; |
15 |
| - var m = str.match(re); |
16 |
| - assert(m[0] === '\'bar\''); |
| 21 | + describe('double quotes', function() { |
| 22 | + it('should match double-quoted strings', function() { |
| 23 | + var match = 'var foo = "bar";'.match(regex()); |
| 24 | + assert.equal(match[0], '"bar"'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should match empty double-quoted strings', function() { |
| 28 | + assert.equal('var foo = "";'.match(regex())[0], '""'); |
| 29 | + assert.equal('var foo = "\'\'";'.match(regex())[0], '"\'\'"'); |
| 30 | + assert.equal(regex().exec('var foo = "\'\'";')[2], '\'\''); |
| 31 | + }); |
17 | 32 | });
|
18 | 33 |
|
19 |
| - it('should match double-quoted strings:', function() { |
20 |
| - var re = regex(); |
21 |
| - var str = 'var foo = "bar";'; |
22 |
| - var m = str.match(re); |
23 |
| - assert(m[0] === '"bar"'); |
| 34 | + describe('backticks', function() { |
| 35 | + it('should match strings in backticks', function() { |
| 36 | + var match = 'var foo = `${bar}`;'.match(regex()); |
| 37 | + assert.equal(match[0], '`${bar}`'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should match empty strings in backticks', function() { |
| 41 | + assert.equal('var foo = ``;'.match(regex())[0], '``'); |
| 42 | + assert.equal('var foo = `""`;'.match(regex())[0], '`""`'); |
| 43 | + assert.equal(regex().exec('var foo = `\'\'`;')[2], '\'\''); |
| 44 | + }); |
24 | 45 | });
|
25 | 46 |
|
26 |
| - it('should match multiple quoted strings:', function() { |
27 |
| - var re = regex(); |
28 |
| - var str = 'var foo = "bar";\nvar bar = \'baz\''; |
29 |
| - var m = str.match(re); |
30 |
| - assert(m[0] === '"bar"'); |
31 |
| - assert(m[1] === '\'baz\''); |
| 47 | + describe('multiple', function() { |
| 48 | + it('should match multiple quoted strings', function() { |
| 49 | + var match = `var foo = "one";\nvar bar = 'two'\nvar baz = \`three\``.match(regex()); |
| 50 | + assert.equal(match[0], '"one"'); |
| 51 | + assert.equal(match[1], `'two'`); |
| 52 | + assert.equal(match[2], `\`three\``); |
| 53 | + }); |
32 | 54 | });
|
33 | 55 |
|
34 |
| - it('should match complex nested quotes:', function() { |
35 |
| - var re = regex(); |
36 |
| - var str = 'foo bar ". // \' \\ . // \' \\ ." baz'; |
37 |
| - var m = str.match(re); |
38 |
| - assert(m[0] === '". // \' \\ . // \' \\ ."'); |
| 56 | + describe('escaping', function() { |
| 57 | + it('should work with escaped quotes', function() { |
| 58 | + var double = `var foo = "bar\\"baz";`.match(regex()); |
| 59 | + assert.equal(double[0], '"bar\\"baz"'); |
| 60 | + |
| 61 | + var single = `var foo = 'bar\\'baz';`.match(regex()); |
| 62 | + assert.equal(single[0], `'bar\\'baz'`); |
| 63 | + |
| 64 | + var both = `var foo = '"bar\\'\\"\\'baz"';`.match(regex()); |
| 65 | + assert.equal(both[0], `'"bar\\'\\"\\'baz"'`); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should match complex nested quotes', function() { |
| 69 | + var double = `foo bar ". // ' \\" \\ . // ' \\ ." baz`.match(regex()); |
| 70 | + assert.equal(double[0], `". // ' \\" \\ . // ' \\ ."`); |
| 71 | + |
| 72 | + var single = `foo bar '. // \\' \\" \\ . // \\' \\ .' baz`.match(regex()); |
| 73 | + assert.equal(single[0], `'. // \\' \\" \\ . // \\' \\ .'`); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should create a match for the inner string', function() { |
| 77 | + var inner = regex().exec(`foo bar \\" \\' '. // \\' \\" \\ . // \\' \\ .' \\'\\' baz`); |
| 78 | + assert.equal(inner[2], '. // \\\' \\" \\ . // \\\' \\ .'); |
| 79 | + }); |
39 | 80 | });
|
40 | 81 | });
|
0 commit comments