diff --git a/bin/_mocha b/bin/_mocha index 5b3c0c5b29..a3b2daf452 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -254,11 +254,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 8a11124cb1..47d175ca9d 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -78,7 +78,7 @@ function Mocha(options) { this.files = []; this.options = options; if (options.grep) this.grep(new RegExp(options.grep)); - if (options.fgrep) this.grep(options.fgrep); + if (options.fgrep) this.fgrep(options.fgrep); this.suite = new exports.Suite('', new exports.Context); this.ui(options.ui); this.bail(options.bail); @@ -220,17 +220,33 @@ Mocha.prototype._growl = function(runner, reporter) { }; /** - * Add regexp to grep, if `re` is a string it is escaped. + * Escape string and add it to grep as a regexp. + * + * @param {String} str + * @return {Mocha} + * @api public + */ + +Mocha.prototype.fgrep = function(str) { + return this.grep(new RegExp(escapeRe(str))); +}; + +/** + * Add regexp to grep. * * @param {RegExp|String} re * @return {Mocha} * @api public */ -Mocha.prototype.grep = function(re){ - this.options.grep = 'string' == typeof re - ? new RegExp(escapeRe(re)) - : re; +Mocha.prototype.grep = function(re) { + if('string' == typeof 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; }; diff --git a/support/tail.js b/support/tail.js index 030177aad7..38589c719e 100644 --- a/support/tail.js +++ b/support/tail.js @@ -139,8 +139,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..e8ebeb11ed 100644 --- a/test/grep.js +++ b/test/grep.js @@ -20,18 +20,35 @@ describe('Mocha', function(){ }) }) - 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;