From 297ab19167c9abdf5e889a5b3fb93bee834267e7 Mon Sep 17 00:00:00 2001 From: jprochazk Date: Thu, 12 Dec 2024 12:01:37 +0100 Subject: [PATCH] add test for python version parsing --- crates/store/re_log_types/src/lib.rs | 72 ++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/crates/store/re_log_types/src/lib.rs b/crates/store/re_log_types/src/lib.rs index 26ea4105ca6d..57ea6d7167ca 100644 --- a/crates/store/re_log_types/src/lib.rs +++ b/crates/store/re_log_types/src/lib.rs @@ -411,26 +411,26 @@ impl std::str::FromStr for PythonVersion { type Err = PythonVersionParseError; fn from_str(s: &str) -> Result { - let (major, rest) = s - .split_once('.') - .ok_or(PythonVersionParseError::MissingMajor)?; - if major.is_empty() { + if s.is_empty() { return Err(PythonVersionParseError::MissingMajor); } - let (minor, rest) = rest + let (major, rest) = s .split_once('.') .ok_or(PythonVersionParseError::MissingMinor)?; - if minor.is_empty() { + if rest.is_empty() { return Err(PythonVersionParseError::MissingMinor); } + let (minor, rest) = rest + .split_once('.') + .ok_or(PythonVersionParseError::MissingPatch)?; + if rest.is_empty() { + return Err(PythonVersionParseError::MissingPatch); + } let pos = rest.bytes().position(|v| !v.is_ascii_digit()); let (patch, suffix) = match pos { Some(pos) => rest.split_at(pos), None => (rest, ""), }; - if patch.is_empty() { - return Err(PythonVersionParseError::MissingPatch); - } Ok(Self { major: major @@ -634,3 +634,57 @@ pub fn build_frame_nr(frame_nr: impl TryInto) -> (Timeline, TimeInt) { frame_nr.try_into().unwrap_or(TimeInt::MIN), ) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_python_version() { + macro_rules! assert_parse_err { + ($input:literal, $expected:pat) => { + let actual = $input.parse::(); + + assert!( + matches!(actual, Err($expected)), + "actual: {actual:?}, expected: {}", + stringify!($expected) + ); + }; + } + + macro_rules! assert_parse_ok { + ($input:literal, $expected:expr) => { + let actual = $input.parse::().expect("failed to parse"); + assert_eq!(actual, $expected); + }; + } + + assert_parse_err!("", PythonVersionParseError::MissingMajor); + assert_parse_err!("3", PythonVersionParseError::MissingMinor); + assert_parse_err!("3.", PythonVersionParseError::MissingMinor); + assert_parse_err!("3.11", PythonVersionParseError::MissingPatch); + assert_parse_err!("3.11.", PythonVersionParseError::MissingPatch); + assert_parse_err!("a.11.0", PythonVersionParseError::InvalidMajor(_)); + assert_parse_err!("3.b.0", PythonVersionParseError::InvalidMinor(_)); + assert_parse_err!("3.11.c", PythonVersionParseError::InvalidPatch(_)); + assert_parse_ok!( + "3.11.0", + PythonVersion { + major: 3, + minor: 11, + patch: 0, + suffix: "".to_owned(), + } + ); + assert_parse_ok!( + "3.11.0a1", + PythonVersion { + major: 3, + minor: 11, + patch: 0, + suffix: "a1".to_owned(), + } + ); + } +}