DateAssert.js revision 5d7303be23f52a46c5455a706a8e9862da8626b1
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /**
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The DateAssert object provides functions to test JavaScript Date objects
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * for a variety of cases.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm *
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @class DateAssert
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @namespace
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @static
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm */
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm Y.DateAssert = {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /**
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Asserts that a date's month, day, and year are equal to another date's.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {Date} expected The expected date.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {Date} actual The actual date to test.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {String} message (Optional) The message to display if the assertion fails.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @method datesAreEqual
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @static
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm */
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen datesAreEqual : function (expected, actual, message){
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen Y.Assert._increment();
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen if (expected instanceof Date && actual instanceof Date){
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen var msg = "";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm //check years first
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (expected.getFullYear() != actual.getFullYear()){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm msg = "Years should be equal.";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm //now check months
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (expected.getMonth() != actual.getMonth()){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm msg = "Months should be equal.";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm //last, check the day of the month
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (expected.getDate() != actual.getDate()){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm msg = "Days of month should be equal.";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (msg.length){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm throw new Y.Assert.ComparisonFailure(Y.Assert._formatMessage(message, msg), expected, actual);
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm } else {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm throw new TypeError("Y.Assert.datesAreEqual(): Expected and actual values must be Date objects.");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm },
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /**
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Asserts that a date's hour, minutes, and seconds are equal to another date's.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {Date} expected The expected date.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {Date} actual The actual date to test.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @param {String} message (Optional) The message to display if the assertion fails.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @method timesAreEqual
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @static
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm */
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm timesAreEqual : function (expected, actual, message){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm Y.Assert._increment();
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (expected instanceof Date && actual instanceof Date){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm var msg = "";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen //check hours first
0fc5ce7045233dae7e15fdc86774370f1b1d73cbjohanengelen if (expected.getHours() != actual.getHours()){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm msg = "Hours should be equal.";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm //now check minutes
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (expected.getMinutes() != actual.getMinutes()){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm msg = "Minutes should be equal.";
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
//last, check the seconds
if (expected.getSeconds() != actual.getSeconds()){
msg = "Seconds should be equal.";
}
if (msg.length){
throw new Y.Assert.ComparisonFailure(Y.Assert._formatMessage(message, msg), expected, actual);
}
} else {
throw new TypeError("DateY.AsserttimesAreEqual(): Expected and actual values must be Date objects.");
}
}
};