From b0a495f0ae70ebbaa0f74ae21c3a1e94a882c6b7 Mon Sep 17 00:00:00 2001 From: OGINO Masanori Date: Sat, 22 Feb 2014 07:34:56 +0900 Subject: [PATCH] Exclude build metadata from equality checking. Build metadata is already excluded from precedence checking in line with the spec. For consistency and providing strict total ordering for Version, build metadata should also be ignored in Eq impl. Closes #12438 Signed-off-by: OGINO Masanori --- src/libsemver/lib.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/libsemver/lib.rs b/src/libsemver/lib.rs index 4c596b11ad6..700dc986794 100644 --- a/src/libsemver/lib.rs +++ b/src/libsemver/lib.rs @@ -72,7 +72,7 @@ impl fmt::Show for Identifier { /// Represents a version number conforming to the semantic versioning scheme. -#[deriving(Clone, Eq)] +#[deriving(Clone)] pub struct Version { /// The major version, to be incremented on incompatible changes. major: uint, @@ -110,6 +110,19 @@ impl fmt::Show for Version { } } +impl cmp::Eq for Version { + #[inline] + fn eq(&self, other: &Version) -> bool { + // We should ignore build metadata here, otherwise versions v1 and v2 + // can exist such that !(v1 < v2) && !(v1 > v2) && v1 != v2, which + // violate strict total ordering rules. + self.major == other.major && + self.minor == other.minor && + self.patch == other.patch && + self.pre == other.pre + } +} + impl cmp::Ord for Version { #[inline] fn lt(&self, other: &Version) -> bool { @@ -347,6 +360,7 @@ fn test_eq() { assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1")); assert_eq!(parse("1.2.3+build.42"), parse("1.2.3+build.42")); assert_eq!(parse("1.2.3-alpha1+42"), parse("1.2.3-alpha1+42")); + assert_eq!(parse("1.2.3+23"), parse("1.2.3+42")); } #[test] @@ -355,7 +369,6 @@ fn test_ne() { assert!(parse("0.0.0") != parse("0.1.0")); assert!(parse("0.0.0") != parse("1.0.0")); assert!(parse("1.2.3-alpha") != parse("1.2.3-beta")); - assert!(parse("1.2.3+23") != parse("1.2.3+42")); } #[test] @@ -376,11 +389,11 @@ fn test_to_str() { #[test] fn test_lt() { - assert!(parse("0.0.0") < parse("1.2.3-alpha2")); - assert!(parse("1.0.0") < parse("1.2.3-alpha2")); - assert!(parse("1.2.0") < parse("1.2.3-alpha2")); - assert!(parse("1.2.3-alpha1") < parse("1.2.3")); - assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2")); + assert!(parse("0.0.0") < parse("1.2.3-alpha2")); + assert!(parse("1.0.0") < parse("1.2.3-alpha2")); + assert!(parse("1.2.0") < parse("1.2.3-alpha2")); + assert!(parse("1.2.3-alpha1") < parse("1.2.3")); + assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2")); assert!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2"))); assert!(!(parse("1.2.3+23") < parse("1.2.3+42"))); } @@ -397,11 +410,11 @@ fn test_le() { #[test] fn test_gt() { - assert!(parse("1.2.3-alpha2") > parse("0.0.0")); - assert!(parse("1.2.3-alpha2") > parse("1.0.0")); - assert!(parse("1.2.3-alpha2") > parse("1.2.0")); - assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1")); - assert!(parse("1.2.3") > parse("1.2.3-alpha2")); + assert!(parse("1.2.3-alpha2") > parse("0.0.0")); + assert!(parse("1.2.3-alpha2") > parse("1.0.0")); + assert!(parse("1.2.3-alpha2") > parse("1.2.0")); + assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1")); + assert!(parse("1.2.3") > parse("1.2.3-alpha2")); assert!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2"))); assert!(!(parse("1.2.3+23") > parse("1.2.3+42"))); } @@ -418,7 +431,6 @@ fn test_ge() { #[test] fn test_spec_order() { - let vs = ["1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta",