Skip to content

Commit

Permalink
Exclude build metadata from equality checking.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
omasanori committed Feb 25, 2014
1 parent 994b48c commit b0a495f
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/libsemver/lib.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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")));
}
Expand All @@ -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")));
}
Expand All @@ -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",
Expand Down

5 comments on commit b0a495f

@bors
Copy link
Contributor

@bors bors commented on b0a495f Feb 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b0a495f Feb 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging omasanori/rust/semver-eq = b0a495f into auto

@bors
Copy link
Contributor

@bors bors commented on b0a495f Feb 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omasanori/rust/semver-eq = b0a495f merged ok, testing candidate = 043c972

@bors
Copy link
Contributor

@bors bors commented on b0a495f Feb 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b0a495f Feb 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 043c972

Please sign in to comment.