Rename hashContaining to objectContaining, since this is javascript. Also call the toString from prettyPrinter

This commit is contained in:
gvanhove
2011-03-10 07:59:19 -08:00
parent 992367dcbc
commit 7158fc2426
7 changed files with 32 additions and 23 deletions

View File

@@ -207,8 +207,8 @@ jasmine.any = function(clazz) {
* @param sample {Object} sample * @param sample {Object} sample
* @returns matchable object for the sample * @returns matchable object for the sample
*/ */
jasmine.hashContaining = function (sample) { jasmine.objectContaining = function (sample) {
return new jasmine.Matchers.HashContaining(sample); return new jasmine.Matchers.ObjectContaining(sample);
}; };
/** /**
@@ -937,11 +937,11 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) {
return b.matches(a); return b.matches(a);
} }
if (a instanceof jasmine.Matchers.HashContaining) { if (a instanceof jasmine.Matchers.ObjectContaining) {
return a.matches(b); return a.matches(b);
} }
if (b instanceof jasmine.Matchers.HashContaining) { if (b instanceof jasmine.Matchers.ObjectContaining) {
return b.matches(a); return b.matches(a);
} }
@@ -1500,11 +1500,11 @@ jasmine.Matchers.Any.prototype.toString = function() {
return '<jasmine.any(' + this.expectedClass + ')>'; return '<jasmine.any(' + this.expectedClass + ')>';
}; };
jasmine.Matchers.HashContaining = function (sample) { jasmine.Matchers.ObjectContaining = function (sample) {
this.sample = sample; this.sample = sample;
}; };
jasmine.Matchers.HashContaining.prototype.matches = function(other, mismatchKeys, mismatchValues) { jasmine.Matchers.ObjectContaining.prototype.matches = function(other, mismatchKeys, mismatchValues) {
mismatchKeys = mismatchKeys || []; mismatchKeys = mismatchKeys || [];
mismatchValues = mismatchValues || []; mismatchValues = mismatchValues || [];
@@ -1526,7 +1526,7 @@ jasmine.Matchers.HashContaining.prototype.matches = function(other, mismatchKeys
return (mismatchKeys.length === 0 && mismatchValues.length === 0); return (mismatchKeys.length === 0 && mismatchValues.length === 0);
}; };
jasmine.Matchers.HashContaining.prototype.toString = function () { jasmine.Matchers.ObjectContaining.prototype.toString = function () {
return "<jasmine.hashContaining(" + jasmine.pp(this.sample) + ")>"; return "<jasmine.hashContaining(" + jasmine.pp(this.sample) + ")>";
}; };
/** /**

View File

@@ -834,14 +834,14 @@ describe("jasmine.Matchers", function() {
describe("with an empty hash", function () { describe("with an empty hash", function () {
var containing; var containing;
beforeEach(function () { beforeEach(function () {
containing = new jasmine.Matchers.HashContaining({}); containing = new jasmine.Matchers.ObjectContaining({});
}); });
it("matches everything", function () { it("matches everything", function () {
expect(containing.matches("foo", [], [])).toBe(true); expect(containing.matches("foo", [], [])).toBe(true);
}); });
it("says it didn't expect to contain anything", function () { it("says it didn't expect to contain anything", function () {
expect(containing.toString()).toEqual("<jasmine.hashContaining({ })>"); expect(containing.toString()).toEqual("<jasmine.objectContaining({ })>");
}); });
}); });
@@ -850,7 +850,7 @@ describe("jasmine.Matchers", function() {
beforeEach(function () { beforeEach(function () {
mismatchKeys = []; mismatchKeys = [];
mismatchValues = []; mismatchValues = [];
containing = new jasmine.Matchers.HashContaining({foo: "fooVal", bar: "barVal"}); containing = new jasmine.Matchers.ObjectContaining({foo: "fooVal", bar: "barVal"});
}); });
it("doesn't match an empty object", function () { it("doesn't match an empty object", function () {
@@ -894,7 +894,7 @@ describe("jasmine.Matchers", function() {
}); });
it("says what it expects to contain", function () { it("says what it expects to contain", function () {
expect(containing.toString()).toEqual("<jasmine.hashContaining(" + jasmine.pp({foo:"fooVal", bar:"barVal"}) + ")>"); expect(containing.toString()).toEqual("<jasmine.objectContaining(" + jasmine.pp({foo:"fooVal", bar:"barVal"}) + ")>");
}); });
}); });
@@ -905,11 +905,11 @@ describe("jasmine.Matchers", function() {
method({a:"b", c:"d"}); method({a:"b", c:"d"});
}); });
it("works correctly for positive matches", function () { it("works correctly for positive matches", function () {
expect(method).toHaveBeenCalledWith(jasmine.hashContaining({a:"b"})); expect(method).toHaveBeenCalledWith(jasmine.objectContaining({a:"b"}));
}); });
it("works correctly for negative matches", function () { it("works correctly for negative matches", function () {
expect(method).not.toHaveBeenCalledWith(jasmine.hashContaining({z:"x"})); expect(method).not.toHaveBeenCalledWith(jasmine.objectContaining({z:"x"}));
}); });
}); });
}); });

View File

@@ -83,5 +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 () {
var containing = new jasmine.Matchers.ObjectContaining({});
spyOn(containing, "toString").andReturn("stringified!");
expect(jasmine.pp(containing)).toEqual("stringified!");
expect(containing.toString).toHaveBeenCalled();
});
}); });

View File

@@ -238,11 +238,11 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) {
return b.matches(a); return b.matches(a);
} }
if (a instanceof jasmine.Matchers.HashContaining) { if (a instanceof jasmine.Matchers.ObjectContaining) {
return a.matches(b); return a.matches(b);
} }
if (b instanceof jasmine.Matchers.HashContaining) { if (b instanceof jasmine.Matchers.ObjectContaining) {
return b.matches(a); return b.matches(a);
} }

View File

@@ -369,11 +369,11 @@ jasmine.Matchers.Any.prototype.toString = function() {
return '<jasmine.any(' + this.expectedClass + ')>'; return '<jasmine.any(' + this.expectedClass + ')>';
}; };
jasmine.Matchers.HashContaining = function (sample) { jasmine.Matchers.ObjectContaining = function (sample) {
this.sample = sample; this.sample = sample;
}; };
jasmine.Matchers.HashContaining.prototype.matches = function(other, mismatchKeys, mismatchValues) { jasmine.Matchers.ObjectContaining.prototype.matches = function(other, mismatchKeys, mismatchValues) {
mismatchKeys = mismatchKeys || []; mismatchKeys = mismatchKeys || [];
mismatchValues = mismatchValues || []; mismatchValues = mismatchValues || [];
@@ -395,6 +395,6 @@ jasmine.Matchers.HashContaining.prototype.matches = function(other, mismatchKeys
return (mismatchKeys.length === 0 && mismatchValues.length === 0); return (mismatchKeys.length === 0 && mismatchValues.length === 0);
}; };
jasmine.Matchers.HashContaining.prototype.toString = function () { jasmine.Matchers.ObjectContaining.prototype.toString = function () {
return "<jasmine.hashContaining(" + jasmine.pp(this.sample) + ")>"; return "<jasmine.objectContaining(" + jasmine.pp(this.sample) + ")>";
}; };

View File

@@ -25,6 +25,8 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
this.emitScalar('<global>'); this.emitScalar('<global>');
} else if (value instanceof jasmine.Matchers.Any) { } else if (value instanceof jasmine.Matchers.Any) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} 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)) {

View File

@@ -197,18 +197,18 @@ jasmine.any = function(clazz) {
}; };
/** /**
* Returns a matchable subset of a hash/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.hashContaining({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.hashContaining = function (sample) { jasmine.objectContaining = function (sample) {
return new jasmine.Matchers.HashContaining(sample); return new jasmine.Matchers.ObjectContaining(sample);
}; };
/** /**