Add tests for Extended

This commit is contained in:
Nathan Fisher 2024-01-10 16:50:32 -05:00
parent 6159bb321b
commit 00b606947f
4 changed files with 126 additions and 10 deletions

View File

@ -1,7 +1,5 @@
use crate::GitRev;
use { use {
crate::{Error, PreRelease, Rapid, SemVer, Simple, MAX_U12}, crate::{Error, GitRev, PreRelease, Rapid, SemVer, Simple, MAX_U12},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };
@ -217,3 +215,89 @@ impl PartialOrd<GitRev> for Extended {
None 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::<Extended>().unwrap(),
"1.0.2.1_beta".parse::<Extended>().unwrap()
);
assert_ne!(
"1.0.2.1_alpha".parse::<Extended>().unwrap(),
"1.0.2.1_alpha2".parse::<Extended>().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);
}
}

View File

@ -1,5 +1,5 @@
use { use {
crate::{Error, Extended, Rapid, SemVer, Simple}, crate::{Error, Extended, Kind, Rapid, SemVer, Simple, Version},
chrono::{offset::Utc, DateTime}, chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
@ -99,6 +99,17 @@ impl PartialOrd<Simple> for GitRev {
} }
} }
impl TryFrom<Version> for GitRev {
type Error = Error;
fn try_from(value: Version) -> Result<Self, Self::Error> {
match value.0 {
Kind::GitRev(g) => Ok(g),
_ => Err(Error::ParseGitRev),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::{thread, time::Duration}; use std::{thread, time::Duration};

View File

@ -122,6 +122,8 @@ impl PartialEq for Kind {
} }
} }
impl Eq for Kind {}
impl PartialOrd for Kind { impl PartialOrd for Kind {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) { 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<std::cmp::Ordering> {
if self.1 == other.1 {
self.0.partial_cmp(&other.0)
} else {
None
}
}
}

View File

@ -143,12 +143,15 @@ impl PartialEq for PreRelease {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
let _one = NonZeroU16::new(1).unwrap(); let _one = NonZeroU16::new(1).unwrap();
match (self, other) { match (self, other) {
(PreRelease::Alpha(Some(_one)), PreRelease::Alpha(None)) (PreRelease::Alpha(Some(n)), PreRelease::Alpha(None))
| (PreRelease::Alpha(None), PreRelease::Alpha(Some(_one))) | (PreRelease::Alpha(None), PreRelease::Alpha(Some(n)))
| (PreRelease::Beta(Some(_one)), PreRelease::Beta(None)) | (PreRelease::Beta(Some(n)), PreRelease::Beta(None))
| (PreRelease::Beta(None), PreRelease::Beta(Some(_one))) | (PreRelease::Beta(None), PreRelease::Beta(Some(n)))
| (PreRelease::RC(Some(_one)), PreRelease::RC(None)) | (PreRelease::RC(Some(n)), PreRelease::RC(None))
| (PreRelease::RC(None), PreRelease::RC(Some(_one))) => true, | (PreRelease::RC(None), PreRelease::RC(Some(n))) => {
let n: u16 = (*n).into();
n == 1
}
_ => { _ => {
let a: u16 = (*self).into(); let a: u16 = (*self).into();
let b: u16 = (*other).into(); let b: u16 = (*other).into();