107 lines
3.7 KiB
Rust
107 lines
3.7 KiB
Rust
// Integration tests for the aniker-gps library
|
|
use aniker_gps::{parse_gga, GpsError, Position};
|
|
|
|
#[test]
|
|
fn test_valid_gga_sentence() {
|
|
// Test with a valid GGA sentence
|
|
let mut gga =
|
|
"$GPGGA,210230,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A".to_string();
|
|
let position = parse_gga(&mut gga).unwrap();
|
|
|
|
// Expected values:
|
|
// Latitude: 38 degrees + 55.4487/60 = 38.924145
|
|
// Longitude: 94 degrees + 46.0071/60 = -94.766785 (negative because W)
|
|
// Altitude: 370.5
|
|
// Time: 21:02:30.00
|
|
assert!((position.lat - 38.924145).abs() < 0.000001);
|
|
assert!((position.long - (-94.766785)).abs() < 0.000001);
|
|
assert!((position.alt - 370.5).abs() < 0.000001);
|
|
assert_eq!(position.time.hours, 21);
|
|
assert_eq!(position.time.minutes, 2);
|
|
assert_eq!(position.time.seconds, 30);
|
|
assert_eq!(position.time.centiseconds, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_sentence_type() {
|
|
// Test with a non-GGA sentence (RMC in this case)
|
|
let mut rmc =
|
|
"$GPRMC,210230,A,3855.4487,N,09446.0071,W,0.0,076.2,130495,003.8,E*69".to_string();
|
|
let result = parse_gga(&mut rmc);
|
|
|
|
// Should return an error due to wrong message type
|
|
assert!(result.is_err());
|
|
assert_eq!(result.unwrap_err(), GpsError::InvalidMessageType);
|
|
}
|
|
|
|
#[test]
|
|
fn test_short_sentence() {
|
|
// Test with a sentence that has fewer fields than expected
|
|
let mut short_gga = "$GPGGA,210230,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M".to_string();
|
|
let result = parse_gga(&mut short_gga);
|
|
|
|
// Should return an error due to wrong sentence length
|
|
assert!(result.is_err());
|
|
assert_eq!(result.unwrap_err(), GpsError::InvalidLength);
|
|
}
|
|
|
|
#[test]
|
|
fn test_timestamp_parsing() {
|
|
// Test with a GGA sentence with a specific timestamp
|
|
let mut gga =
|
|
"$GPGGA,123456,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A".to_string();
|
|
let position = parse_gga(&mut gga).unwrap();
|
|
|
|
// Check that the timestamp is correctly parsed
|
|
assert_eq!(position.time.hours, 12);
|
|
assert_eq!(position.time.minutes, 34);
|
|
assert_eq!(position.time.seconds, 56);
|
|
assert_eq!(position.time.centiseconds, 0);
|
|
|
|
// Test with a different timestamp including centiseconds
|
|
let mut gga2 =
|
|
"$GPGGA,235959.50,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A".to_string();
|
|
let position2 = parse_gga(&mut gga2).unwrap();
|
|
|
|
// Check that the timestamp is correctly parsed
|
|
assert_eq!(position2.time.hours, 23);
|
|
assert_eq!(position2.time.minutes, 59);
|
|
assert_eq!(position2.time.seconds, 59);
|
|
assert_eq!(position2.time.centiseconds, 50);
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_timestamp() {
|
|
// Test with an invalid timestamp format
|
|
let mut gga =
|
|
"$GPGGA,2359.50,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A".to_string();
|
|
let result = parse_gga(&mut gga);
|
|
|
|
// Should return a parse error for the timestamp field
|
|
assert!(result.is_err());
|
|
match result.unwrap_err() {
|
|
GpsError::ParseError(msg) => {
|
|
assert!(msg.len() > 0);
|
|
}
|
|
_ => panic!("Expected ParseError for invalid timestamp"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_altitude() {
|
|
// Test with invalid altitude value
|
|
let mut gga = "$GPGGA,21030,3855.4487,N,09446.0071,W,1,07,1.1,ABC,M,-29.5,M,,*7A".to_string();
|
|
let result = parse_gga(&mut gga);
|
|
|
|
// Should return a parse error for the altitude field
|
|
assert!(result.is_err());
|
|
match result.unwrap_err() {
|
|
GpsError::ParseError(msg) => {
|
|
// The actual error message might be different, so we'll just check that it's a ParseError
|
|
// without checking the specific message content
|
|
assert!(msg.len() > 0);
|
|
}
|
|
_ => panic!("Expected ParseError for invalid altitude"),
|
|
}
|
|
}
|