Skip to content

Commit

Permalink
Merge pull request mochajs#368 from antoviaque/issue124
Browse files Browse the repository at this point in the history
Fixes issues mochajs#124 and mochajs#166 - Proper display of suites/tests count when using --grep
  • Loading branch information
tj committed Apr 10, 2012
2 parents 19ed306 + 0b7b6f6 commit 2e56414
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/_mocha
Expand Up @@ -269,10 +269,10 @@ function load(files, fn) {
function run(suite, fn) {
suite.emit('run');
var runner = new Runner(suite);
var reporter = new Reporter(runner);
runner.globals(program.globals);
if (program.ignoreLeaks) runner.ignoreLeaks = true;
if (program.grep) runner.grep(new RegExp(program.grep));
var reporter = new Reporter(runner);
if (program.growl) growl(runner, reporter);
runner.run(fn);
}
Expand Down
32 changes: 30 additions & 2 deletions lib/runner.js
Expand Up @@ -53,7 +53,8 @@ function Runner(suite) {
Runner.prototype.__proto__ = EventEmitter.prototype;

/**
* Run tests with full titles matching `re`.
* Run tests with full titles matching `re`. Updates runner.total
* with number of tests matched.
*
* @param {RegExp} re
* @return {Runner} for chaining
Expand All @@ -63,9 +64,33 @@ Runner.prototype.__proto__ = EventEmitter.prototype;
Runner.prototype.grep = function(re){
debug('grep %s', re);
this._grep = re;
this.total = this.grepTotal(this.suite);

return this;
};

/**
* Returns the number of tests matching the grep search for the
* given suite.
*
* @param {Suite} suite
* @return {Number}
* @api public
*/

Runner.prototype.grepTotal = function(suite) {
var self = this;
var total = 0;

suite.eachTest(function(test){
if (self._grep.test(test.fullTitle())){
total++;
};
});

return total;
};

/**
* Allow the given `arr` of globals.
*
Expand Down Expand Up @@ -346,7 +371,10 @@ Runner.prototype.runSuite = function(suite, fn){
, i = 0;

debug('run suite %s', suite.fullTitle());
this.emit('suite', this.suite = suite);

if(self.grepTotal(suite)) {
this.emit('suite', this.suite = suite);
}

function next() {
var curr = suite.suites[i++];
Expand Down
22 changes: 22 additions & 0 deletions lib/suite.js
Expand Up @@ -245,3 +245,25 @@ Suite.prototype.total = function(){
return sum + suite.total();
}, 0) + this.tests.length;
};

/**
* Iterates through each suite recursively to find
* all tests. Applies a function in the format
* `fn(test)`.
*
* @param {Function} fn
* @return {Suite}
* @api public
*/

Suite.prototype.eachTest = function(fn){
var self = this;
utils.forEach(self.tests, function(test){
fn(test);
});
utils.forEach(self.suites, function(suite){
suite.eachTest(fn);
});
return this;
};

26 changes: 24 additions & 2 deletions test/runner.js
@@ -1,7 +1,8 @@

var mocha = require('../')
, Suite = mocha.Suite
, Runner = mocha.Runner;
, Runner = mocha.Runner
, Test = mocha.Test;

describe('Runner', function(){
var suite, runner;
Expand All @@ -11,6 +12,27 @@ describe('Runner', function(){
runner = new Runner(suite);
})

describe('.grep()', function(){
it('should update the runner.total with number of matched tests', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
var newRunner = new Runner(suite);
newRunner.grep(/lions/);
newRunner.total.should.equal(2);
})
})

describe('.grepTotal()', function(){
it('should return the total number of matched tests', function(){
suite.addTest(new Test('im a test about lions'));
suite.addTest(new Test('im another test about lions'));
suite.addTest(new Test('im a test about bears'));
runner.grep(/lions/);
runner.grepTotal(suite).should.equal(2);
})
})

describe('.globals()', function(){
it('should default to the known globals', function(){
runner.globals().length.should.be.above(10);
Expand Down Expand Up @@ -102,4 +124,4 @@ describe('Runner', function(){
runner.failHook(hook, err);
})
})
})
})
48 changes: 48 additions & 0 deletions test/suite.js
Expand Up @@ -249,4 +249,52 @@ describe('Suite', function(){
});
});
});

describe('.eachTest(fn)', function(){
beforeEach(function(){
this.suite = new Suite('A Suite');
});

describe('when there are no nested suites or tests', function(){
it('should return 0', function(){
var counter = 0;
function fn(){
counter++;
}
this.suite.eachTest(fn);
counter.should.equal(0);
});
});

describe('when there are several tests in the suite', function(){
it('should return the number', function(){
this.suite.addTest(new Test('a child test'));
this.suite.addTest(new Test('another child test'));

var counter = 0;
function fn(){
counter++;
}
this.suite.eachTest(fn);
counter.should.equal(2);
});
});

describe('when there are several levels of nested suites', function(){
it('should return the number', function(){
this.suite.addTest(new Test('a child test'));
var suite = (new Suite('a child suite'));
suite.addTest(new Test('a test in a child suite'));
this.suite.addSuite(suite);

var counter = 0;
function fn(){
counter++;
}
this.suite.eachTest(fn);
counter.should.equal(2);
});
});

});
});

0 comments on commit 2e56414

Please sign in to comment.