Skip to content

Commit 6a9b45d

Browse files
committed
feature(mocha/grep): improve grep - issue mochajs#808
Add the ability to pass grep as a regexp-like string. (query in the browser or flag in the cli). This improvement gives you pass a regexp that including a flag, and solved mochajs#808 as well.
1 parent f9fad1b commit 6a9b45d

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

bin/_mocha

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ mocha.suite.bail(program.bail);
254254

255255
// --grep
256256

257-
if (program.grep) mocha.grep(new RegExp(program.grep));
257+
if (program.grep) mocha.grep(program.grep);
258258

259259
// --fgrep
260260

261-
if (program.fgrep) mocha.grep(program.fgrep);
261+
if (program.fgrep) mocha.fgrep(program.fgrep);
262262

263263
// --invert
264264

lib/mocha.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function Mocha(options) {
7878
this.files = [];
7979
this.options = options;
8080
if (options.grep) this.grep(new RegExp(options.grep));
81-
if (options.fgrep) this.grep(options.fgrep);
81+
if (options.fgrep) this.fgrep(options.fgrep);
8282
this.suite = new exports.Suite('', new exports.Context);
8383
this.ui(options.ui);
8484
this.bail(options.bail);
@@ -220,17 +220,33 @@ Mocha.prototype._growl = function(runner, reporter) {
220220
};
221221

222222
/**
223-
* Add regexp to grep, if `re` is a string it is escaped.
223+
* Escape string and add it to grep as a regexp.
224+
*
225+
* @param {String} str
226+
* @return {Mocha}
227+
* @api public
228+
*/
229+
230+
Mocha.prototype.fgrep = function(str) {
231+
return this.grep(new RegExp(escapeRe(str)));
232+
};
233+
234+
/**
235+
* Add regexp to grep.
224236
*
225237
* @param {RegExp|String} re
226238
* @return {Mocha}
227239
* @api public
228240
*/
229241

230-
Mocha.prototype.grep = function(re){
231-
this.options.grep = 'string' == typeof re
232-
? new RegExp(escapeRe(re))
233-
: re;
242+
Mocha.prototype.grep = function(re) {
243+
if('string' == typeof re) {
244+
// extract args if it's regex-like, i.e: [string, pattern, flag]
245+
var arg = re.match(/^\/(.*)\/(g|i|)$|.*/);
246+
this.options.grep = new RegExp(arg[1] || arg[0], arg[2]);
247+
} else {
248+
this.options.grep = re;
249+
}
234250
return this;
235251
};
236252

support/tail.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ mocha.run = function(fn){
139139
mocha.globals('location');
140140

141141
var query = Mocha.utils.parseQuery(global.location.search || '');
142-
if (query.grep) mocha.grep(new RegExp(query.grep));
143-
if (query.fgrep) mocha.grep(query.fgrep);
142+
if (query.grep) mocha.grep(query.grep);
143+
if (query.fgrep) mocha.fgrep(query.fgrep);
144144
if (query.invert) mocha.invert();
145145

146146
return Mocha.prototype.run.call(mocha, function(err){

test/grep.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,35 @@ describe('Mocha', function(){
2020
})
2121
})
2222

23-
describe('.grep()', function(){
24-
it('should add a RegExp to the mocha.options object', function(){
25-
var mocha = new Mocha;
26-
mocha.grep(/foo/);
27-
mocha.options.grep.toString().should.equal('/foo/');
28-
})
23+
describe('.grep()', function() {
24+
// Test helper
25+
function testGrep(mocha) {
26+
return function testGrep(grep, expected) {
27+
mocha.grep(grep);
28+
mocha.options.grep.toString().should.equal(expected);
29+
}
30+
}
2931

30-
it('should convert grep string to a RegExp', function(){
31-
var mocha = new Mocha;
32-
mocha.grep('foo');
33-
mocha.options.grep.toString().should.equal('/foo/');
34-
})
32+
it('should add a RegExp to the mocha.options object', function() {
33+
var test = testGrep(new Mocha);
34+
test(/foo/, '/foo/');
35+
});
36+
37+
it('should convert grep string to a RegExp', function() {
38+
var test = testGrep(new Mocha);
39+
test('foo', '/foo/');
40+
test('^foo.*bar$', '/^foo.*bar$/');
41+
test('^@.*(?=\\(\\)$)', '/^@.*(?=\\(\\)$)/');
42+
});
43+
44+
it('should covert grep regex-like string to a RegExp', function() {
45+
var test = testGrep(new Mocha);
46+
test('/foo/', '/foo/');
47+
// Keep the flags
48+
test('/baz/i', '/baz/i');
49+
test('/bar/g', '/bar/g');
50+
test('/^foo(.*)bar/g', '/^foo(.*)bar/g');
51+
});
3552

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

0 commit comments

Comments
 (0)