| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.testing.asserts'); |
| 16 |
goog.setTestOnly(); |
| 17 |
|
| 18 |
goog.require('goog.testing.JsUnitException'); |
| 19 |
|
| 20 |
// TODO(user): Copied from JsUnit with some small modifications, we should |
| 21 |
// reimplement the asserters. |
| 22 |
|
| 23 |
var DOUBLE_EQUALITY_PREDICATE = function(var1, var2) { |
| 24 |
return var1 == var2; |
| 25 |
}; |
| 26 |
var JSUNIT_UNDEFINED_VALUE = void 0; |
| 27 |
var TO_STRING_EQUALITY_PREDICATE = function(var1, var2) { |
| 28 |
return var1.toString() === var2.toString(); |
| 29 |
}; |
| 30 |
var OUTPUT_NEW_LINE_THRESHOLD = 40; |
| 31 |
|
| 32 |
|
| 33 |
/** @typedef {function(?, ?):boolean} */ |
| 34 |
var PredicateFunctionType; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* @const {{ |
| 39 |
* String : PredicateFunctionType, |
| 40 |
* Number : PredicateFunctionType, |
| 41 |
* Boolean : PredicateFunctionType, |
| 42 |
* Date : PredicateFunctionType, |
| 43 |
* RegExp : PredicateFunctionType, |
| 44 |
* Function : PredicateFunctionType |
| 45 |
* }} |
| 46 |
*/ |
| 47 |
var PRIMITIVE_EQUALITY_PREDICATES = { |
| 48 |
'String': DOUBLE_EQUALITY_PREDICATE, |
| 49 |
'Number': DOUBLE_EQUALITY_PREDICATE, |
| 50 |
'Boolean': DOUBLE_EQUALITY_PREDICATE, |
| 51 |
'Date': function(date1, date2) { return date1.getTime() == date2.getTime(); }, |
| 52 |
'RegExp': TO_STRING_EQUALITY_PREDICATE, |
| 53 |
'Function': TO_STRING_EQUALITY_PREDICATE |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Compares equality of two numbers, allowing them to differ up to a given |
| 59 |
* tolerance. |
| 60 |
* @param {number} var1 A number. |
| 61 |
* @param {number} var2 A number. |
| 62 |
* @param {number} tolerance the maximum allowed difference. |
| 63 |
* @return {boolean} Whether the two variables are sufficiently close. |
| 64 |
* @private |
| 65 |
*/ |
| 66 |
goog.testing.asserts.numberRoughEqualityPredicate_ = function( |
| 67 |
var1, var2, tolerance) { |
| 68 |
return Math.abs(var1 - var2) <= tolerance; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* @type {Object<string, function(*, *, number): boolean>} |
| 74 |
* @private |
| 75 |
*/ |
| 76 |
goog.testing.asserts.primitiveRoughEqualityPredicates_ = { |
| 77 |
'Number': goog.testing.asserts.numberRoughEqualityPredicate_ |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
var _trueTypeOf = function(something) { |
| 82 |
var result = typeof something; |
| 83 |
try { |
| 84 |
switch (result) { |
| 85 |
case 'string': |
| 86 |
break; |
| 87 |
case 'boolean': |
| 88 |
break; |
| 89 |
case 'number': |
| 90 |
break; |
| 91 |
case 'object': |
| 92 |
if (something == null) { |
| 93 |
result = 'null'; |
| 94 |
break; |
| 95 |
} |
| 96 |
case 'function': |
| 97 |
switch (something.constructor) { |
| 98 |
case new String('').constructor: |
| 99 |
result = 'String'; |
| 100 |
break; |
| 101 |
case new Boolean(true).constructor: |
| 102 |
result = 'Boolean'; |
| 103 |
break; |
| 104 |
case new Number(0).constructor: |
| 105 |
result = 'Number'; |
| 106 |
break; |
| 107 |
case new Array().constructor: |
| 108 |
result = 'Array'; |
| 109 |
break; |
| 110 |
case new RegExp().constructor: |
| 111 |
result = 'RegExp'; |
| 112 |
break; |
| 113 |
case new Date().constructor: |
| 114 |
result = 'Date'; |
| 115 |
break; |
| 116 |
case Function: |
| 117 |
result = 'Function'; |
| 118 |
break; |
| 119 |
default: |
| 120 |
var m = |
| 121 |
something.constructor.toString().match(/function\s*([^( ]+)\(/); |
| 122 |
if (m) { |
| 123 |
result = m[1]; |
| 124 |
} else { |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
break; |
| 129 |
} |
| 130 |
} catch (e) { |
| 131 |
} finally { |
| 132 |
result = result.substr(0, 1).toUpperCase() + result.substr(1); |
| 133 |
} |
| 134 |
return result; |
| 135 |
}; |
| 136 |
|
| 137 |
var _displayStringForValue = function(aVar) { |
| 138 |
var result; |
| 139 |
try { |
| 140 |
result = '<' + String(aVar) + '>'; |
| 141 |
} catch (ex) { |
| 142 |
result = '<toString failed: ' + ex.message + '>'; |
| 143 |
// toString does not work on this object :-( |
| 144 |
} |
| 145 |
if (!(aVar === null || aVar === JSUNIT_UNDEFINED_VALUE)) { |
| 146 |
result += ' (' + _trueTypeOf(aVar) + ')'; |
| 147 |
} |
| 148 |
return result; |
| 149 |
}; |
| 150 |
|
| 151 |
/** @param {?} failureMessage */ |
| 152 |
var fail = goog.testing.asserts.fail = function(failureMessage) { |
| 153 |
goog.testing.asserts.raiseException('Call to fail()', failureMessage); |
| 154 |
}; |
| 155 |
|
| 156 |
var argumentsIncludeComments = function(expectedNumberOfNonCommentArgs, args) { |
| 157 |
return args.length == expectedNumberOfNonCommentArgs + 1; |
| 158 |
}; |
| 159 |
|
| 160 |
var commentArg = function(expectedNumberOfNonCommentArgs, args) { |
| 161 |
if (argumentsIncludeComments(expectedNumberOfNonCommentArgs, args)) { |
| 162 |
return args[0]; |
| 163 |
} |
| 164 |
|
| 165 |
return null; |
| 166 |
}; |
| 167 |
|
| 168 |
var nonCommentArg = function( |
| 169 |
desiredNonCommentArgIndex, expectedNumberOfNonCommentArgs, args) { |
| 170 |
return argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) ? |
| 171 |
args[desiredNonCommentArgIndex] : |
| 172 |
args[desiredNonCommentArgIndex - 1]; |
| 173 |
}; |
| 174 |
|
| 175 |
var _validateArguments = function(expectedNumberOfNonCommentArgs, args) { |
| 176 |
var valid = args.length == expectedNumberOfNonCommentArgs || |
| 177 |
args.length == expectedNumberOfNonCommentArgs + 1 && |
| 178 |
goog.isString(args[0]); |
| 179 |
if (!valid) { |
| 180 |
goog.testing.asserts.raiseException( |
| 181 |
'Incorrect arguments passed to assert function.\n' + |
| 182 |
'Expected ' + expectedNumberOfNonCommentArgs + ' argument(s) plus ' + |
| 183 |
'optional comment; got ' + args.length + '.'); |
| 184 |
} |
| 185 |
|
| 186 |
}; |
| 187 |
|
| 188 |
/** |
| 189 |
* @return {?} goog.testing.TestCase or null |
| 190 |
* We suppress the lint error and we explicitly do not goog.require() |
| 191 |
* goog.testing.TestCase to avoid a build time dependency cycle. |
| 192 |
* @suppress {missingRequire|undefinedNames|undefinedVars|missingProperties} |
| 193 |
* @private |
| 194 |
*/ |
| 195 |
var _getCurrentTestCase = function() { |
| 196 |
// Some users of goog.testing.asserts do not use goog.testing.TestRunner and |
| 197 |
// they do not include goog.testing.TestCase. Exceptions will not be |
| 198 |
// completely correct for these users. |
| 199 |
if (!goog.testing.TestCase) { |
| 200 |
if (goog.global.console) { |
| 201 |
goog.global.console.error( |
| 202 |
'Missing goog.testing.TestCase, ' + |
| 203 |
'add /* @suppress {extraRequire} */' + |
| 204 |
'goog.require(\'goog.testing.TestCase\''); |
| 205 |
} |
| 206 |
return null; |
| 207 |
} |
| 208 |
return goog.testing.TestCase.getActiveTestCase(); |
| 209 |
}; |
| 210 |
|
| 211 |
var _assert = function(comment, booleanValue, failureMessage) { |
| 212 |
if (!booleanValue) { |
| 213 |
goog.testing.asserts.raiseException(comment, failureMessage); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* @param {*} expected The expected value. |
| 220 |
* @param {*} actual The actual value. |
| 221 |
* @return {string} A failure message of the values don't match. |
| 222 |
* @private |
| 223 |
*/ |
| 224 |
goog.testing.asserts.getDefaultErrorMsg_ = function(expected, actual) { |
| 225 |
var expectedDisplayString = _displayStringForValue(expected); |
| 226 |
var actualDisplayString = _displayStringForValue(actual); |
| 227 |
var shouldUseNewLines = |
| 228 |
expectedDisplayString.length > OUTPUT_NEW_LINE_THRESHOLD || |
| 229 |
actualDisplayString.length > OUTPUT_NEW_LINE_THRESHOLD; |
| 230 |
var msg = [ |
| 231 |
'Expected', expectedDisplayString, 'but was', actualDisplayString |
| 232 |
].join(shouldUseNewLines ? '\n' : ' '); |
| 233 |
|
| 234 |
if ((typeof expected == 'string') && (typeof actual == 'string')) { |
| 235 |
// Try to find a human-readable difference. |
| 236 |
var limit = Math.min(expected.length, actual.length); |
| 237 |
var commonPrefix = 0; |
| 238 |
while (commonPrefix < limit && |
| 239 |
expected.charAt(commonPrefix) == actual.charAt(commonPrefix)) { |
| 240 |
commonPrefix++; |
| 241 |
} |
| 242 |
|
| 243 |
var commonSuffix = 0; |
| 244 |
while (commonSuffix < limit && |
| 245 |
expected.charAt(expected.length - commonSuffix - 1) == |
| 246 |
actual.charAt(actual.length - commonSuffix - 1)) { |
| 247 |
commonSuffix++; |
| 248 |
} |
| 249 |
|
| 250 |
if (commonPrefix + commonSuffix > limit) { |
| 251 |
commonSuffix = 0; |
| 252 |
} |
| 253 |
|
| 254 |
if (commonPrefix > 2 || commonSuffix > 2) { |
| 255 |
var printString = function(str) { |
| 256 |
var startIndex = Math.max(0, commonPrefix - 2); |
| 257 |
var endIndex = Math.min(str.length, str.length - (commonSuffix - 2)); |
| 258 |
return (startIndex > 0 ? '...' : '') + |
| 259 |
str.substring(startIndex, endIndex) + |
| 260 |
(endIndex < str.length ? '...' : ''); |
| 261 |
}; |
| 262 |
|
| 263 |
var expectedPrinted = printString(expected); |
| 264 |
var expectedActual = printString(actual); |
| 265 |
var shouldUseNewLinesInDiff = |
| 266 |
expectedPrinted.length > OUTPUT_NEW_LINE_THRESHOLD || |
| 267 |
expectedActual.length > OUTPUT_NEW_LINE_THRESHOLD; |
| 268 |
msg += '\nDifference was at position ' + commonPrefix + '. ' + [ |
| 269 |
'Expected', '[' + expectedPrinted + ']', 'vs. actual', |
| 270 |
'[' + expectedActual + ']' |
| 271 |
].join(shouldUseNewLinesInDiff ? '\n' : ' '); |
| 272 |
} |
| 273 |
} |
| 274 |
return msg; |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 280 |
* @param {*=} opt_b The value to assert (2 args only). |
| 281 |
*/ |
| 282 |
var assert = goog.testing.asserts.assert = function(a, opt_b) { |
| 283 |
_validateArguments(1, arguments); |
| 284 |
var comment = commentArg(1, arguments); |
| 285 |
var booleanValue = nonCommentArg(1, 1, arguments); |
| 286 |
|
| 287 |
_assert( |
| 288 |
comment, goog.isBoolean(booleanValue), 'Bad argument to assert(boolean)'); |
| 289 |
_assert(comment, booleanValue, 'Call to assert(boolean) with false'); |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Asserts that the function throws an error. |
| 295 |
* |
| 296 |
* @param {!(string|Function)} a The assertion comment or the function to call. |
| 297 |
* @param {!Function=} opt_b The function to call (if the first argument of |
| 298 |
* {@code assertThrows} was the comment). |
| 299 |
* @return {*} The error thrown by the function. |
| 300 |
* @throws {goog.testing.JsUnitException} If the assertion failed. |
| 301 |
*/ |
| 302 |
var assertThrows = goog.testing.asserts.assertThrows = function(a, opt_b) { |
| 303 |
_validateArguments(1, arguments); |
| 304 |
var func = nonCommentArg(1, 1, arguments); |
| 305 |
var comment = commentArg(1, arguments); |
| 306 |
_assert( |
| 307 |
comment, typeof func == 'function', |
| 308 |
'Argument passed to assertThrows is not a function'); |
| 309 |
|
| 310 |
try { |
| 311 |
func(); |
| 312 |
} catch (e) { |
| 313 |
if (e && goog.isString(e['stacktrace']) && goog.isString(e['message'])) { |
| 314 |
// Remove the stack trace appended to the error message by Opera 10.0 |
| 315 |
var startIndex = e['message'].length - e['stacktrace'].length; |
| 316 |
if (e['message'].indexOf(e['stacktrace'], startIndex) == startIndex) { |
| 317 |
e['message'] = e['message'].substr(0, startIndex - 14); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
var testCase = _getCurrentTestCase(); |
| 322 |
if (e && e['isJsUnitException'] && testCase && |
| 323 |
testCase.failOnUnreportedAsserts) { |
| 324 |
goog.testing.asserts.raiseException( |
| 325 |
comment, |
| 326 |
'Function passed to assertThrows caught a JsUnitException (usually ' + |
| 327 |
'from an assert or call to fail()). If this is expected, use ' + |
| 328 |
'assertThrowsJsUnitException instead.'); |
| 329 |
} |
| 330 |
|
| 331 |
return e; |
| 332 |
} |
| 333 |
goog.testing.asserts.raiseException( |
| 334 |
comment, 'No exception thrown from function passed to assertThrows'); |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* Asserts that the function does not throw an error. |
| 340 |
* |
| 341 |
* @param {!(string|Function)} a The assertion comment or the function to call. |
| 342 |
* @param {!Function=} opt_b The function to call (if the first argument of |
| 343 |
* {@code assertNotThrows} was the comment). |
| 344 |
* @return {*} The return value of the function. |
| 345 |
* @throws {goog.testing.JsUnitException} If the assertion failed. |
| 346 |
*/ |
| 347 |
var assertNotThrows = goog.testing.asserts.assertNotThrows = function( |
| 348 |
a, opt_b) { |
| 349 |
_validateArguments(1, arguments); |
| 350 |
var comment = commentArg(1, arguments); |
| 351 |
var func = nonCommentArg(1, 1, arguments); |
| 352 |
_assert( |
| 353 |
comment, typeof func == 'function', |
| 354 |
'Argument passed to assertNotThrows is not a function'); |
| 355 |
|
| 356 |
try { |
| 357 |
return func(); |
| 358 |
} catch (e) { |
| 359 |
comment = comment ? (comment + '\n') : ''; |
| 360 |
comment += 'A non expected exception was thrown from function passed to ' + |
| 361 |
'assertNotThrows'; |
| 362 |
// Some browsers don't have a stack trace so at least have the error |
| 363 |
// description. |
| 364 |
var stackTrace = e['stack'] || e['stacktrace'] || e.toString(); |
| 365 |
goog.testing.asserts.raiseException(comment, stackTrace); |
| 366 |
} |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
/** |
| 371 |
* Asserts that the given callback function results in a JsUnitException when |
| 372 |
* called, and that the resulting failure message matches the given expected |
| 373 |
* message. |
| 374 |
* @param {function() : void} callback Function to be run expected to result |
| 375 |
* in a JsUnitException (usually contains a call to an assert). |
| 376 |
* @param {string=} opt_expectedMessage Failure message expected to be given |
| 377 |
* with the exception. |
| 378 |
* @return {!goog.testing.JsUnitException} The error thrown by the function. |
| 379 |
* @throws {goog.testing.JsUnitException} If the function did not throw a |
| 380 |
* JsUnitException. |
| 381 |
*/ |
| 382 |
var assertThrowsJsUnitException = goog.testing.asserts |
| 383 |
.assertThrowsJsUnitException = function( |
| 384 |
callback, opt_expectedMessage) { |
| 385 |
try { |
| 386 |
goog.testing.asserts.callWithoutLogging(callback); |
| 387 |
} catch (e) { |
| 388 |
var testCase = _getCurrentTestCase(); |
| 389 |
if (testCase) { |
| 390 |
testCase.invalidateAssertionException(e); |
| 391 |
} else { |
| 392 |
goog.global.console.error( |
| 393 |
'Failed to remove expected exception: no test case is installed.'); |
| 394 |
} |
| 395 |
|
| 396 |
if (!e.isJsUnitException) { |
| 397 |
goog.testing.asserts.fail('Expected a JsUnitException'); |
| 398 |
} |
| 399 |
|
| 400 |
if (typeof opt_expectedMessage != 'undefined' && |
| 401 |
e.message != opt_expectedMessage) { |
| 402 |
goog.testing.asserts.fail( |
| 403 |
'Expected message [' + opt_expectedMessage + '] but got [' + |
| 404 |
e.message + ']'); |
| 405 |
} |
| 406 |
|
| 407 |
return e; |
| 408 |
} |
| 409 |
|
| 410 |
var msg = 'Expected a failure'; |
| 411 |
if (typeof opt_expectedMessage != 'undefined') { |
| 412 |
msg += ': ' + opt_expectedMessage; |
| 413 |
} |
| 414 |
throw new goog.testing.JsUnitException(msg); |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 420 |
* @param {*=} opt_b The value to assert (2 args only). |
| 421 |
*/ |
| 422 |
var assertTrue = goog.testing.asserts.assertTrue = function(a, opt_b) { |
| 423 |
_validateArguments(1, arguments); |
| 424 |
var comment = commentArg(1, arguments); |
| 425 |
var booleanValue = nonCommentArg(1, 1, arguments); |
| 426 |
|
| 427 |
_assert( |
| 428 |
comment, goog.isBoolean(booleanValue), |
| 429 |
'Bad argument to assertTrue(boolean)'); |
| 430 |
_assert(comment, booleanValue, 'Call to assertTrue(boolean) with false'); |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
/** |
| 435 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 436 |
* @param {*=} opt_b The value to assert (2 args only). |
| 437 |
*/ |
| 438 |
var assertFalse = goog.testing.asserts.assertFalse = function(a, opt_b) { |
| 439 |
_validateArguments(1, arguments); |
| 440 |
var comment = commentArg(1, arguments); |
| 441 |
var booleanValue = nonCommentArg(1, 1, arguments); |
| 442 |
|
| 443 |
_assert( |
| 444 |
comment, goog.isBoolean(booleanValue), |
| 445 |
'Bad argument to assertFalse(boolean)'); |
| 446 |
_assert(comment, !booleanValue, 'Call to assertFalse(boolean) with true'); |
| 447 |
}; |
| 448 |
|
| 449 |
|
| 450 |
/** |
| 451 |
* @param {*} a The expected value (2 args) or the debug message (3 args). |
| 452 |
* @param {*} b The actual value (2 args) or the expected value (3 args). |
| 453 |
* @param {*=} opt_c The actual value (3 args only). |
| 454 |
*/ |
| 455 |
var assertEquals = goog.testing.asserts.assertEquals = function(a, b, opt_c) { |
| 456 |
_validateArguments(2, arguments); |
| 457 |
var var1 = nonCommentArg(1, 2, arguments); |
| 458 |
var var2 = nonCommentArg(2, 2, arguments); |
| 459 |
_assert( |
| 460 |
commentArg(2, arguments), var1 === var2, |
| 461 |
goog.testing.asserts.getDefaultErrorMsg_(var1, var2)); |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
/** |
| 466 |
* @param {*} a The expected value (2 args) or the debug message (3 args). |
| 467 |
* @param {*} b The actual value (2 args) or the expected value (3 args). |
| 468 |
* @param {*=} opt_c The actual value (3 args only). |
| 469 |
*/ |
| 470 |
var assertNotEquals = goog.testing.asserts.assertNotEquals = function( |
| 471 |
a, b, opt_c) { |
| 472 |
_validateArguments(2, arguments); |
| 473 |
var var1 = nonCommentArg(1, 2, arguments); |
| 474 |
var var2 = nonCommentArg(2, 2, arguments); |
| 475 |
_assert( |
| 476 |
commentArg(2, arguments), var1 !== var2, |
| 477 |
'Expected not to be ' + _displayStringForValue(var2)); |
| 478 |
}; |
| 479 |
|
| 480 |
|
| 481 |
/** |
| 482 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 483 |
* @param {*=} opt_b The value to assert (2 args only). |
| 484 |
*/ |
| 485 |
var assertNull = goog.testing.asserts.assertNull = function(a, opt_b) { |
| 486 |
_validateArguments(1, arguments); |
| 487 |
var aVar = nonCommentArg(1, 1, arguments); |
| 488 |
_assert( |
| 489 |
commentArg(1, arguments), aVar === null, |
| 490 |
goog.testing.asserts.getDefaultErrorMsg_(null, aVar)); |
| 491 |
}; |
| 492 |
|
| 493 |
|
| 494 |
/** |
| 495 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 496 |
* @param {*=} opt_b The value to assert (2 args only). |
| 497 |
*/ |
| 498 |
var assertNotNull = goog.testing.asserts.assertNotNull = function(a, opt_b) { |
| 499 |
_validateArguments(1, arguments); |
| 500 |
var aVar = nonCommentArg(1, 1, arguments); |
| 501 |
_assert( |
| 502 |
commentArg(1, arguments), aVar !== null, |
| 503 |
'Expected not to be ' + _displayStringForValue(null)); |
| 504 |
}; |
| 505 |
|
| 506 |
|
| 507 |
/** |
| 508 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 509 |
* @param {*=} opt_b The value to assert (2 args only). |
| 510 |
*/ |
| 511 |
var assertUndefined = goog.testing.asserts.assertUndefined = function( |
| 512 |
a, opt_b) { |
| 513 |
_validateArguments(1, arguments); |
| 514 |
var aVar = nonCommentArg(1, 1, arguments); |
| 515 |
_assert( |
| 516 |
commentArg(1, arguments), aVar === JSUNIT_UNDEFINED_VALUE, |
| 517 |
goog.testing.asserts.getDefaultErrorMsg_(JSUNIT_UNDEFINED_VALUE, aVar)); |
| 518 |
}; |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 523 |
* @param {*=} opt_b The value to assert (2 args only). |
| 524 |
*/ |
| 525 |
var assertNotUndefined = goog.testing.asserts.assertNotUndefined = function( |
| 526 |
a, opt_b) { |
| 527 |
_validateArguments(1, arguments); |
| 528 |
var aVar = nonCommentArg(1, 1, arguments); |
| 529 |
_assert( |
| 530 |
commentArg(1, arguments), aVar !== JSUNIT_UNDEFINED_VALUE, |
| 531 |
'Expected not to be ' + _displayStringForValue(JSUNIT_UNDEFINED_VALUE)); |
| 532 |
}; |
| 533 |
|
| 534 |
|
| 535 |
/** |
| 536 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 537 |
* @param {*=} opt_b The value to assert (2 args only). |
| 538 |
*/ |
| 539 |
var assertNotNullNorUndefined = |
| 540 |
goog.testing.asserts.assertNotNullNorUndefined = function(a, opt_b) { |
| 541 |
_validateArguments(1, arguments); |
| 542 |
goog.testing.asserts.assertNotNull.apply(null, arguments); |
| 543 |
goog.testing.asserts.assertNotUndefined.apply(null, arguments); |
| 544 |
}; |
| 545 |
|
| 546 |
|
| 547 |
/** |
| 548 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 549 |
* @param {*=} opt_b The value to assert (2 args only). |
| 550 |
*/ |
| 551 |
var assertNonEmptyString = goog.testing.asserts.assertNonEmptyString = function( |
| 552 |
a, opt_b) { |
| 553 |
_validateArguments(1, arguments); |
| 554 |
var aVar = nonCommentArg(1, 1, arguments); |
| 555 |
_assert( |
| 556 |
commentArg(1, arguments), aVar !== JSUNIT_UNDEFINED_VALUE && |
| 557 |
aVar !== null && typeof aVar == 'string' && aVar !== '', |
| 558 |
'Expected non-empty string but was ' + _displayStringForValue(aVar)); |
| 559 |
}; |
| 560 |
|
| 561 |
|
| 562 |
/** |
| 563 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 564 |
* @param {*=} opt_b The value to assert (2 args only). |
| 565 |
*/ |
| 566 |
var assertNaN = goog.testing.asserts.assertNaN = function(a, opt_b) { |
| 567 |
_validateArguments(1, arguments); |
| 568 |
var aVar = nonCommentArg(1, 1, arguments); |
| 569 |
_assert(commentArg(1, arguments), isNaN(aVar), 'Expected NaN'); |
| 570 |
}; |
| 571 |
|
| 572 |
|
| 573 |
/** |
| 574 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 575 |
* @param {*=} opt_b The value to assert (2 args only). |
| 576 |
*/ |
| 577 |
var assertNotNaN = goog.testing.asserts.assertNotNaN = function(a, opt_b) { |
| 578 |
_validateArguments(1, arguments); |
| 579 |
var aVar = nonCommentArg(1, 1, arguments); |
| 580 |
_assert(commentArg(1, arguments), !isNaN(aVar), 'Expected not NaN'); |
| 581 |
}; |
| 582 |
|
| 583 |
|
| 584 |
/** |
| 585 |
* Runs a function in an environment where test failures are not logged. This is |
| 586 |
* useful for testing test code, where failures can be a normal part of a test. |
| 587 |
* @param {function() : void} fn Function to run without logging failures. |
| 588 |
*/ |
| 589 |
var callWithoutLogging = |
| 590 |
goog.testing.asserts.callWithoutLogging = function(fn) { |
| 591 |
var testRunner = goog.global['G_testRunner']; |
| 592 |
var oldLogTestFailure = testRunner['logTestFailure']; |
| 593 |
try { |
| 594 |
// Any failures in the callback shouldn't be recorded. |
| 595 |
testRunner['logTestFailure'] = undefined; |
| 596 |
fn(); |
| 597 |
} finally { |
| 598 |
testRunner['logTestFailure'] = oldLogTestFailure; |
| 599 |
} |
| 600 |
}; |
| 601 |
|
| 602 |
|
| 603 |
/** |
| 604 |
* The return value of the equality predicate passed to findDifferences below, |
| 605 |
* in cases where the predicate can't test the input variables for equality. |
| 606 |
* @type {?string} |
| 607 |
*/ |
| 608 |
goog.testing.asserts.EQUALITY_PREDICATE_CANT_PROCESS = null; |
| 609 |
|
| 610 |
|
| 611 |
/** |
| 612 |
* The return value of the equality predicate passed to findDifferences below, |
| 613 |
* in cases where the input vriables are equal. |
| 614 |
* @type {?string} |
| 615 |
*/ |
| 616 |
goog.testing.asserts.EQUALITY_PREDICATE_VARS_ARE_EQUAL = ''; |
| 617 |
|
| 618 |
|
| 619 |
/** |
| 620 |
* Determines if two items of any type match, and formulates an error message |
| 621 |
* if not. |
| 622 |
* @param {*} expected Expected argument to match. |
| 623 |
* @param {*} actual Argument as a result of performing the test. |
| 624 |
* @param {(function(string, *, *): ?string)=} opt_equalityPredicate An optional |
| 625 |
* function that can be used to check equality of variables. It accepts 3 |
| 626 |
* arguments: type-of-variables, var1, var2 (in that order) and returns an |
| 627 |
* error message if the variables are not equal, |
| 628 |
* goog.testing.asserts.EQUALITY_PREDICATE_VARS_ARE_EQUAL if the variables |
| 629 |
* are equal, or |
| 630 |
* goog.testing.asserts.EQUALITY_PREDICATE_CANT_PROCESS if the predicate |
| 631 |
* couldn't check the input variables. The function will be called only if |
| 632 |
* the types of var1 and var2 are identical. |
| 633 |
* @return {?string} Null on success, error message on failure. |
| 634 |
*/ |
| 635 |
goog.testing.asserts.findDifferences = function( |
| 636 |
expected, actual, opt_equalityPredicate) { |
| 637 |
var failures = []; |
| 638 |
var seen1 = []; |
| 639 |
var seen2 = []; |
| 640 |
|
| 641 |
// To avoid infinite recursion when the two parameters are self-referential |
| 642 |
// along the same path of properties, keep track of the object pairs already |
| 643 |
// seen in this call subtree, and abort when a cycle is detected. |
| 644 |
function innerAssertWithCycleCheck(var1, var2, path) { |
| 645 |
// This is used for testing, so we can afford to be slow (but more |
| 646 |
// accurate). So we just check whether var1 is in seen1. If we |
| 647 |
// found var1 in index i, we simply need to check whether var2 is |
| 648 |
// in seen2[i]. If it is, we do not recurse to check var1/var2. If |
| 649 |
// it isn't, we know that the structures of the two objects must be |
| 650 |
// different. |
| 651 |
// |
| 652 |
// This is based on the fact that values at index i in seen1 and |
| 653 |
// seen2 will be checked for equality eventually (when |
| 654 |
// innerAssertImplementation(seen1[i], seen2[i], path) finishes). |
| 655 |
for (var i = 0; i < seen1.length; ++i) { |
| 656 |
var match1 = seen1[i] === var1; |
| 657 |
var match2 = seen2[i] === var2; |
| 658 |
if (match1 || match2) { |
| 659 |
if (!match1 || !match2) { |
| 660 |
// Asymmetric cycles, so the objects have different structure. |
| 661 |
failures.push('Asymmetric cycle detected at ' + path); |
| 662 |
} |
| 663 |
return; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
seen1.push(var1); |
| 668 |
seen2.push(var2); |
| 669 |
innerAssertImplementation(var1, var2, path); |
| 670 |
seen1.pop(); |
| 671 |
seen2.pop(); |
| 672 |
} |
| 673 |
|
| 674 |
var equalityPredicate = opt_equalityPredicate || function(type, var1, var2) { |
| 675 |
var typedPredicate = PRIMITIVE_EQUALITY_PREDICATES[type]; |
| 676 |
if (!typedPredicate) { |
| 677 |
return goog.testing.asserts.EQUALITY_PREDICATE_CANT_PROCESS; |
| 678 |
} |
| 679 |
var equal = typedPredicate(var1, var2); |
| 680 |
return equal ? goog.testing.asserts.EQUALITY_PREDICATE_VARS_ARE_EQUAL : |
| 681 |
goog.testing.asserts.getDefaultErrorMsg_(var1, var2); |
| 682 |
}; |
| 683 |
|
| 684 |
/** |
| 685 |
* @param {*} var1 An item in the expected object. |
| 686 |
* @param {*} var2 The corresponding item in the actual object. |
| 687 |
* @param {string} path Their path in the objects. |
| 688 |
* @suppress {missingProperties} The map_ property is unknown to the compiler |
| 689 |
* unless goog.structs.Map is loaded. |
| 690 |
*/ |
| 691 |
function innerAssertImplementation(var1, var2, path) { |
| 692 |
if (var1 === var2) { |
| 693 |
return; |
| 694 |
} |
| 695 |
|
| 696 |
var typeOfVar1 = _trueTypeOf(var1); |
| 697 |
var typeOfVar2 = _trueTypeOf(var2); |
| 698 |
|
| 699 |
if (typeOfVar1 == typeOfVar2) { |
| 700 |
var isArray = typeOfVar1 == 'Array'; |
| 701 |
var errorMessage = equalityPredicate(typeOfVar1, var1, var2); |
| 702 |
if (errorMessage != |
| 703 |
goog.testing.asserts.EQUALITY_PREDICATE_CANT_PROCESS) { |
| 704 |
if (errorMessage != |
| 705 |
goog.testing.asserts.EQUALITY_PREDICATE_VARS_ARE_EQUAL) { |
| 706 |
failures.push(path + ': ' + errorMessage); |
| 707 |
} |
| 708 |
} else if (isArray && var1.length != var2.length) { |
| 709 |
failures.push( |
| 710 |
path + ': Expected ' + var1.length + '-element array ' + |
| 711 |
'but got a ' + var2.length + '-element array'); |
| 712 |
} else if (typeOfVar1 == 'String') { |
| 713 |
if (var1 != var2) { |
| 714 |
failures.push( |
| 715 |
path + ': Expected String "' + var1 + '" ' + |
| 716 |
'but got "' + var2 + '"'); |
| 717 |
} |
| 718 |
} else { |
| 719 |
var childPath = path + (isArray ? '[%s]' : (path ? '.%s' : '%s')); |
| 720 |
// These type checks do not use _trueTypeOf because that does not work |
| 721 |
// for polyfilled Map/Set. Note that these checks may potentially fail |
| 722 |
// if var1 comes from a different window. |
| 723 |
if ((typeof Map != 'undefined' && var1 instanceof Map) || |
| 724 |
(typeof Set != 'undefined' && var1 instanceof Set)) { |
| 725 |
var1.forEach(function(value, key) { |
| 726 |
if (var2.has(key)) { |
| 727 |
// For a map, the values must be compared, but with Set, checking |
| 728 |
// that the second set contains the first set's "keys" is |
| 729 |
// sufficient. |
| 730 |
if (var2.get) { |
| 731 |
innerAssertWithCycleCheck( |
| 732 |
value, var2.get(key), childPath.replace('%s', key)); |
| 733 |
} |
| 734 |
} else { |
| 735 |
failures.push( |
| 736 |
key + ' not present in actual ' + (path || typeOfVar2)); |
| 737 |
} |
| 738 |
}); |
| 739 |
|
| 740 |
var2.forEach(function(value, key) { |
| 741 |
if (!var1.has(key)) { |
| 742 |
failures.push( |
| 743 |
key + ' not present in expected ' + (path || typeOfVar1)); |
| 744 |
} |
| 745 |
}); |
| 746 |
} else if (!var1['__iterator__']) { |
| 747 |
// if an object has an __iterator__ property, we have no way of |
| 748 |
// actually inspecting its raw properties, and JS 1.7 doesn't |
| 749 |
// overload [] to make it possible for someone to generically |
| 750 |
// use what the iterator returns to compare the object-managed |
| 751 |
// properties. This gets us into deep poo with things like |
| 752 |
// goog.structs.Map, at least on systems that support iteration. |
| 753 |
for (var prop in var1) { |
| 754 |
if (isArray && goog.testing.asserts.isArrayIndexProp_(prop)) { |
| 755 |
// Skip array indices for now. We'll handle them later. |
| 756 |
continue; |
| 757 |
} |
| 758 |
|
| 759 |
if (prop in var2) { |
| 760 |
innerAssertWithCycleCheck( |
| 761 |
var1[prop], var2[prop], childPath.replace('%s', prop)); |
| 762 |
} else { |
| 763 |
failures.push( |
| 764 |
'property ' + prop + ' not present in actual ' + |
| 765 |
(path || typeOfVar2)); |
| 766 |
} |
| 767 |
} |
| 768 |
// make sure there aren't properties in var2 that are missing |
| 769 |
// from var1. if there are, then by definition they don't |
| 770 |
// match. |
| 771 |
for (var prop in var2) { |
| 772 |
if (isArray && goog.testing.asserts.isArrayIndexProp_(prop)) { |
| 773 |
// Skip array indices for now. We'll handle them later. |
| 774 |
continue; |
| 775 |
} |
| 776 |
|
| 777 |
if (!(prop in var1)) { |
| 778 |
failures.push( |
| 779 |
'property ' + prop + ' not present in expected ' + |
| 780 |
(path || typeOfVar1)); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
// Handle array indices by iterating from 0 to arr.length. |
| 785 |
// |
| 786 |
// Although all browsers allow holes in arrays, browsers |
| 787 |
// are inconsistent in what they consider a hole. For example, |
| 788 |
// "[0,undefined,2]" has a hole on IE but not on Firefox. |
| 789 |
// |
| 790 |
// Because our style guide bans for...in iteration over arrays, |
| 791 |
// we assume that most users don't care about holes in arrays, |
| 792 |
// and that it is ok to say that a hole is equivalent to a slot |
| 793 |
// populated with 'undefined'. |
| 794 |
if (isArray) { |
| 795 |
for (prop = 0; prop < var1.length; prop++) { |
| 796 |
innerAssertWithCycleCheck( |
| 797 |
var1[prop], var2[prop], |
| 798 |
childPath.replace('%s', String(prop))); |
| 799 |
} |
| 800 |
} |
| 801 |
} else { |
| 802 |
// special-case for closure objects that have iterators |
| 803 |
if (goog.isFunction(var1.equals)) { |
| 804 |
// use the object's own equals function, assuming it accepts an |
| 805 |
// object and returns a boolean |
| 806 |
if (!var1.equals(var2)) { |
| 807 |
failures.push( |
| 808 |
'equals() returned false for ' + (path || typeOfVar1)); |
| 809 |
} |
| 810 |
} else if (var1.map_) { |
| 811 |
// assume goog.structs.Map or goog.structs.Set, where comparing |
| 812 |
// their private map_ field is sufficient |
| 813 |
innerAssertWithCycleCheck( |
| 814 |
var1.map_, var2.map_, childPath.replace('%s', 'map_')); |
| 815 |
} else { |
| 816 |
// else die, so user knows we can't do anything |
| 817 |
failures.push( |
| 818 |
'unable to check ' + (path || typeOfVar1) + |
| 819 |
' for equality: it has an iterator we do not ' + |
| 820 |
'know how to handle. please add an equals method'); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
} else { |
| 825 |
failures.push( |
| 826 |
path + ' ' + goog.testing.asserts.getDefaultErrorMsg_(var1, var2)); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
innerAssertWithCycleCheck(expected, actual, ''); |
| 831 |
return failures.length == 0 ? null : goog.testing.asserts.getDefaultErrorMsg_( |
| 832 |
expected, actual) + |
| 833 |
'\n ' + failures.join('\n '); |
| 834 |
}; |
| 835 |
|
| 836 |
|
| 837 |
/** |
| 838 |
* Notes: |
| 839 |
* Object equality has some nasty browser quirks, and this implementation is |
| 840 |
* not 100% correct. For example, |
| 841 |
* |
| 842 |
* <code> |
| 843 |
* var a = [0, 1, 2]; |
| 844 |
* var b = [0, 1, 2]; |
| 845 |
* delete a[1]; |
| 846 |
* b[1] = undefined; |
| 847 |
* assertObjectEquals(a, b); // should fail, but currently passes |
| 848 |
* </code> |
| 849 |
* |
| 850 |
* See asserts_test.html for more interesting edge cases. |
| 851 |
* |
| 852 |
* The first comparison object provided is the expected value, the second is |
| 853 |
* the actual. |
| 854 |
* |
| 855 |
* @param {*} a Assertion message or comparison object. |
| 856 |
* @param {*} b Comparison object. |
| 857 |
* @param {*=} opt_c Comparison object, if an assertion message was provided. |
| 858 |
*/ |
| 859 |
var assertObjectEquals = goog.testing.asserts.assertObjectEquals = function( |
| 860 |
a, b, opt_c) { |
| 861 |
_validateArguments(2, arguments); |
| 862 |
var v1 = nonCommentArg(1, 2, arguments); |
| 863 |
var v2 = nonCommentArg(2, 2, arguments); |
| 864 |
var failureMessage = commentArg(2, arguments) ? commentArg(2, arguments) : ''; |
| 865 |
var differences = goog.testing.asserts.findDifferences(v1, v2); |
| 866 |
|
| 867 |
_assert(failureMessage, !differences, differences); |
| 868 |
}; |
| 869 |
|
| 870 |
|
| 871 |
/** |
| 872 |
* Similar to assertObjectEquals above, but accepts a tolerance margin. |
| 873 |
* |
| 874 |
* @param {*} a Assertion message or comparison object. |
| 875 |
* @param {*} b Comparison object. |
| 876 |
* @param {*} c Comparison object or tolerance. |
| 877 |
* @param {*=} opt_d Tolerance, if an assertion message was provided. |
| 878 |
*/ |
| 879 |
var assertObjectRoughlyEquals = goog.testing.asserts.assertObjectRoughlyEquals = |
| 880 |
function(a, b, c, opt_d) { |
| 881 |
_validateArguments(3, arguments); |
| 882 |
var v1 = nonCommentArg(1, 3, arguments); |
| 883 |
var v2 = nonCommentArg(2, 3, arguments); |
| 884 |
var tolerance = nonCommentArg(3, 3, arguments); |
| 885 |
var failureMessage = commentArg(3, arguments) ? commentArg(3, arguments) : ''; |
| 886 |
var equalityPredicate = function(type, var1, var2) { |
| 887 |
var typedPredicate = |
| 888 |
goog.testing.asserts.primitiveRoughEqualityPredicates_[type]; |
| 889 |
if (!typedPredicate) { |
| 890 |
return goog.testing.asserts.EQUALITY_PREDICATE_CANT_PROCESS; |
| 891 |
} |
| 892 |
var equal = typedPredicate(var1, var2, tolerance); |
| 893 |
return equal ? goog.testing.asserts.EQUALITY_PREDICATE_VARS_ARE_EQUAL : |
| 894 |
goog.testing.asserts.getDefaultErrorMsg_(var1, var2) + |
| 895 |
' which was more than ' + tolerance + ' away'; |
| 896 |
}; |
| 897 |
var differences = |
| 898 |
goog.testing.asserts.findDifferences(v1, v2, equalityPredicate); |
| 899 |
|
| 900 |
_assert(failureMessage, !differences, differences); |
| 901 |
}; |
| 902 |
|
| 903 |
|
| 904 |
/** |
| 905 |
* Compares two arbitrary objects for non-equalness. |
| 906 |
* |
| 907 |
* All the same caveats as for assertObjectEquals apply here: |
| 908 |
* Undefined values may be confused for missing values, or vice versa. |
| 909 |
* |
| 910 |
* @param {*} a Assertion message or comparison object. |
| 911 |
* @param {*} b Comparison object. |
| 912 |
* @param {*=} opt_c Comparison object, if an assertion message was provided. |
| 913 |
*/ |
| 914 |
var assertObjectNotEquals = |
| 915 |
goog.testing.asserts.assertObjectNotEquals = function(a, b, opt_c) { |
| 916 |
_validateArguments(2, arguments); |
| 917 |
var v1 = nonCommentArg(1, 2, arguments); |
| 918 |
var v2 = nonCommentArg(2, 2, arguments); |
| 919 |
var failureMessage = |
| 920 |
commentArg(2, arguments) ? commentArg(2, arguments) : ''; |
| 921 |
var differences = goog.testing.asserts.findDifferences(v1, v2); |
| 922 |
|
| 923 |
_assert(failureMessage, differences, 'Objects should not be equal'); |
| 924 |
}; |
| 925 |
|
| 926 |
|
| 927 |
/** |
| 928 |
* Compares two arrays ignoring negative indexes and extra properties on the |
| 929 |
* array objects. Use case: Internet Explorer adds the index, lastIndex and |
| 930 |
* input enumerable fields to the result of string.match(/regexp/g), which makes |
| 931 |
* assertObjectEquals fail. |
| 932 |
* @param {*} a The expected array (2 args) or the debug message (3 args). |
| 933 |
* @param {*} b The actual array (2 args) or the expected array (3 args). |
| 934 |
* @param {*=} opt_c The actual array (3 args only). |
| 935 |
*/ |
| 936 |
var assertArrayEquals = goog.testing.asserts.assertArrayEquals = function( |
| 937 |
a, b, opt_c) { |
| 938 |
_validateArguments(2, arguments); |
| 939 |
var v1 = nonCommentArg(1, 2, arguments); |
| 940 |
var v2 = nonCommentArg(2, 2, arguments); |
| 941 |
var failureMessage = commentArg(2, arguments) ? commentArg(2, arguments) : ''; |
| 942 |
|
| 943 |
var typeOfVar1 = _trueTypeOf(v1); |
| 944 |
_assert( |
| 945 |
failureMessage, typeOfVar1 == 'Array', |
| 946 |
'Expected an array for assertArrayEquals but found a ' + typeOfVar1); |
| 947 |
|
| 948 |
var typeOfVar2 = _trueTypeOf(v2); |
| 949 |
_assert( |
| 950 |
failureMessage, typeOfVar2 == 'Array', |
| 951 |
'Expected an array for assertArrayEquals but found a ' + typeOfVar2); |
| 952 |
|
| 953 |
goog.testing.asserts.assertObjectEquals( |
| 954 |
failureMessage, Array.prototype.concat.call(v1), |
| 955 |
Array.prototype.concat.call(v2)); |
| 956 |
}; |
| 957 |
|
| 958 |
|
| 959 |
/** |
| 960 |
* Compares two objects that can be accessed like an array and assert that |
| 961 |
* each element is equal. |
| 962 |
* @param {string|Object} a Failure message (3 arguments) |
| 963 |
* or object #1 (2 arguments). |
| 964 |
* @param {Object} b Object #2 (2 arguments) or object #1 (3 arguments). |
| 965 |
* @param {Object=} opt_c Object #2 (3 arguments). |
| 966 |
*/ |
| 967 |
var assertElementsEquals = goog.testing.asserts.assertElementsEquals = function( |
| 968 |
a, b, opt_c) { |
| 969 |
_validateArguments(2, arguments); |
| 970 |
|
| 971 |
var v1 = nonCommentArg(1, 2, arguments); |
| 972 |
var v2 = nonCommentArg(2, 2, arguments); |
| 973 |
var failureMessage = commentArg(2, arguments) ? commentArg(2, arguments) : ''; |
| 974 |
|
| 975 |
if (!v1) { |
| 976 |
goog.testing.asserts.assert(failureMessage, !v2); |
| 977 |
} else { |
| 978 |
goog.testing.asserts.assertEquals( |
| 979 |
'length mismatch: ' + failureMessage, v1.length, v2.length); |
| 980 |
for (var i = 0; i < v1.length; ++i) { |
| 981 |
goog.testing.asserts.assertEquals( |
| 982 |
'mismatch at index ' + i + ': ' + failureMessage, v1[i], v2[i]); |
| 983 |
} |
| 984 |
} |
| 985 |
}; |
| 986 |
|
| 987 |
|
| 988 |
/** |
| 989 |
* Compares two objects that can be accessed like an array and assert that |
| 990 |
* each element is roughly equal. |
| 991 |
* @param {string|Object} a Failure message (4 arguments) |
| 992 |
* or object #1 (3 arguments). |
| 993 |
* @param {Object} b Object #1 (4 arguments) or object #2 (3 arguments). |
| 994 |
* @param {Object|number} c Object #2 (4 arguments) or tolerance (3 arguments). |
| 995 |
* @param {number=} opt_d tolerance (4 arguments). |
| 996 |
*/ |
| 997 |
var assertElementsRoughlyEqual = |
| 998 |
goog.testing.asserts.assertElementsRoughlyEqual = function(a, b, c, opt_d) { |
| 999 |
_validateArguments(3, arguments); |
| 1000 |
|
| 1001 |
var v1 = nonCommentArg(1, 3, arguments); |
| 1002 |
var v2 = nonCommentArg(2, 3, arguments); |
| 1003 |
var tolerance = nonCommentArg(3, 3, arguments); |
| 1004 |
var failureMessage = |
| 1005 |
commentArg(3, arguments) ? commentArg(3, arguments) : ''; |
| 1006 |
|
| 1007 |
if (!v1) { |
| 1008 |
goog.testing.asserts.assert(failureMessage, !v2); |
| 1009 |
} else { |
| 1010 |
goog.testing.asserts.assertEquals( |
| 1011 |
'length mismatch: ' + failureMessage, v1.length, v2.length); |
| 1012 |
for (var i = 0; i < v1.length; ++i) { |
| 1013 |
goog.testing.asserts.assertRoughlyEquals( |
| 1014 |
failureMessage, v1[i], v2[i], tolerance); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
}; |
| 1018 |
|
| 1019 |
|
| 1020 |
/** |
| 1021 |
* Compares two array-like objects without taking their order into account. |
| 1022 |
* @param {string|IArrayLike} a Assertion message or the |
| 1023 |
* expected elements. |
| 1024 |
* @param {IArrayLike} b Expected elements or the actual |
| 1025 |
* elements. |
| 1026 |
* @param {IArrayLike=} opt_c Actual elements. |
| 1027 |
*/ |
| 1028 |
var assertSameElements = goog.testing.asserts.assertSameElements = function( |
| 1029 |
a, b, opt_c) { |
| 1030 |
_validateArguments(2, arguments); |
| 1031 |
var expected = nonCommentArg(1, 2, arguments); |
| 1032 |
var actual = nonCommentArg(2, 2, arguments); |
| 1033 |
var message = commentArg(2, arguments); |
| 1034 |
|
| 1035 |
goog.testing.asserts.assertTrue( |
| 1036 |
'Bad arguments to assertSameElements(opt_message, expected: ' + |
| 1037 |
'ArrayLike, actual: ArrayLike)', |
| 1038 |
goog.isArrayLike(expected) && goog.isArrayLike(actual)); |
| 1039 |
|
| 1040 |
// Clones expected and actual and converts them to real arrays. |
| 1041 |
expected = goog.testing.asserts.toArray_(expected); |
| 1042 |
actual = goog.testing.asserts.toArray_(actual); |
| 1043 |
// TODO(user): It would be great to show only the difference |
| 1044 |
// between the expected and actual elements. |
| 1045 |
_assert( |
| 1046 |
message, expected.length == actual.length, 'Expected ' + expected.length + |
| 1047 |
' elements: [' + expected + '], ' + |
| 1048 |
'got ' + actual.length + ' elements: [' + actual + ']'); |
| 1049 |
|
| 1050 |
var toFind = goog.testing.asserts.toArray_(expected); |
| 1051 |
for (var i = 0; i < actual.length; i++) { |
| 1052 |
var index = goog.testing.asserts.indexOf_(toFind, actual[i]); |
| 1053 |
_assert( |
| 1054 |
message, index != -1, |
| 1055 |
'Expected [' + expected + '], got [' + actual + ']'); |
| 1056 |
toFind.splice(index, 1); |
| 1057 |
} |
| 1058 |
}; |
| 1059 |
|
| 1060 |
|
| 1061 |
/** |
| 1062 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 1063 |
* @param {*=} opt_b The value to assert (2 args only). |
| 1064 |
*/ |
| 1065 |
var assertEvaluatesToTrue = |
| 1066 |
goog.testing.asserts.assertEvaluatesToTrue = function(a, opt_b) { |
| 1067 |
_validateArguments(1, arguments); |
| 1068 |
var value = nonCommentArg(1, 1, arguments); |
| 1069 |
if (!value) { |
| 1070 |
_assert( |
| 1071 |
commentArg(1, arguments), false, 'Expected to evaluate to true'); |
| 1072 |
} |
| 1073 |
}; |
| 1074 |
|
| 1075 |
|
| 1076 |
/** |
| 1077 |
* @param {*} a The value to assert (1 arg) or debug message (2 args). |
| 1078 |
* @param {*=} opt_b The value to assert (2 args only). |
| 1079 |
*/ |
| 1080 |
var assertEvaluatesToFalse = |
| 1081 |
goog.testing.asserts.assertEvaluatesToFalse = function(a, opt_b) { |
| 1082 |
_validateArguments(1, arguments); |
| 1083 |
var value = nonCommentArg(1, 1, arguments); |
| 1084 |
if (value) { |
| 1085 |
_assert( |
| 1086 |
commentArg(1, arguments), false, 'Expected to evaluate to false'); |
| 1087 |
} |
| 1088 |
}; |
| 1089 |
|
| 1090 |
|
| 1091 |
/** |
| 1092 |
* Compares two HTML snippets. |
| 1093 |
* |
| 1094 |
* Take extra care if attributes are involved. {@code assertHTMLEquals}'s |
| 1095 |
* implementation isn't prepared for complex cases. For example, the following |
| 1096 |
* comparisons erroneously fail: |
| 1097 |
* <pre> |
| 1098 |
* assertHTMLEquals('<a href="x" target="y">', '<a target="y" href="x">'); |
| 1099 |
* assertHTMLEquals('<div class="a b">', '<div class="b a">'); |
| 1100 |
* assertHTMLEquals('<input disabled>', '<input disabled="disabled">'); |
| 1101 |
* </pre> |
| 1102 |
* |
| 1103 |
* When in doubt, use {@code goog.testing.dom.assertHtmlMatches}. |
| 1104 |
* |
| 1105 |
* @param {*} a The expected value (2 args) or the debug message (3 args). |
| 1106 |
* @param {*} b The actual value (2 args) or the expected value (3 args). |
| 1107 |
* @param {*=} opt_c The actual value (3 args only). |
| 1108 |
*/ |
| 1109 |
var assertHTMLEquals = goog.testing.asserts.assertHTMLEquals = function( |
| 1110 |
a, b, opt_c) { |
| 1111 |
_validateArguments(2, arguments); |
| 1112 |
var var1 = nonCommentArg(1, 2, arguments); |
| 1113 |
var var2 = nonCommentArg(2, 2, arguments); |
| 1114 |
var var1Standardized = standardizeHTML(var1); |
| 1115 |
var var2Standardized = standardizeHTML(var2); |
| 1116 |
|
| 1117 |
_assert( |
| 1118 |
commentArg(2, arguments), var1Standardized === var2Standardized, |
| 1119 |
goog.testing.asserts.getDefaultErrorMsg_( |
| 1120 |
var1Standardized, var2Standardized)); |
| 1121 |
}; |
| 1122 |
|
| 1123 |
|
| 1124 |
/** |
| 1125 |
* Compares two CSS property values to make sure that they represent the same |
| 1126 |
* things. This will normalize values in the browser. For example, in Firefox, |
| 1127 |
* this assertion will consider "rgb(0, 0, 255)" and "#0000ff" to be identical |
| 1128 |
* values for the "color" property. This function won't normalize everything -- |
| 1129 |
* for example, in most browsers, "blue" will not match "#0000ff". It is |
| 1130 |
* intended only to compensate for unexpected normalizations performed by |
| 1131 |
* the browser that should also affect your expected value. |
| 1132 |
* @param {string} a Assertion message, or the CSS property name. |
| 1133 |
* @param {string} b CSS property name, or the expected value. |
| 1134 |
* @param {string} c The expected value, or the actual value. |
| 1135 |
* @param {string=} opt_d The actual value. |
| 1136 |
*/ |
| 1137 |
var assertCSSValueEquals = goog.testing.asserts.assertCSSValueEquals = function( |
| 1138 |
a, b, c, opt_d) { |
| 1139 |
_validateArguments(3, arguments); |
| 1140 |
var propertyName = nonCommentArg(1, 3, arguments); |
| 1141 |
var expectedValue = nonCommentArg(2, 3, arguments); |
| 1142 |
var actualValue = nonCommentArg(3, 3, arguments); |
| 1143 |
var expectedValueStandardized = |
| 1144 |
standardizeCSSValue(propertyName, expectedValue); |
| 1145 |
var actualValueStandardized = standardizeCSSValue(propertyName, actualValue); |
| 1146 |
|
| 1147 |
_assert( |
| 1148 |
commentArg(3, arguments), |
| 1149 |
expectedValueStandardized == actualValueStandardized, |
| 1150 |
goog.testing.asserts.getDefaultErrorMsg_( |
| 1151 |
expectedValueStandardized, actualValueStandardized)); |
| 1152 |
}; |
| 1153 |
|
| 1154 |
|
| 1155 |
/** |
| 1156 |
* @param {*} a The expected value (2 args) or the debug message (3 args). |
| 1157 |
* @param {*} b The actual value (2 args) or the expected value (3 args). |
| 1158 |
* @param {*=} opt_c The actual value (3 args only). |
| 1159 |
*/ |
| 1160 |
var assertHashEquals = goog.testing.asserts.assertHashEquals = function( |
| 1161 |
a, b, opt_c) { |
| 1162 |
_validateArguments(2, arguments); |
| 1163 |
var var1 = nonCommentArg(1, 2, arguments); |
| 1164 |
var var2 = nonCommentArg(2, 2, arguments); |
| 1165 |
var message = commentArg(2, arguments); |
| 1166 |
for (var key in var1) { |
| 1167 |
_assert( |
| 1168 |
message, key in var2, |
| 1169 |
'Expected hash had key ' + key + ' that was not found'); |
| 1170 |
_assert( |
| 1171 |
message, var1[key] == var2[key], 'Value for key ' + key + |
| 1172 |
' mismatch - expected = ' + var1[key] + ', actual = ' + var2[key]); |
| 1173 |
} |
| 1174 |
|
| 1175 |
for (var key in var2) { |
| 1176 |
_assert( |
| 1177 |
message, key in var1, |
| 1178 |
'Actual hash had key ' + key + ' that was not expected'); |
| 1179 |
} |
| 1180 |
}; |
| 1181 |
|
| 1182 |
|
| 1183 |
/** |
| 1184 |
* @param {*} a The expected value (3 args) or the debug message (4 args). |
| 1185 |
* @param {*} b The actual value (3 args) or the expected value (4 args). |
| 1186 |
* @param {*} c The tolerance (3 args) or the actual value (4 args). |
| 1187 |
* @param {*=} opt_d The tolerance (4 args only). |
| 1188 |
*/ |
| 1189 |
var assertRoughlyEquals = goog.testing.asserts.assertRoughlyEquals = function( |
| 1190 |
a, b, c, opt_d) { |
| 1191 |
_validateArguments(3, arguments); |
| 1192 |
var expected = nonCommentArg(1, 3, arguments); |
| 1193 |
var actual = nonCommentArg(2, 3, arguments); |
| 1194 |
var tolerance = nonCommentArg(3, 3, arguments); |
| 1195 |
_assert( |
| 1196 |
commentArg(3, arguments), |
| 1197 |
goog.testing.asserts.numberRoughEqualityPredicate_( |
| 1198 |
expected, actual, tolerance), |
| 1199 |
'Expected ' + expected + ', but got ' + actual + ' which was more than ' + |
| 1200 |
tolerance + ' away'); |
| 1201 |
}; |
| 1202 |
|
| 1203 |
|
| 1204 |
/** |
| 1205 |
* Checks if the test value is a member of the given container. Uses |
| 1206 |
* container.indexOf as the underlying function, so this works for strings |
| 1207 |
* and arrays. |
| 1208 |
* @param {*} a Failure message (3 arguments) or the test value |
| 1209 |
* (2 arguments). |
| 1210 |
* @param {*} b The test value (3 arguments) or the container |
| 1211 |
* (2 arguments). |
| 1212 |
* @param {*=} opt_c The container. |
| 1213 |
*/ |
| 1214 |
var assertContains = goog.testing.asserts.assertContains = function( |
| 1215 |
a, b, opt_c) { |
| 1216 |
_validateArguments(2, arguments); |
| 1217 |
var contained = nonCommentArg(1, 2, arguments); |
| 1218 |
var container = nonCommentArg(2, 2, arguments); |
| 1219 |
_assert( |
| 1220 |
commentArg(2, arguments), |
| 1221 |
goog.testing.asserts.contains_(container, contained), |
| 1222 |
'Expected \'' + container + '\' to contain \'' + contained + '\''); |
| 1223 |
}; |
| 1224 |
|
| 1225 |
|
| 1226 |
/** |
| 1227 |
* Checks if the given element is not the member of the given container. |
| 1228 |
* @param {*} a Failure message (3 arguments) or the contained element |
| 1229 |
* (2 arguments). |
| 1230 |
* @param {*} b The contained element (3 arguments) or the container |
| 1231 |
* (2 arguments). |
| 1232 |
* @param {*=} opt_c The container. |
| 1233 |
*/ |
| 1234 |
var assertNotContains = goog.testing.asserts.assertNotContains = function( |
| 1235 |
a, b, opt_c) { |
| 1236 |
_validateArguments(2, arguments); |
| 1237 |
var contained = nonCommentArg(1, 2, arguments); |
| 1238 |
var container = nonCommentArg(2, 2, arguments); |
| 1239 |
_assert( |
| 1240 |
commentArg(2, arguments), |
| 1241 |
!goog.testing.asserts.contains_(container, contained), |
| 1242 |
'Expected \'' + container + '\' not to contain \'' + contained + '\''); |
| 1243 |
}; |
| 1244 |
|
| 1245 |
|
| 1246 |
/** |
| 1247 |
* Checks if the given string matches the given regular expression. |
| 1248 |
* @param {*} a Failure message (3 arguments) or the expected regular |
| 1249 |
* expression as a string or RegExp (2 arguments). |
| 1250 |
* @param {*} b The regular expression (3 arguments) or the string to test |
| 1251 |
* (2 arguments). |
| 1252 |
* @param {*=} opt_c The string to test. |
| 1253 |
*/ |
| 1254 |
var assertRegExp = goog.testing.asserts.assertRegExp = function(a, b, opt_c) { |
| 1255 |
_validateArguments(2, arguments); |
| 1256 |
var regexp = nonCommentArg(1, 2, arguments); |
| 1257 |
var string = nonCommentArg(2, 2, arguments); |
| 1258 |
if (typeof(regexp) == 'string') { |
| 1259 |
regexp = new RegExp(regexp); |
| 1260 |
} |
| 1261 |
_assert( |
| 1262 |
commentArg(2, arguments), regexp.test(string), |
| 1263 |
'Expected \'' + string + '\' to match RegExp ' + regexp.toString()); |
| 1264 |
}; |
| 1265 |
|
| 1266 |
|
| 1267 |
/** |
| 1268 |
* Converts an array like object to array or clones it if it's already array. |
| 1269 |
* @param {IArrayLike} arrayLike The collection. |
| 1270 |
* @return {!Array<?>} Copy of the collection as array. |
| 1271 |
* @private |
| 1272 |
*/ |
| 1273 |
goog.testing.asserts.toArray_ = function(arrayLike) { |
| 1274 |
var ret = []; |
| 1275 |
for (var i = 0; i < arrayLike.length; i++) { |
| 1276 |
ret[i] = arrayLike[i]; |
| 1277 |
} |
| 1278 |
return ret; |
| 1279 |
}; |
| 1280 |
|
| 1281 |
|
| 1282 |
/** |
| 1283 |
* Finds the position of the first occurrence of an element in a container. |
| 1284 |
* @param {IArrayLike} container |
| 1285 |
* The array to find the element in. |
| 1286 |
* @param {*} contained Element to find. |
| 1287 |
* @return {number} Index of the first occurrence or -1 if not found. |
| 1288 |
* @private |
| 1289 |
*/ |
| 1290 |
goog.testing.asserts.indexOf_ = function(container, contained) { |
| 1291 |
if (container.indexOf) { |
| 1292 |
return container.indexOf(contained); |
| 1293 |
} else { |
| 1294 |
// IE6/7 do not have indexOf so do a search. |
| 1295 |
for (var i = 0; i < container.length; i++) { |
| 1296 |
if (container[i] === contained) { |
| 1297 |
return i; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
return -1; |
| 1301 |
} |
| 1302 |
}; |
| 1303 |
|
| 1304 |
|
| 1305 |
/** |
| 1306 |
* Tells whether the array contains the given element. |
| 1307 |
* @param {IArrayLike} container The array to |
| 1308 |
* find the element in. |
| 1309 |
* @param {*} contained Element to find. |
| 1310 |
* @return {boolean} Whether the element is in the array. |
| 1311 |
* @private |
| 1312 |
*/ |
| 1313 |
goog.testing.asserts.contains_ = function(container, contained) { |
| 1314 |
// TODO(user): Can we check for container.contains as well? |
| 1315 |
// That would give us support for most goog.structs (though weird results |
| 1316 |
// with anything else with a contains method, like goog.math.Range). Falling |
| 1317 |
// back with container.some would catch all iterables, too. |
| 1318 |
return goog.testing.asserts.indexOf_(container, contained) != -1; |
| 1319 |
}; |
| 1320 |
|
| 1321 |
var standardizeHTML = function(html) { |
| 1322 |
var translator = document.createElement('DIV'); |
| 1323 |
translator.innerHTML = html; |
| 1324 |
|
| 1325 |
// Trim whitespace from result (without relying on goog.string) |
| 1326 |
return translator.innerHTML.replace(/^\s+|\s+$/g, ''); |
| 1327 |
}; |
| 1328 |
|
| 1329 |
|
| 1330 |
/** |
| 1331 |
* Standardizes a CSS value for a given property by applying it to an element |
| 1332 |
* and then reading it back. |
| 1333 |
* @param {string} propertyName CSS property name. |
| 1334 |
* @param {string} value CSS value. |
| 1335 |
* @return {string} Normalized CSS value. |
| 1336 |
*/ |
| 1337 |
var standardizeCSSValue = function(propertyName, value) { |
| 1338 |
var styleDeclaration = document.createElement('DIV').style; |
| 1339 |
styleDeclaration[propertyName] = value; |
| 1340 |
return styleDeclaration[propertyName]; |
| 1341 |
}; |
| 1342 |
|
| 1343 |
|
| 1344 |
/** |
| 1345 |
* Raises a JsUnit exception with the given comment. If the exception is |
| 1346 |
* unexpectedly caught during a unit test, it will be rethrown so that it is |
| 1347 |
* seen by the test framework. |
| 1348 |
* @param {string} comment A summary for the exception. |
| 1349 |
* @param {string=} opt_message A description of the exception. |
| 1350 |
*/ |
| 1351 |
goog.testing.asserts.raiseException = function(comment, opt_message) { |
| 1352 |
var e = new goog.testing.JsUnitException(comment, opt_message); |
| 1353 |
|
| 1354 |
var testCase = _getCurrentTestCase(); |
| 1355 |
if (testCase) { |
| 1356 |
testCase.raiseAssertionException(e); |
| 1357 |
} else { |
| 1358 |
goog.global.console.error( |
| 1359 |
'Failed to save thrown exception: no test case is installed.'); |
| 1360 |
throw e; |
| 1361 |
} |
| 1362 |
}; |
| 1363 |
|
| 1364 |
|
| 1365 |
/** |
| 1366 |
* Helper function for assertObjectEquals. |
| 1367 |
* @param {string} prop A property name. |
| 1368 |
* @return {boolean} If the property name is an array index. |
| 1369 |
* @private |
| 1370 |
*/ |
| 1371 |
goog.testing.asserts.isArrayIndexProp_ = function(prop) { |
| 1372 |
return (prop | 0) == prop; |
| 1373 |
}; |
| 1374 |
|
| 1375 |
/** @define {boolean} */ |
| 1376 |
goog.define('goog.EXPORT_ASSERTIONS', true); |
| 1377 |
/* |
| 1378 |
* These symbols are both exported in the global namespace (for legacy |
| 1379 |
* reasons) and as part of the goog.testing.asserts namespace. Although they |
| 1380 |
* can be used globally in tests, these symbols are allowed to be imported for |
| 1381 |
* cleaner typing. |
| 1382 |
*/ |
| 1383 |
if (goog.EXPORT_ASSERTIONS) { |
| 1384 |
goog.exportSymbol('fail', fail); |
| 1385 |
goog.exportSymbol('assert', assert); |
| 1386 |
goog.exportSymbol('assertThrows', assertThrows); |
| 1387 |
goog.exportSymbol('assertNotThrows', assertNotThrows); |
| 1388 |
goog.exportSymbol('assertThrowsJsUnitException', assertThrowsJsUnitException); |
| 1389 |
goog.exportSymbol('assertTrue', assertTrue); |
| 1390 |
goog.exportSymbol('assertFalse', assertFalse); |
| 1391 |
goog.exportSymbol('assertEquals', assertEquals); |
| 1392 |
goog.exportSymbol('assertNotEquals', assertNotEquals); |
| 1393 |
goog.exportSymbol('assertNull', assertNull); |
| 1394 |
goog.exportSymbol('assertNotNull', assertNotNull); |
| 1395 |
goog.exportSymbol('assertUndefined', assertUndefined); |
| 1396 |
goog.exportSymbol('assertNotUndefined', assertNotUndefined); |
| 1397 |
goog.exportSymbol('assertNotNullNorUndefined', assertNotNullNorUndefined); |
| 1398 |
goog.exportSymbol('assertNonEmptyString', assertNonEmptyString); |
| 1399 |
goog.exportSymbol('assertNaN', assertNaN); |
| 1400 |
goog.exportSymbol('assertNotNaN', assertNotNaN); |
| 1401 |
goog.exportSymbol('assertObjectEquals', assertObjectEquals); |
| 1402 |
goog.exportSymbol('assertObjectRoughlyEquals', assertObjectRoughlyEquals); |
| 1403 |
goog.exportSymbol('assertObjectNotEquals', assertObjectNotEquals); |
| 1404 |
goog.exportSymbol('assertArrayEquals', assertArrayEquals); |
| 1405 |
goog.exportSymbol('assertElementsEquals', assertElementsEquals); |
| 1406 |
goog.exportSymbol('assertElementsRoughlyEqual', assertElementsRoughlyEqual); |
| 1407 |
goog.exportSymbol('assertSameElements', assertSameElements); |
| 1408 |
goog.exportSymbol('assertEvaluatesToTrue', assertEvaluatesToTrue); |
| 1409 |
goog.exportSymbol('assertEvaluatesToFalse', assertEvaluatesToFalse); |
| 1410 |
goog.exportSymbol('assertHTMLEquals', assertHTMLEquals); |
| 1411 |
goog.exportSymbol('assertHashEquals', assertHashEquals); |
| 1412 |
goog.exportSymbol('assertRoughlyEquals', assertRoughlyEquals); |
| 1413 |
goog.exportSymbol('assertContains', assertContains); |
| 1414 |
goog.exportSymbol('assertNotContains', assertNotContains); |
| 1415 |
goog.exportSymbol('assertRegExp', assertRegExp); |
| 1416 |
} |