Use jasmineToString for printing out complicated matchers

This commit is contained in:
gvanhove
2011-03-14 18:50:08 -07:00
parent 7158fc2426
commit 4f2fcff15a
4 changed files with 49 additions and 10 deletions

View File

@@ -914,6 +914,47 @@ describe("jasmine.Matchers", function() {
});
});
describe("Matchers.Any", function () {
var any;
describe(".jasmineToString", function () {
describe("with Object", function () {
it ("says it's looking for an object", function () {
any = jasmine.any(Object);
expect(any.jasmineToString()).toMatch(/<jasmine\.any\(function Object.*\)>/);
});
});
describe("with Function", function () {
it ("says it's looking for a function", function () {
any = jasmine.any(Function);
expect(any.jasmineToString()).toMatch(/<jasmine\.any\(function Function.*\)>/);
});
});
describe("with String", function () {
it ("says it's looking for a string", function () {
any = jasmine.any(String);
expect(any.jasmineToString()).toMatch(/<jasmine\.any\(function String.*\)>/);
});
});
describe("with Number", function () {
it ("says it's looking for a number", function () {
any = jasmine.any(Number);
expect(any.jasmineToString()).toMatch(/<jasmine\.any\(function Number.*\)>/);
});
});
describe("with some other defined 'class'", function () {
it ("says it's looking for an object", function () {
function MyClass () {}
any = jasmine.any(MyClass);
expect(any.jasmineToString()).toMatch(/<jasmine\.any\(function MyClass.*\)>/);
});
});
});
});
describe("all matchers", function() {
it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() {
expect(match(true).toBe(true)).toBeUndefined();

View File

@@ -83,12 +83,12 @@ describe("jasmine.pp", function () {
expect(jasmine.pp(jasmine.createSpy("something"))).toEqual("spy on something");
});
it("calls toString for ObjectContaining objects", function () {
var containing = new jasmine.Matchers.ObjectContaining({});
spyOn(containing, "toString").andReturn("stringified!");
it("should stringify objects that implement jasmineToString", function () {
var obj = {
jasmineToString: function () { return "strung"; }
};
expect(jasmine.pp(containing)).toEqual("stringified!");
expect(containing.toString).toHaveBeenCalled();
expect(jasmine.pp(obj)).toEqual("strung");
});
});

View File

@@ -365,7 +365,7 @@ jasmine.Matchers.Any.prototype.matches = function(other) {
return other instanceof this.expectedClass;
};
jasmine.Matchers.Any.prototype.toString = function() {
jasmine.Matchers.Any.prototype.jasmineToString = function() {
return '<jasmine.any(' + this.expectedClass + ')>';
};

View File

@@ -23,10 +23,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
this.emitScalar('null');
} else if (value === jasmine.getGlobal()) {
this.emitScalar('<global>');
} else if (value instanceof jasmine.Matchers.Any) {
this.emitScalar(value.toString());
} else if (value instanceof jasmine.Matchers.ObjectContaining) {
this.emitScalar(value.toString());
} else if (value.hasOwnProperty("jasmineToString")) {
this.emitScalar(value.jasmineToString());
} else if (typeof value === 'string') {
this.emitString(value);
} else if (jasmine.isSpy(value)) {