Merge branch 'matcher-refactor' of git://github.com/Xian/jasmine into xian_jasmine

This commit is contained in:
ragaskar 2009-11-19 19:21:47 -08:00
commit f99a5ff577
9 changed files with 279 additions and 326 deletions

View File

@ -89,9 +89,7 @@ jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
for (var i = 0; i < resultItems.length; i++) { for (var i = 0; i < resultItems.length; i++) {
var result = resultItems[i]; var result = resultItems[i];
if (result.passed && !result.passed()) { if (result.passed && !result.passed()) {
var resultMessageDiv = this.createDom('div', {className: 'resultMessage fail'}); specDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
resultMessageDiv.innerHTML = result.message; // todo: lame; mend
specDiv.appendChild(resultMessageDiv);
specDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); specDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
} }
} }

View File

@ -8,7 +8,7 @@ describe("jasmine.Matchers", function() {
}); });
function match(value) { function match(value) {
return new jasmine.Matchers(env, value, mockSpec); return new env.matchersClass(env, value, mockSpec);
} }
it("toEqual with primitives, objects, dates, html nodes, etc.", function() { it("toEqual with primitives, objects, dates, html nodes, etc.", function() {
@ -161,8 +161,7 @@ describe("jasmine.Matchers", function() {
expect(result.matcherName).toEqual("toMatch"); expect(result.matcherName).toEqual("toMatch");
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
expect(result.message).toMatch(jasmine.pp(actual)); expect(result.message).toEqual("Expected 'a' to match 'b'.");
expect(result.message).toMatch(new RegExp(expected).toString());
expect(result.expected).toEqual(expected); expect(result.expected).toEqual(expected);
expect(result.actual).toEqual(actual); expect(result.actual).toEqual(actual);
}); });
@ -177,9 +176,7 @@ describe("jasmine.Matchers", function() {
expect(result.matcherName).toEqual("toNotMatch"); expect(result.matcherName).toEqual("toNotMatch");
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
expect(result.message).toMatch(expected.toString()); expect(result.message).toEqual("Expected 'a' to not match /a/.");
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch("not");
expect(result.expected).toEqual(expected); expect(result.expected).toEqual(expected);
expect(result.actual).toEqual(actual); expect(result.actual).toEqual(actual);
}); });
@ -193,9 +190,7 @@ describe("jasmine.Matchers", function() {
expect(result.matcherName).toEqual("toNotMatch"); expect(result.matcherName).toEqual("toNotMatch");
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
expect(result.message).toMatch(jasmine.pp(str)); expect(result.message).toEqual("Expected 'a' to not match 'a'.");
expect(result.message).toMatch(new RegExp(str).toString());
expect(result.message).toMatch("not");
expect(result.expected).toEqual(str); expect(result.expected).toEqual(str);
expect(result.actual).toEqual(str); expect(result.actual).toEqual(str);
}); });
@ -213,7 +208,7 @@ describe("jasmine.Matchers", function() {
expect(result.matcherName).toEqual("toBeDefined"); expect(result.matcherName).toEqual("toBeDefined");
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
expect(result.message).toEqual('Expected actual to not be undefined.'); expect(result.message).toEqual('Expected undefined to be defined.');
expect(result.actual).toEqual(undefined); expect(result.actual).toEqual(undefined);
}); });
@ -297,7 +292,7 @@ describe("jasmine.Matchers", function() {
expect(result.matcherName).toEqual("toBeTruthy"); expect(result.matcherName).toEqual("toBeTruthy");
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
expect(result.message).toEqual("Expected actual to be truthy"); expect(result.message).toEqual("Expected false to be truthy.");
expect(result.actual).toEqual(false); expect(result.actual).toEqual(false);
}); });
@ -440,9 +435,9 @@ describe("jasmine.Matchers", function() {
}); });
it("toThrow", function() { it("toThrow", function() {
var expected = new jasmine.Matchers(env, function() { var expected = match(function() {
throw new Error("Fake Error"); throw new Error("Fake Error");
}, mockSpec); });
expect(expected.toThrow()).toEqual(true); expect(expected.toThrow()).toEqual(true);
expect(expected.toThrow("Fake Error")).toEqual(true); expect(expected.toThrow("Fake Error")).toEqual(true);
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true); expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);
@ -463,7 +458,7 @@ describe("jasmine.Matchers", function() {
} catch (e) { } catch (e) {
exception = e; exception = e;
} }
;
expect(exception).toBeDefined(); expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function'); expect(exception.message).toEqual('Actual is not a function');
@ -476,50 +471,75 @@ describe("jasmine.Matchers", function() {
}); });
describe("wasCalled, wasNotCalled, wasCalledWith", function() { describe("spy matchers >>", function() {
var TestClass; var TestClass;
beforeEach(function() { beforeEach(function() {
TestClass = { someFunction: function() { TestClass = {
} }; normalFunction: function() {
}); },
describe('without spies', function() { spyFunction: jasmine.createSpy("My spy")
it('should always show an error', function () { };
expect(match(TestClass.someFunction).wasCalled()).toEqual(false);
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.message).toEqual("Actual is not a spy.");
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.message).toEqual("Actual is not a spy.");
});
}); });
describe('with spies', function () { function shouldThrowAnExceptionWhenInvokedOnANonSpy(methodName) {
return function() {
expect(function() {
match(TestClass.normalFunction)[methodName]();
}).toThrow('Expected a spy, but got Function.');
beforeEach(function () { expect(function() {
TestClass.someFunction = jasmine.createSpy("My spy"); match(undefined)[methodName]();
}).toThrow('Expected a spy, but got undefined.');
expect(function() {
match({some:'object'})[methodName]();
}).toThrow('Expected a spy, but got { some : \'object\' }.');
};
}
describe("wasCalled", function() {
it("should pass iff the spy was called", function() {
expect(match(TestClass.spyFunction).wasCalled()).toEqual(false);
TestClass.spyFunction();
expect(match(TestClass.spyFunction).wasCalled()).toEqual(true);
}); });
it("should track if it was called", function() { it("should throw an exception when invoked with any arguments", function() {
expect(match(TestClass.someFunction).wasCalled()).toEqual(false); expect(function() {
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(true); match(TestClass.normalFunction).wasCalled("unwanted argument");
TestClass.someFunction();
expect(match(TestClass.someFunction).wasCalled()).toEqual(true);
expect(function () {
match(TestClass.someFunction).wasCalled('some arg');
}).toThrow('wasCalled does not take arguments, use wasCalledWith'); }).toThrow('wasCalled does not take arguments, use wasCalledWith');
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
}); });
it('should return true if it was called with the expected args', function() { it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalled'));
TestClass.someFunction('a', 'b', 'c'); });
expect(match(TestClass.someFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
describe("wasNotCalled", function() {
it("should pass iff the spy was not called", function() {
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
TestClass.spyFunction();
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(false);
});
it("should throw an exception when invoked with any arguments", function() {
expect(function() {
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
}).toThrow('wasNotCalled does not take arguments');
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalled'));
});
describe("wasCalledWith", function() {
it('wasCalledWith should return true if it was called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
expect(match(TestClass.spyFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
}); });
it('should return false if it was not called with the expected args', function() { it('should return false if it was not called with the expected args', function() {
TestClass.someFunction('a', 'b', 'c'); TestClass.spyFunction('a', 'b', 'c');
var expected = match(TestClass.someFunction); var expected = match(TestClass.spyFunction);
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false); expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.passed()).toEqual(false); expect(result.passed()).toEqual(false);
@ -528,67 +548,42 @@ describe("jasmine.Matchers", function() {
}); });
it('should allow matches across multiple calls', function() { it('should allow matches across multiple calls', function() {
var expected = match(TestClass.someFunction); var expected = match(TestClass.spyFunction);
TestClass.someFunction('a', 'b', 'c'); TestClass.spyFunction('a', 'b', 'c');
TestClass.someFunction('d', 'e', 'f'); TestClass.spyFunction('d', 'e', 'f');
expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true); expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true);
expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true); expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true);
expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false); expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false);
}); });
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalledWith'));
describe("to build an ExpectationResult", function () {
beforeEach(function() {
var currentSuite;
var spec;
currentSuite = env.describe('default current suite', function() {
spec = env.it();
}, spec);
TestClass = { someFunction: function(a, b) {
} };
spec.spyOn(TestClass, 'someFunction');
});
it("should should handle the case of a spy", function() {
TestClass.someFunction('a', 'c');
var matcher = match(TestClass.someFunction);
matcher.wasCalledWith('a', 'b');
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toMatch("['a', 'b']");
expect(result.message).toMatch("['a', 'c']");
expect(result.actual).toEqual(TestClass.someFunction);
expect(result.expected).toEqual(['a','b']);
});
});
}); });
}); });
});
describe("wasCalledWith to build an ExpectationResult", function () {
var TestClass;
beforeEach(function() {
var currentSuite;
var spec;
currentSuite = env.describe('default current suite', function() {
spec = env.it();
}, spec);
TestClass = { someFunction: function(a, b) {
} };
spec.spyOn(TestClass, 'someFunction');
});
it("should handle case of actual not being a spy", function() {
var matcher = match();
matcher.wasCalledWith('a', 'b');
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toEqual("Actual is not a spy");
expect(result.actual).toEqual(undefined);
expect(result.expected).toEqual(['a','b']);
matcher = match('foo');
matcher.wasCalledWith('a', 'b');
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toEqual("Actual is not a spy");
expect(result.actual).toEqual('foo');
expect(result.expected).toEqual(['a','b']);
});
it("should should handle the case of a spy", function() {
TestClass.someFunction('a', 'c');
var matcher = match(TestClass.someFunction);
matcher.wasCalledWith('a', 'b');
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toMatch("['a', 'b']");
expect(result.message).toMatch("['a', 'c']");
expect(result.actual).toEqual(TestClass.someFunction);
expect(result.expected).toEqual(['a','b']);
});
});
})
;

