diff --git a/Makefile b/Makefile index a35499c882..096144201c 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ lib-cov: test: test-unit -test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers +test-all: test-bdd test-tdd test-qunit test-exports test-unit test-only test-jsapi test-compilers test-jsapi: @node test/jsapi @@ -72,16 +72,16 @@ test-exports: --ui exports \ test/acceptance/interfaces/exports -test-grep: +test-only: @./bin/mocha \ --reporter $(REPORTER) \ - --grep fast \ + --only fast \ test/acceptance/misc/grep test-invert: @./bin/mocha \ --reporter $(REPORTER) \ - --grep slow \ + --only slow \ --invert \ test/acceptance/misc/grep @@ -123,4 +123,4 @@ tm: mkdir -p $(TM_DEST) cp -fr editors/$(TM_BUNDLE) $(TM_DEST) -.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm clean +.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-only tm clean diff --git a/bin/_mocha b/bin/_mocha index 446fca5d56..7fe46591d9 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -60,8 +60,8 @@ program .option('-r, --require ', 'require the given module') .option('-R, --reporter ', 'specify the reporter to use', 'dot') .option('-u, --ui ', 'specify user-interface (bdd|tdd|exports)', 'bdd') - .option('-g, --grep ', 'only run tests matching ') - .option('-i, --invert', 'inverts --grep matches') + .option('-o, --only ', 'only run tests containing ') + .option('-i, --invert', 'inverts --only and --only-regexp matches') .option('-t, --timeout ', 'set test-case timeout in milliseconds [2000]') .option('-s, --slow ', '"slow" test threshold in milliseconds [75]') .option('-w, --watch', 'watch files for changes') @@ -78,6 +78,7 @@ program .option('--interfaces', 'display available interfaces') .option('--reporters', 'display available reporters') .option('--compilers :,...', 'use the given module(s) to compile files', list, []) + .option('--only-regexp ', 'only run tests matching ') program.name = 'mocha'; @@ -217,9 +218,13 @@ if (program.timeout) mocha.suite.timeout(program.timeout); mocha.suite.bail(program.bail); -// --grep +// --only -if (program.grep) mocha.grep(new RegExp(program.grep)); +if (program.only) mocha.only(program.only); + +// --only-regexp + +if (program.onlyRegexp) mocha.only(new RegExp(program.onlyRegexp)); // --invert diff --git a/lib/mocha.js b/lib/mocha.js index 4604f6cdc4..fd0fed6c40 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -64,7 +64,8 @@ function Mocha(options) { options = options || {}; this.files = []; this.options = options; - this.grep(options.grep); + if (options.only) this.only(options.only); + if (options['only-regexp']) this.only(new RegExp(options['only-regexp'])); this.suite = new exports.Suite('', new exports.Context); this.ui(options.ui); this.reporter(options.reporter); @@ -165,17 +166,17 @@ Mocha.prototype._growl = function(runner, reporter) { }; /** - * Add regexp to grep, if `re` is a string it is escaped. + * Add a pattern to grep, if `pattern` is a string, it is escaped. * - * @param {RegExp|String} re + * @param {RegExp|String} pattern * @return {Mocha} * @api public */ -Mocha.prototype.grep = function(re){ - this.options.grep = 'string' == typeof re - ? new RegExp(utils.escapeRegexp(re)) - : re; +Mocha.prototype.only = function(pattern){ + this.options.grep = 'string' == typeof pattern + ? new RegExp(utils.escapeRegexp(pattern)) + : pattern; return this; }; diff --git a/support/tail.js b/support/tail.js index 9b7a533bb9..a83269ce00 100644 --- a/support/tail.js +++ b/support/tail.js @@ -100,7 +100,8 @@ process.on = function(e, fn){ mocha.globals('location'); var query = Mocha.utils.parseQuery(window.location.search || ''); - if (query.grep) mocha.grep(query.grep); + if (query.only) mocha.only(query.only); + if (query['only-regexp']) mocha.only(new RegExp(query['only-regexp'])); if (query.invert) mocha.invert(); return Mocha.prototype.run.call(mocha, function(){ diff --git a/test/grep.js b/test/grep.js index b4be3715f1..1097b088b8 100644 --- a/test/grep.js +++ b/test/grep.js @@ -2,34 +2,46 @@ var Mocha = require('../'); describe('Mocha', function(){ - describe('"grep" option', function(){ + describe('"only-regexp" option', 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/'); + var mocha = new Mocha({ 'only-regexp': /foo()/ }); + mocha.options.grep.toString().should.equal('/foo()/'); }) - it('should convert grep string to a RegExp', 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({ 'only-regexp': 'foo()' }); + mocha.options.grep.toString().should.equal('/foo()/'); }) }) - describe('.grep()', function(){ + describe('.only()', 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/'); + mocha.only(/foo()/); + mocha.options.grep.toString().should.equal('/foo()/'); }) - it('should convert grep string to a RegExp', function(){ + it('should convert string to an escaped RegExp', function(){ var mocha = new Mocha; - mocha.grep('foo'); - mocha.options.grep.toString().should.equal('/foo/'); + mocha.only('foo()'); + mocha.options.grep.toString().should.equal('/foo\\(\\)/'); }) it('should return it\'s parent Mocha object for chainability', function(){ var mocha = new Mocha; - mocha.grep().should.equal(mocha); + mocha.only().should.equal(mocha); + }) + }) + + describe('"only" option', function(){ + it('should add a RegExp to the mocha.options object', function(){ + var mocha = new Mocha({ 'only': /foo()/ }); + mocha.options.grep.toString().should.equal('/foo()/'); + }) + + it('should convert string to an escaped RegExp', function(){ + var mocha = new Mocha({ 'only': 'foo()' }); + mocha.options.grep.toString().should.equal('/foo\\(\\)/'); }) })