Skip to content

Commit

Permalink
feature(mocha/grep): improve grep - issue #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 or flag in the cli).
This improvement gives you pass a regexp that including a flag, and
solved #808 as well.
  • Loading branch information
a8m authored and boneskull committed Jul 2, 2016
1 parent b52b175 commit 6126e38
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 40 deletions.
4 changes: 2 additions & 2 deletions bin/_mocha
Expand Up @@ -253,11 +253,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
4 changes: 2 additions & 2 deletions browser-entry.js
Expand Up @@ -132,8 +132,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
22 changes: 19 additions & 3 deletions lib/mocha.js
Expand Up @@ -80,7 +80,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);
Expand Down Expand Up @@ -246,6 +246,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.
*
Expand All @@ -256,10 +267,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.
*
Expand Down
61 changes: 39 additions & 22 deletions test/grep.js
Expand Up @@ -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();
})
})
})
mocha.options.invert.should.be.ok;
});
});
});
7 changes: 6 additions & 1 deletion 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');
});
Expand Down
34 changes: 24 additions & 10 deletions test/integration/options.js
Expand Up @@ -125,15 +125,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();
});
});
});

Expand All @@ -143,7 +157,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();
Expand Down

0 comments on commit 6126e38

Please sign in to comment.