View File

@ -32,6 +32,10 @@ describe("jasmine.pp", function () {
}, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }"); }, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }");
}); });
it("should stringify RegExp objects properly", function() {
expect(jasmine.pp(/x|y|z/)).toEqual("/x|y|z/");
});
it("should indicate circular object references", function() { it("should indicate circular object references", function() {
var sampleValue = {foo: 'hello'}; var sampleValue = {foo: 'hello'};
sampleValue.nested = sampleValue; sampleValue.nested = sampleValue;
@ -69,5 +73,21 @@ describe("jasmine.pp", function () {
expect(jasmine.pp(window)).toEqual("<window>"); expect(jasmine.pp(window)).toEqual("<window>");
}); });
it("should stringify Date objects properly", function() {
var now = new Date();
expect(jasmine.pp(now)).toEqual("Date(" + now.toString() + ")");
});
it("should stringify spy objects properly", function() {
var TestObject = {
someFunction: function() {
}
};
spyOn(TestObject, 'someFunction');
expect(jasmine.pp(TestObject.someFunction)).toEqual("spy on someFunction");
expect(jasmine.pp(jasmine.createSpy("something"))).toEqual("spy on something");
});
}); });

View File

@ -122,6 +122,19 @@ describe("TrivialReporter", function() {
var divs = body.getElementsByTagName("div"); var divs = body.getElementsByTagName("div");
expect(divs[3].innerHTML).toEqual("Expected 'a' to be null, but it was not"); expect(divs[3].innerHTML).toEqual("Expected 'a' to be null, but it was not");
}); });
it("should add the failure message to the DOM (non-toEquals matchers)", function() {
expectationResult = new jasmine.ExpectationResult({
matcherName: "toBeNull", passed: false, message: "Expected '1 < 2' to <b>e null, & it was not"
});
spyOn(results, 'getItems').andReturn([expectationResult]);
trivialReporter.reportSpecResults(spec);
var divs = body.getElementsByTagName("div");
expect(divs[3].innerHTML).toEqual("Expected '1 &lt; 2' to &lt;b&gt;e null, &amp; it was not");
});
}); });
}); });

