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 <masanori.ogino@gmail.com>
This commit is contained in:
parent
994b48cf52
commit
b0a495f0ae
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user