| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Expect is used for tests that do not want to make use of the | 6 * Expect is used for tests that do not want to make use of the |
| 7 * Dart unit test library - for example, the core language tests. | 7 * Dart unit test library - for example, the core language tests. |
| 8 * Third parties are discouraged from using this, and should use | 8 * Third parties are discouraged from using this, and should use |
| 9 * the expect() function in the unit test library instead for | 9 * the expect() function in the unit test library instead for |
| 10 * test assertions. | 10 * test assertions. |
| 11 */ | 11 */ |
| 12 class Expect { | 12 class Expect { |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Checks whether the expected and actual values are equal (using [:==:]). | 15 * Checks whether the expected and actual values are equal (using `==`). |
| 16 */ | 16 */ |
| 17 static void equals(var expected, var actual, [String reason = null]) { | 17 static void equals(var expected, var actual, [String reason = null]) { |
| 18 if (expected == actual) return; | 18 if (expected == actual) return; |
| 19 String msg = _getMessage(reason); | 19 String msg = _getMessage(reason); |
| 20 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); | 20 _fail("Expect.equals(expected: <$expected>, actual: <$actual>$msg) fails."); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Checks whether the actual value is a bool and its value is true. | 24 * Checks whether the actual value is a bool and its value is true. |
| 25 */ | 25 */ |
| (...skipping 25 matching lines...) Expand all Loading... |
| 51 * Checks whether [actual] is not null. | 51 * Checks whether [actual] is not null. |
| 52 */ | 52 */ |
| 53 static void isNotNull(actual, [String reason = null]) { | 53 static void isNotNull(actual, [String reason = null]) { |
| 54 if (null !== actual) return; | 54 if (null !== actual) return; |
| 55 String msg = _getMessage(reason); | 55 String msg = _getMessage(reason); |
| 56 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); | 56 _fail("Expect.isNotNull(actual: <$actual>$msg) fails."); |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Checks whether the expected and actual values are identical | 60 * Checks whether the expected and actual values are identical |
| 61 * (using [:===:]). | 61 * (using `===`). |
| 62 */ | 62 */ |
| 63 static void identical(var expected, var actual, [String reason = null]) { | 63 static void identical(var expected, var actual, [String reason = null]) { |
| 64 if (expected === actual) return; | 64 if (expected === actual) return; |
| 65 String msg = _getMessage(reason); | 65 String msg = _getMessage(reason); |
| 66 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " | 66 _fail("Expect.identical(expected: <$expected>, actual: <$actual>$msg) " |
| 67 "fails."); | 67 "fails."); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Unconditional failure. | 70 // Unconditional failure. |
| 71 static void fail(String msg) { | 71 static void fail(String msg) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 93 } | 93 } |
| 94 | 94 |
| 95 static void notEquals(unexpected, actual, [String reason = null]) { | 95 static void notEquals(unexpected, actual, [String reason = null]) { |
| 96 if (unexpected != actual) return; | 96 if (unexpected != actual) return; |
| 97 String msg = _getMessage(reason); | 97 String msg = _getMessage(reason); |
| 98 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " | 98 _fail("Expect.notEquals(unexpected: <$unexpected>, actual:<$actual>$msg) " |
| 99 "fails."); | 99 "fails."); |
| 100 } | 100 } |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Checks that all elements in [expected] and [actual] are equal [:==:]. | 103 * Checks that all elements in [expected] and [actual] are equal `==`. |
| 104 * This is different than the typical check for identity equality [:===:] | 104 * This is different than the typical check for identity equality `===` |
| 105 * used by the standard list implementation. It should also produce nicer | 105 * used by the standard list implementation. It should also produce nicer |
| 106 * error messages than just calling [:Expect.equals(expected, actual):]. | 106 * error messages than just calling `Expect.equals(expected, actual)`. |
| 107 */ | 107 */ |
| 108 static void listEquals(List expected, List actual, [String reason = null]) { | 108 static void listEquals(List expected, List actual, [String reason = null]) { |
| 109 String msg = _getMessage(reason); | 109 String msg = _getMessage(reason); |
| 110 int n = Math.min(expected.length, actual.length); | 110 int n = Math.min(expected.length, actual.length); |
| 111 for (int i = 0; i < n; i++) { | 111 for (int i = 0; i < n; i++) { |
| 112 if (expected[i] != actual[i]) { | 112 if (expected[i] != actual[i]) { |
| 113 _fail('Expect.listEquals(at index $i, ' | 113 _fail('Expect.listEquals(at index $i, ' |
| 114 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); | 114 'expected: <${expected[i]}>, actual: <${actual[i]}>$msg) fails'); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 // We check on length at the end in order to provide better error | 117 // We check on length at the end in order to provide better error |
| 118 // messages when an unexpected item is inserted in a list. | 118 // messages when an unexpected item is inserted in a list. |
| 119 if (expected.length != actual.length) { | 119 if (expected.length != actual.length) { |
| 120 _fail('Expect.listEquals(list length, ' | 120 _fail('Expect.listEquals(list length, ' |
| 121 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' | 121 'expected: <${expected.length}>, actual: <${actual.length}>$msg) ' |
| 122 'fails'); | 122 'fails'); |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 | 125 |
| 126 /** | 126 /** |
| 127 * Checks that all [expected] and [actual] have the same set of keys (using | 127 * Checks that all [expected] and [actual] have the same set of keys (using |
| 128 * the semantics of [Map.containsKey] to determine what "same" means. For | 128 * the semantics of [Map.containsKey] to determine what "same" means. For |
| 129 * each key, checks that the values in both maps are equal using [:==:]. | 129 * each key, checks that the values in both maps are equal using `==`. |
| 130 */ | 130 */ |
| 131 static void mapEquals(Map expected, Map actual, [String reason = null]) { | 131 static void mapEquals(Map expected, Map actual, [String reason = null]) { |
| 132 String msg = _getMessage(reason); | 132 String msg = _getMessage(reason); |
| 133 | 133 |
| 134 // Make sure all of the values are present in both and match. | 134 // Make sure all of the values are present in both and match. |
| 135 for (final key in expected.getKeys()) { | 135 for (final key in expected.getKeys()) { |
| 136 if (!actual.containsKey(key)) { | 136 if (!actual.containsKey(key)) { |
| 137 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); | 137 _fail('Expect.mapEquals(missing expected key: <$key>$msg) fails'); |
| 138 } | 138 } |
| 139 | 139 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 251 } |
| 252 _fail(sb.toString()); | 252 _fail(sb.toString()); |
| 253 } | 253 } |
| 254 | 254 |
| 255 /** | 255 /** |
| 256 * Calls the function [f] and verifies that it throws an exception. | 256 * Calls the function [f] and verifies that it throws an exception. |
| 257 * The optional [check] function can provide additional validation | 257 * The optional [check] function can provide additional validation |
| 258 * that the correct exception is being thrown. For example, to check | 258 * that the correct exception is being thrown. For example, to check |
| 259 * the type of the exception you could write this: | 259 * the type of the exception you could write this: |
| 260 * | 260 * |
| 261 * [: | 261 * Expect.throws(myThrowingFunction, (e) => e is MyException); |
| 262 * Expect.throws(myThrowingFunction, | |
| 263 * (e) { return e is MyException}) | |
| 264 * :] | |
| 265 */ | 262 */ |
| 266 static void throws(void f(), | 263 static void throws(void f(), |
| 267 [_CheckExceptionFn check = null, | 264 [_CheckExceptionFn check = null, |
| 268 String reason = null]) { | 265 String reason = null]) { |
| 269 try { | 266 try { |
| 270 f(); | 267 f(); |
| 271 } catch (var e) { | 268 } catch (var e) { |
| 272 if (check !== null) { | 269 if (check !== null) { |
| 273 Expect.isTrue(check(e)); | 270 Expect.isTrue(check(e)); |
| 274 } | 271 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 286 } | 283 } |
| 287 } | 284 } |
| 288 | 285 |
| 289 typedef bool _CheckExceptionFn(exception); | 286 typedef bool _CheckExceptionFn(exception); |
| 290 | 287 |
| 291 class ExpectException implements Exception { | 288 class ExpectException implements Exception { |
| 292 ExpectException(this.message); | 289 ExpectException(this.message); |
| 293 String toString() => message; | 290 String toString() => message; |
| 294 String message; | 291 String message; |
| 295 } | 292 } |
| OLD | NEW |