make router assert more strict (#67672)

This commit is contained in:
chunhtai 2020-10-08 14:12:03 -07:00 committed by GitHub
parent 973404a27f
commit cb0bdb4994
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View File

@ -245,7 +245,12 @@ class Router<T> extends StatefulWidget {
this.routeInformationParser,
required this.routerDelegate,
this.backButtonDispatcher,
}) : assert(routeInformationProvider == null || routeInformationParser != null),
}) : assert(
(routeInformationProvider == null) == (routeInformationParser == null),
'You must provide both routeInformationProvider and routeInformationParser '
'if this router parses route information. Otheriwse, they should both '
'be null.'
),
assert(routerDelegate != null),
super(key: key);

View File

@ -118,6 +118,50 @@ void main() {
expect(find.text('popped'), findsOneWidget);
});
testWidgets('Router throw when passes only routeInformationProvider', (WidgetTester tester) async {
final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider();
provider.value = const RouteInformation(
location: 'initial',
);
try {
Router<RouteInformation>(
routeInformationProvider: provider,
routerDelegate: SimpleRouterDelegate(
builder: (BuildContext context, RouteInformation information) {
return Text(information.location);
},
),
);
} on AssertionError catch(e) {
expect(
e.message,
'You must provide both routeInformationProvider and '
'routeInformationParser if this router parses route information. '
'Otheriwse, they should both be null.'
);
}
});
testWidgets('Router throw when passes only routeInformationParser', (WidgetTester tester) async {
try {
Router<RouteInformation>(
routeInformationParser: SimpleRouteInformationParser(),
routerDelegate: SimpleRouterDelegate(
builder: (BuildContext context, RouteInformation information) {
return Text(information.location);
},
),
);
} on AssertionError catch(e) {
expect(
e.message,
'You must provide both routeInformationProvider and '
'routeInformationParser if this router parses route information. '
'Otheriwse, they should both be null.'
);
}
});
testWidgets('PopNavigatorRouterDelegateMixin works', (WidgetTester tester) async {
final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider();
provider.value = const RouteInformation(