Skip to content

Commit

Permalink
Prepare mockito for null safety:
Browse files Browse the repository at this point in the history
Change [_findAfter] to throw instead of return null, in the case that a match could not be found. The way the code is before, where `firstWhere` can return null via `orElse`, the type of list is inferred to be a list of nullable elements. The migration tool feeds this nullability up very high. Removing the `orElse` and catching the possible StateError is safer.

* Add /*late*/ hints for the migration tool.

PiperOrigin-RevId: 317387499
  • Loading branch information
srawlins committed Jul 9, 2020
1 parent e5e59e2 commit d41f6bb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/iss/iss.dart
Expand Up @@ -22,7 +22,7 @@ import 'package:http/http.dart';
class IssLocator {
final Client client;

Point<double> _position;
/*late*/ Point<double> _position;
Future _ongoingRequest;

IssLocator(this.client);
Expand Down
11 changes: 5 additions & 6 deletions lib/src/mock.dart
Expand Up @@ -658,9 +658,8 @@ class _VerifyCall {
}

RealCall _findAfter(DateTime dt) {
return matchingInvocations.firstWhere(
(inv) => !inv.verified && inv.timeStamp.isAfter(dt),
orElse: () => null);
return matchingInvocations
.firstWhere((inv) => !inv.verified && inv.timeStamp.isAfter(dt));
}

void _checkWith(bool never) {
Expand Down Expand Up @@ -1001,11 +1000,11 @@ _InOrderVerification get verifyInOrder {
_verifyCalls.clear();
var matchedCalls = <RealCall>[];
for (var verifyCall in tmpVerifyCalls) {
var matched = verifyCall._findAfter(dt);
if (matched != null) {
try {
var matched = verifyCall._findAfter(dt);
matchedCalls.add(matched);
dt = matched.timeStamp;
} else {
} on StateError {
var mocks = tmpVerifyCalls.map((vc) => vc.mock).toSet();
var allInvocations =
mocks.expand((m) => m._realCalls).toList(growable: false);
Expand Down
2 changes: 1 addition & 1 deletion test/invocation_matcher_test.dart
Expand Up @@ -153,7 +153,7 @@ abstract class Interface {
///
/// Any call always returns an [Invocation].
class Stub implements Interface {
static Invocation lastInvocation;
static /*late*/ Invocation lastInvocation;

const Stub();

Expand Down

0 comments on commit d41f6bb

Please sign in to comment.