From ce4a167521c895758ab7402ba5938ba151b2b351 Mon Sep 17 00:00:00 2001 From: Jarrod Overson Date: Sat, 15 Jun 2013 09:38:39 -0700 Subject: [PATCH] Added phantom polyfill, closes #63 --- Gruntfile.js | 31 +++++++++++++------ README.md | 26 ++++++++-------- tasks/helpers/phantom-polyfill.js | 24 ++++++++++++++ tasks/jasmine.js | 5 ++- tasks/jasmine/templates/DefaultRunner.tmpl | 2 +- tasks/lib/jasmine.js | 6 ++++ test/expected/defaultTemplate.html | 1 + .../phantom-polyfills/spec/test-spec.js | 10 ++++++ test/fixtures/phantom-polyfills/src/test.js | 5 +++ test/jasmine_test.js | 1 + 10 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 tasks/helpers/phantom-polyfill.js create mode 100644 test/fixtures/phantom-polyfills/spec/test-spec.js create mode 100644 test/fixtures/phantom-polyfills/src/test.js diff --git a/Gruntfile.js b/Gruntfile.js index 9a775cd..dc3a6e2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -20,20 +20,31 @@ module.exports = function(grunt) { } }, jasmine: { - options: { - specs: 'test/fixtures/pivotal/spec/*Spec.js', - helpers: 'test/fixtures/pivotal/spec/*Helper.js', - junit: { - path: 'junit' - } - }, pivotal: { - src: 'test/fixtures/pivotal/src/**/*.js' + src: 'test/fixtures/pivotal/src/**/*.js', + options: { + specs: 'test/fixtures/pivotal/spec/*Spec.js', + helpers: 'test/fixtures/pivotal/spec/*Helper.js', + junit: { + path: 'junit' + } + }, + }, + phantom_polyfills: { + src: 'test/fixtures/phantom-polyfills/src/**/*.js', + options : { + specs : 'test/fixtures/phantom-polyfills/spec/**/*.js', + } }, legacyVersion: { src: 'test/fixtures/pivotal/src/**/*.js', options: { - version: '1.2.0' + specs: 'test/fixtures/pivotal/spec/*Spec.js', + helpers: 'test/fixtures/pivotal/spec/*Helper.js', + version: '1.2.0', + junit: { + path: 'junit' + } } }, deepOutfile: { @@ -45,6 +56,8 @@ module.exports = function(grunt) { customTemplate: { src: 'test/fixtures/pivotal/src/**/*.js', options: { + specs: 'test/fixtures/pivotal/spec/*Spec.js', + helpers: 'test/fixtures/pivotal/spec/*Helper.js', template: 'test/fixtures/customTemplate/custom.tmpl', junit: { path: 'junit/customTemplate', diff --git a/README.md b/README.md index e5daab0..f927ab2 100644 --- a/README.md +++ b/README.md @@ -236,21 +236,21 @@ for more information on the RequireJS template. ## Release History - * 2013-04-02   v0.4.2   bumped grunt-lib-phantomjs to 0.3.0/1.9 (closes merged addressed - * 2013-03-07   v0.4.0   bumped grunt-lib-phantomjs to 0.2.0/1.8 allowed spec/vendor/helper list to return non-matching files (e.g. for remote, http) merged merged - * 2013-02-23   v0.3.3   Added better console output (via Gabor Kiss @Neverl) - * 2013-02-16   v0.3.2   Ensure Gruntfile.js is included on npm. - * 2013-02-14   v0.3.1   First official release for Grunt 0.4.0. - * 2013-01-21   v0.3.1rc7   Exposed phantom and sendMessage to templates - * 2013-01-21   v0.3.0rc7   Updated dependencies for grunt v0.4.0rc6/rc7 - * 2013-01-07   v0.3.0rc5   Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc api. Added JUnit xml output (via Kelvin Luck @vitch) Passing console.log from browser to verbose grunt logging Support for templates as separate node modules Removed internal requirejs template (see grunt-template-jasmine-requirejs) - * 2012-12-02   v0.2.0   Generalized requirejs template config Added loader plugin Tests for templates Updated jasmine to 1.3.0 - * 2012-11-23   v0.1.2   Updated for new grunt/grunt-contrib apis - * 2012-11-06   v0.1.1   Fixed race condition in requirejs template - * 2012-11-06   v0.1.0   Ported grunt-jasmine-runner and grunt-jasmine-task to grunt-contrib + * 2013-04-03   v0.4.2   bumped grunt-lib-phantomjs to 0.3.0/1.9 (closes merged addressed + * 2013-03-08   v0.4.0   bumped grunt-lib-phantomjs to 0.2.0/1.8 allowed spec/vendor/helper list to return non-matching files (e.g. for remote, http) merged merged + * 2013-02-24   v0.3.3   Added better console output (via Gabor Kiss @Neverl) + * 2013-02-17   v0.3.2   Ensure Gruntfile.js is included on npm. + * 2013-02-15   v0.3.1   First official release for Grunt 0.4.0. + * 2013-01-22   v0.3.1rc7   Exposed phantom and sendMessage to templates + * 2013-01-22   v0.3.0rc7   Updated dependencies for grunt v0.4.0rc6/rc7 + * 2013-01-08   v0.3.0rc5   Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc api. Added JUnit xml output (via Kelvin Luck @vitch) Passing console.log from browser to verbose grunt logging Support for templates as separate node modules Removed internal requirejs template (see grunt-template-jasmine-requirejs) + * 2012-12-03   v0.2.0   Generalized requirejs template config Added loader plugin Tests for templates Updated jasmine to 1.3.0 + * 2012-11-24   v0.1.2   Updated for new grunt/grunt-contrib apis + * 2012-11-07   v0.1.1   Fixed race condition in requirejs template + * 2012-11-07   v0.1.0   Ported grunt-jasmine-runner and grunt-jasmine-task to grunt-contrib --- Task submitted by [Jarrod Overson](http://jarrodoverson.com) -*This file was generated on Fri Apr 12 2013 19:45:58.* +*This file was generated on Sat Jun 15 2013 09:12:10.* diff --git a/tasks/helpers/phantom-polyfill.js b/tasks/helpers/phantom-polyfill.js new file mode 100644 index 0000000..80a9f73 --- /dev/null +++ b/tasks/helpers/phantom-polyfill.js @@ -0,0 +1,24 @@ + + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind + +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + FNOP = function () {}, + fBound = function () { + return fToBind.apply(this instanceof FNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + FNOP.prototype = this.prototype; + fBound.prototype = new FNOP(); + + return fBound; + }; +} diff --git a/tasks/jasmine.js b/tasks/jasmine.js index ad162ed..13274b6 100644 --- a/tasks/jasmine.js +++ b/tasks/jasmine.js @@ -198,11 +198,14 @@ module.exports = function(grunt) { phantomjs.on('jasmine.reportRunnerResults',function(){ var dur = (new Date()).getTime() - thisRun.start_time; var spec_str = thisRun.executed_specs + (thisRun.executed_specs === 1 ? " spec " : " specs "); + grunt.verbose.writeln('Runner finished'); + if (thisRun.executed_specs === 0) { + grunt.warn('No specs executed, is there a configuration error?'); + } if (!grunt.option('verbose')) { grunt.log.writeln(''); grunt.log.write(status.log); } - grunt.verbose.writeln('Runner finished'); grunt.log.writeln(spec_str + 'in ' + (dur/1000) + "s."); }); diff --git a/tasks/jasmine/templates/DefaultRunner.tmpl b/tasks/jasmine/templates/DefaultRunner.tmpl index 1c58236..65d8f31 100644 --- a/tasks/jasmine/templates/DefaultRunner.tmpl +++ b/tasks/jasmine/templates/DefaultRunner.tmpl @@ -7,7 +7,7 @@ <% }) %> <% with (scripts) { %> - <% [].concat(jasmine, vendor, helpers, src, specs, reporters, start).forEach(function(script){ %> + <% [].concat(polyfills, jasmine, vendor, helpers, src, specs, reporters, start).forEach(function(script){ %> <% }) %> <% }; %> diff --git a/tasks/lib/jasmine.js b/tasks/lib/jasmine.js index 37ca8fd..aadf229 100644 --- a/tasks/lib/jasmine.js +++ b/tasks/lib/jasmine.js @@ -35,6 +35,7 @@ exports.init = function(grunt, phantomjs) { exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine.js', 'jasmine.js'); exports.copyTempFile(__dirname + '/../../vendor/jasmine-' + options.version + '/jasmine-html.js', 'jasmine-html.js'); exports.copyTempFile(__dirname + '/../jasmine/jasmine-helper.js', 'jasmine-helper.js'); + exports.copyTempFile(__dirname + '/../helpers/phantom-polyfill.js', 'phantom-polyfill.js'); var reporters = [ tempDir + '/reporter.js' @@ -51,6 +52,10 @@ exports.init = function(grunt, phantomjs) { jasmineCss = jasmineCss.concat(options.styles); + var polyfills = [ + tempDir + '/phantom-polyfill.js' + ]; + var jasmineCore = [ tempDir + '/jasmine.js', tempDir + '/jasmine-html.js' @@ -62,6 +67,7 @@ exports.init = function(grunt, phantomjs) { temp : tempDir, css : exports.getRelativeFileList(outdir, jasmineCss), scripts : { + polyfills : exports.getRelativeFileList(outdir, polyfills), jasmine : exports.getRelativeFileList(outdir, jasmineCore), helpers : exports.getRelativeFileList(outdir, options.helpers), specs : exports.getRelativeFileList(outdir, options.specs), diff --git a/test/expected/defaultTemplate.html b/test/expected/defaultTemplate.html index 7b75cc9..470d11b 100644 --- a/test/expected/defaultTemplate.html +++ b/test/expected/defaultTemplate.html @@ -6,6 +6,7 @@ + diff --git a/test/fixtures/phantom-polyfills/spec/test-spec.js b/test/fixtures/phantom-polyfills/spec/test-spec.js new file mode 100644 index 0000000..0592b74 --- /dev/null +++ b/test/fixtures/phantom-polyfills/spec/test-spec.js @@ -0,0 +1,10 @@ +describe("Polyfills", function() { + + describe("Function.prototype.bind", function(){ + it("should allow contexts to be bound to a function", function() { + var fn = bindTest.bind({foo : 'bar'}); + expect(fn()).toBe('bar'); + }); + + }); +}); diff --git a/test/fixtures/phantom-polyfills/src/test.js b/test/fixtures/phantom-polyfills/src/test.js new file mode 100644 index 0000000..87a3f60 --- /dev/null +++ b/test/fixtures/phantom-polyfills/src/test.js @@ -0,0 +1,5 @@ + + +function bindTest() { + return this.foo; +} \ No newline at end of file diff --git a/test/jasmine_test.js b/test/jasmine_test.js index 42ff1f5..2a37d17 100644 --- a/test/jasmine_test.js +++ b/test/jasmine_test.js @@ -22,6 +22,7 @@ exports.jasmine = { var context = { css : ['css/a.css'], scripts : { + polyfills : ['p1.js'], jasmine : ['J1.js','J2.js'], helpers : ['H1.js','H2.js'], specs : ['SPEC1.js','SPEC2.js'],