Skip to content

Commit

Permalink
Merge pull request #1808 from a8m/improve-grep-issue-808
Browse files Browse the repository at this point in the history
feature(mocha/grep): improve grep - issue #808
  • Loading branch information
boneskull committed Aug 16, 2015
2 parents 0b9876b + 194f19e commit 148b33e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 39 deletions.
4 changes: 2 additions & 2 deletions bin/_mocha
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions lib/mocha.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions support/browser-entry.js
Expand Up @@ -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){
Expand Down
59 changes: 38 additions & 21 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;
})
})
})
});
});
});
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 @@ -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();
});
});
});

Expand All @@ -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();
Expand Down

0 comments on commit 148b33e

Please sign in to comment.