From 9d185e51508546a65bc235db26e3b72cd66d13b9 Mon Sep 17 00:00:00 2001 From: Troy Kohler Date: Sat, 24 Aug 2024 20:00:37 +0200 Subject: [PATCH 1/5] increase test coverage for src/sentences/utils.rs --- src/sentences/utils.rs | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/sentences/utils.rs b/src/sentences/utils.rs index 5ef6b5e..3fe4d8c 100644 --- a/src/sentences/utils.rs +++ b/src/sentences/utils.rs @@ -18,6 +18,7 @@ use num_traits::float::FloatCore; use crate::Error; +// has tests pub fn parse_hms(i: &str) -> IResult<&str, NaiveTime> { map_res( tuple(( @@ -57,6 +58,7 @@ const MILLISECS_PER_MINUTE: u32 = 60000; const MILLISECS_PER_HOUR: u32 = 3600000; /// Parses values like `125619,` and `125619.5,` to [`Duration`] +// has tests pub fn parse_duration_hms(i: &str) -> IResult<&str, Duration> { map_res( tuple(( @@ -134,10 +136,12 @@ pub fn do_parse_magnetic_variation(i: &str) -> IResult<&str, f32> { Ok((i, variation_deg)) } +// has tests pub(crate) fn parse_lat_lon(i: &str) -> IResult<&str, Option<(f64, f64)>> { alt((map(tag(",,,"), |_| None), map(do_parse_lat_lon, Some)))(i) } +// has tests pub(crate) fn parse_magnetic_variation(i: &str) -> IResult<&str, Option> { alt(( map(tag(","), |_| None), @@ -145,6 +149,7 @@ pub(crate) fn parse_magnetic_variation(i: &str) -> IResult<&str, Option> { ))(i) } +// has tests pub(crate) fn parse_date(i: &str) -> IResult<&str, NaiveDate> { map_res( tuple(( @@ -181,14 +186,17 @@ pub(crate) fn parse_num(data: &str) -> Result data.parse::().map_err(|_| "parse of number failed") } +// added pub(crate) fn parse_float_num(input: &str) -> Result { str::parse::(input).map_err(|_| "parse of float number failed") } +// added pub(crate) fn number(i: &str) -> IResult<&str, T> { map_res(digit1, parse_num)(i) } +// added pub(crate) fn parse_number_in_range( i: &str, lower_bound: T, @@ -210,6 +218,7 @@ where /// # Errors /// /// If `&str` length > `MAX_LEN` it returns a [`Error::ParameterLength`] error. +// added pub(crate) fn array_string( string: &str, ) -> Result, Error> { @@ -221,6 +230,8 @@ pub(crate) fn array_string( #[cfg(test)] mod tests { + use core::result; + use approx::assert_relative_eq; use super::*; @@ -315,4 +326,70 @@ mod tests { let result = parse_magnetic_variation("12,Q"); assert!(result.is_err()); } + + #[test] + fn test_parse_array_string() { + let result = array_string::<5>("12345"); + assert!(result.is_ok()); + let result = array_string::<5>("123456"); + assert!(result.is_err()); + } + + #[test] + fn test_parse_number_in_range() { + let result = parse_number_in_range::("12", 10, 20); + assert!(result.is_ok()); + let result = parse_number_in_range::("9", 10, 20); + assert!(result.is_err()); + let result = parse_number_in_range::("21", 10, 20); + assert!(result.is_err()); + } + + #[test] + fn test_parse_number() { + let result = parse_num::("12"); + assert!(result.is_ok()); + let result = parse_num::("12.5"); + assert!(result.is_err()); + } + + #[test] + fn test_parse_float_num() { + let result = parse_float_num::("12.5"); + assert!(result.is_ok()); + let result = parse_float_num::("12"); + assert!(result.is_ok()); + } + + #[test] + fn test_parse_lat_lon() { + let (_, lat_lon) = parse_lat_lon("4807.038,N,01131.324,E").unwrap(); + assert!(lat_lon.is_some()); + let (_, lat_lon) = parse_lat_lon(",,,,").unwrap(); + assert!(lat_lon.is_none()); + + let lat_lon = parse_lat_lon("51.5074,0.1278"); + assert_eq!(lat_lon.is_err(), true); + + let lat_lon = parse_lat_lon("1234.567,N,09876.543,W"); + assert_eq!(lat_lon.is_ok(), true); + + let lat_lon = parse_lat_lon("0000.000,S,00000.000,E"); + assert_eq!(lat_lon.is_ok(), true); + + let lat_lon = parse_lat_lon("1234.567,S,09876.543,E"); + assert_eq!(lat_lon.is_ok(), true); + + let lat_lon = parse_lat_lon("1234.567,S,09876.543,E"); + assert!(lat_lon.is_ok()); + + let lat_lon = parse_lat_lon("40.7128,"); + assert!(lat_lon.is_err()); + + let lat_lon = parse_lat_lon(", -74.0060"); + assert!(lat_lon.is_err()); + + let lat_lon = parse_lat_lon("abc,def"); + assert!(lat_lon.is_err()); + } } From 6d7497a4a38dbdc8cd44f4a14d880585b4c6c014 Mon Sep 17 00:00:00 2001 From: Troy Kohler Date: Sat, 24 Aug 2024 20:03:32 +0200 Subject: [PATCH 2/5] clean out comments --- src/sentences/utils.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/sentences/utils.rs b/src/sentences/utils.rs index 3fe4d8c..7fa621c 100644 --- a/src/sentences/utils.rs +++ b/src/sentences/utils.rs @@ -18,7 +18,6 @@ use num_traits::float::FloatCore; use crate::Error; -// has tests pub fn parse_hms(i: &str) -> IResult<&str, NaiveTime> { map_res( tuple(( @@ -58,7 +57,7 @@ const MILLISECS_PER_MINUTE: u32 = 60000; const MILLISECS_PER_HOUR: u32 = 3600000; /// Parses values like `125619,` and `125619.5,` to [`Duration`] -// has tests + pub fn parse_duration_hms(i: &str) -> IResult<&str, Duration> { map_res( tuple(( @@ -136,12 +135,10 @@ pub fn do_parse_magnetic_variation(i: &str) -> IResult<&str, f32> { Ok((i, variation_deg)) } -// has tests pub(crate) fn parse_lat_lon(i: &str) -> IResult<&str, Option<(f64, f64)>> { alt((map(tag(",,,"), |_| None), map(do_parse_lat_lon, Some)))(i) } -// has tests pub(crate) fn parse_magnetic_variation(i: &str) -> IResult<&str, Option> { alt(( map(tag(","), |_| None), @@ -149,7 +146,6 @@ pub(crate) fn parse_magnetic_variation(i: &str) -> IResult<&str, Option> { ))(i) } -// has tests pub(crate) fn parse_date(i: &str) -> IResult<&str, NaiveDate> { map_res( tuple(( @@ -186,17 +182,14 @@ pub(crate) fn parse_num(data: &str) -> Result data.parse::().map_err(|_| "parse of number failed") } -// added pub(crate) fn parse_float_num(input: &str) -> Result { str::parse::(input).map_err(|_| "parse of float number failed") } -// added pub(crate) fn number(i: &str) -> IResult<&str, T> { map_res(digit1, parse_num)(i) } -// added pub(crate) fn parse_number_in_range( i: &str, lower_bound: T, @@ -218,7 +211,6 @@ where /// # Errors /// /// If `&str` length > `MAX_LEN` it returns a [`Error::ParameterLength`] error. -// added pub(crate) fn array_string( string: &str, ) -> Result, Error> { @@ -230,7 +222,6 @@ pub(crate) fn array_string( #[cfg(test)] mod tests { - use core::result; use approx::assert_relative_eq; From 869d0570bbb2f329ef7e961bda2d1860950caf1e Mon Sep 17 00:00:00 2001 From: Troy Kohler Date: Sat, 24 Aug 2024 20:06:06 +0200 Subject: [PATCH 3/5] trim whitespaces --- src/sentences/utils.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/sentences/utils.rs b/src/sentences/utils.rs index 7fa621c..6455add 100644 --- a/src/sentences/utils.rs +++ b/src/sentences/utils.rs @@ -57,7 +57,6 @@ const MILLISECS_PER_MINUTE: u32 = 60000; const MILLISECS_PER_HOUR: u32 = 3600000; /// Parses values like `125619,` and `125619.5,` to [`Duration`] - pub fn parse_duration_hms(i: &str) -> IResult<&str, Duration> { map_res( tuple(( @@ -222,7 +221,6 @@ pub(crate) fn array_string( #[cfg(test)] mod tests { - use approx::assert_relative_eq; use super::*; From 2061f9ccebc22c8ee0706c0d7d6951696a07f793 Mon Sep 17 00:00:00 2001 From: Troy Kohler Date: Sat, 21 Sep 2024 21:30:54 +0200 Subject: [PATCH 4/5] assert errors explicitly --- src/sentences/utils.rs | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/sentences/utils.rs b/src/sentences/utils.rs index 495c9f8..5ffbcbf 100644 --- a/src/sentences/utils.rs +++ b/src/sentences/utils.rs @@ -222,6 +222,9 @@ pub(crate) fn array_string( #[cfg(test)] mod tests { + use core::error; + use core::result; + use approx::assert_relative_eq; use super::*; @@ -321,26 +324,39 @@ mod tests { fn test_parse_array_string() { let result = array_string::<5>("12345"); assert!(result.is_ok()); + + let err_expected = Error::ParameterLength { + max_length: 5, + parameter_length: 6, + }; let result = array_string::<5>("123456"); - assert!(result.is_err()); + assert_eq!(result, Err(err_expected)); } #[test] fn test_parse_number_in_range() { let result = parse_number_in_range::("12", 10, 20); assert!(result.is_ok()); + let result = parse_number_in_range::("9", 10, 20); - assert!(result.is_err()); + let nom_error_expected = nom::error::Error::new("9", nom::error::ErrorKind::MapRes); + let err = result.unwrap_err(); + assert_eq!(err, nom::Err::Error(nom_error_expected)); + let result = parse_number_in_range::("21", 10, 20); - assert!(result.is_err()); + let nom_error_expected = nom::error::Error::new("21", nom::error::ErrorKind::MapRes); + let err = result.unwrap_err(); + assert_eq!(err, nom::Err::Error(nom_error_expected)); } #[test] fn test_parse_number() { let result = parse_num::("12"); assert!(result.is_ok()); + let result = parse_num::("12.5"); - assert!(result.is_err()); + let err_expected = "parse of number failed"; + assert_eq!(result, Err(err_expected)); } #[test] @@ -349,6 +365,10 @@ mod tests { assert!(result.is_ok()); let result = parse_float_num::("12"); assert!(result.is_ok()); + + let result = parse_float_num::("12.5.5"); + let err_expected = "parse of float number failed"; + assert_eq!(result, Err(err_expected)); } #[test] @@ -359,7 +379,9 @@ mod tests { assert!(lat_lon.is_none()); let lat_lon = parse_lat_lon("51.5074,0.1278"); - assert_eq!(lat_lon.is_err(), true); + let err_expected = nom::error::Error::new("0.1278", nom::error::ErrorKind::OneOf); + let err = lat_lon.unwrap_err(); + assert_eq!(err, nom::Err::Error(err_expected)); let lat_lon = parse_lat_lon("1234.567,N,09876.543,W"); assert_eq!(lat_lon.is_ok(), true); @@ -377,9 +399,13 @@ mod tests { assert!(lat_lon.is_err()); let lat_lon = parse_lat_lon(", -74.0060"); - assert!(lat_lon.is_err()); + let err_expected = nom::error::Error::new(", -74.0060", nom::error::ErrorKind::MapRes); + let err = lat_lon.unwrap_err(); + assert_eq!(err, nom::Err::Error(err_expected)); let lat_lon = parse_lat_lon("abc,def"); - assert!(lat_lon.is_err()); + let err_expected = nom::error::Error::new("abc,def", nom::error::ErrorKind::MapRes); + let err = lat_lon.unwrap_err(); + assert_eq!(err, nom::Err::Error(err_expected)); } } From 4a9bb7b9c4876c441c6952a6d8ff3c1be535cde6 Mon Sep 17 00:00:00 2001 From: Troy Kohler Date: Sat, 21 Sep 2024 21:32:11 +0200 Subject: [PATCH 5/5] remove unused imports --- src/sentences/utils.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/sentences/utils.rs b/src/sentences/utils.rs index 5ffbcbf..d28d911 100644 --- a/src/sentences/utils.rs +++ b/src/sentences/utils.rs @@ -222,8 +222,6 @@ pub(crate) fn array_string( #[cfg(test)] mod tests { - use core::error; - use core::result; use approx::assert_relative_eq;