Permalink
Browse files

Added YUITest.ObjectAssert.areSame

  • Loading branch information...
1 parent 956c0c7 commit c175debc96ad098e93ed51ce2744c52fe1686584 @ebi committed Nov 24, 2010
@@ -13,7 +13,6 @@ var YUITest = {
version: "@VERSION@"
};
-
/**
* Simple custom event implementation.
* @namespace YUITest
@@ -129,7 +128,6 @@ YUITest.EventTarget.prototype = {
};
-
/**
* Object containing helper methods.
* @namespace YUITest
@@ -177,7 +175,6 @@ YUITest.Util = {
};
-
/**
* Error is thrown whenever an assertion fails. It provides methods
* to more easily get at error information and also provides a base class
@@ -230,7 +227,6 @@ YUITest.AssertionError.prototype = {
}
};
-
/**
* ComparisonFailure is subclass of Error that is thrown whenever
* a comparison between two values fails. It provides mechanisms to retrieve
@@ -288,7 +284,6 @@ YUITest.ComparisonFailure.prototype.getMessage = function(){
return this.message + "\nExpected: " + this.expected + " (" + (typeof this.expected) + ")" +
"\nActual: " + this.actual + " (" + (typeof this.actual) + ")";
};
-
/**
* ShouldError is subclass of Error that is thrown whenever
* a test is expected to throw an error but doesn't.
@@ -318,7 +313,6 @@ YUITest.ShouldError.prototype = new YUITest.AssertionError();
//restore constructor
YUITest.ShouldError.prototype.constructor = YUITest.ShouldError;
-
/**
* ShouldFail is subclass of AssertionError that is thrown whenever
* a test was expected to fail but did not.
@@ -348,7 +342,6 @@ YUITest.ShouldFail.prototype = new YUITest.AssertionError();
//restore constructor
YUITest.ShouldFail.prototype.constructor = YUITest.ShouldFail;
-
/**
* UnexpectedError is subclass of AssertionError that is thrown whenever
* an error occurs within the course of a test and the test was not expected
@@ -394,7 +387,6 @@ YUITest.UnexpectedError.prototype = new YUITest.AssertionError();
//restore constructor
YUITest.UnexpectedError.prototype.constructor = YUITest.UnexpectedError;
-
/**
* UnexpectedValue is subclass of Error that is thrown whenever
* a value was unexpected in its scope. This typically means that a test
@@ -445,7 +437,6 @@ YUITest.UnexpectedValue.prototype.getMessage = function(){
return this.message + "\nUnexpected: " + this.unexpected + " (" + (typeof this.unexpected) + ") ";
};
-
/**
* Represents a stoppage in test execution to wait for an amount of time before
* continuing.
@@ -472,7 +463,6 @@ YUITest.Wait = function (segment, delay) {
*/
this.delay = (typeof delay == "number" ? delay : 0);
};
-
/**
* The Assert object provides functions to test JavaScript values against
@@ -946,7 +936,6 @@ YUITest.Assert = {
};
-
/**
* The ArrayAssert object provides functions to test JavaScript array objects
* for a variety of cases.
@@ -1317,7 +1306,6 @@ YUITest.ArrayAssert = {
};
-
/**
* The ObjectAssert object provides functions to test JavaScript objects
* for a variety of cases.
@@ -1350,6 +1338,43 @@ YUITest.ObjectAssert = {
},
/**
+ * Assert that an object has all of the same properties and property values
+ * as the expected one. It doesn't consider prototype properties.
+ * @param {Object} expected The object with all expected properties and values.
+ * @param {Object} actual The object to inspect.
+ * @param {String} message (Optional) The message to display if the assertion fails.
+ * @method areSame
+ * @static
+ */
+ areSame: function (expected, actual, message) {
+ YUITest.Assert._increment();
+
+
+ if ('object' !== typeof actual) {
+ throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Value should be an object."), actual);
+ }
+ if ('object' !== typeof expected) {
+ throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Value should be an object."), expected);
+ }
+
+ for (var key in expected) {
+ if (expected.hasOwnProperty(key)) {
+ if ('object' === typeof actual[key] || 'object' === typeof expected[key]) {
+ YUITest.ObjectAssert.areSame(expected[key], actual[key], message);
+ } else if (actual[key] !== expected[key]) {
+ throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal for property " + key), expected[key], actual[key]);
+ }
+ }
+ }
+
+ for (key in actual) {
+ if (actual.hasOwnProperty(key) && 'undefined' === typeof expected[key]) {
+ throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, 'Property is not defined on actual object ' + key));
+ }
+ }
+ },
+
+ /**
* Asserts that an object has a property with the given name.
* @param {String} propertyName The name of the property to test.
* @param {Object} object The object to search.
@@ -1496,7 +1521,6 @@ YUITest.ObjectAssert = {
};
-
/**
* The DateAssert object provides functions to test JavaScript Date objects
* for a variety of cases.
@@ -1580,7 +1604,6 @@ YUITest.DateAssert = {
}
};
-
/**
* Creates a new mock object.
* @namespace YUITest
@@ -1803,7 +1826,6 @@ YUITest.Mock.Value.Object = YUITest.Mock.Value(YUITest.Assert.isObject);
* @type Function
*/
YUITest.Mock.Value.Function = YUITest.Mock.Value(YUITest.Assert.isFunction);
-
/**
* Test case containing various tests to run.
* @param template An object containing any number of test methods, other methods,
@@ -1918,7 +1940,6 @@ YUITest.TestCase.prototype = {
}
};
-
/**
* A test suite that can contain a collection of TestCase and TestSuite objects.
@@ -2002,7 +2023,6 @@ YUITest.TestSuite.prototype = {
}
};
-
/**
* An object object containing test result formatting methods.
* @namespace YUITest
@@ -2246,7 +2266,6 @@ YUITest.TestFormat = function(){
};
}();
-
/**
* An object object containing coverage result formatting methods.
* @namespace YUITest
@@ -2290,7 +2309,6 @@ YUITest.CoverageFormat = {
}
};
-
/**
* Runs test suites and test cases, providing events to allowing for the
@@ -3163,4 +3181,3 @@ YUITest.CoverageFormat = {
return new TestRunner();
}();
-

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit c175deb

Please sign in to comment.