Skip to content

Commit

Permalink
feature(mocha/grep): improve grep - issue mochajs#808
Browse files Browse the repository at this point in the history
Add the ability to pass grep as a regexp-like string(query in the
browser, and flag in the console).
This improvement gives you pass a regexp that including a flag.
e.g: '/[^0-9]/g', '/a/i', etc...
  • Loading branch information
a8m committed Apr 10, 2015
1 parent f9fad1b commit a7085b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
6 changes: 3 additions & 3 deletions bin/_mocha
Expand Up @@ -16,7 +16,7 @@ var program = require('commander'),
getOptions = require('./options'),
mocha = new Mocha;

/**
/**
* Save timer references to avoid Sinon interfering (see GH-237).
*/

Expand Down Expand Up @@ -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

Expand Down
28 changes: 22 additions & 6 deletions lib/mocha.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 2 additions & 2 deletions support/tail.js
Expand Up @@ -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){
Expand Down
39 changes: 28 additions & 11 deletions test/grep.js
Expand Up @@ -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('/^\/(.*)\/(g|i|)$|.*/g', '/^\\/(.*)\\/(g|i|)$|.*/g');
});

it('should return it\'s parent Mocha object for chainability', function(){
var mocha = new Mocha;
Expand Down

0 comments on commit a7085b1

Please sign in to comment.