OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // values. | 47 // values. |
48 // For known primitive values, please use assertEquals. | 48 // For known primitive values, please use assertEquals. |
49 var assertSame; | 49 var assertSame; |
50 | 50 |
51 // Expected and found values are identical primitive values or functions | 51 // Expected and found values are identical primitive values or functions |
52 // or similarly structured objects (checking internal properties | 52 // or similarly structured objects (checking internal properties |
53 // of, e.g., Number and Date objects, the elements of arrays | 53 // of, e.g., Number and Date objects, the elements of arrays |
54 // and the properties of non-Array objects). | 54 // and the properties of non-Array objects). |
55 var assertEquals; | 55 var assertEquals; |
56 | 56 |
| 57 var assertEqualsAsync; |
57 | 58 |
58 // The difference between expected and found value is within certain tolerance. | 59 // The difference between expected and found value is within certain tolerance. |
59 var assertEqualsDelta; | 60 var assertEqualsDelta; |
60 | 61 |
61 // The found object is an Array with the same length and elements | 62 // The found object is an Array with the same length and elements |
62 // as the expected object. The expected object doesn't need to be an Array, | 63 // as the expected object. The expected object doesn't need to be an Array, |
63 // as long as it's "array-ish". | 64 // as long as it's "array-ish". |
64 var assertArrayEquals; | 65 var assertArrayEquals; |
65 | 66 |
66 // The found object must have the same enumerable properties as the | 67 // The found object must have the same enumerable properties as the |
(...skipping 19 matching lines...) Expand all Loading... |
86 // Checks that the found value is *not* null. | 87 // Checks that the found value is *not* null. |
87 var assertNotNull; | 88 var assertNotNull; |
88 | 89 |
89 // Assert that the passed function or eval code throws an exception. | 90 // Assert that the passed function or eval code throws an exception. |
90 // The optional second argument is an exception constructor that the | 91 // The optional second argument is an exception constructor that the |
91 // thrown exception is checked against with "instanceof". | 92 // thrown exception is checked against with "instanceof". |
92 // The optional third argument is a message type string that is compared | 93 // The optional third argument is a message type string that is compared |
93 // to the type property on the thrown exception. | 94 // to the type property on the thrown exception. |
94 var assertThrows; | 95 var assertThrows; |
95 | 96 |
| 97 var assertThrowsAsync; |
| 98 |
96 // Assert that the passed function throws an exception. | 99 // Assert that the passed function throws an exception. |
97 // The exception is checked against the second argument using assertEquals. | 100 // The exception is checked against the second argument using assertEquals. |
98 var assertThrowsEquals; | 101 var assertThrowsEquals; |
99 | 102 |
100 // Assert that the passed function or eval code does not throw an exception. | 103 // Assert that the passed function or eval code does not throw an exception. |
101 var assertDoesNotThrow; | 104 var assertDoesNotThrow; |
102 | 105 |
103 // Asserts that the found value is an instance of the constructor passed | 106 // Asserts that the found value is an instance of the constructor passed |
104 // as the second argument. | 107 // as the second argument. |
105 var assertInstanceof; | 108 var assertInstanceof; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 }; | 282 }; |
280 | 283 |
281 | 284 |
282 assertEquals = function assertEquals(expected, found, name_opt) { | 285 assertEquals = function assertEquals(expected, found, name_opt) { |
283 if (!deepEquals(found, expected)) { | 286 if (!deepEquals(found, expected)) { |
284 fail(PrettyPrint(expected), found, name_opt); | 287 fail(PrettyPrint(expected), found, name_opt); |
285 } | 288 } |
286 }; | 289 }; |
287 | 290 |
288 | 291 |
| 292 assertEqualsAsync = function assertEqualsAsync(expected, run, msg) { |
| 293 var actual; |
| 294 var hadValue = false; |
| 295 var hadError = false; |
| 296 var promise = run(); |
| 297 |
| 298 if (typeof promise !== "object" || typeof promise.then !== "function") { |
| 299 throw new MjsUnitAssertionError( |
| 300 "Expected " + run.toString() + |
| 301 " to return a Promise, but it returned " + PrettyPrint(promise)); |
| 302 } |
| 303 |
| 304 promise.then(function(value) { hadValue = true; actual = value; }, |
| 305 function(error) { hadError = true; actual = error; }); |
| 306 |
| 307 assertFalse(hadValue || hadError); |
| 308 |
| 309 flushMicrotasks(); |
| 310 |
| 311 if (hadError) throw actual; |
| 312 |
| 313 assertTrue( |
| 314 hadValue, "Expected '" + run.toString() + "' to produce a value"); |
| 315 |
| 316 assertEquals(expected, actual, msg); |
| 317 }; |
| 318 |
289 assertEqualsDelta = | 319 assertEqualsDelta = |
290 function assertEqualsDelta(expected, found, delta, name_opt) { | 320 function assertEqualsDelta(expected, found, delta, name_opt) { |
291 assertTrue(Math.abs(expected - found) <= delta, name_opt); | 321 assertTrue(Math.abs(expected - found) <= delta, name_opt); |
292 }; | 322 }; |
293 | 323 |
294 | 324 |
295 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { | 325 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) { |
296 var start = ""; | 326 var start = ""; |
297 if (name_opt) { | 327 if (name_opt) { |
298 start = name_opt + " - "; | 328 start = name_opt + " - "; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 } | 395 } |
366 if (arguments.length >= 3) { | 396 if (arguments.length >= 3) { |
367 assertEquals(e.type, cause_opt); | 397 assertEquals(e.type, cause_opt); |
368 } | 398 } |
369 // Success. | 399 // Success. |
370 return; | 400 return; |
371 } | 401 } |
372 throw new MjsUnitAssertionError("Did not throw exception"); | 402 throw new MjsUnitAssertionError("Did not throw exception"); |
373 }; | 403 }; |
374 | 404 |
| 405 assertThrowsAsync = function assertThrowsAsync(run, errorType, message) { |
| 406 var actual; |
| 407 var hadValue = false; |
| 408 var hadError = false; |
| 409 var promise = run(); |
| 410 |
| 411 if (typeof promise !== "object" || typeof promise.then !== "function") { |
| 412 throw new MjsUnitAssertionError( |
| 413 "Expected " + run.toString() + |
| 414 " to return a Promise, but it returned " + PrettyPrint(promise)); |
| 415 } |
| 416 |
| 417 promise.then(function(value) { hadValue = true; actual = value; }, |
| 418 function(error) { hadError = true; actual = error; }); |
| 419 |
| 420 assertFalse(hadValue || hadError); |
| 421 |
| 422 flushMicrotasks(); |
| 423 |
| 424 if (!hadError) { |
| 425 throw new MjsUnitAssertionError( |
| 426 "Expected " + run + "() to throw " + errorType.name + |
| 427 ", but did not throw."); |
| 428 } |
| 429 if (!(actual instanceof errorType)) |
| 430 throw new MjsUnitAssertionError( |
| 431 "Expected " + run + "() to throw " + errorType.name + |
| 432 ", but threw '" + actual + "'"); |
| 433 if (message !== void 0 && actual.message !== message) |
| 434 throw new MjsUnitAssertionError( |
| 435 "Expected " + run + "() to throw '" + message + "', but threw '" + |
| 436 actual.message + "'"); |
| 437 }; |
| 438 |
375 | 439 |
376 assertThrowsEquals = function assertThrowsEquals(fun, val) { | 440 assertThrowsEquals = function assertThrowsEquals(fun, val) { |
377 try { | 441 try { |
378 fun(); | 442 fun(); |
379 } catch(e) { | 443 } catch(e) { |
380 assertEquals(val, e); | 444 assertEquals(val, e); |
381 return; | 445 return; |
382 } | 446 } |
383 throw new MjsUnitAssertionError("Did not throw exception"); | 447 throw new MjsUnitAssertionError("Did not throw exception"); |
384 }; | 448 }; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 if (sync_opt === undefined) sync_opt = ""; | 507 if (sync_opt === undefined) sync_opt = ""; |
444 assertTrue(OptimizationStatus(fun, sync_opt) !== 1, name_opt); | 508 assertTrue(OptimizationStatus(fun, sync_opt) !== 1, name_opt); |
445 } | 509 } |
446 | 510 |
447 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { | 511 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) { |
448 if (sync_opt === undefined) sync_opt = ""; | 512 if (sync_opt === undefined) sync_opt = ""; |
449 assertTrue(OptimizationStatus(fun, sync_opt) !== 2, name_opt); | 513 assertTrue(OptimizationStatus(fun, sync_opt) !== 2, name_opt); |
450 } | 514 } |
451 | 515 |
452 })(); | 516 })(); |
OLD | NEW |