Skip to content

Commit

Permalink
add test for python version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed Dec 12, 2024
1 parent ec1c6b6 commit 297ab19
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions crates/store/re_log_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,26 @@ impl std::str::FromStr for PythonVersion {
type Err = PythonVersionParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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
Expand Down Expand Up @@ -634,3 +634,57 @@ pub fn build_frame_nr(frame_nr: impl TryInto<TimeInt>) -> (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::<PythonVersion>();

assert!(
matches!(actual, Err($expected)),
"actual: {actual:?}, expected: {}",
stringify!($expected)
);
};
}

macro_rules! assert_parse_ok {
($input:literal, $expected:expr) => {
let actual = $input.parse::<PythonVersion>().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(),
}
);
}
}

0 comments on commit 297ab19

Please sign in to comment.