Skip to content

Commit aff35af

Browse files
authored
fix: add assert to NavigationWaypoint constructor (#387)
* fix: add assert to NavigationWaypoint - Add assert to not allow using target and placeID both, at the same time
1 parent 5a83661 commit aff35af

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

lib/src/types/navigation_destinations.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class NavigationDisplayOptions {
182182

183183
/// Navigation waypoint with different constructors based in on type of
184184
/// initialization.
185+
///
186+
/// Either a [target] in the form of [LatLng] or a [placeID] must be provided.
187+
///
185188
/// {@category Navigation}
186189
class NavigationWaypoint {
187190
/// Initializer for [NavigationWaypoint].
@@ -194,6 +197,10 @@ class NavigationWaypoint {
194197
}) : assert(
195198
target != null || placeID != null,
196199
'Either target or placeID must be provided',
200+
),
201+
assert(
202+
target == null || placeID == null,
203+
'Cannot provide both target and placeID at the same time.',
197204
);
198205

199206
/// Initialize waypoint with coordinates.

test/google_navigation_flutter_test.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -995,31 +995,35 @@ void main() {
995995
}
996996

997997
// Continue to the next destination.
998-
final NavigationWaypointDto waypointIn = NavigationWaypointDto(
998+
final NavigationWaypointDto targetWaypointIn = NavigationWaypointDto(
999999
title: 'Title',
10001000
target: LatLngDto(latitude: 0.4, longitude: 0.5),
1001-
placeID: 'id',
10021001
preferSameSideOfRoad: true,
10031002
preferredSegmentHeading: 50,
10041003
);
10051004

10061005
when(
10071006
sessionMockApi.continueToNextDestination(),
1008-
).thenReturn(waypointIn);
1007+
).thenReturn(targetWaypointIn);
10091008
final NavigationWaypoint? waypointOut =
10101009
await GoogleMapsNavigator.continueToNextDestination();
10111010
expect(waypointOut, isNotNull);
10121011
if (waypointOut != null) {
1013-
expect(waypointIn.title, waypointOut.title);
1014-
expect(waypointIn.target?.latitude, waypointOut.target?.latitude);
1015-
expect(waypointIn.target?.longitude, waypointOut.target?.longitude);
1016-
expect(waypointIn.placeID, waypointOut.placeID);
1012+
expect(targetWaypointIn.title, waypointOut.title);
10171013
expect(
1018-
waypointIn.preferSameSideOfRoad,
1014+
targetWaypointIn.target?.latitude,
1015+
waypointOut.target?.latitude,
1016+
);
1017+
expect(
1018+
targetWaypointIn.target?.longitude,
1019+
waypointOut.target?.longitude,
1020+
);
1021+
expect(
1022+
targetWaypointIn.preferSameSideOfRoad,
10191023
waypointOut.preferSameSideOfRoad,
10201024
);
10211025
expect(
1022-
waypointIn.preferredSegmentHeading,
1026+
targetWaypointIn.preferredSegmentHeading,
10231027
waypointOut.preferredSegmentHeading,
10241028
);
10251029
}

test/navigation_types_test.dart

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ import 'package:google_navigation_flutter/src/method_channel/convert/navigation_
1818
import 'package:google_navigation_flutter/src/method_channel/method_channel.dart';
1919

2020
void main() {
21-
late NavigationWaypointDto waypointDto;
21+
late NavigationWaypointDto placeIDWaypointDto;
22+
late NavigationWaypointDto targetWaypointDto;
2223
late NavigationWaypoint waypoint;
2324
late Destinations destinations;
2425
late NavigationDisplayOptions displayOptions;
2526
late RoutingOptions routingOptions;
2627

2728
setUp(() {
28-
waypointDto = NavigationWaypointDto(
29+
targetWaypointDto = NavigationWaypointDto(
2930
title: 'testTitle',
3031
target: LatLngDto(latitude: 5.0, longitude: 6.0),
32+
preferSameSideOfRoad: true,
33+
preferredSegmentHeading: 50,
34+
);
35+
placeIDWaypointDto = NavigationWaypointDto(
36+
title: 'testTitle',
3137
placeID: 'testID',
3238
preferSameSideOfRoad: true,
3339
preferredSegmentHeading: 50,
@@ -58,18 +64,46 @@ void main() {
5864

5965
group('NavigationWaypoint tests', () {
6066
test('tests Navigation Waypoint conversion from DTO', () {
61-
final NavigationWaypoint gmsWaypoint = waypointDto.toNavigationWaypoint();
62-
expect(gmsWaypoint.title, waypointDto.title);
63-
expect(gmsWaypoint.target?.latitude, waypointDto.target?.latitude);
64-
expect(gmsWaypoint.target?.longitude, waypointDto.target?.longitude);
65-
expect(gmsWaypoint.placeID, waypointDto.placeID);
67+
final NavigationWaypoint targetGmsWaypoint =
68+
targetWaypointDto.toNavigationWaypoint();
69+
expect(targetGmsWaypoint.title, targetWaypointDto.title);
70+
expect(
71+
targetGmsWaypoint.target?.latitude,
72+
targetWaypointDto.target?.latitude,
73+
);
74+
expect(
75+
targetGmsWaypoint.target?.longitude,
76+
targetWaypointDto.target?.longitude,
77+
);
78+
expect(targetGmsWaypoint.placeID, targetWaypointDto.placeID);
79+
expect(
80+
targetGmsWaypoint.preferSameSideOfRoad,
81+
targetWaypointDto.preferSameSideOfRoad,
82+
);
83+
expect(
84+
targetGmsWaypoint.preferredSegmentHeading,
85+
targetWaypointDto.preferredSegmentHeading,
86+
);
87+
88+
final NavigationWaypoint placeIDGmsWaypoint =
89+
placeIDWaypointDto.toNavigationWaypoint();
90+
expect(placeIDGmsWaypoint.title, placeIDWaypointDto.title);
91+
expect(
92+
placeIDGmsWaypoint.target?.latitude,
93+
placeIDWaypointDto.target?.latitude,
94+
);
6695
expect(
67-
gmsWaypoint.preferSameSideOfRoad,
68-
waypointDto.preferSameSideOfRoad,
96+
placeIDGmsWaypoint.target?.longitude,
97+
placeIDWaypointDto.target?.longitude,
6998
);
99+
expect(placeIDGmsWaypoint.placeID, placeIDWaypointDto.placeID);
70100
expect(
71-
gmsWaypoint.preferredSegmentHeading,
72-
waypointDto.preferredSegmentHeading,
101+
placeIDGmsWaypoint.preferSameSideOfRoad,
102+
placeIDWaypointDto.preferSameSideOfRoad,
103+
);
104+
expect(
105+
placeIDGmsWaypoint.preferredSegmentHeading,
106+
placeIDWaypointDto.preferredSegmentHeading,
73107
);
74108
});
75109

@@ -85,6 +119,19 @@ void main() {
85119
waypointDto2.preferredSegmentHeading,
86120
);
87121
});
122+
123+
test('tests Navigation Waypoint creation asserts', () {
124+
expect(
125+
() => NavigationWaypoint(
126+
title: 'test',
127+
target: const LatLng(latitude: 5.0, longitude: 6.0),
128+
placeID: 'testID',
129+
),
130+
throwsAssertionError,
131+
);
132+
133+
expect(() => NavigationWaypoint(title: 'test'), throwsAssertionError);
134+
});
88135
});
89136

90137
group('NavigationDestinationEventMessage tests', () {

0 commit comments

Comments
 (0)