Skip to content

Commit

Permalink
auto merge of #12454 : omasanori/rust/semver-eq, r=alexcrichton
Browse files Browse the repository at this point in the history
Closes #12438
  • Loading branch information
bors committed Feb 24, 2014
2 parents a5342d5 + b9d7f01 commit 7e60fc6
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/libsemver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ToStr 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 @@ -125,6 +125,19 @@ impl ToStr 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 @@ -362,6 +375,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 @@ -370,7 +384,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 @@ -391,11 +404,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 @@ -412,11 +425,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 @@ -433,7 +446,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

0 comments on commit 7e60fc6

Please sign in to comment.