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
+41
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() { describe("all matchers", function() {
it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() { it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() {
expect(match(true).toBe(true)).toBeUndefined(); expect(match(true).toBe(true)).toBeUndefined();
+5 -5
View File
@@ -83,12 +83,12 @@ describe("jasmine.pp", function () {
expect(jasmine.pp(jasmine.createSpy("something"))).toEqual("spy on something"); expect(jasmine.pp(jasmine.createSpy("something"))).toEqual("spy on something");
}); });
it("calls toString for ObjectContaining objects", function () { it("should stringify objects that implement jasmineToString", function () {
var containing = new jasmine.Matchers.ObjectContaining({}); var obj = {
spyOn(containing, "toString").andReturn("stringified!"); jasmineToString: function () { return "strung"; }
};
expect(jasmine.pp(containing)).toEqual("stringified!"); expect(jasmine.pp(obj)).toEqual("strung");
expect(containing.toString).toHaveBeenCalled();
}); });
}); });
+1 -1
View File
@@ -365,7 +365,7 @@ jasmine.Matchers.Any.prototype.matches = function(other) {
return other instanceof this.expectedClass; return other instanceof this.expectedClass;
}; };
jasmine.Matchers.Any.prototype.toString = function() { jasmine.Matchers.Any.prototype.jasmineToString = function() {
return '<jasmine.any(' + this.expectedClass + ')>'; return '<jasmine.any(' + this.expectedClass + ')>';
}; };
+2 -4
View File
@@ -23,10 +23,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
this.emitScalar('null'); this.emitScalar('null');
} else if (value === jasmine.getGlobal()) { } else if (value === jasmine.getGlobal()) {
this.emitScalar('<global>'); this.emitScalar('<global>');
} else if (value instanceof jasmine.Matchers.Any) { } else if (value.hasOwnProperty("jasmineToString")) {
this.emitScalar(value.toString()); this.emitScalar(value.jasmineToString());
} else if (value instanceof jasmine.Matchers.ObjectContaining) {
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)) { } else if (jasmine.isSpy(value)) {