Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 6 /** |
| 7 * Returns a matcher that matches empty strings, maps or collections. | 7 * Returns a matcher that matches empty strings, maps or collections. |
| 8 */ | 8 */ |
| 9 final Matcher isEmpty = const _Empty(); | 9 final Matcher isEmpty = const _Empty(); |
| 10 | 10 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 class isInstanceOf<T> extends BaseMatcher { | 119 class isInstanceOf<T> extends BaseMatcher { |
| 120 final String _name; | 120 final String _name; |
| 121 const isInstanceOf([name = 'specified type']) : this._name = name; | 121 const isInstanceOf([name = 'specified type']) : this._name = name; |
| 122 bool matches(obj) => obj is T; | 122 bool matches(obj) => obj is T; |
| 123 // The description here is lame :-( | 123 // The description here is lame :-( |
| 124 Description describe(Description description) => | 124 Description describe(Description description) => |
| 125 description.add('an instance of ${_name}'); | 125 description.add('an instance of ${_name}'); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * A matcher that matches functions that throw exceptions when called. | 129 * This can be used to match two kinds of objects: |
| 130 * The value passed to expect() should be a reference to the function. | 130 * |
| 131 * Note that the function cannot take arguments; to handle this | 131 * * A [Function] that throws an exception when called. The function cannot |
| 132 * a wrapper will have to be created. | 132 * take any arguments. If you want to test that a function expecting |
| 133 * The function will be called once upon success, or twice upon failure | 133 * arguments throws, wrap it in another zero-argument function that calls |
| 134 * (the second time to get the failure description). | 134 * the one you want to test. The function will be called once upon success, |
| 135 * or twice upon failure (the second time to get the failure description). | |
|
Siggi Cherem (dart-lang)
2012/06/13 00:04:39
I know this is not part of your change, but I'm co
Bob Nystrom
2012/06/13 20:22:06
Yeah, I share your concerns here. We can discuss t
| |
| 136 * | |
| 137 * * A [Future] that completes with an exception. Note that this creates an | |
| 138 * asynchronous expectation. The call to `expect()` that includes this will | |
| 139 * return immediately and execution will continue. Later, when the future | |
| 140 * completes, the actual expectation will run. | |
| 135 */ | 141 */ |
| 136 final Matcher throws = const _Throws(); | 142 final Matcher throws = const _Throws(); |
| 137 | 143 |
| 138 /** | 144 /** |
| 139 * Returns a matcher that matches a function call against an exception, | 145 * This can be used to match two kinds of objects: |
| 140 * which is in turn constrained by a [matcher]. | 146 * |
| 141 * The value passed to expect() should be a reference to the function. | 147 * * A [Function] that throws an exception when called. The function cannot |
| 142 * Note that the function cannot take arguments; to handle this | 148 * take any arguments. If you want to test that a function expecting |
| 143 * a wrapper will have to be created. | 149 * arguments throws, wrap it in another zero-argument function that calls |
| 144 * The function will be called once upon success, or twice upon failure | 150 * the one you want to test. The function will be called once upon success, |
| 145 * (the second time to get the failure description). | 151 * or twice upon failure (the second time to get the failure description). |
| 152 * | |
| 153 * * A [Future] that completes with an exception. Note that this creates an | |
| 154 * asynchronous expectation. The call to `expect()` that includes this will | |
| 155 * return immediately and execution will continue. Later, when the future | |
| 156 * completes, the actual expectation will run. | |
| 157 * | |
| 158 * In both cases, when an exception is thrown, this will test that the exception | |
| 159 * object matches [matcher]. If [matcher] is not an instance of [Matcher], it | |
|
Siggi Cherem (dart-lang)
2012/06/13 00:04:39
should we then rename [matcher] to something more
Bob Nystrom
2012/06/13 20:22:06
The other places in the API where we call wrapMatc
| |
| 160 * will implicitly be treated as `equals(matcher)`. | |
| 146 */ | 161 */ |
| 147 Matcher throwsA(Matcher matcher) => new _Throws(matcher); | 162 Matcher throwsA(matcher) => new _Throws(wrapMatcher(matcher)); |
| 148 | 163 |
| 149 /** | 164 /** |
| 150 * A matcher that matches a function call against no exception. | 165 * A matcher that matches a function call against no exception. |
| 151 * The function will be called once. Any exceptions will be silently swallowed. | 166 * The function will be called once. Any exceptions will be silently swallowed. |
| 152 * The value passed to expect() should be a reference to the function. | 167 * The value passed to expect() should be a reference to the function. |
| 153 * Note that the function cannot take arguments; to handle this | 168 * Note that the function cannot take arguments; to handle this |
| 154 * a wrapper will have to be created. | 169 * a wrapper will have to be created. |
| 155 */ | 170 */ |
| 156 final Matcher returnsNormally = const _ReturnsNormally(); | 171 final Matcher returnsNormally = const _ReturnsNormally(); |
| 157 | 172 |
| 158 class _Throws extends BaseMatcher { | 173 class _Throws extends BaseMatcher { |
| 159 final Matcher _matcher; | 174 final Matcher _matcher; |
| 160 | 175 |
| 161 const _Throws([Matcher matcher = null]) : this._matcher = matcher; | 176 const _Throws([Matcher matcher = null]) : this._matcher = matcher; |
| 162 | 177 |
| 163 bool matches(item) { | 178 bool matches(item) { |
| 179 if (item is Future) { | |
| 180 // Queue up an asynchronous expectation that validates when the future | |
| 181 // completes. | |
| 182 item.onComplete(expectAsync1((future) { | |
| 183 if (future.hasValue) { | |
| 184 expect(false, | |
| 185 "Expected future to fail, but succeeded with '${future.value}'."); | |
| 186 } else if (_matcher != null) { | |
| 187 expect(future.exception, _matcher); | |
| 188 } | |
| 189 })); | |
| 190 | |
| 191 // It hasn't failed yet. | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 164 try { | 195 try { |
| 165 item(); | 196 item(); |
| 166 return false; | 197 return false; |
| 167 } catch (final e) { | 198 } catch (final e) { |
| 168 return _matcher == null || _matcher.matches(e); | 199 return _matcher == null || _matcher.matches(e); |
| 169 } | 200 } |
| 170 } | 201 } |
| 171 | 202 |
| 172 Description describe(Description description) { | 203 Description describe(Description description) { |
| 173 if (_matcher == null) { | 204 if (_matcher == null) { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 } | 432 } |
| 402 } else if (item is Map) { | 433 } else if (item is Map) { |
| 403 return item.containsKey(_expected); | 434 return item.containsKey(_expected); |
| 404 } | 435 } |
| 405 return false; | 436 return false; |
| 406 } | 437 } |
| 407 | 438 |
| 408 Description describe(Description description) => | 439 Description describe(Description description) => |
| 409 description.add('contains ').addDescriptionOf(_expected); | 440 description.add('contains ').addDescriptionOf(_expected); |
| 410 } | 441 } |
| OLD | NEW |