View File

@ -10,7 +10,7 @@ jasmine.Env = function() {
this.reporter = new jasmine.MultiReporter(); this.reporter = new jasmine.MultiReporter();
this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
this.lastUpdate = 0; this.lastUpdate = 0;
this.specFilter = function() { this.specFilter = function() {
return true; return true;
@ -19,6 +19,17 @@ jasmine.Env = function() {
this.nextSpecId_ = 0; this.nextSpecId_ = 0;
this.nextSuiteId_ = 0; this.nextSuiteId_ = 0;
this.equalityTesters_ = []; this.equalityTesters_ = [];
// wrap matchers
this.matchersClass = function() {
jasmine.Matchers.apply(this, arguments);
};
jasmine.util.inherit(this.matchersClass, jasmine.Matchers);
for (var methodName in jasmine.Matchers.prototype) {
var orig = jasmine.Matchers.prototype[methodName];
this.matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig);
}
}; };

View File

@ -24,15 +24,25 @@ jasmine.Matchers.prototype.report = function(result, failing_message, details) {
return result; return result;
}; };
jasmine.Matchers.matcherFn_ = function(matcherName, options) { jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
return function () { return function() {
jasmine.util.extend(this, options);
var matcherArgs = jasmine.util.argsToArray(arguments); var matcherArgs = jasmine.util.argsToArray(arguments);
var args = [this.actual].concat(matcherArgs); var result = matcherFunction.apply(this, arguments);
var result = options.test.apply(this, args);
var message; var message;
if (!result) { if (!result) {
message = options.message.apply(this, args); if (this.message) {
message = this.message.apply(this, arguments);
} else {
var englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); });
message = "Expected " + jasmine.pp(this.actual) + " " + englishyPredicate;
if (matcherArgs.length > 0) {
for (var i = 0; i < matcherArgs.length; i++) {
if (i > 0) message += ",";
message += " " + jasmine.pp(matcherArgs[i]);
}
}
message += ".";
}
} }
var expectationResult = new jasmine.ExpectationResult({ var expectationResult = new jasmine.ExpectationResult({
matcherName: matcherName, matcherName: matcherName,
@ -54,27 +64,17 @@ jasmine.Matchers.matcherFn_ = function(matcherName, options) {
* @param expected * @param expected
*/ */
jasmine.Matchers.prototype.toBe = jasmine.Matchers.matcherFn_('toBe', { jasmine.Matchers.prototype.toBe = function(expected) {
test: function (actual, expected) { return this.actual === expected;
return actual === expected; };
},
message: function(actual, expected) {
return "Expected " + jasmine.pp(actual) + " to be " + jasmine.pp(expected);
}
});
/** /**
* toNotBe: compares the actual to the expected using !== * toNotBe: compares the actual to the expected using !==
* @param expected * @param expected
*/ */
jasmine.Matchers.prototype.toNotBe = jasmine.Matchers.matcherFn_('toNotBe', { jasmine.Matchers.prototype.toNotBe = function(expected) {
test: function (actual, expected) { return this.actual !== expected;
return actual !== expected; };
},
message: function(actual, expected) {
return "Expected " + jasmine.pp(actual) + " to not be " + jasmine.pp(expected);
}
});
/** /**
* toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc.
@ -82,27 +82,17 @@ jasmine.Matchers.prototype.toNotBe = jasmine.Matchers.matcherFn_('toNotBe', {
* @param expected * @param expected
*/ */
jasmine.Matchers.prototype.toEqual = jasmine.Matchers.matcherFn_('toEqual', { jasmine.Matchers.prototype.toEqual = function(expected) {
test: function (actual, expected) { return this.env.equals_(this.actual, expected);
return this.env.equals_(actual, expected); };
},
message: function(actual, expected) {
return "Expected " + jasmine.pp(actual) + " to equal " + jasmine.pp(expected);
}
});
/** /**
* toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual
* @param expected * @param expected
*/ */
jasmine.Matchers.prototype.toNotEqual = jasmine.Matchers.matcherFn_('toNotEqual', { jasmine.Matchers.prototype.toNotEqual = function(expected) {
test: function (actual, expected) { return !this.env.equals_(this.actual, expected);
return !this.env.equals_(actual, expected); };
},
message: function(actual, expected) {
return "Expected " + jasmine.pp(actual) + " to not equal " + jasmine.pp(expected);
}
});
/** /**
* Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes
@ -110,228 +100,143 @@ jasmine.Matchers.prototype.toNotEqual = jasmine.Matchers.matcherFn_('toNotEqual'
* *
* @param reg_exp * @param reg_exp
*/ */
jasmine.Matchers.prototype.toMatch = jasmine.Matchers.matcherFn_('toMatch', { jasmine.Matchers.prototype.toMatch = function(expected) {
test: function(actual, expected) { return new RegExp(expected).test(this.actual);
return new RegExp(expected).test(actual); };
},
message: function(actual, expected) {
return jasmine.pp(actual) + " does not match the regular expression " + new RegExp(expected).toString();
}
});
/** /**
* Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
* @param reg_exp * @param reg_exp
*/ */
jasmine.Matchers.prototype.toNotMatch = function(expected) {
jasmine.Matchers.prototype.toNotMatch = jasmine.Matchers.matcherFn_('toNotMatch', { return !(new RegExp(expected).test(this.actual));
test: function(actual, expected) { };
return !(new RegExp(expected).test(actual));
},
message: function(actual, expected) {
return jasmine.pp(actual) + " should not match " + new RegExp(expected).toString();
}
});
/** /**
* Matcher that compares the acutal to undefined. * Matcher that compares the actual to undefined.
*/ */
jasmine.Matchers.prototype.toBeDefined = function() {
jasmine.Matchers.prototype.toBeDefined = jasmine.Matchers.matcherFn_('toBeDefined', { return (this.actual !== undefined);
test: function(actual) { };
return (actual !== undefined);
},
message: function() {
return 'Expected actual to not be undefined.';
}
});
/** /**
* Matcher that compares the acutal to undefined. * Matcher that compares the actual to undefined.
*/ */
jasmine.Matchers.prototype.toBeUndefined = function() {
jasmine.Matchers.prototype.toBeUndefined = jasmine.Matchers.matcherFn_('toBeUndefined', { return (this.actual === undefined);
test: function(actual) { };
return (actual === undefined);
},
message: function(actual) {
return 'Expected ' + jasmine.pp(actual) + ' to be undefined.';
}
});
/** /**
* Matcher that compares the actual to null. * Matcher that compares the actual to null.
*
*/ */
jasmine.Matchers.prototype.toBeNull = jasmine.Matchers.matcherFn_('toBeNull', { jasmine.Matchers.prototype.toBeNull = function() {
test: function(actual) { return (this.actual === null);
return (actual === null); };
},
message: function(actual) {
return 'Expected ' + jasmine.pp(actual) + ' to be null.';
}
});
/** /**
* Matcher that boolean not-nots the actual. * Matcher that boolean not-nots the actual.
*/ */
jasmine.Matchers.prototype.toBeTruthy = jasmine.Matchers.matcherFn_('toBeTruthy', { jasmine.Matchers.prototype.toBeTruthy = function() {
test: function(actual) { return !!this.actual;
return !!actual; };
},
message: function() {
return 'Expected actual to be truthy';
}
});
/** /**
* Matcher that boolean nots the actual. * Matcher that boolean nots the actual.
*/ */
jasmine.Matchers.prototype.toBeFalsy = jasmine.Matchers.matcherFn_('toBeFalsy', { jasmine.Matchers.prototype.toBeFalsy = function() {
test: function(actual) { return !this.actual;
return !actual; };
},
message: function(actual) {
return 'Expected ' + jasmine.pp(actual) + ' to be falsy';
}
});
/** /**
* Matcher that checks to see if the acutal, a Jasmine spy, was called. * Matcher that checks to see if the actual, a Jasmine spy, was called.
*/ */
jasmine.Matchers.prototype.wasCalled = function() {
jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.matcherFn_('wasCalled', { if (arguments.length > 0) {
getActual_: function() { throw new Error('wasCalled does not take arguments, use wasCalledWith');
var args = jasmine.util.argsToArray(arguments);
if (args.length > 1) {
throw(new Error('wasCalled does not take arguments, use wasCalledWith'));
}
return args.splice(0, 1)[0];
},
test: function() {
var actual = this.getActual_.apply(this, arguments);
if (!actual || !actual.isSpy) {
return false;
}
return actual.wasCalled;
},
message: function() {
var actual = this.getActual_.apply(this, arguments);
if (!actual || !actual.isSpy) {
return 'Actual is not a spy.';
}
return "Expected spy " + actual.identity + " to have been called.";
} }
});
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
return "Expected spy " + this.actual.identity + " to have been called.";
};
return this.actual.wasCalled;
};
/** /**
* Matcher that checks to see if the acutal, a Jasmine spy, was not called. * Matcher that checks to see if the actual, a Jasmine spy, was not called.
*/ */
jasmine.Matchers.prototype.wasNotCalled = jasmine.Matchers.matcherFn_('wasNotCalled', { jasmine.Matchers.prototype.wasNotCalled = function() {
getActual_: function() { if (arguments.length > 0) {
var args = jasmine.util.argsToArray(arguments); throw new Error('wasNotCalled does not take arguments');
return args.splice(0, 1)[0];
},
test: function() {
var actual = this.getActual_.apply(this, arguments);
if (!actual || !actual.isSpy) {
return false;
}
return !actual.wasCalled;
},
message: function() {
var actual = this.getActual_.apply(this, arguments);
if (!actual || !actual.isSpy) {
return 'Actual is not a spy.';
}
return "Expected spy " + actual.identity + " to not have been called.";
} }
});
jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.matcherFn_('wasCalledWith', { if (!jasmine.isSpy(this.actual)) {
test: function() { throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
var args = jasmine.util.argsToArray(arguments);
var actual = args.splice(0, 1)[0];
if (!actual || !actual.isSpy) {
return false;
}
return this.env.contains_(actual.argsForCall, args);
},
message: function() {
var args = jasmine.util.argsToArray(arguments);
var actual = args.splice(0, 1)[0];
var message;
if (!actual || !actual.isSpy) {
message = 'Actual is not a spy';
} else {
message = "Expected spy to have been called with " + jasmine.pp(args) + " but was called with " + actual.argsForCall;
}
return message;
} }
});
this.message = function() {
return "Expected spy " + this.actual.identity + " to not have been called.";
};
return !this.actual.wasCalled;
};
/** /**
* Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. * Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
* *
* @example * @example
* *
*/ */
jasmine.Matchers.prototype.wasCalledWith = function() {
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
return "Expected spy to have been called with " + jasmine.pp(arguments) + " but was called with " + jasmine.pp(this.actual.argsForCall);
};
return this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
};
/** /**
* Matcher that checks that the expected item is an element in the actual Array. * Matcher that checks that the expected item is an element in the actual Array.
* *
* @param {Object} item * @param {Object} item
*/ */
jasmine.Matchers.prototype.toContain = function(expected) {
jasmine.Matchers.prototype.toContain = jasmine.Matchers.matcherFn_('toContain', { return this.env.contains_(this.actual, expected);
test: function(actual, expected) { };
return this.env.contains_(actual, expected);
},
message: function(actual, expected) {
return 'Expected ' + jasmine.pp(actual) + ' to contain ' + jasmine.pp(expected);
}
});
/** /**
* Matcher that checks that the expected item is NOT an element in the actual Array. * Matcher that checks that the expected item is NOT an element in the actual Array.
* *
* @param {Object} item * @param {Object} item
*/ */
jasmine.Matchers.prototype.toNotContain = jasmine.Matchers.matcherFn_('toNotContain', { jasmine.Matchers.prototype.toNotContain = function(expected) {
test: function(actual, expected) { return !this.env.contains_(this.actual, expected);
return !this.env.contains_(actual, expected); };
},
message: function(actual, expected) {
return 'Expected ' + jasmine.pp(actual) + ' to not contain ' + jasmine.pp(expected);
}
});
jasmine.Matchers.prototype.toBeLessThan = jasmine.Matchers.matcherFn_('toBeLessThan', { jasmine.Matchers.prototype.toBeLessThan = function(expected) {
test: function(actual, expected) { return this.actual < expected;
return actual < expected; };
},
message: function(actual, expected) {
return 'Expected ' + jasmine.pp(actual) + ' to be less than ' + jasmine.pp(expected);
}
});
jasmine.Matchers.prototype.toBeGreaterThan = jasmine.Matchers.matcherFn_('toBeGreaterThan', { jasmine.Matchers.prototype.toBeGreaterThan = function(expected) {
test: function(actual, expected) { return this.actual > expected;
return actual > expected; };
},
message: function(actual, expected) {
return 'Expected ' + jasmine.pp(actual) + ' to be greater than ' + jasmine.pp(expected);
}
});
/** /**
* Matcher that checks that the expected exception was thrown by the actual. * Matcher that checks that the expected exception was thrown by the actual.
* *
* @param {String} expectedException * @param {String} expectedException
*/ */
jasmine.Matchers.prototype.toThrow = jasmine.Matchers.matcherFn_('toThrow', { jasmine.Matchers.prototype.toThrow = function(expected) {
getException_: function(actual, expected) { function getException_(actual, expected) {
var exception; var exception;
if (typeof actual != 'function') { if (typeof actual != 'function') {
throw new Error('Actual is not a function'); throw new Error('Actual is not a function');
@ -342,24 +247,25 @@ jasmine.Matchers.prototype.toThrow = jasmine.Matchers.matcherFn_('toThrow', {
exception = e; exception = e;
} }
return exception; return exception;
}, }
test: function(actual, expected) {
var result = false; var result = false;
var exception = this.getException_(actual, expected); var exception = getException_(this.actual, expected);
if (exception) { if (exception) {
result = (expected === undefined || this.env.equals_(exception.message || exception, expected.message || expected)); result = (expected === undefined || this.env.equals_(exception.message || exception, expected.message || expected));
} }
return result;
}, this.message = function(expected) {
message: function(actual, expected) { var exception = getException_(this.actual, expected);
var exception = this.getException_(actual, expected);
if (exception && (expected === undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { if (exception && (expected === undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception ].join(' '); return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception ].join(' ');
} else { } else {
return "Expected function to throw an exception."; return "Expected function to throw an exception.";
} }
} };
});
return result;
};
jasmine.Matchers.Any = function(expectedClass) { jasmine.Matchers.Any = function(expectedClass) {
this.expectedClass = expectedClass; this.expectedClass = expectedClass;

View File

@ -9,11 +9,9 @@ jasmine.PrettyPrinter = function() {
* Formats a value in a nice, human-readable string. * Formats a value in a nice, human-readable string.
* *
* @param value * @param value
* @returns {String}
*/ */
jasmine.PrettyPrinter.prototype.format = function(value) { jasmine.PrettyPrinter.prototype.format = function(value) {
if (this.ppNestLevel_ > 40) { if (this.ppNestLevel_ > 40) {
// return '(jasmine.pp nested too deeply!)';
throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); throw new Error('jasmine.PrettyPrinter: format() nested too deeply!');
} }
@ -29,12 +27,16 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} else if (typeof value === 'string') { } else if (typeof value === 'string') {
this.emitString(value); this.emitString(value);
} else if (jasmine.isSpy(value)) {
this.emitScalar("spy on " + value.identity);
} else if (typeof value === 'function') { } else if (typeof value === 'function') {
this.emitScalar('Function'); this.emitScalar('Function');
} else if (typeof value.nodeType === 'number') { } else if (typeof value.nodeType === 'number') {
this.emitScalar('HTMLNode'); this.emitScalar('HTMLNode');
} else if (value instanceof Date) { } else if (value instanceof Date) {
this.emitScalar('Date(' + value + ')'); this.emitScalar('Date(' + value + ')');
} else if (value instanceof RegExp) {
this.emitScalar(value.toString());
} else if (value.__Jasmine_been_here_before__) { } else if (value.__Jasmine_been_here_before__) {
this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>'); this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>');
} else if (jasmine.isArray_(value) || typeof value == 'object') { } else if (jasmine.isArray_(value) || typeof value == 'object') {

View File

@ -10,11 +10,9 @@ jasmine.Spec = function(env, suite, description) {
if (!env) { if (!env) {
throw new Error('jasmine.Env() required'); throw new Error('jasmine.Env() required');
} }
;
if (!suite) { if (!suite) {
throw new Error('jasmine.Suite() required'); throw new Error('jasmine.Suite() required');
} }
;
var spec = this; var spec = this;
spec.id = env.nextSpecId ? env.nextSpecId() : null; spec.id = env.nextSpecId ? env.nextSpecId() : null;
spec.env = env; spec.env = env;
@ -91,7 +89,7 @@ jasmine.Spec.prototype.fail = function (e) {
}; };
jasmine.Spec.prototype.getMatchersClass_ = function() { jasmine.Spec.prototype.getMatchersClass_ = function() {
return this.matchersClass || jasmine.Matchers; return this.matchersClass || this.env.matchersClass;
}; };
jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {

View File

@ -19,7 +19,7 @@ jasmine.unimplementedMethod_ = function() {
jasmine.DEFAULT_UPDATE_INTERVAL = 250; jasmine.DEFAULT_UPDATE_INTERVAL = 250;
/** /**
* Allows for bound functions to be comapred. Internal use only. * Allows for bound functions to be compared. Internal use only.
* *
* @ignore * @ignore
* @private * @private
@ -324,6 +324,16 @@ jasmine.createSpy = function(name) {
return spyObj; return spyObj;
}; };
/**
* Determines whether an object is a spy.
*
* @param {jasmine.Spy|Object} putativeSpy
* @returns {Boolean}
*/
jasmine.isSpy = function(putativeSpy) {
return putativeSpy && putativeSpy.isSpy;
};
/** /**
* Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something * Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something
* large in one call. * large in one call.