diff --git a/bin/_mocha b/bin/_mocha index 0086568523..dc0e82bf1b 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -258,11 +258,11 @@ mocha.suite.bail(program.bail); // --grep -if (program.grep) mocha.grep(new RegExp(program.grep)); +if (program.grep) mocha.grep(program.grep); // --fgrep -if (program.fgrep) mocha.grep(program.fgrep); +if (program.fgrep) mocha.fgrep(program.fgrep); // --invert diff --git a/lib/mocha.js b/lib/mocha.js index 1817ccf2c6..4799377654 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -79,7 +79,7 @@ function Mocha(options) { this.grep(new RegExp(options.grep)); } if (options.fgrep) { - this.grep(options.fgrep); + this.fgrep(options.fgrep); } this.suite = new exports.Suite('', new exports.Context()); this.ui(options.ui); @@ -240,6 +240,17 @@ Mocha.prototype._growl = function(runner, reporter) { }); }; +/** + * Escape string and add it to grep as a regexp. + * + * @api public + * @param str + * @returns {Mocha} + */ +Mocha.prototype.fgrep = function(str) { + return this.grep(new RegExp(escapeRe(str))); +}; + /** * Add regexp to grep, if `re` is a string it is escaped. * @@ -248,10 +259,15 @@ Mocha.prototype._growl = function(runner, reporter) { * @return {Mocha} */ Mocha.prototype.grep = function(re) { - this.options.grep = typeof re === 'string' ? new RegExp(escapeRe(re)) : re; + if (utils.isString(re)) { + // extract args if it's regex-like, i.e: [string, pattern, flag] + var arg = re.match(/^\/(.*)\/(g|i|)$|.*/); + this.options.grep = new RegExp(arg[1] || arg[0], arg[2]); + } else { + this.options.grep = re; + } return this; }; - /** * Invert `.grep()` matches. * diff --git a/support/browser-entry.js b/support/browser-entry.js index 7655807fae..5bd93af4c2 100644 --- a/support/browser-entry.js +++ b/support/browser-entry.js @@ -126,8 +126,8 @@ mocha.run = function(fn){ mocha.globals('location'); var query = Mocha.utils.parseQuery(global.location.search || ''); - if (query.grep) mocha.grep(new RegExp(query.grep)); - if (query.fgrep) mocha.grep(query.fgrep); + if (query.grep) mocha.grep(query.grep); + if (query.fgrep) mocha.fgrep(query.fgrep); if (query.invert) mocha.invert(); return Mocha.prototype.run.call(mocha, function(err){ diff --git a/test/grep.js b/test/grep.js index 633aab4e63..bbc5c55eba 100644 --- a/test/grep.js +++ b/test/grep.js @@ -5,44 +5,61 @@ describe('Mocha', function(){ it('should add a RegExp to the mocha.options object', function(){ var mocha = new Mocha({ grep: /foo.*/ }); mocha.options.grep.toString().should.equal('/foo.*/'); - }) + }); it('should convert string to a RegExp', function(){ var mocha = new Mocha({ grep: 'foo.*' }); mocha.options.grep.toString().should.equal('/foo.*/'); - }) - }) + }); + }); describe('"fgrep" option', function(){ it('should escape and convert string to a RegExp', function(){ var mocha = new Mocha({ fgrep: 'foo.*' }); mocha.options.grep.toString().should.equal('/foo\\.\\*/'); - }) - }) + }); + }); - describe('.grep()', function(){ - it('should add a RegExp to the mocha.options object', function(){ - var mocha = new Mocha; - mocha.grep(/foo/); - mocha.options.grep.toString().should.equal('/foo/'); - }) + describe('.grep()', function() { + // Test helper + function testGrep(mocha) { + return function testGrep(grep, expected) { + mocha.grep(grep); + mocha.options.grep.toString().should.equal(expected); + } + } - it('should convert grep string to a RegExp', function(){ - var mocha = new Mocha; - mocha.grep('foo'); - mocha.options.grep.toString().should.equal('/foo/'); - }) + it('should add a RegExp to the mocha.options object', function() { + var test = testGrep(new Mocha); + test(/foo/, '/foo/'); + }); + + it('should convert grep string to a RegExp', function() { + var test = testGrep(new Mocha); + test('foo', '/foo/'); + test('^foo.*bar$', '/^foo.*bar$/'); + test('^@.*(?=\\(\\)$)', '/^@.*(?=\\(\\)$)/'); + }); + + it('should covert grep regex-like string to a RegExp', function() { + var test = testGrep(new Mocha); + test('/foo/', '/foo/'); + // Keep the flags + test('/baz/i', '/baz/i'); + test('/bar/g', '/bar/g'); + test('/^foo(.*)bar/g', '/^foo(.*)bar/g'); + }); it('should return it\'s parent Mocha object for chainability', function(){ var mocha = new Mocha; mocha.grep().should.equal(mocha); - }) - }) + }); + }); describe('"invert" option', function(){ it('should add a Boolean to the mocha.options object', function(){ var mocha = new Mocha({ invert: true }); mocha.options.invert.should.be.ok; - }) - }) -}) + }); + }); +}); diff --git a/test/integration/fixtures/options/grep.js b/test/integration/fixtures/options/grep.js index 5e163f5ee7..94785dc7db 100644 --- a/test/integration/fixtures/options/grep.js +++ b/test/integration/fixtures/options/grep.js @@ -1,10 +1,15 @@ describe('grep', function() { + describe('Match', function() { + it('should run', function(){}); + it('should also run', function() {}); + }); + describe('match', function() { it('should run', function(){}); it('should also run', function() {}); }); - describe('fail', function(){ + describe('fail', function() { it('should not be ran', function() { throw new Error('Spec should not run'); }); diff --git a/test/integration/options.js b/test/integration/options.js index db3710913b..d4a32a03fe 100644 --- a/test/integration/options.js +++ b/test/integration/options.js @@ -111,15 +111,29 @@ describe('options', function() { }); }); - it('runs specs matching a RegExp', function(done) { - args = ['--grep', '.*']; - run('options/grep.js', args, function(err, res) { - assert(!err); - assert.equal(res.stats.pending, 0); - assert.equal(res.stats.passes, 2); - assert.equal(res.stats.failures, 1); - assert.equal(res.code, 1); - done(); + describe('runs specs matching a RegExp', function() { + it('with RegExp like strings(pattern follow by flag)', function(done) { + args = ['--grep', '/match/i']; + run('options/grep.js', args, function(err, res) { + assert(!err); + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 4); + assert.equal(res.stats.failures, 0); + assert.equal(res.code, 0); + done(); + }); + }); + + it('string as pattern', function(done) { + args = ['--grep', '.*']; + run('options/grep.js', args, function(err, res) { + assert(!err); + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 4); + assert.equal(res.stats.failures, 1); + assert.equal(res.code, 1); + done(); + }); }); }); @@ -129,7 +143,7 @@ describe('options', function() { run('options/grep.js', args, function(err, res) { assert(!err); assert.equal(res.stats.pending, 0); - assert.equal(res.stats.passes, 2); + assert.equal(res.stats.passes, 4); assert.equal(res.stats.failures, 0); assert.equal(res.code, 0); done();