Skip to content

Commit

Permalink
Allow empty elevation tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Crowder committed May 27, 2022
1 parent 73d8dcc commit 9cd5e4b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/parser/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,27 @@ mod tests {
}

#[test]
fn consume_no_body() {
fn consume_no_body_with_err() {
// must have string content
let result = consume!("<foo></foo>", GpxVersion::Gpx11, "foo", false);

assert!(result.is_err());
}

#[test]
fn consume_no_body_via_complete_tag() {
let result = consume!("<foo></foo>", GpxVersion::Gpx11, "foo", true);

assert!(result.is_ok());
}

#[test]
fn consume_no_body_via_self_closing_tag() {
let result = consume!("<foo/>", GpxVersion::Gpx11, "foo", true);

assert!(result.is_ok());
}

#[test]
fn consume_different_ending_tag() {
// this is just illegal
Expand Down
7 changes: 6 additions & 1 deletion src/parser/waypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ pub fn consume<R: Read>(context: &mut Context<R>, tagname: &'static str) -> GpxR
match name.local_name.as_ref() {
"ele" => {
// Cast the elevation to an f64, from a string.
waypoint.elevation = Some(string::consume(context, "ele", false)?.parse()?)
// empty elevation tags will be parsed as 0.00
waypoint.elevation = Some(
string::consume(context, "ele", true)?
.parse()
.unwrap_or_default(),
)
}
"speed" if context.version == GpxVersion::Gpx10 => {
// Speed is from GPX 1.0
Expand Down
27 changes: 27 additions & 0 deletions tests/fixtures/wahoo_example.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="http://ridewithgps.com/">
<metadata>
<name>04/24/22</name>
<link href="https://ridewithgps.com/trips/1">
<text>04/24/22</text>
</link>
<time>2022-04-24T13:54:33Z</time>
</metadata>
<trk>
<name>04/24/22</name>
<trkseg>
<trkpt lat="45.454350" lon="-121.933136">
<ele/>
<time>2022-04-24T20:54:33Z</time>
</trkpt>
<trkpt lat="45.454350" lon="-121.933136">
<ele/>
<time>2022-04-24T20:54:34Z</time>
</trkpt>
<trkpt lat="45.454350" lon="-121.933136">
<ele/>
<time>2022-04-24T20:54:35Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
21 changes: 21 additions & 0 deletions tests/gpx_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ fn gpx_reader_read_test_gpsies() {
assert_eq!(points[2].elevation, Some(305.0));
}

#[test]
fn gpx_reader_read_test_empty_elevation() {
let file = File::open("tests/fixtures/wahoo_example.gpx").unwrap();
let reader = BufReader::new(file);

let result = read(reader);
assert!(result.is_ok());
let res = result.unwrap();

// Test for every single point in the file.
for track in &res.tracks {
for segment in &track.segments {
for point in &segment.points {
// Elevation should default to 0.00
let elevation = point.elevation.unwrap();
assert!(elevation == 0.00);
}
}
}
}

#[test]
fn gpx_reader_read_test_garmin_activity() {
let file = File::open("tests/fixtures/garmin-activity.gpx").unwrap();
Expand Down

0 comments on commit 9cd5e4b

Please sign in to comment.