Skip to content

Commit

Permalink
fixes issue1
Browse files Browse the repository at this point in the history
Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>
  • Loading branch information
ansrivas authored and gngeorgiev committed Nov 18, 2021
1 parent 6785d56 commit dd15c55
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,63 @@ impl PartialEq for Version {

impl PartialOrd for Version {
fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Eq for Version {}

impl Ord for Version {
fn cmp(&self, other: &Version) -> Ordering {
let res = self.compare_main(other);
Some(match res {
match res {
Ordering::Equal => self.compare_pre(other),
_ => res,
})
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn vec_compare<U, V>(va: &[U], vb: &[V]) -> bool
where
U: AsRef<str>,
V: AsRef<str>,
{
(va.len() == vb.len()) && va.iter().zip(vb).all(|(a, b)| a.as_ref() == b.as_ref())
}
#[test]
fn test_sort() {
// Create a vector of semver_rs::Version
let mut input_versions_list = vec![
"1.2.3-dev",
"1.2.3-dev.1",
"1.2.3-dev.cache",
"1.2.3",
"2.0.0",
"1.1.1",
]
.iter()
.flat_map(|version| Version::new(version).parse())
.collect::<Vec<Version>>();
// Sort it inplace
input_versions_list.sort();
// Create the expected Vec<&str>
let output: Vec<String> = input_versions_list
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();

let expected = vec![
"1.1.1",
"1.2.3-dev",
"1.2.3-dev.1",
"1.2.3-dev.cache",
"1.2.3",
"2.0.0",
];
assert!(vec_compare(&output, &expected));
}
}

0 comments on commit dd15c55

Please sign in to comment.