-
Notifications
You must be signed in to change notification settings - Fork 6
/
GPXParserTests.swift
694 lines (628 loc) · 29.7 KB
/
GPXParserTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//
// GPXKit - MIT License - Copyright © 2024 Markus Müller. All rights reserved.
//
import CustomDump
import Foundation
import GPXKit
import Testing
#if canImport(FoundationXML)
import FoundationXML
#endif
@Suite
struct GPXParserTests {
func parseXML(_ xml: String) throws -> GPXTrack {
try GPXFileParser(xmlString: xml).parse().get()
}
@Test
func testImportingAnEmptyGPXString() {
#expect(throws: GPXParserError.parseError(1, 0)) {
try GPXFileParser(xmlString: "").parse().get()
}
}
@Test
func testParsingGPXFilesWithoutATrack() throws {
let sut = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
<time>2020-03-17T11:27:02Z</time>
</metadata>
</gpx>
""")
expectNoDifference("", sut.title)
expectNoDifference(nil, sut.description)
#expect(expectedDate(for: "2020-03-17T11:27:02Z") == sut.date)
expectNoDifference([], sut.trackPoints)
expectNoDifference([], sut.graph.heightMap)
expectNoDifference([], sut.graph.segments)
expectNoDifference([], sut.graph.climbs())
}
@Test
func testParsingTrackTitlesAndDescription() throws {
let result = try parseXML(
"""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
<time>2020-03-17T11:27:02Z</time>
</metadata>
<trk>
<name>Frühjahrsgeschlender ach wie schön!</name><desc>Track description</desc>
<type>1</type>
</trk>
</gpx>
"""
)
expectNoDifference("Frühjahrsgeschlender ach wie schön!", result.title)
expectNoDifference("Track description", result.description)
}
@Test
func testParsingTrackSegmentsWithoutExtensions() throws {
let result = try parseXML(testXMLWithoutExtensions)
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: expectedDate(for: "2020-07-03T13:20:50.000Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:45:48Z")
)
]
assertTracksAreEqual(GPXTrack(
date: expectedDate(for: "2020-03-18T12:39:47Z"),
title: "Haus- und Seenrunde Ausdauer",
trackPoints: expected
), result)
}
@Test
func testParsingTrackSegmentsWithDefaultExtensions() throws {
let result = try parseXML(testXMLData)
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: expectedDate(for: "2020-03-18T12:39:47Z"),
power: Measurement<UnitPower>(value: 42, unit: .watts),
cadence: 40,
heartrate: 97,
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius),
speed: Measurement(value: 1.23456, unit: .metersPerSecond)
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"),
power: Measurement<UnitPower>(value: 272, unit: .watts),
cadence: 45,
heartrate: 87,
temperature: Measurement<UnitTemperature>(value: 20.5, unit: .celsius),
speed: Measurement(value: 0.12345, unit: .metersPerSecond)
)
]
try assertTracksAreEqual(GPXTrack(
date: expectedDate(for: "2020-03-18T12:39:47Z"),
title: "Haus- und Seenrunde Ausdauer",
description: "Track description",
trackPoints: expected
), #require(result))
}
@Test
func testParsingTrackSegmentsWithNameSpacedExtensions() throws {
let result = try parseXML(namespacedTestXMLData)
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: expectedDate(for: "2020-03-18T12:39:47Z"),
power: Measurement<UnitPower>(value: 166, unit: .watts),
cadence: 99,
heartrate: 90,
temperature: Measurement<UnitTemperature>(value: 22, unit: .celsius),
speed: Measurement<UnitSpeed>(value: 1.23456, unit: .metersPerSecond)
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"),
power: Measurement<UnitPower>(value: 230, unit: .watts),
cadence: 101,
heartrate: 92,
temperature: Measurement<UnitTemperature>(value: 21, unit: .celsius),
speed: Measurement<UnitSpeed>(value: 0.123456, unit: .metersPerSecond)
)
]
assertTracksAreEqual(GPXTrack(
date: expectedDate(for: "2020-03-18T12:39:47Z"),
title: "Haus- und Seenrunde Ausdauer",
description: "Track description",
trackPoints: expected
), result)
}
@Test
func testParsingTrackSegmentsWithoutTimeHaveANilDate() throws {
let result = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
</metadata>
<trk>
<name>Haus- und Seenrunde Ausdauer</name>
<type>1</type>
<trkseg>
<trkpt lat="51.2760600" lon="12.3769500">
<ele>114.2</ele>
</trkpt>
<trkpt lat="51.2760420" lon="12.3769760">
<ele>114.0</ele>
</trkpt>
</trkseg>
</trk>
</gpx>
""")
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: nil
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: nil
)
]
try assertTracksAreEqual(GPXTrack(
date: nil,
title: "Haus- und Seenrunde Ausdauer",
trackPoints: expected
), #require(result))
}
@Test
func testParsingTrackWithoutElevation() throws {
let result = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
</metadata>
<trk>
<name>Haus- und Seenrunde Ausdauer</name>
<type>1</type>
<trkseg>
<trkpt lat="51.2760600" lon="12.3769500"></trkpt>
<trkpt lat="51.2760420" lon="12.3769760"></trkpt>
</trkseg>
</trk>
</gpx>
""")
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 0),
date: nil
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 0),
date: nil
)
]
try assertTracksAreEqual(GPXTrack(
date: nil,
title: "Haus- und Seenrunde Ausdauer",
trackPoints: expected
), result)
}
@Test
func testTrackPointsDateWithFraction() throws {
let result = try parseXML(sampleGPX)
let date = try #require(result.trackPoints.first?.date)
expectNoDifference(1351121380, date.timeIntervalSince1970)
}
@Test
func testTrackLength() throws {
let result = try parseXML(sampleGPX)
let distance = result.graph.distance
let elevation = result.graph.elevationGain
#expect(distance.isApproximatelyEqual(to: 3100.5625, absoluteTolerance: 10))
#expect(elevation.isApproximatelyEqual(to: 115.19, absoluteTolerance: 0.1))
}
@Test
func testTracksWithoutElevationInTheGPXHaveAnElevationOfZero() throws {
let result = try parseXML(given(points: [.leipzig, .postPlatz, .dehner]))
#expect(result.graph.elevationGain == 0)
}
@Test
func testItInterpolatesElevationGapsWithElevationAtStartEndEndOfTheTrack() throws {
// 0m: 100, 250m: nil, 500m: 120
let start = TestGPXPoint.leipzig.with { $0.elevation = 100 }
let points: [TestGPXPoint] = [
start,
start.offset(east: 250).with { $0.elevation = nil },
start.offset(east: 400).with { $0.elevation = nil },
start.offset(east: 450).with { $0.elevation = nil },
start.offset(east: 500).with { $0.elevation = 120 },
start.offset(east: 600).with { $0.elevation = nil },
start.offset(east: 700).with { $0.elevation = nil },
start.offset(east: 800).with { $0.elevation = 300 }
]
let result = try parseXML(given(points: points))
let expected: [Coordinate] = [
Coordinate(points[0]),
Coordinate(points[1].with { $0.elevation = expectedElevation(
start: points[0],
end: points[4],
distanceFromStart: points[0].distance(to: points[1])
) }),
Coordinate(points[2].with { $0.elevation = expectedElevation(
start: points[0],
end: points[4],
distanceFromStart: points[0].distance(to: points[2])
) }),
Coordinate(points[3].with { $0.elevation = expectedElevation(
start: points[0],
end: points[4],
distanceFromStart: points[0].distance(to: points[3])
) }),
Coordinate(points[4]),
Coordinate(points[5].with { $0.elevation = expectedElevation(
start: points[4],
end: points[7],
distanceFromStart: points[4].distance(to: points[5])
) }),
Coordinate(points[6].with { $0.elevation = expectedElevation(
start: points[4],
end: points[7],
distanceFromStart: points[4].distance(to: points[6])
) }),
Coordinate(points[7])
]
expectNoDifference(expected, result.trackPoints.map(\.coordinate))
}
@Test
func testItTakesTheFirstElevationWhenTheTrackStartsWithNoElevation() throws {
let start = TestGPXPoint.leipzig.with { $0.elevation = nil }
let points: [TestGPXPoint] = [
start,
start.offset(east: 250).with { $0.elevation = nil },
start.offset(east: 400).with { $0.elevation = nil },
start.offset(east: 500).with { $0.elevation = 120 }
]
let result = try parseXML(given(points: points))
let expected: [Coordinate] = [
Coordinate(points[0].with { $0.elevation = 120 }),
Coordinate(points[1].with { $0.elevation = 120 }),
Coordinate(points[2].with { $0.elevation = 120 }),
Coordinate(points[3])
]
expectNoDifference(expected, result.trackPoints.map(\.coordinate))
}
@Test
func testItTakesTheLastElevationWhenTheTrackEndsWithNoElevation() throws {
let start = TestGPXPoint.leipzig.with { $0.elevation = 170 }
let points: [TestGPXPoint] = [
start,
start.offset(east: 250).with { $0.elevation = nil },
start.offset(east: 400),
start.offset(east: 500).with { $0.elevation = nil }
]
let result = try parseXML(given(points: points))
let expected: [Coordinate] = [
Coordinate(points[0]),
Coordinate(points[1].with { $0.elevation = expectedElevation(
start: points[0],
end: points[2],
distanceFromStart: points[0].distance(to: points[1])
) }),
Coordinate(points[2]),
Coordinate(points[3].with { $0.elevation = 170 })
]
expectNoDifference(expected, result.trackPoints.map(\.coordinate))
}
@Test
func testEmptySegmentIsEmptyTrackPoints() throws {
let result = try parseXML(given(points: []))
expectNoDifference([], result.trackPoints)
}
@Test
func testParsingKeywords() throws {
let result = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
<keywords>one two three \n \t four </keywords>
</metadata>
<trk>
<name>Haus- und Seenrunde Ausdauer</name>
<type>1</type>
<trkseg>
<trkpt lat="51.2760600" lon="12.3769500">
<ele>114.2</ele>
</trkpt>
<trkpt lat="51.2760420" lon="12.3769760">
<ele>114.0</ele>
</trkpt>
</trkseg>
</trk>
</gpx>
""")
let sut = try #require(result)
expectNoDifference(["one", "two", "three", "four"], sut.keywords)
}
@Test
func testParsingAFileWithoutWaypointDefinitionsHasEmptyWaypoints() throws {
let sut = try parseXML(testXMLData)
#expect(sut.waypoints == nil)
}
@Test
func testParsingWaypointAttributes() throws {
let sut = try parseXML(testXMLDataContainingWaypoint)
let waypointStart = Waypoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500),
date: expectedDate(for: "2020-03-18T12:39:47Z"),
name: "Start",
comment: "start comment",
description: "This is the start"
)
let waypointFinish = Waypoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760),
date: expectedDate(for: "2020-03-18T12:39:48Z"),
name: "Finish",
comment: "finish comment",
description: "This is the finish"
)
expectNoDifference([waypointStart, waypointFinish], sut.waypoints)
}
@Test
func testParsingRouteFiles() throws {
let sut = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<rte>
<name>Haus- und Seenrunde Ausdauer</name>
<rtept lat="51.2760600" lon="12.3769500">
<ele>114.2</ele>
</rtept>
<rtept lat="51.2760420" lon="12.3769760">
<ele>114.0</ele>
</rtept>
</rte>
</gpx>
""")
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: nil
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114),
date: nil
)
]
try assertTracksAreEqual(GPXTrack(
date: nil,
title: "Haus- und Seenrunde Ausdauer",
trackPoints: expected
), sut)
}
@Test
func testParsingTrackWithoutNameHaveAnEmptyName() throws {
let result = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
</metadata>
<trk>
<type>1</type>
<trkseg>
<trkpt lat="51.2760600" lon="12.3769500">
<ele>114.2</ele>
</trkpt>
<trkpt lat="51.2760420" lon="12.3769760">
<ele>114.0</ele>
</trkpt>
</trkseg>
</trk>
</gpx>
""")
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: nil
),
TrackPoint(
coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: nil
)
]
try assertTracksAreEqual(
GPXTrack(
date: nil,
title: "",
trackPoints: expected
),
#require(result)
)
}
@Test
func testParsingWaypointWithEmptyTrack() throws {
let input = """
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2" version="1.1" creator="gpxgenerator.com">
<metadata>
<name>gpxgenerator_path</name>
<author>
<name>gpx.studio</name>
<link href="https://gpx.studio"></link>
</author>
</metadata>
<trk></trk>
<wpt lat="53.060632820504345" lon="5.6932974383264616">
<ele>8.4</ele>
<sym> </sym>
</wpt>
<wpt lat="53.06485377614443" lon="5.702670398232679">
<ele>8.3</ele>
<sym> </sym>
</wpt>
</gpx>
"""
let sut = GPXFileParser(xmlString: input)
let track = try sut.parse().get()
expectNoDifference([], track.trackPoints)
expectNoDifference([], track.graph.heightMap)
expectNoDifference([], track.graph.segments)
expectNoDifference(.zero, track.graph.distance)
expectNoDifference(.zero, track.graph.elevationGain)
expectNoDifference([
Waypoint(coordinate: .init(latitude: 53.060632820504345, longitude: 5.6932974383264616, elevation: 8.4)),
Waypoint(coordinate: .init(latitude: 53.06485377614443, longitude: 5.702670398232679, elevation: 8.3))
], track.waypoints)
}
@Test
func testParsingWaypointWithoutTrack() throws {
let input = """
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2" version="1.1" creator="gpxgenerator.com">
<metadata>
<name>gpxgenerator_path</name>
<author>
<name>gpx.studio</name>
<link href="https://gpx.studio"></link>
</author>
</metadata>
<wpt lat="53.060632820504345" lon="5.6932974383264616">
<ele>8.4</ele>
<sym> </sym>
</wpt>
<wpt lat="53.06485377614443" lon="5.702670398232679">
<ele>8.3</ele>
<sym> </sym>
</wpt>
</gpx>
"""
let sut = GPXFileParser(xmlString: input)
let track = try sut.parse().get()
expectNoDifference([], track.trackPoints)
expectNoDifference([], track.graph.heightMap)
expectNoDifference([], track.graph.segments)
expectNoDifference(.zero, track.graph.distance)
expectNoDifference(.zero, track.graph.elevationGain)
expectNoDifference([
Waypoint(coordinate: .init(latitude: 53.060632820504345, longitude: 5.6932974383264616, elevation: 8.4)),
Waypoint(coordinate: .init(latitude: 53.06485377614443, longitude: 5.702670398232679, elevation: 8.3))
], track.waypoints)
}
@Test
func testParsingTrackWithMultipleSegments() throws {
let result = try parseXML("""
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="StravaGPX"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
</metadata>
<trk>
<type>1</type>
<trkseg>
<trkpt lat="53.0736462" lon="13.1756965">
<ele>51.71003342</ele>
<time>2023-05-20T08:20:07Z</time>
</trkpt>
<trkpt lat="53.0736242" lon="13.1757405">
<ele>51.7000351</ele>
<time>2023-05-20T08:20:08Z</time>
</trkpt>
<trkpt lat="53.0735992" lon="13.1757855">
<ele>51.7000351</ele>
<time>2023-05-20T08:20:09Z</time>
</trkpt>
<trkpt lat="53.0735793" lon="13.1758284">
<ele>51.67003632</ele>
<time>2023-05-20T08:20:10Z</time>
</trkpt>
<trkpt lat="53.0735543" lon="13.1758994">
<ele>51.66003799</ele>
<time>2023-05-20T08:20:11Z</time>
</trkpt>
</trkseg>
<trkseg>
<trkpt lat="53.186896" lon="13.132096">
<ele>54.38999939</ele>
<time>2023-05-20T10:35:19Z</time>
</trkpt>
<trkpt lat="53.186909" lon="13.132093">
<ele>54.34999847</ele>
<time>2023-05-20T10:35:20Z</time>
</trkpt>
<trkpt lat="53.1869289" lon="13.1320901">
<ele>54.22999954</ele>
<time>2023-05-20T10:35:21Z</time>
</trkpt>
<trkpt lat="53.1869399" lon="13.1320881">
<ele>54.16999817</ele>
<time>2023-05-20T10:35:22Z</time>
</trkpt>
<trkpt lat="53.1869479" lon="13.1320822">
<ele>54.13999939</ele>
<time>2023-05-20T10:35:23Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
""")
let expected = [
TrackPoint(
coordinate: Coordinate(latitude: 53.0736462, longitude: 13.1756965, elevation: 51.71003342),
date: expectedDate(for: "2023-05-20T08:20:07Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.0736242, longitude: 13.1757405, elevation: 51.7000351),
date: expectedDate(for: "2023-05-20T08:20:08Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.0735992, longitude: 13.1757855, elevation: 51.7000351),
date: expectedDate(for: "2023-05-20T08:20:09Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.0735793, longitude: 13.1758284, elevation: 51.67003632),
date: expectedDate(for: "2023-05-20T08:20:10Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.0735543, longitude: 13.1758994, elevation: 51.66003799),
date: expectedDate(for: "2023-05-20T08:20:11Z")
),
// 2nd segment
TrackPoint(
coordinate: Coordinate(latitude: 53.186896, longitude: 13.132096, elevation: 54.38999939),
date: expectedDate(for: "2023-05-20T10:35:19Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.186909, longitude: 13.132093, elevation: 54.34999847),
date: expectedDate(for: "2023-05-20T10:35:20Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.1869289, longitude: 13.1320901, elevation: 54.22999954),
date: expectedDate(for: "2023-05-20T10:35:21Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.1869399, longitude: 13.1320881, elevation: 54.16999817),
date: expectedDate(for: "2023-05-20T10:35:22Z")
),
TrackPoint(
coordinate: Coordinate(latitude: 53.1869479, longitude: 13.1320822, elevation: 54.13999939),
date: expectedDate(for: "2023-05-20T10:35:23Z")
)
]
try assertTracksAreEqual(
GPXTrack(
date: nil,
title: "",
trackPoints: expected,
segments: [
.init(
range: 0 ..< 5, distance: expected[0 ..< 5].expectedDistance()
),
.init(
range: 5 ..< 10, distance: expected[5 ..< 10].expectedDistance()
)
]
),
result
)
}
}