Fix comparisons of versions with and without a pre-release ID

Semver says that 1.0.0-alpha < 1.0.0 but the comparison didn't account
for this case.
This commit is contained in:
Oliver Hamlet
2022-02-04 16:22:55 +00:00
parent 6127a7973a
commit d874dfcdbc
+21 -1
View File
@@ -229,7 +229,14 @@ impl PartialOrd for Version {
match self_release_ids.partial_cmp(&other_release_ids) {
Some(Ordering::Equal) | None => {
self.pre_release_ids.partial_cmp(&other.pre_release_ids)
match (
self.pre_release_ids.is_empty(),
other.pre_release_ids.is_empty(),
) {
(true, false) => Some(Ordering::Greater),
(false, true) => Some(Ordering::Less),
_ => self.pre_release_ids.partial_cmp(&other.pre_release_ids),
}
}
r => r,
}
@@ -636,6 +643,19 @@ mod tests {
assert!(Version::from("10.0.5") > Version::from("5.0.10"));
}
#[test]
fn version_eq_should_consider_versions_that_differ_by_the_presence_of_a_pre_release_id_to_be_not_equal(
) {
assert_ne!(Version::from("1.0.0"), Version::from("1.0.0-alpha"));
}
#[test]
fn version_partial_cmp_should_treat_the_absence_of_a_pre_release_id_as_greater_than_its_presence(
) {
assert!(Version::from("1.0.0-alpha") < Version::from("1.0.0"));
assert!(Version::from("1.0.0") > Version::from("1.0.0-alpha"));
}
#[test]
fn version_eq_should_compare_pre_release_identifiers() {
assert_eq!(