From 00b606947f3adf5540f52f1ff99b3cc8d2f991d3 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Wed, 10 Jan 2024 16:50:32 -0500 Subject: [PATCH] Add tests for Extended --- src/extended.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++-- src/gitrev.rs | 13 ++++++- src/lib.rs | 18 ++++++++++ src/prerelease.rs | 15 ++++---- 4 files changed, 126 insertions(+), 10 deletions(-) diff --git a/src/extended.rs b/src/extended.rs index 4911846..931b805 100644 --- a/src/extended.rs +++ b/src/extended.rs @@ -1,7 +1,5 @@ -use crate::GitRev; - use { - crate::{Error, PreRelease, Rapid, SemVer, Simple, MAX_U12}, + crate::{Error, GitRev, PreRelease, Rapid, SemVer, Simple, MAX_U12}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; @@ -217,3 +215,89 @@ impl PartialOrd for Extended { None } } + +#[cfg(test)] +mod tests { + use std::num::NonZeroU16; + + use super::*; + + #[test] + fn from_str() { + let mut ex: Extended = "1.2.3.4".parse().unwrap(); + assert_eq!( + ex, + Extended { + major: 1, + minor: 2, + patch: 3, + build: 4, + pre: PreRelease::None, + } + ); + ex = "3.0.14.1_beta2".parse().unwrap(); + assert_eq!( + ex, + Extended { + major: 3, + minor: 0, + patch: 14, + build: 1, + pre: PreRelease::Beta(Some(NonZeroU16::new(2).unwrap())), + } + ); + } + + #[test] + fn to_string() { + let mut ex = Extended { + major: 2, + minor: 11, + patch: 0, + build: 1, + pre: PreRelease::None, + }; + assert_eq!(ex.to_string(), "2.11.0.1"); + ex.pre = PreRelease::RC(None); + assert_eq!(ex.to_string(), "2.11.0.1_rc"); + ex.pre = PreRelease::Alpha(Some(NonZeroU16::new(3).unwrap())); + assert_eq!(ex.to_string(), "2.11.0.1_alpha3"); + } + + #[test] + fn eq() { + assert_eq!( + "1.0.2.1_beta1".parse::().unwrap(), + "1.0.2.1_beta".parse::().unwrap() + ); + assert_ne!( + "1.0.2.1_alpha".parse::().unwrap(), + "1.0.2.1_alpha2".parse::().unwrap() + ); + } + + #[test] + fn ord() { + let a: Extended = "1.0.14.1".parse().unwrap(); + let b: Extended = "1.0.14.1_alpha4".parse().unwrap(); + let c: Extended = "1.0.14.1_beta2".parse().unwrap(); + let d: Extended = "1.0.14.1_beta3".parse().unwrap(); + let e: Extended = "2.0.14.1".parse().unwrap(); + let f: Extended = "2.1.14.1".parse().unwrap(); + let g: Extended = "2.1.13.1".parse().unwrap(); + assert!(a > b); + assert!(b < a); + assert!(a > c); + assert!(b < c); + assert!(c < a); + assert!(c > b); + assert!(d > b); + assert!(d < a); + assert!(e > a); + assert!(f > e); + assert!(g < f); + assert!(d < e); + assert!(e < f); + assert!(f > g); + } +} diff --git a/src/gitrev.rs b/src/gitrev.rs index 50c584e..e7c34b3 100644 --- a/src/gitrev.rs +++ b/src/gitrev.rs @@ -1,5 +1,5 @@ use { - crate::{Error, Extended, Rapid, SemVer, Simple}, + crate::{Error, Extended, Kind, Rapid, SemVer, Simple, Version}, chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, @@ -99,6 +99,17 @@ impl PartialOrd for GitRev { } } +impl TryFrom for GitRev { + type Error = Error; + + fn try_from(value: Version) -> Result { + match value.0 { + Kind::GitRev(g) => Ok(g), + _ => Err(Error::ParseGitRev), + } + } +} + #[cfg(test)] mod test { use std::{thread, time::Duration}; diff --git a/src/lib.rs b/src/lib.rs index 333fb47..2dbd867 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,6 +122,8 @@ impl PartialEq for Kind { } } +impl Eq for Kind {} + impl PartialOrd for Kind { fn partial_cmp(&self, other: &Self) -> Option { match (self, other) { @@ -146,3 +148,19 @@ impl PartialOrd for Kind { } } } + +impl PartialEq for Version { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 && self.1 == other.1 + } +} + +impl PartialOrd for Version { + fn partial_cmp(&self, other: &Self) -> Option { + if self.1 == other.1 { + self.0.partial_cmp(&other.0) + } else { + None + } + } +} diff --git a/src/prerelease.rs b/src/prerelease.rs index 39687ff..164beb2 100644 --- a/src/prerelease.rs +++ b/src/prerelease.rs @@ -143,12 +143,15 @@ impl PartialEq for PreRelease { fn eq(&self, other: &Self) -> bool { let _one = NonZeroU16::new(1).unwrap(); match (self, other) { - (PreRelease::Alpha(Some(_one)), PreRelease::Alpha(None)) - | (PreRelease::Alpha(None), PreRelease::Alpha(Some(_one))) - | (PreRelease::Beta(Some(_one)), PreRelease::Beta(None)) - | (PreRelease::Beta(None), PreRelease::Beta(Some(_one))) - | (PreRelease::RC(Some(_one)), PreRelease::RC(None)) - | (PreRelease::RC(None), PreRelease::RC(Some(_one))) => true, + (PreRelease::Alpha(Some(n)), PreRelease::Alpha(None)) + | (PreRelease::Alpha(None), PreRelease::Alpha(Some(n))) + | (PreRelease::Beta(Some(n)), PreRelease::Beta(None)) + | (PreRelease::Beta(None), PreRelease::Beta(Some(n))) + | (PreRelease::RC(Some(n)), PreRelease::RC(None)) + | (PreRelease::RC(None), PreRelease::RC(Some(n))) => { + let n: u16 = (*n).into(); + n == 1 + } _ => { let a: u16 = (*self).into(); let b: u16 = (*other).into();