1
1
package de.westnordost.streetcomplete.data.import
2
2
3
3
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
4
- import org.junit.runner.RunWith
5
- import org.robolectric.RobolectricTestRunner
4
+ import kotlinx.coroutines.flow.toList
5
+ import kotlinx.coroutines.runBlocking
6
6
import kotlin.test.Test
7
7
import kotlin.test.assertEquals
8
8
import kotlin.test.assertFails
9
9
10
- @RunWith(RobolectricTestRunner ::class )
11
10
class GpxImportParseTest {
12
11
@Test
13
- fun `successfully parses minimal sample track` () {
12
+ fun successfullyParsesMinimalSampleTrack () = runBlocking {
14
13
val originalTrackPoints = arrayListOf (
15
14
TrackPoint (" 22.22" , " 172.3" ),
16
15
TrackPoint (" 39.11111" , " -179.999" ),
@@ -20,13 +19,17 @@ class GpxImportParseTest {
20
19
TrackPoint (" -72.0" , " 0.3" ),
21
20
)
22
21
23
- val inputGpx = minimalGpxBuilder(originalTrackPoints)
22
+ val inputGpx =
23
+ minimalGpxBuilder(originalTrackPoints)
24
24
25
- assertSuccess(originalTrackPoints, parseGpx(inputGpx))
25
+ assertSuccess(
26
+ originalTrackPoints,
27
+ parseGpx(inputGpx)
28
+ )
26
29
}
27
30
28
31
@Test
29
- fun `concatenates multiple track segments` () {
32
+ fun concatenatesMultipleTrackSegments () = runBlocking {
30
33
val trackPointsSegment1 = arrayListOf (
31
34
TrackPoint (" -56.0" , " 0.0" ),
32
35
TrackPoint (" 57.57" , " 172.3" )
@@ -53,11 +56,14 @@ class GpxImportParseTest {
53
56
append(" </gpx>" )
54
57
}
55
58
56
- assertSuccess(trackPointsSegment1 + trackPointsSegment2, parseGpx(inputGpx))
59
+ assertSuccess(
60
+ trackPointsSegment1 + trackPointsSegment2,
61
+ parseGpx(inputGpx)
62
+ )
57
63
}
58
64
59
65
@Test
60
- fun `process multiple tracks and segments` () {
66
+ fun processesMultipleTracksAndSegments () = runBlocking {
61
67
val trackPoints1 = arrayListOf (
62
68
TrackPoint (" -12.33" , " 0.0" ),
63
69
TrackPoint (" 74.1" , " -122.34" )
@@ -95,21 +101,32 @@ class GpxImportParseTest {
95
101
append(" </gpx>" )
96
102
}
97
103
98
- assertSuccess(trackPoints1 + trackPoints2 + trackPoints3, parseGpx(inputGpx))
104
+ assertSuccess(
105
+ trackPoints1 + trackPoints2 + trackPoints3,
106
+ parseGpx(inputGpx)
107
+ )
99
108
}
100
109
101
110
@Test
102
- fun `throws on invalid trackPoints` () {
111
+ fun throwsOnInvalidTrackPoints (): Unit = runBlocking {
103
112
assertFails {
104
- parseGpx(minimalGpxBuilder(listOf (TrackPoint (" 99.0" , " -12.1" ))))
113
+ parseGpx(
114
+ minimalGpxBuilder(
115
+ listOf (TrackPoint (" 99.0" , " -12.1" ))
116
+ )
117
+ )
105
118
}
106
119
assertFails {
107
- parseGpx(minimalGpxBuilder(listOf (TrackPoint (" -11.5" , " -181.0" ))))
120
+ parseGpx(
121
+ minimalGpxBuilder(
122
+ listOf (TrackPoint (" -11.5" , " -181.0" ))
123
+ )
124
+ )
108
125
}
109
126
}
110
127
111
128
@Test
112
- fun `throws on non-gpx files` () {
129
+ fun throwsOnNonGpxFiles (): Unit = runBlocking {
113
130
val nonGpxXml = """
114
131
<xml>
115
132
</xml>
@@ -120,7 +137,7 @@ class GpxImportParseTest {
120
137
}
121
138
122
139
@Test
123
- fun `Exhausting outer before inner sequence yields no elements` () {
140
+ fun exhaustingOuterBeforeInnerFlowYieldsNoElements () = runBlocking {
124
141
val inputGpx = minimalGpxBuilder(
125
142
arrayListOf (
126
143
TrackPoint (" -39.654" , " 180" ),
@@ -129,28 +146,28 @@ class GpxImportParseTest {
129
146
)
130
147
131
148
// exhausting outer first
132
- val incorrectlyRetrievedSegments = parseGpxFile(inputGpx.byteInputStream()).toList();
149
+ val incorrectlyRetrievedSegments = parseGpxFile(inputGpx.byteInputStream()).toList()
133
150
assertEquals(
134
151
1 , incorrectlyRetrievedSegments.size,
135
152
" Exhausting outer first fails to retrieve the track segment"
136
153
)
137
154
assertEquals(
138
- 0 , incorrectlyRetrievedSegments.first().count (),
155
+ emptyList() , incorrectlyRetrievedSegments.first().toList (),
139
156
" Exhausting outer first unexpectedly yields track points"
140
157
)
141
158
142
159
// exhausting inner first
143
- val correctlyRetrievedSegments =
144
- parseGpxFile(inputGpx.byteInputStream()).flatMap { it.toList() }
160
+ val correctlyRetrievedSegments = parseGpx(inputGpx)
145
161
assertEquals(
146
- 2 , correctlyRetrievedSegments.count() ,
162
+ 2 , correctlyRetrievedSegments.size ,
147
163
" Exhausting inner first fails to retrieve track points"
148
164
)
149
165
}
150
166
151
167
@Test
152
- fun `handles additional data gracefully` () {
153
- val originalTrackPoints = arrayListOf (TrackPoint (" 88" , " -19" ))
168
+ fun handlesAdditionalDataGracefully () = runBlocking {
169
+ val originalTrackPoints =
170
+ arrayListOf (TrackPoint (" 88" , " -19" ))
154
171
155
172
val inputGpx = buildString {
156
173
append(" <gpx version='1.1' xmlns='http://www.topografix.com/GPX/1/1'>" )
@@ -167,28 +184,31 @@ class GpxImportParseTest {
167
184
append(" </gpx>" )
168
185
}
169
186
170
- assertSuccess(originalTrackPoints, parseGpx(inputGpx))
187
+ assertSuccess(
188
+ originalTrackPoints,
189
+ parseGpx(inputGpx)
190
+ )
171
191
}
172
- }
173
192
174
- private fun assertSuccess (
175
- originalTrackPoints : List <TrackPoint >,
176
- parseResult : List <LatLon >,
177
- ) {
178
- assertEquals(
179
- originalTrackPoints.size, parseResult.size,
180
- " Not all trackPoints are retrieved"
181
- )
182
- originalTrackPoints.map{it.toLatLon()}.zip(parseResult).forEach { pair ->
183
- assertEquals(
184
- expected = pair.component1().latitude,
185
- actual = pair.component2().latitude,
186
- " Latitudes don't match"
187
- )
193
+ private fun assertSuccess (
194
+ originalTrackPoints : List <TrackPoint >,
195
+ parseResult : List <LatLon >,
196
+ ) {
188
197
assertEquals(
189
- expected = pair.component1().longitude,
190
- actual = pair.component2().longitude,
191
- " Longitudes don't match"
198
+ originalTrackPoints.size, parseResult.size,
199
+ " Not all trackPoints are retrieved"
192
200
)
201
+ originalTrackPoints.map { it.toLatLon() }.zip(parseResult).forEach { pair ->
202
+ assertEquals(
203
+ expected = pair.component1().latitude,
204
+ actual = pair.component2().latitude,
205
+ " Latitudes don't match"
206
+ )
207
+ assertEquals(
208
+ expected = pair.component1().longitude,
209
+ actual = pair.component2().longitude,
210
+ " Longitudes don't match"
211
+ )
212
+ }
193
213
}
194
214
}
0 commit comments