From 01d842fdfdaf023851a1a729c1d83b9a0475db22 Mon Sep 17 00:00:00 2001 From: Lee Byrd & Christian Williams Date: Tue, 22 Jun 2010 16:22:09 -0700 Subject: [PATCH] Removed references to 'window'; using jasmine.getGlobal() instead in order to better support non-browser environments. Better protection around access to console. The global object is now pretty-printed as "", not "". Tests are a little closer to passing in node.js. --- lib/TrivialReporter.js | 3 ++- lib/jasmine-0.10.4.js | 39 +++++++++++++++++++++------------- spec/saucelabs.yml | 24 --------------------- spec/suites/BaseSpec.js | 10 +++++++++ spec/suites/MatchersSpec.js | 14 ++++++------ spec/suites/PrettyPrintSpec.js | 4 ++-- src/PrettyPrinter.js | 4 ++-- src/Reporters.js | 1 + src/base.js | 16 ++++++++++---- src/mock-timeout.js | 16 +++++++------- 10 files changed, 69 insertions(+), 62 deletions(-) delete mode 100644 spec/saucelabs.yml diff --git a/lib/TrivialReporter.js b/lib/TrivialReporter.js index 6bee4ab7..8d988dfa 100644 --- a/lib/TrivialReporter.js +++ b/lib/TrivialReporter.js @@ -154,7 +154,8 @@ jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) { }; jasmine.TrivialReporter.prototype.log = function() { - console.log.apply(console, arguments); + var console = jasmine.getGlobal().console; + if (console && console.log) console.log.apply(console, arguments); }; jasmine.TrivialReporter.prototype.getLocation = function() { diff --git a/lib/jasmine-0.10.4.js b/lib/jasmine-0.10.4.js index 6c16a0ec..a6f9b838 100644 --- a/lib/jasmine-0.10.4.js +++ b/lib/jasmine-0.10.4.js @@ -26,6 +26,14 @@ jasmine.undefined = jasmine.___undefined___; */ jasmine.DEFAULT_UPDATE_INTERVAL = 250; +jasmine.getGlobal = function() { + function getGlobal() { + return this; + } + + return getGlobal(); +}; + /** * Allows for bound functions to be compared. Internal use only. * @@ -46,10 +54,10 @@ jasmine.bindOriginal_ = function(base, name) { } }; -jasmine.setTimeout = jasmine.bindOriginal_(window, 'setTimeout'); -jasmine.clearTimeout = jasmine.bindOriginal_(window, 'clearTimeout'); -jasmine.setInterval = jasmine.bindOriginal_(window, 'setInterval'); -jasmine.clearInterval = jasmine.bindOriginal_(window, 'clearInterval'); +jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); +jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); +jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); +jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); jasmine.MessageResult = function(values) { this.type = 'log'; @@ -1542,8 +1550,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) { this.emitScalar('undefined'); } else if (value === null) { this.emitScalar('null'); - } else if (value.navigator && value.frames && value.setTimeout) { - this.emitScalar(''); + } else if (value === jasmine.getGlobal()) { + this.emitScalar(''); } else if (value instanceof jasmine.Matchers.Any) { this.emitScalar(value.toString()); } else if (typeof value === 'string') { @@ -1771,6 +1779,7 @@ jasmine.Reporters.reporter = function(callbacks) { that.doCallback(that.callbacks.specCallback, spec); }, log: function (str) { + var console = jasmine.getGlobal().console; if (console && console.log) console.log(str); } }; @@ -2324,10 +2333,10 @@ jasmine.Clock = { }, real: { - setTimeout: window.setTimeout, - clearTimeout: window.clearTimeout, - setInterval: window.setInterval, - clearInterval: window.clearInterval + setTimeout: jasmine.getGlobal().setTimeout, + clearTimeout: jasmine.getGlobal().clearTimeout, + setInterval: jasmine.getGlobal().setInterval, + clearInterval: jasmine.getGlobal().clearInterval }, assertInstalled: function() { @@ -2341,7 +2350,7 @@ jasmine.Clock = { jasmine.Clock.installed = jasmine.Clock.real; //else for IE support -window.setTimeout = function(funcToCall, millis) { +jasmine.getGlobal().setTimeout = function(funcToCall, millis) { if (jasmine.Clock.installed.setTimeout.apply) { return jasmine.Clock.installed.setTimeout.apply(this, arguments); } else { @@ -2349,7 +2358,7 @@ window.setTimeout = function(funcToCall, millis) { } }; -window.setInterval = function(funcToCall, millis) { +jasmine.getGlobal().setInterval = function(funcToCall, millis) { if (jasmine.Clock.installed.setInterval.apply) { return jasmine.Clock.installed.setInterval.apply(this, arguments); } else { @@ -2357,7 +2366,7 @@ window.setInterval = function(funcToCall, millis) { } }; -window.clearTimeout = function(timeoutKey) { +jasmine.getGlobal().clearTimeout = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearTimeout.apply(this, arguments); } else { @@ -2365,7 +2374,7 @@ window.clearTimeout = function(timeoutKey) { } }; -window.clearInterval = function(timeoutKey) { +jasmine.getGlobal().clearInterval = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearInterval.apply(this, arguments); } else { @@ -2378,5 +2387,5 @@ jasmine.version_= { "major": 0, "minor": 10, "build": 4, - "revision": 1277243292 + "revision": 1277248808 }; diff --git a/spec/saucelabs.yml b/spec/saucelabs.yml deleted file mode 100644 index 8a46d1c3..00000000 --- a/spec/saucelabs.yml +++ /dev/null @@ -1,24 +0,0 @@ -local: - application_framework: :selenium -# -# Possible Sauce Labs configurations as of 2009/11/19 -# From: http://saucelabs.com/products/docs/sauce-ondemand/browsers -# os: "Windows 2003" -# browser: "iexplore" -# browser-version: "6.", "7.", "8." -# browser: "firefox" -# browser-version: "2.", "3.0", "3.5" -# browser: "safari" -# browser-version: "3.", "4." -# browser: "opera" -# browser-version: "9." -# browser: "googlechrome" -# browser-version: "" -# os: "Linux" -# browser: "firefox" -# browser-version: "3." -saucelabs: - application_framework: :external - selenium_server_address: "saucelabs.com" - selenium_browser_key: '{"username": "--YOUR-SAUCELABS-USERNAME--", "access-key": "--YOUR-SAUCELABS-ACCESS-KEY--", "os": "Linux", "browser": "firefox", "browser-version": "3."}' - application_port: "80" diff --git a/spec/suites/BaseSpec.js b/spec/suites/BaseSpec.js index 33256f92..1e884cc7 100644 --- a/spec/suites/BaseSpec.js +++ b/spec/suites/BaseSpec.js @@ -12,4 +12,14 @@ describe("jasmine.log", function() { jasmine.log(1, 2, 3); expect(jasmine.getEnv().currentSpec.log).wasCalledWith(1, 2, 3); }); +}); + +describe("jasmine.getGlobal", function() { + it("should return the global object", function() { + var globalObject = (function() { + return this; + })(); + + expect(jasmine.getGlobal()).toBe(globalObject); + }); }); \ No newline at end of file diff --git a/spec/suites/MatchersSpec.js b/spec/suites/MatchersSpec.js index 2c4b90bc..ec83ba48 100644 --- a/spec/suites/MatchersSpec.js +++ b/spec/suites/MatchersSpec.js @@ -20,7 +20,7 @@ describe("jasmine.Matchers", function() { return spec.addMatcherResult.mostRecentCall.args[0]; } - it("toEqual with primitives, objects, dates, html nodes, etc.", function() { + it("toEqual with primitives, objects, dates, etc.", function() { expect(match(true).toEqual(true)).toEqual(true); expect(match({foo:'bar'}).toEqual(null)).toEqual(false); @@ -40,11 +40,6 @@ describe("jasmine.Matchers", function() { circularGraph.referenceToSelf = circularGraph; expect((match(circularGraph).toEqual(circularGraph))).toEqual(true); - var nodeA = document.createElement('div'); - var nodeB = document.createElement('div'); - expect((match(nodeA).toEqual(nodeA))).toEqual(true); - expect((match(nodeA).toEqual(nodeB))).toEqual(false); - expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2009, 1, 3, 15, 17, 19, 1234)))).toEqual(false); expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2008, 1, 3, 15, 17, 19, 1234)))).toEqual(true); @@ -64,6 +59,13 @@ describe("jasmine.Matchers", function() { expect((match(new Number('5')).toNotEqual(5))).toBe(false); }); + it("toEqual with DOM nodes", function() { + var nodeA = document.createElement('div'); + var nodeB = document.createElement('div'); + expect((match(nodeA).toEqual(nodeA))).toEqual(true); + expect((match(nodeA).toEqual(nodeB))).toEqual(false); + }); + it("toEqual to build an Expectation Result", function() { var actual = 'a'; var matcher = match(actual); diff --git a/spec/suites/PrettyPrintSpec.js b/spec/suites/PrettyPrintSpec.js index 1f046f2c..22cba29c 100644 --- a/spec/suites/PrettyPrintSpec.js +++ b/spec/suites/PrettyPrintSpec.js @@ -69,8 +69,8 @@ describe("jasmine.pp", function () { expect(jasmine.pp('some html string &', false)).toEqual('\'some html string &\''); }); - it("should abbreviate window objects", function() { - expect(jasmine.pp(window)).toEqual(""); + it("should abbreviate the global (usually window) object", function() { + expect(jasmine.pp(jasmine.getGlobal())).toEqual(""); }); it("should stringify Date objects properly", function() { diff --git a/src/PrettyPrinter.js b/src/PrettyPrinter.js index 06b38c3c..e7d8b291 100644 --- a/src/PrettyPrinter.js +++ b/src/PrettyPrinter.js @@ -21,8 +21,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) { this.emitScalar('undefined'); } else if (value === null) { this.emitScalar('null'); - } else if (value.navigator && value.frames && value.setTimeout) { - this.emitScalar(''); + } else if (value === jasmine.getGlobal()) { + this.emitScalar(''); } else if (value instanceof jasmine.Matchers.Any) { this.emitScalar(value.toString()); } else if (typeof value === 'string') { diff --git a/src/Reporters.js b/src/Reporters.js index dcd4be4d..a3533853 100644 --- a/src/Reporters.js +++ b/src/Reporters.js @@ -34,6 +34,7 @@ jasmine.Reporters.reporter = function(callbacks) { that.doCallback(that.callbacks.specCallback, spec); }, log: function (str) { + var console = jasmine.getGlobal().console; if (console && console.log) console.log(str); } }; diff --git a/src/base.js b/src/base.js index e066b97a..704df6b0 100755 --- a/src/base.js +++ b/src/base.js @@ -26,6 +26,14 @@ jasmine.undefined = jasmine.___undefined___; */ jasmine.DEFAULT_UPDATE_INTERVAL = 250; +jasmine.getGlobal = function() { + function getGlobal() { + return this; + } + + return getGlobal(); +}; + /** * Allows for bound functions to be compared. Internal use only. * @@ -46,10 +54,10 @@ jasmine.bindOriginal_ = function(base, name) { } }; -jasmine.setTimeout = jasmine.bindOriginal_(window, 'setTimeout'); -jasmine.clearTimeout = jasmine.bindOriginal_(window, 'clearTimeout'); -jasmine.setInterval = jasmine.bindOriginal_(window, 'setInterval'); -jasmine.clearInterval = jasmine.bindOriginal_(window, 'clearInterval'); +jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); +jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); +jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); +jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); jasmine.MessageResult = function(values) { this.type = 'log'; diff --git a/src/mock-timeout.js b/src/mock-timeout.js index f3e39cbc..136d0243 100755 --- a/src/mock-timeout.js +++ b/src/mock-timeout.js @@ -126,10 +126,10 @@ jasmine.Clock = { }, real: { - setTimeout: window.setTimeout, - clearTimeout: window.clearTimeout, - setInterval: window.setInterval, - clearInterval: window.clearInterval + setTimeout: jasmine.getGlobal().setTimeout, + clearTimeout: jasmine.getGlobal().clearTimeout, + setInterval: jasmine.getGlobal().setInterval, + clearInterval: jasmine.getGlobal().clearInterval }, assertInstalled: function() { @@ -143,7 +143,7 @@ jasmine.Clock = { jasmine.Clock.installed = jasmine.Clock.real; //else for IE support -window.setTimeout = function(funcToCall, millis) { +jasmine.getGlobal().setTimeout = function(funcToCall, millis) { if (jasmine.Clock.installed.setTimeout.apply) { return jasmine.Clock.installed.setTimeout.apply(this, arguments); } else { @@ -151,7 +151,7 @@ window.setTimeout = function(funcToCall, millis) { } }; -window.setInterval = function(funcToCall, millis) { +jasmine.getGlobal().setInterval = function(funcToCall, millis) { if (jasmine.Clock.installed.setInterval.apply) { return jasmine.Clock.installed.setInterval.apply(this, arguments); } else { @@ -159,7 +159,7 @@ window.setInterval = function(funcToCall, millis) { } }; -window.clearTimeout = function(timeoutKey) { +jasmine.getGlobal().clearTimeout = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearTimeout.apply(this, arguments); } else { @@ -167,7 +167,7 @@ window.clearTimeout = function(timeoutKey) { } }; -window.clearInterval = function(timeoutKey) { +jasmine.getGlobal().clearInterval = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearInterval.apply(this, arguments); } else {