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 "<global>", not "<window>".
Tests are a little closer to passing in node.js.
This commit is contained in:
Lee Byrd & Christian Williams 2010-06-22 16:22:09 -07:00
parent e60f22a2e5
commit 01d842fdfd
10 changed files with 69 additions and 62 deletions

View File

@ -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() {

View File

@ -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('<window>');
} else if (value === jasmine.getGlobal()) {
this.emitScalar('<global>');
} 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
};

View File

@ -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"

View File

@ -13,3 +13,13 @@ describe("jasmine.log", function() {
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);
});
});

View File

@ -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);

View File

@ -69,8 +69,8 @@ describe("jasmine.pp", function () {
expect(jasmine.pp('some <b>html string</b> &', false)).toEqual('\'some <b>html string</b> &\'');
});
it("should abbreviate window objects", function() {
expect(jasmine.pp(window)).toEqual("<window>");
it("should abbreviate the global (usually window) object", function() {
expect(jasmine.pp(jasmine.getGlobal())).toEqual("<global>");
});
it("should stringify Date objects properly", function() {

View File

@ -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('<window>');
} else if (value === jasmine.getGlobal()) {
this.emitScalar('<global>');
} else if (value instanceof jasmine.Matchers.Any) {
this.emitScalar(value.toString());
} else if (typeof value === 'string') {

View File

@ -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);
}
};

View File

@ -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';

View File

@ -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 {