| OLD | NEW |
| 1 library typed_mock; | 1 library typed_mock; |
| 2 | 2 |
| 3 | |
| 4 _InvocationMatcher _lastMatcher; | 3 _InvocationMatcher _lastMatcher; |
| 5 | 4 |
| 6 /// Enables stubbing methods. | 5 /// Enables stubbing methods. |
| 7 /// | 6 /// |
| 8 /// Use it when you want the mock to return a particular value when a particular | 7 /// Use it when you want the mock to return a particular value when a particular |
| 9 /// method, getter or setter is called. | 8 /// method, getter or setter is called. |
| 10 /// | 9 /// |
| 11 /// when(obj.testProperty).thenReturn(10); | 10 /// when(obj.testProperty).thenReturn(10); |
| 12 /// expect(obj.testProperty, 10); // pass | 11 /// expect(obj.testProperty, 10); // pass |
| 13 /// | 12 /// |
| (...skipping 18 matching lines...) Expand all Loading... |
| 32 // set behavior | 31 // set behavior |
| 33 var behavior = new Behavior._(_lastMatcher); | 32 var behavior = new Behavior._(_lastMatcher); |
| 34 _lastMatcher._behavior = behavior; | 33 _lastMatcher._behavior = behavior; |
| 35 return behavior; | 34 return behavior; |
| 36 } finally { | 35 } finally { |
| 37 // clear to prevent memory leak | 36 // clear to prevent memory leak |
| 38 _lastMatcher = null; | 37 _lastMatcher = null; |
| 39 } | 38 } |
| 40 } | 39 } |
| 41 | 40 |
| 42 | |
| 43 /// Clears all interactions remembered so far. | 41 /// Clears all interactions remembered so far. |
| 44 resetInteractions(TypedMock mock) { | 42 resetInteractions(TypedMock mock) { |
| 45 mock._invocations.clear(); | 43 mock._invocations.clear(); |
| 46 mock._verifiedInvocations.clear(); | 44 mock._verifiedInvocations.clear(); |
| 47 } | 45 } |
| 48 | 46 |
| 49 | |
| 50 /// Verifies certain behavior happened a specified number of times. | 47 /// Verifies certain behavior happened a specified number of times. |
| 51 Verifier verify(_ignored) { | 48 Verifier verify(_ignored) { |
| 52 try { | 49 try { |
| 53 var mock = _lastMatcher._mock; | 50 var mock = _lastMatcher._mock; |
| 54 mock._removeLastInvocation(); | 51 mock._removeLastInvocation(); |
| 55 // set verifier | 52 // set verifier |
| 56 return new Verifier._(mock, _lastMatcher); | 53 return new Verifier._(mock, _lastMatcher); |
| 57 } finally { | 54 } finally { |
| 58 // clear to prevent memory leak | 55 // clear to prevent memory leak |
| 59 _lastMatcher = null; | 56 _lastMatcher = null; |
| 60 } | 57 } |
| 61 } | 58 } |
| 62 | 59 |
| 63 | |
| 64 /// Verifies that the given mock doesn't have any unverified interaction. | 60 /// Verifies that the given mock doesn't have any unverified interaction. |
| 65 void verifyNoMoreInteractions(TypedMock mock) { | 61 void verifyNoMoreInteractions(TypedMock mock) { |
| 66 var notVerified = mock._computeNotVerifiedInvocations(); | 62 var notVerified = mock._computeNotVerifiedInvocations(); |
| 67 // OK | 63 // OK |
| 68 if (notVerified.isEmpty) { | 64 if (notVerified.isEmpty) { |
| 69 return; | 65 return; |
| 70 } | 66 } |
| 71 // fail | 67 // fail |
| 72 var invocationsString = _getInvocationsString(notVerified); | 68 var invocationsString = _getInvocationsString(notVerified); |
| 73 throw new VerifyError('Unexpected interactions:\n$invocationsString'); | 69 throw new VerifyError('Unexpected interactions:\n$invocationsString'); |
| 74 } | 70 } |
| 75 | 71 |
| 76 | |
| 77 /// Verifies that no interactions happened on the given mock. | 72 /// Verifies that no interactions happened on the given mock. |
| 78 void verifyZeroInteractions(TypedMock mock) { | 73 void verifyZeroInteractions(TypedMock mock) { |
| 79 var invocations = mock._invocations; | 74 var invocations = mock._invocations; |
| 80 // OK | 75 // OK |
| 81 if (invocations.isEmpty) { | 76 if (invocations.isEmpty) { |
| 82 return; | 77 return; |
| 83 } | 78 } |
| 84 // fail | 79 // fail |
| 85 var invocationsString = _getInvocationsString(invocations); | 80 var invocationsString = _getInvocationsString(invocations); |
| 86 throw new VerifyError('Unexpected interactions:\n$invocationsString'); | 81 throw new VerifyError('Unexpected interactions:\n$invocationsString'); |
| 87 } | 82 } |
| 88 | 83 |
| 89 | |
| 90 /// [VerifyError] is thrown when one of the [verify] checks fails. | 84 /// [VerifyError] is thrown when one of the [verify] checks fails. |
| 91 class VerifyError { | 85 class VerifyError { |
| 92 final String message; | 86 final String message; |
| 93 VerifyError(this.message); | 87 VerifyError(this.message); |
| 94 String toString() => 'VerifyError: $message'; | 88 String toString() => 'VerifyError: $message'; |
| 95 } | 89 } |
| 96 | 90 |
| 97 | |
| 98 String _getInvocationsString(Iterable<Invocation> invocations) { | 91 String _getInvocationsString(Iterable<Invocation> invocations) { |
| 99 var buffer = new StringBuffer(); | 92 var buffer = new StringBuffer(); |
| 100 invocations.forEach((invocation) { | 93 invocations.forEach((invocation) { |
| 101 var member = invocation.memberName; | 94 var member = invocation.memberName; |
| 102 buffer.write(member); | 95 buffer.write(member); |
| 103 buffer.write(' '); | 96 buffer.write(' '); |
| 104 buffer.write(invocation.positionalArguments); | 97 buffer.write(invocation.positionalArguments); |
| 105 buffer.write(' '); | 98 buffer.write(' '); |
| 106 buffer.write(invocation.namedArguments); | 99 buffer.write(invocation.namedArguments); |
| 107 buffer.writeln(); | 100 buffer.writeln(); |
| 108 }); | 101 }); |
| 109 return buffer.toString(); | 102 return buffer.toString(); |
| 110 } | 103 } |
| 111 | 104 |
| 112 | |
| 113 class _InvocationMatcher { | 105 class _InvocationMatcher { |
| 114 final Symbol _member; | 106 final Symbol _member; |
| 115 final TypedMock _mock; | 107 final TypedMock _mock; |
| 116 final List<ArgumentMatcher> _matchers = []; | 108 final List<ArgumentMatcher> _matchers = []; |
| 117 | 109 |
| 118 Behavior _behavior; | 110 Behavior _behavior; |
| 119 | 111 |
| 120 _InvocationMatcher(this._mock, this._member, Invocation invocation) { | 112 _InvocationMatcher(this._mock, this._member, Invocation invocation) { |
| 121 invocation.positionalArguments.forEach((argument) { | 113 invocation.positionalArguments.forEach((argument) { |
| 122 ArgumentMatcher matcher; | 114 ArgumentMatcher matcher; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 138 var matcher = _matchers[i]; | 130 var matcher = _matchers[i]; |
| 139 var argument = arguments[i]; | 131 var argument = arguments[i]; |
| 140 if (!matcher.matches(argument)) { | 132 if (!matcher.matches(argument)) { |
| 141 return false; | 133 return false; |
| 142 } | 134 } |
| 143 } | 135 } |
| 144 return true; | 136 return true; |
| 145 } | 137 } |
| 146 } | 138 } |
| 147 | 139 |
| 148 | |
| 149 class Behavior { | 140 class Behavior { |
| 150 final _InvocationMatcher _matcher; | 141 final _InvocationMatcher _matcher; |
| 151 | 142 |
| 152 Behavior._(this._matcher); | 143 Behavior._(this._matcher); |
| 153 | 144 |
| 154 bool _thenFunctionEnabled = false; | 145 bool _thenFunctionEnabled = false; |
| 155 Function _thenFunction; | 146 Function _thenFunction; |
| 156 | 147 |
| 157 bool _returnAlwaysEnabled = false; | 148 bool _returnAlwaysEnabled = false; |
| 158 var _returnAlways; | 149 var _returnAlways; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 216 } |
| 226 // exception | 217 // exception |
| 227 if (_throwExceptionEnabled) { | 218 if (_throwExceptionEnabled) { |
| 228 throw _throwException; | 219 throw _throwException; |
| 229 } | 220 } |
| 230 // no value | 221 // no value |
| 231 return null; | 222 return null; |
| 232 } | 223 } |
| 233 } | 224 } |
| 234 | 225 |
| 235 | |
| 236 class Verifier { | 226 class Verifier { |
| 237 final TypedMock _mock; | 227 final TypedMock _mock; |
| 238 final _InvocationMatcher _matcher; | 228 final _InvocationMatcher _matcher; |
| 239 | 229 |
| 240 Verifier._(this._mock, this._matcher); | 230 Verifier._(this._mock, this._matcher); |
| 241 | 231 |
| 242 /// Marks matching interactions as verified and never fails. | 232 /// Marks matching interactions as verified and never fails. |
| 243 void any() { | 233 void any() { |
| 244 // mark as verified, but don't check the actual count | 234 // mark as verified, but don't check the actual count |
| 245 _count(); | 235 _count(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 if (!_matcher.match(invocation)) { | 296 if (!_matcher.match(invocation)) { |
| 307 return; | 297 return; |
| 308 } | 298 } |
| 309 _mock._verifiedInvocations.add(invocation); | 299 _mock._verifiedInvocations.add(invocation); |
| 310 times++; | 300 times++; |
| 311 }); | 301 }); |
| 312 return times; | 302 return times; |
| 313 } | 303 } |
| 314 } | 304 } |
| 315 | 305 |
| 316 | |
| 317 /// A class to extend mocks from. | 306 /// A class to extend mocks from. |
| 318 /// It supports specifying behavior using [when] and validation of interactions | 307 /// It supports specifying behavior using [when] and validation of interactions |
| 319 /// using [verify]. | 308 /// using [verify]. |
| 320 /// | 309 /// |
| 321 /// abstract class Name { | 310 /// abstract class Name { |
| 322 /// String get firstName; | 311 /// String get firstName; |
| 323 /// String get lastName; | 312 /// String get lastName; |
| 324 /// } | 313 /// } |
| 325 /// class NameMock extends TypedMock implements Name { | 314 /// class NameMock extends TypedMock implements Name { |
| 326 /// noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 315 /// noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 Iterable<Invocation> _computeNotVerifiedInvocations() { | 350 Iterable<Invocation> _computeNotVerifiedInvocations() { |
| 362 notVerified(e) => !_verifiedInvocations.contains(e); | 351 notVerified(e) => !_verifiedInvocations.contains(e); |
| 363 return _invocations.where(notVerified); | 352 return _invocations.where(notVerified); |
| 364 } | 353 } |
| 365 | 354 |
| 366 void _removeLastInvocation() { | 355 void _removeLastInvocation() { |
| 367 _invocations.removeLast(); | 356 _invocations.removeLast(); |
| 368 } | 357 } |
| 369 } | 358 } |
| 370 | 359 |
| 371 | |
| 372 /// [ArgumentMatcher] checks whether the given argument satisfies some | 360 /// [ArgumentMatcher] checks whether the given argument satisfies some |
| 373 /// condition. | 361 /// condition. |
| 374 abstract class ArgumentMatcher { | 362 abstract class ArgumentMatcher { |
| 375 const ArgumentMatcher(); | 363 const ArgumentMatcher(); |
| 376 | 364 |
| 377 /// Checks whether this matcher accepts the given argument. | 365 /// Checks whether this matcher accepts the given argument. |
| 378 bool matches(val); | 366 bool matches(val); |
| 379 } | 367 } |
| 380 | 368 |
| 381 | |
| 382 class _ArgumentMatcher_equals extends ArgumentMatcher { | 369 class _ArgumentMatcher_equals extends ArgumentMatcher { |
| 383 final expected; | 370 final expected; |
| 384 | 371 |
| 385 const _ArgumentMatcher_equals(this.expected); | 372 const _ArgumentMatcher_equals(this.expected); |
| 386 | 373 |
| 387 @override | 374 @override |
| 388 bool matches(val) { | 375 bool matches(val) { |
| 389 return val == expected; | 376 return val == expected; |
| 390 } | 377 } |
| 391 } | 378 } |
| 392 | 379 |
| 393 | |
| 394 class _ArgumentMatcher_anyBool extends ArgumentMatcher { | 380 class _ArgumentMatcher_anyBool extends ArgumentMatcher { |
| 395 const _ArgumentMatcher_anyBool(); | 381 const _ArgumentMatcher_anyBool(); |
| 396 | 382 |
| 397 @override | 383 @override |
| 398 bool matches(val) { | 384 bool matches(val) { |
| 399 return val is bool; | 385 return val is bool; |
| 400 } | 386 } |
| 401 } | 387 } |
| 402 | 388 |
| 403 /// Matches any [bool] value. | 389 /// Matches any [bool] value. |
| 404 final anyBool = const _ArgumentMatcher_anyBool() as dynamic; | 390 final anyBool = const _ArgumentMatcher_anyBool() as dynamic; |
| 405 | 391 |
| 406 | |
| 407 class _ArgumentMatcher_anyInt extends ArgumentMatcher { | 392 class _ArgumentMatcher_anyInt extends ArgumentMatcher { |
| 408 const _ArgumentMatcher_anyInt(); | 393 const _ArgumentMatcher_anyInt(); |
| 409 | 394 |
| 410 @override | 395 @override |
| 411 bool matches(val) { | 396 bool matches(val) { |
| 412 return val is int; | 397 return val is int; |
| 413 } | 398 } |
| 414 } | 399 } |
| 415 | 400 |
| 416 /// Matches any [int] value. | 401 /// Matches any [int] value. |
| 417 final anyInt = const _ArgumentMatcher_anyInt() as dynamic; | 402 final anyInt = const _ArgumentMatcher_anyInt() as dynamic; |
| 418 | 403 |
| 419 | |
| 420 class _ArgumentMatcher_anyObject extends ArgumentMatcher { | 404 class _ArgumentMatcher_anyObject extends ArgumentMatcher { |
| 421 const _ArgumentMatcher_anyObject(); | 405 const _ArgumentMatcher_anyObject(); |
| 422 | 406 |
| 423 @override | 407 @override |
| 424 bool matches(val) { | 408 bool matches(val) { |
| 425 return true; | 409 return true; |
| 426 } | 410 } |
| 427 } | 411 } |
| 428 | 412 |
| 429 /// Matches any [Object] (or subclass) value. | 413 /// Matches any [Object] (or subclass) value. |
| 430 final anyObject = const _ArgumentMatcher_anyObject() as dynamic; | 414 final anyObject = const _ArgumentMatcher_anyObject() as dynamic; |
| 431 | 415 |
| 432 | |
| 433 class _ArgumentMatcher_anyString extends ArgumentMatcher { | 416 class _ArgumentMatcher_anyString extends ArgumentMatcher { |
| 434 const _ArgumentMatcher_anyString(); | 417 const _ArgumentMatcher_anyString(); |
| 435 | 418 |
| 436 @override | 419 @override |
| 437 bool matches(val) { | 420 bool matches(val) { |
| 438 return val is String; | 421 return val is String; |
| 439 } | 422 } |
| 440 } | 423 } |
| 441 | 424 |
| 442 /// Matches any [String] value. | 425 /// Matches any [String] value. |
| 443 final anyString = const _ArgumentMatcher_anyString() as dynamic; | 426 final anyString = const _ArgumentMatcher_anyString() as dynamic; |
| OLD | NEW |