From bdcde2f29303586048b286887ebe46810e03d28e Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Tue, 10 Apr 2012 16:33:57 -0400 Subject: [PATCH 1/8] Fix issue #325 (add better grep support to js api) --- support/tail.js | 10 ++++++++++ test/browser/grep.html | 26 ++++++++++++++++++++++++++ test/browser/grep.js | 15 +++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 test/browser/grep.html create mode 100644 test/browser/grep.js diff --git a/support/tail.js b/support/tail.js index e3d447274c..3604b30aff 100644 --- a/support/tail.js +++ b/support/tail.js @@ -128,6 +128,15 @@ window.mocha = require('mocha'); suite.emit('pre-require', window); }; + /** + * Set a string to grep for. + */ + + mocha.grep = function(re){ + if ('string' === typeof re) options.grep = re; + return this; + }; + /** * Run mocha, returning the Runner. */ @@ -139,6 +148,7 @@ window.mocha = require('mocha'); var reporter = new Reporter(runner); var query = parse(window.location.search || ""); if (query.grep) runner.grep(new RegExp(query.grep)); + if (options.grep) runner.grep(new RegExp(options.grep)); if (options.ignoreLeaks) runner.ignoreLeaks = true; if (options.globals) runner.globals(options.globals); runner.globals(['location']); diff --git a/test/browser/grep.html b/test/browser/grep.html new file mode 100644 index 0000000000..49323dfa33 --- /dev/null +++ b/test/browser/grep.html @@ -0,0 +1,26 @@ + + + Mocha + + + + + + + + + +
+ + diff --git a/test/browser/grep.js b/test/browser/grep.js new file mode 100644 index 0000000000..62b1eaad07 --- /dev/null +++ b/test/browser/grep.js @@ -0,0 +1,15 @@ +describe('grep', function() { + it('should add regex to mocha options', function() { + var re = new RegExp('grep'); + + function reEqual(r1, r2){ + return r1.source === r2.source + && r1.global === r2.global + && r1.ignoreCase === r2.ignoreCase + && r1.multiline === r2.multiline; + } + + assert(reEqual(runner._grep, re)); + + }); +}); From 4b1669cd1c53d5124c0e8f222283c791e1be8915 Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sat, 14 Apr 2012 02:12:26 -0400 Subject: [PATCH 2/8] Fix issue #325 - add better grep support to js api --- lib/mocha.js | 17 +++++++++++++++++ test/jsapi/grep.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/jsapi/grep.js diff --git a/lib/mocha.js b/lib/mocha.js index 71d96f8e0d..99310a3bed 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -59,6 +59,7 @@ function image(name) { * - `globals` array of accepted globals * - `timeout` timeout in milliseconds * - `ignoreLeaks` ignore global leaks + * - `grep` string or regexp to filter tests with * * @param {Object} options * @api public @@ -66,6 +67,8 @@ function image(name) { function Mocha(options) { options = options || {}; + if ('string' === typeof options.grep) options.grep = new RegExp(options.grep); + if (options.grep && !'RegExp' === options.grep.constructor.name) options.grep = /.*/; this.files = []; this.options = options; this.suite = new exports.Suite('', new exports.Context); @@ -154,6 +157,20 @@ Mocha.prototype.growl = function(runner, reporter) { }); }; +/** + * Add regexp to grep for to the options object + * + * @param {RegExp} or {String} re + * @return {Mocha} + * @api public + */ + +Mocha.prototype.grep = function(re){ + if ('string' === typeof re) re = new RegExp(re); + if (re && 'RegExp' === re.constructor.name) this.options.grep = re; + return this; +}; + /** * Run tests and invoke `fn()` when complete. * diff --git a/test/jsapi/grep.js b/test/jsapi/grep.js new file mode 100644 index 0000000000..c4211cec8c --- /dev/null +++ b/test/jsapi/grep.js @@ -0,0 +1,44 @@ +var Mocha = require('../../'); + +describe('Mocha', function(){ + + beforeEach(function(){ + this.reEqual = function(r1, r2){ + return r1.source === r2.source + && r1.global === r2.global + && r1.ignoreCase === r2.ignoreCase + && r1.multiline === r2.multiline; + } + }) + + describe('constructor options.grep', function(){ + it('should add a RegExp to the mocha.options object', function(){ + var mocha = new Mocha({grep:/foo/}); + this.reEqual(/foo/, mocha.options.grep).should.be.ok; + }) + + it('should convert grep string to a RegExp', function(){ + var mocha = new Mocha({grep:'foo'}); + mocha.options.grep.constructor.name.should.equal('RegExp'); + }) + }) + + describe('.grep()', function(){ + it('should add a RegExp to the mocha.options object', function(){ + var mocha = new Mocha(); + mocha.grep(/foo/); + this.reEqual(/foo/, mocha.options.grep).should.be.ok; + }) + + it('should convert grep string to a RegExp', function(){ + var mocha = new Mocha(); + mocha.grep('foo'); + mocha.options.grep.constructor.name.should.equal('RegExp'); + }) + + it('should return it\'s parent Mocha object for chainability', function(){ + var mocha = new Mocha(); + mocha.grep().should.equal(mocha); + }) + }) +}) From 341edcb56025e82ce80b51482480f549cc3c39e0 Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sat, 14 Apr 2012 02:21:15 -0400 Subject: [PATCH 3/8] reverse grep changes to browser api --- support/tail.js | 9 --------- test/browser/grep.html | 26 -------------------------- test/browser/grep.js | 15 --------------- 3 files changed, 50 deletions(-) delete mode 100644 test/browser/grep.html delete mode 100644 test/browser/grep.js diff --git a/support/tail.js b/support/tail.js index 3604b30aff..7383088ed2 100644 --- a/support/tail.js +++ b/support/tail.js @@ -128,15 +128,6 @@ window.mocha = require('mocha'); suite.emit('pre-require', window); }; - /** - * Set a string to grep for. - */ - - mocha.grep = function(re){ - if ('string' === typeof re) options.grep = re; - return this; - }; - /** * Run mocha, returning the Runner. */ diff --git a/test/browser/grep.html b/test/browser/grep.html deleted file mode 100644 index 49323dfa33..0000000000 --- a/test/browser/grep.html +++ /dev/null @@ -1,26 +0,0 @@ - - - Mocha - - - - - - - - - -
- - diff --git a/test/browser/grep.js b/test/browser/grep.js deleted file mode 100644 index 62b1eaad07..0000000000 --- a/test/browser/grep.js +++ /dev/null @@ -1,15 +0,0 @@ -describe('grep', function() { - it('should add regex to mocha options', function() { - var re = new RegExp('grep'); - - function reEqual(r1, r2){ - return r1.source === r2.source - && r1.global === r2.global - && r1.ignoreCase === r2.ignoreCase - && r1.multiline === r2.multiline; - } - - assert(reEqual(runner._grep, re)); - - }); -}); From 03de8e24da20698b76713b8bd936e54f6e31907f Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sat, 14 Apr 2012 11:30:22 -0400 Subject: [PATCH 4/8] made tests more explicit --- test/jsapi/grep.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/jsapi/grep.js b/test/jsapi/grep.js index c4211cec8c..f93cb9093a 100644 --- a/test/jsapi/grep.js +++ b/test/jsapi/grep.js @@ -19,7 +19,7 @@ describe('Mocha', function(){ it('should convert grep string to a RegExp', function(){ var mocha = new Mocha({grep:'foo'}); - mocha.options.grep.constructor.name.should.equal('RegExp'); + this.reEqual(/foo/, mocha.options.grep).should.be.ok; }) }) @@ -33,7 +33,7 @@ describe('Mocha', function(){ it('should convert grep string to a RegExp', function(){ var mocha = new Mocha(); mocha.grep('foo'); - mocha.options.grep.constructor.name.should.equal('RegExp'); + this.reEqual(/foo/, mocha.options.grep).should.be.ok; }) it('should return it\'s parent Mocha object for chainability', function(){ From ac22ba1c3ea7903306e297c714c06843ce34ac77 Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sat, 14 Apr 2012 12:09:27 -0400 Subject: [PATCH 5/8] browser test css added --- test/browser/style.css | 137 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 test/browser/style.css diff --git a/test/browser/style.css b/test/browser/style.css new file mode 100644 index 0000000000..b49d91d512 --- /dev/null +++ b/test/browser/style.css @@ -0,0 +1,137 @@ + +body { + font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; + padding: 60px 50px; +} + +#mocha h1, h2 { + margin: 0; +} + +#mocha h1 { + margin-top: 15px; + font-size: 1em; + font-weight: 200; +} + +#mocha .suite .suite h1 { + margin-top: 0; + font-size: .8em; +} + +#mocha h2 { + font-size: 12px; + font-weight: normal; + cursor: pointer; +} + +#mocha .suite { + margin-left: 15px; +} + +#mocha .test { + margin-left: 15px; +} + +#mocha .test:hover h2::after { + position: relative; + top: 0; + right: -10px; + content: '(view source)'; + font-size: 12px; + font-family: arial; + color: #888; +} + +#mocha .test.pending:hover h2::after { + content: '(pending)'; + font-family: arial; +} + +#mocha .test.pass::before { + content: '✓'; + font-size: 12px; + display: block; + float: left; + margin-right: 5px; + color: #00c41c; +} + +#mocha .test.pending { + color: #0b97c4; +} + +#mocha .test.pending::before { + content: '◦'; + color: #0b97c4; +} + +#mocha .test.fail { + color: #c00; +} + +#mocha .test.fail pre { + color: black; +} + +#mocha .test.fail::before { + content: '✖'; + font-size: 12px; + display: block; + float: left; + margin-right: 5px; + color: #c00; +} + +#mocha .test pre.error { + color: #c00; +} + +#mocha .test pre { + display: inline-block; + font: 12px/1.5 monaco, monospace; + margin: 5px; + padding: 15px; + border: 1px solid #eee; + border-bottom-color: #ddd; + -webkit-border-radius: 3px; + -webkit-box-shadow: 0 1px 3px #eee; +} + +#error { + color: #c00; + font-size: 1.5 em; + font-weight: 100; + letter-spacing: 1px; +} + +#stats { + position: fixed; + top: 15px; + right: 10px; + font-size: 12px; + margin: 0; + color: #888; +} + +#stats .progress { + float: right; + padding-top: 0; +} + +#stats em { + color: black; +} + +#stats li { + display: inline-block; + margin: 0 5px; + list-style: none; + padding-top: 11px; +} + +code .comment { color: #ddd } +code .init { color: #2F6FAD } +code .string { color: #5890AD } +code .keyword { color: #8A6343 } +code .number { color: #2F6FAD } From e715b157be070babb377e97da46ec1716033eef1 Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sat, 14 Apr 2012 12:23:47 -0400 Subject: [PATCH 6/8] cleaning up some stray additions --- support/tail.js | 1 - test/browser/style.css | 137 ----------------------------------------- 2 files changed, 138 deletions(-) delete mode 100644 test/browser/style.css diff --git a/support/tail.js b/support/tail.js index 7383088ed2..e3d447274c 100644 --- a/support/tail.js +++ b/support/tail.js @@ -139,7 +139,6 @@ window.mocha = require('mocha'); var reporter = new Reporter(runner); var query = parse(window.location.search || ""); if (query.grep) runner.grep(new RegExp(query.grep)); - if (options.grep) runner.grep(new RegExp(options.grep)); if (options.ignoreLeaks) runner.ignoreLeaks = true; if (options.globals) runner.globals(options.globals); runner.globals(['location']); diff --git a/test/browser/style.css b/test/browser/style.css deleted file mode 100644 index b49d91d512..0000000000 --- a/test/browser/style.css +++ /dev/null @@ -1,137 +0,0 @@ - -body { - font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; - padding: 60px 50px; -} - -#mocha h1, h2 { - margin: 0; -} - -#mocha h1 { - margin-top: 15px; - font-size: 1em; - font-weight: 200; -} - -#mocha .suite .suite h1 { - margin-top: 0; - font-size: .8em; -} - -#mocha h2 { - font-size: 12px; - font-weight: normal; - cursor: pointer; -} - -#mocha .suite { - margin-left: 15px; -} - -#mocha .test { - margin-left: 15px; -} - -#mocha .test:hover h2::after { - position: relative; - top: 0; - right: -10px; - content: '(view source)'; - font-size: 12px; - font-family: arial; - color: #888; -} - -#mocha .test.pending:hover h2::after { - content: '(pending)'; - font-family: arial; -} - -#mocha .test.pass::before { - content: '✓'; - font-size: 12px; - display: block; - float: left; - margin-right: 5px; - color: #00c41c; -} - -#mocha .test.pending { - color: #0b97c4; -} - -#mocha .test.pending::before { - content: '◦'; - color: #0b97c4; -} - -#mocha .test.fail { - color: #c00; -} - -#mocha .test.fail pre { - color: black; -} - -#mocha .test.fail::before { - content: '✖'; - font-size: 12px; - display: block; - float: left; - margin-right: 5px; - color: #c00; -} - -#mocha .test pre.error { - color: #c00; -} - -#mocha .test pre { - display: inline-block; - font: 12px/1.5 monaco, monospace; - margin: 5px; - padding: 15px; - border: 1px solid #eee; - border-bottom-color: #ddd; - -webkit-border-radius: 3px; - -webkit-box-shadow: 0 1px 3px #eee; -} - -#error { - color: #c00; - font-size: 1.5 em; - font-weight: 100; - letter-spacing: 1px; -} - -#stats { - position: fixed; - top: 15px; - right: 10px; - font-size: 12px; - margin: 0; - color: #888; -} - -#stats .progress { - float: right; - padding-top: 0; -} - -#stats em { - color: black; -} - -#stats li { - display: inline-block; - margin: 0 5px; - list-style: none; - padding-top: 11px; -} - -code .comment { color: #ddd } -code .init { color: #2F6FAD } -code .string { color: #5890AD } -code .keyword { color: #8A6343 } -code .number { color: #2F6FAD } From 200a73dc7d8a7aefef736ae5a644913e950659fb Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Sun, 15 Apr 2012 14:14:46 -0400 Subject: [PATCH 7/8] clean up string conversion to RE in js API --- lib/mocha.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/mocha.js b/lib/mocha.js index 99310a3bed..19b8f6de40 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -67,8 +67,9 @@ function image(name) { function Mocha(options) { options = options || {}; - if ('string' === typeof options.grep) options.grep = new RegExp(options.grep); - if (options.grep && !'RegExp' === options.grep.constructor.name) options.grep = /.*/; + options.grep = 'string' == typeof options.grep + ? options.grep = new RegExp(options.grep) + : options.grep; this.files = []; this.options = options; this.suite = new exports.Suite('', new exports.Context); @@ -166,9 +167,10 @@ Mocha.prototype.growl = function(runner, reporter) { */ Mocha.prototype.grep = function(re){ - if ('string' === typeof re) re = new RegExp(re); - if (re && 'RegExp' === re.constructor.name) this.options.grep = re; - return this; + this.options.grep = 'string' == typeof re + ? re = new RegExp(re) + : re; + return this; }; /** From dc65a34abfed7eaa767e8bd23e3848a4a1ea4269 Mon Sep 17 00:00:00 2001 From: Dave McKenna Date: Mon, 16 Apr 2012 14:19:25 -0400 Subject: [PATCH 8/8] removed unnecessary assignment --- lib/mocha.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mocha.js b/lib/mocha.js index 19b8f6de40..ed7e5bf55a 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -68,7 +68,7 @@ function image(name) { function Mocha(options) { options = options || {}; options.grep = 'string' == typeof options.grep - ? options.grep = new RegExp(options.grep) + ? new RegExp(options.grep) : options.grep; this.files = []; this.options = options; @@ -168,7 +168,7 @@ Mocha.prototype.growl = function(runner, reporter) { Mocha.prototype.grep = function(re){ this.options.grep = 'string' == typeof re - ? re = new RegExp(re) + ? new RegExp(re) : re; return this; };