var isCommonJS = typeof window == "undefined"; |
var isCommonJS = typeof window == "undefined"; |
|
|
/** |
/** |
* Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework. |
* Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework. |
* |
* |
* @namespace |
* @namespace |
*/ |
*/ |
var jasmine = {}; |
var jasmine = {}; |
if (isCommonJS) exports.jasmine = jasmine; |
if (isCommonJS) exports.jasmine = jasmine; |
/** |
/** |
* @private |
* @private |
*/ |
*/ |
jasmine.unimplementedMethod_ = function() { |
jasmine.unimplementedMethod_ = function () { |
throw new Error("unimplemented method"); |
throw new Error("unimplemented method"); |
}; |
}; |
|
|
/** |
/** |
* Use <code>jasmine.undefined</code> instead of <code>undefined</code>, since <code>undefined</code> is just |
* Use <code>jasmine.undefined</code> instead of <code>undefined</code>, since <code>undefined</code> is just |
* a plain old variable and may be redefined by somebody else. |
* a plain old variable and may be redefined by somebody else. |
* |
* |
* @private |
* @private |
*/ |
*/ |
jasmine.undefined = jasmine.___undefined___; |
jasmine.undefined = jasmine.___undefined___; |
|
|
/** |
/** |
* Show diagnostic messages in the console if set to true |
* Show diagnostic messages in the console if set to true |
* |
* |
*/ |
*/ |
jasmine.VERBOSE = false; |
jasmine.VERBOSE = false; |
|
|
/** |
/** |
* Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. |
* Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. |
* |
* |
*/ |
*/ |
jasmine.DEFAULT_UPDATE_INTERVAL = 250; |
jasmine.DEFAULT_UPDATE_INTERVAL = 250; |
|
|
/** |
/** |
* Default timeout interval in milliseconds for waitsFor() blocks. |
* Default timeout interval in milliseconds for waitsFor() blocks. |
*/ |
*/ |
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; |
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; |
|
|
jasmine.getGlobal = function() { |
jasmine.getGlobal = function () { |
function getGlobal() { |
function getGlobal() { |
return this; |
return this; |
} |
} |
|
|
return getGlobal(); |
return getGlobal(); |
}; |
}; |
|
|
/** |
/** |
* Allows for bound functions to be compared. Internal use only. |
* Allows for bound functions to be compared. Internal use only. |
* |
* |
* @ignore |
* @ignore |
* @private |
* @private |
* @param base {Object} bound 'this' for the function |
* @param base {Object} bound 'this' for the function |
* @param name {Function} function to find |
* @param name {Function} function to find |
*/ |
*/ |
jasmine.bindOriginal_ = function(base, name) { |
jasmine.bindOriginal_ = function (base, name) { |
var original = base[name]; |
var original = base[name]; |
if (original.apply) { |
if (original.apply) { |
return function() { |
return function () { |
return original.apply(base, arguments); |
return original.apply(base, arguments); |
}; |
}; |
} else { |
} else { |
// IE support |
// IE support |
return jasmine.getGlobal()[name]; |
return jasmine.getGlobal()[name]; |
} |
} |
}; |
}; |
|
|
jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); |
jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); |
jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); |
jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); |
jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); |
jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); |
jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); |
jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); |
|
|
jasmine.MessageResult = function(values) { |
jasmine.MessageResult = function (values) { |
this.type = 'log'; |
this.type = 'log'; |
this.values = values; |
this.values = values; |
this.trace = new Error(); // todo: test better |
this.trace = new Error(); // todo: test better |
}; |
}; |
|
|
jasmine.MessageResult.prototype.toString = function() { |
jasmine.MessageResult.prototype.toString = function () { |
var text = ""; |
var text = ""; |
for (var i = 0; i < this.values.length; i++) { |
for (var i = 0; i < this.values.length; i++) { |
if (i > 0) text += " "; |
if (i > 0) text += " "; |
if (jasmine.isString_(this.values[i])) { |
if (jasmine.isString_(this.values[i])) { |
text += this.values[i]; |
text += this.values[i]; |
} else { |
} else { |
text += jasmine.pp(this.values[i]); |
text += jasmine.pp(this.values[i]); |
} |
} |
} |
} |
return text; |
return text; |
}; |
}; |
|
|
jasmine.ExpectationResult = function(params) { |
jasmine.ExpectationResult = function (params) { |
this.type = 'expect'; |
this.type = 'expect'; |
this.matcherName = params.matcherName; |
this.matcherName = params.matcherName; |
this.passed_ = params.passed; |
this.passed_ = params.passed; |
this.expected = params.expected; |
this.expected = params.expected; |
this.actual = params.actual; |
this.actual = params.actual; |
this.message = this.passed_ ? 'Passed.' : params.message; |
this.message = this.passed_ ? 'Passed.' : params.message; |
|
|
var trace = (params.trace || new Error(this.message)); |
var trace = (params.trace || new Error(this.message)); |
this.trace = this.passed_ ? '' : trace; |
this.trace = this.passed_ ? '' : trace; |
}; |
}; |
|
|
jasmine.ExpectationResult.prototype.toString = function () { |
jasmine.ExpectationResult.prototype.toString = function () { |
return this.message; |
return this.message; |
}; |
}; |
|
|
jasmine.ExpectationResult.prototype.passed = function () { |
jasmine.ExpectationResult.prototype.passed = function () { |
return this.passed_; |
return this.passed_; |
}; |
}; |
|
|
/** |
/** |
* Getter for the Jasmine environment. Ensures one gets created |
* Getter for the Jasmine environment. Ensures one gets created |
*/ |
*/ |
jasmine.getEnv = function() { |
jasmine.getEnv = function () { |
var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(); |
var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(); |
return env; |
return env; |
}; |
}; |
|
|
/** |
/** |
* @ignore |
* @ignore |
* @private |
* @private |
* @param value |
* @param value |
* @returns {Boolean} |
* @returns {Boolean} |
*/ |
*/ |
jasmine.isArray_ = function(value) { |
jasmine.isArray_ = function (value) { |
return jasmine.isA_("Array", value); |
return jasmine.isA_("Array", value); |
}; |
}; |
|
|
/** |
/** |
* @ignore |
* @ignore |
* @private |
* @private |
* @param value |
* @param value |
* @returns {Boolean} |
* @returns {Boolean} |
*/ |
*/ |
jasmine.isString_ = function(value) { |
jasmine.isString_ = function (value) { |
return jasmine.isA_("String", value); |
return jasmine.isA_("String", value); |
}; |
}; |
|
|
/** |
/** |
* @ignore |
* @ignore |
* @private |
* @private |
* @param value |
* @param value |
* @returns {Boolean} |
* @returns {Boolean} |
*/ |
*/ |
jasmine.isNumber_ = function(value) { |
jasmine.isNumber_ = function (value) { |
return jasmine.isA_("Number", value); |
return jasmine.isA_("Number", value); |
}; |
}; |
|
|
/** |
/** |
* @ignore |
* @ignore |
* @private |
* @private |
* @param {String} typeName |
* @param {String} typeName |
* @param value |
* @param value |
* @returns {Boolean} |
* @returns {Boolean} |
*/ |
*/ |
jasmine.isA_ = function(typeName, value) { |
jasmine.isA_ = function (typeName, value) { |
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; |
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; |
}; |
}; |
|
|
/** |
/** |
* Pretty printer for expecations. Takes any object and turns it into a human-readable string. |
* Pretty printer for expecations. Takes any object and turns it into a human-readable string. |
* |
* |
* @param value {Object} an object to be outputted |
* @param value {Object} an object to be outputted |
* @returns {String} |
* @returns {String} |
*/ |
*/ |
jasmine.pp = function(value) { |
jasmine.pp = function (value) { |
var stringPrettyPrinter = new jasmine.StringPrettyPrinter(); |
var stringPrettyPrinter = new jasmine.StringPrettyPrinter(); |
stringPrettyPrinter.format(value); |
stringPrettyPrinter.format(value); |
return stringPrettyPrinter.string; |
return stringPrettyPrinter.string; |
}; |
}; |
|
|
/** |
/** |
* Returns true if the object is a DOM Node. |
* Returns true if the object is a DOM Node. |
* |
* |
* @param {Object} obj object to check |
* @param {Object} obj object to check |
* @returns {Boolean} |
* @returns {Boolean} |
*/ |
*/ |
jasmine.isDomNode = function(obj) { |
jasmine.isDomNode = function (obj) { |
return obj.nodeType > 0; |
return obj.nodeType > 0; |
}; |
}; |
|
|
/** |
/** |
* Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter. |
* Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter. |
* |
* |
* @example |
* @example |
* // don't care about which function is passed in, as long as it's a function |
* // don't care about which function is passed in, as long as it's a function |
* expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function)); |
* expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function)); |
* |
* |
* @param {Class} clazz |
* @param {Class} clazz |
* @returns matchable object of the type clazz |
* @returns matchable object of the type clazz |
*/ |
*/ |
jasmine.any = function(clazz) { |
jasmine.any = function (clazz) { |
return new jasmine.Matchers.Any(clazz); |
return new jasmine.Matchers.Any(clazz); |
}; |
}; |
|
|
/** |
/** |
* Returns a matchable subset of a JSON object. For use in expectations when you don't care about all of the |
* Returns a matchable subset of a JSON object. For use in expectations when you don't care about all of the |
* attributes on the object. |
* attributes on the object. |
* |
* |
* @example |
* @example |
* // don't care about any other attributes than foo. |
* // don't care about any other attributes than foo. |
* expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({foo: "bar"}); |
* expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({foo: "bar"}); |
* |
* |
* @param sample {Object} sample |
* @param sample {Object} sample |
* @returns matchable object for the sample |
* @returns matchable object for the sample |
*/ |
*/ |
jasmine.objectContaining = function (sample) { |
jasmine.objectContaining = function (sample) { |
return new jasmine.Matchers.ObjectContaining(sample); |
return new jasmine.Matchers.ObjectContaining(sample); |
}; |
}; |
|
|
/** |
/** |
* Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. |
* Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. |
* |
* |
* Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine |
* Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine |
* expectation syntax. Spies can be checked if they were called or not and what the calling params were. |
* expectation syntax. Spies can be checked if they were called or not and what the calling params were. |
* |
* |
* A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). |
* A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). |
* |
* |
* Spies are torn down at the end of every spec. |
* Spies are torn down at the end of every spec. |
* |
* |
* Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj. |
* Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj. |
* |
* |
* @example |
* @example |
* // a stub |
* // a stub |
* var myStub = jasmine.createSpy('myStub'); // can be used anywhere |
* var myStub = jasmine.createSpy('myStub'); // can be used anywhere |
* |
* |
* // spy example |
* // spy example |
* var foo = { |
* var foo = { |
* not: function(bool) { return !bool; } |
* not: function(bool) { return !bool; } |
* } |
* } |
* |
* |
* // actual foo.not will not be called, execution stops |
* // actual foo.not will not be called, execution stops |
* spyOn(foo, 'not'); |
* spyOn(foo, 'not'); |
|
|
// foo.not spied upon, execution will continue to implementation |
// foo.not spied upon, execution will continue to implementation |
* spyOn(foo, 'not').andCallThrough(); |
* spyOn(foo, 'not').andCallThrough(); |
* |
* |
* // fake example |
* // fake example |
* var foo = { |
* var foo = { |
* not: function(bool) { return !bool; } |
* not: function(bool) { return !bool; } |
* } |
* } |
* |
* |
* // foo.not(val) will return val |
* // foo.not(val) will return val |
* spyOn(foo, 'not').andCallFake(function(value) {return value;}); |
* spyOn(foo, 'not').andCallFake(function(value) {return value;}); |
* |
* |
* // mock example |
* // mock example |
* foo.not(7 == 7); |
* foo.not(7 == 7); |
* expect(foo.not).toHaveBeenCalled(); |
* expect(foo.not).toHaveBeenCalled(); |
* expect(foo.not).toHaveBeenCalledWith(true); |
* expect(foo.not).toHaveBeenCalledWith(true); |
* |
* |
* @constructor |
* @constructor |
* @see spyOn, jasmine.createSpy, jasmine.createSpyObj |
* @see spyOn, jasmine.createSpy, jasmine.createSpyObj |
* @param {String} name |
* @param {String} name |
*/ |
*/ |
jasmine.Spy = function(name) { |
jasmine.Spy = function (name) { |
/** |
/** |
* The name of the spy, if provided. |
* The name of the spy, if provided. |
*/ |
*/ |
this.identity = name || 'unknown'; |
this.identity = name || 'unknown'; |
/** |
/** |
* Is this Object a spy? |
* Is this Object a spy? |
*/ |
*/ |
this.isSpy = true; |
this.isSpy = true; |
/** |
/** |
* The actual function this spy stubs. |
* The actual function this spy stubs. |
*/ |
*/ |
this.plan = function() { |
this.plan = function () { |
}; |
}; |
/** |
/** |
* Tracking of the most recent call to the spy. |
* Tracking of the most recent call to the spy. |
* @example |
* @example |
* var mySpy = jasmine.createSpy('foo'); |
* var mySpy = jasmine.createSpy('foo'); |
* mySpy(1, 2); |
* mySpy(1, 2); |
* mySpy.mostRecentCall.args = [1, 2]; |
* mySpy.mostRecentCall.args = [1, 2]; |
*/ |
*/ |
this.mostRecentCall = {}; |
this.mostRecentCall = {}; |
|
|
/** |
/** |
* Holds arguments for each call to the spy, indexed by call count |
* Holds arguments for each call to the spy, indexed by call count |
* @example |
* @example |
* var mySpy = jasmine.createSpy('foo'); |
* var mySpy = jasmine.createSpy('foo'); |
* mySpy(1, 2); |
* mySpy(1, 2); |
* mySpy(7, 8); |
* mySpy(7, 8); |
* mySpy.mostRecentCall.args = [7, 8]; |
* mySpy.mostRecentCall.args = [7, 8]; |
* mySpy.argsForCall[0] = [1, 2]; |
* mySpy.argsForCall[0] = [1, 2]; |
* mySpy.argsForCall[1] = [7, 8]; |
* mySpy.argsForCall[1] = [7, 8]; |
*/ |
*/ |
this.argsForCall = []; |
this.argsForCall = []; |
this.calls = []; |
this.calls = []; |
}; |
}; |
|
|
/** |
/** |
* Tells a spy to call through to the actual implemenatation. |
* Tells a spy to call through to the actual implemenatation. |
* |
* |
* @example |
* @example |
* var foo = { |
* var foo = { |
* bar: function() { // do some stuff } |
* bar: function() { // do some stuff } |
* } |
* } |
* |
* |
* // defining a spy on an existing property: foo.bar |
* // defining a spy on an existing property: foo.bar |
* spyOn(foo, 'bar').andCallThrough(); |
* spyOn(foo, 'bar').andCallThrough(); |
*/ |
*/ |
jasmine.Spy.prototype.andCallThrough = function() { |
jasmine.Spy.prototype.andCallThrough = function () { |
this.plan = this.originalValue; |
this.plan = this.originalValue; |
return this; |
return this; |
}; |
}; |
|
|
/** |
/** |
* For setting the return value of a spy. |
* For setting the return value of a spy. |
* |
* |
* @example |
* @example |
* // defining a spy from scratch: foo() returns 'baz' |
* // defining a spy from scratch: foo() returns 'baz' |
* var foo = jasmine.createSpy('spy on foo').andReturn('baz'); |
* var foo = jasmine.createSpy('spy on foo').andReturn('baz'); |
* |
* |
* // defining a spy on an existing property: foo.bar() returns 'baz' |
* // defining a spy on an existing property: foo.bar() returns 'baz' |
* spyOn(foo, 'bar').andReturn('baz'); |
* spyOn(foo, 'bar').andReturn('baz'); |
* |
* |
* @param {Object} value |
* @param {Object} value |
*/ |
*/ |
jasmine.Spy.prototype.andReturn = function(value) { |
jasmine.Spy.prototype.andReturn = function (value) { |
this.plan = function() { |
this.plan = function () { |
return value; |
return value; |
}; |
}; |
return this; |
return this; |
}; |
}; |
|
|
/** |
/** |
* For throwing an exception when a spy is called. |
* For throwing an exception when a spy is called. |
* |
* |
* @example |
* @example |
* // defining a spy from scratch: foo() throws an exception w/ message 'ouch' |
* // defining a spy from scratch: foo() throws an exception w/ message 'ouch' |
* var foo = jasmine.createSpy('spy on foo').andThrow('baz'); |
* var foo = jasmine.createSpy('spy on foo').andThrow('baz'); |
* |
* |
* // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch' |
* // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch' |
* spyOn(foo, 'bar').andThrow('baz'); |
* spyOn(foo, 'bar').andThrow('baz'); |
* |
* |
* @param {String} exceptionMsg |
* @param {String} exceptionMsg |
*/ |
*/ |
jasmine.Spy.prototype.andThrow = function(exceptionMsg) { |
jasmine.Spy.prototype.andThrow = function (exceptionMsg) { |
this.plan = function() { |
this.plan = function () { |
throw exceptionMsg; |
throw exceptionMsg; |
}; |
}; |
return this; |
return this; |
}; |
}; |
|
|
/** |
/** |
* Calls an alternate implementation when a spy is called. |
* Calls an alternate implementation when a spy is called. |
* |
* |
* @example |
* @example |
* var baz = function() { |
* var baz = function() { |
* // do some stuff, return something |
* // do some stuff, return something |
* } |
* } |
* // defining a spy from scratch: foo() calls the function baz |
* // defining a spy from scratch: foo() calls the function baz |
* var foo = jasmine.createSpy('spy on foo').andCall(baz); |
* var foo = jasmine.createSpy('spy on foo').andCall(baz); |
* |
* |
* // defining a spy on an existing property: foo.bar() calls an anonymnous function |
* // defining a spy on an existing property: foo.bar() calls an anonymnous function |
* spyOn(foo, 'bar').andCall(function() { return 'baz';} ); |
* spyOn(foo, 'bar').andCall(function() { return 'baz';} ); |
* |
* |
* @param {Function} fakeFunc |
* @param {Function} fakeFunc |
*/ |
*/ |
jasmine.Spy.prototype.andCallFake = function(fakeFunc) { |
jasmine.Spy.prototype.andCallFake = function (fakeFunc) { |
this.plan = fakeFunc; |
this.plan = fakeFunc; |
return this; |
return this; |
}; |
}; |
|
|
/** |
/** |
* Resets all of a spy's the tracking variables so that it can be used again. |
* Resets all of a spy's the tracking variables so that it can be used again. |
* |
* |
* @example |
* @example |
* spyOn(foo, 'bar'); |
* spyOn(foo, 'bar'); |
* |
* |
* foo.bar(); |
* foo.bar(); |
* |
* |
* expect(foo.bar.callCount).toEqual(1); |
* expect(foo.bar.callCount).toEqual(1); |
* |
* |
* foo.bar.reset(); |
* foo.bar.reset(); |
* |
* |
* expect(foo.bar.callCount).toEqual(0); |
* expect(foo.bar.callCount).toEqual(0); |