diff --git a/src/eval.rs b/src/eval.rs index e6e38949..cecdbb25 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -42,135 +42,67 @@ fn matches_impl(cmp: &Comparator, ver: &Version) -> bool { } fn matches_exact(cmp: &Comparator, ver: &Version) -> bool { - if ver.major != cmp.major { - return false; - } - - if let Some(minor) = cmp.minor { - if ver.minor != minor { - return false; - } - } - - if let Some(patch) = cmp.patch { - if ver.patch != patch { - return false; - } - } - - ver.pre == cmp.pre + ver.major == cmp.major + && cmp.minor.map_or(true, |minor| ver.minor == minor) + && cmp.patch.map_or(true, |patch| ver.patch == patch) + && ver.pre == cmp.pre } fn matches_greater(cmp: &Comparator, ver: &Version) -> bool { if ver.major != cmp.major { - return ver.major > cmp.major; + ver.major > cmp.major + } else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) { + ver.minor > minor + } else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) { + ver.patch > patch + } else { + ver.pre > cmp.pre } - - match cmp.minor { - None => return false, - Some(minor) => { - if ver.minor != minor { - return ver.minor > minor; - } - } - } - - match cmp.patch { - None => return false, - Some(patch) => { - if ver.patch != patch { - return ver.patch > patch; - } - } - } - - ver.pre > cmp.pre } fn matches_less(cmp: &Comparator, ver: &Version) -> bool { if ver.major != cmp.major { - return ver.major < cmp.major; + ver.major < cmp.major + } else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) { + ver.minor < minor + } else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) { + ver.patch < patch + } else { + ver.pre < cmp.pre } - - match cmp.minor { - None => return false, - Some(minor) => { - if ver.minor != minor { - return ver.minor < minor; - } - } - } - - match cmp.patch { - None => return false, - Some(patch) => { - if ver.patch != patch { - return ver.patch < patch; - } - } - } - - ver.pre < cmp.pre } fn matches_tilde(cmp: &Comparator, ver: &Version) -> bool { - if ver.major != cmp.major { - return false; - } - - if let Some(minor) = cmp.minor { - if ver.minor != minor { - return false; - } - } - - if let Some(patch) = cmp.patch { - if ver.patch != patch { - return ver.patch > patch; - } + if !ver.pre.is_empty() || !cmp.pre.is_empty() { + matches_exact(cmp, ver) + } else if ver.major != cmp.major { + false + } else if cmp.minor.map_or(false, |minor| ver.minor != minor) { + false + } else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) { + ver.patch > patch + } else { + true } - - ver.pre >= cmp.pre } fn matches_caret(cmp: &Comparator, ver: &Version) -> bool { - if ver.major != cmp.major { - return false; - } - - let minor = match cmp.minor { - None => return true, - Some(minor) => minor, - }; - - let patch = match cmp.patch { - None => { - if cmp.major > 0 { - return ver.minor >= minor; - } else { - return ver.minor == minor; - } + if !ver.pre.is_empty() || !cmp.pre.is_empty() { + matches_exact(cmp, ver) + } else if ver.major != cmp.major { + false + } else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) { + // if major is 0 than minor is considered as major + if cmp.major == 0 { + false + } else { + ver.minor > minor } - Some(patch) => patch, - }; - - if cmp.major > 0 { - if ver.minor != minor { - return ver.minor > minor; - } else if ver.patch != patch { - return ver.patch > patch; - } - } else if minor > 0 { - if ver.minor != minor { - return false; - } else if ver.patch != patch { - return ver.patch > patch; - } - } else if ver.minor != minor || ver.patch != patch { - return false; + } else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) { + ver.patch > patch + } else { + true } - - ver.pre >= cmp.pre } fn pre_is_compatible(cmp: &Comparator, ver: &Version) -> bool { diff --git a/tests/test_version_req.rs b/tests/test_version_req.rs index 98a03ac8..20c0d38d 100644 --- a/tests/test_version_req.rs +++ b/tests/test_version_req.rs @@ -21,7 +21,7 @@ use semver::VersionReq; fn assert_match_all(req: &VersionReq, versions: &[&str]) { for string in versions { let parsed = version(string); - assert!(req.matches(&parsed), "did not match {}", string); + assert!(req.matches(&parsed), "{} did not match {}", req, string); } } @@ -29,7 +29,7 @@ fn assert_match_all(req: &VersionReq, versions: &[&str]) { fn assert_match_none(req: &VersionReq, versions: &[&str]) { for string in versions { let parsed = version(string); - assert!(!req.matches(&parsed), "matched {}", string); + assert!(!req.matches(&parsed), "{} matched {}", req, string); } } @@ -105,14 +105,18 @@ pub fn test_less_than() { &["2.1.0", "2.2.0-alpha1", "2.0.0-alpha2", "1.0.0-alpha2"], ); + let ref r = req("<1.0"); + assert_match_none(r, &["1.0.0-beta"]); + + // All the following should probably be in test_multiple let ref r = req(">1.0.0-alpha, <1.0.0"); assert_match_all(r, &["1.0.0-beta"]); let ref r = req(">1.0.0-alpha, <1.0"); - assert_match_none(r, &["1.0.0-beta"]); + assert_match_all(r, &["1.0.0-beta"]); let ref r = req(">1.0.0-alpha, <1"); - assert_match_none(r, &["1.0.0-beta"]); + assert_match_all(r, &["1.0.0-beta"]); } #[test] @@ -192,8 +196,19 @@ pub fn test_tilde() { assert_match_none(r, &["1.2.1", "1.9.0", "1.0.9", "2.0.1", "0.1.3"]); let ref r = req("~1.2.3-beta.2"); - assert_match_all(r, &["1.2.3", "1.2.4", "1.2.3-beta.2", "1.2.3-beta.4"]); - assert_match_none(r, &["1.3.3", "1.1.4", "1.2.3-beta.1", "1.2.4-beta.2"]); + assert_match_all(r, &["1.2.3-beta.2"]); + assert_match_none( + r, + &[ + "1.1.4", + "1.2.3", + "1.2.4", + "1.2.3-beta.1", + "1.2.3-beta.4", + "1.2.4-beta.2", + "1.3.3", + ], + ); } #[test] @@ -218,21 +233,16 @@ pub fn test_caret() { assert_match_none(r, &["0.1.2-beta", "0.1.3-alpha", "0.2.0-pre"]); let ref r = req("^0.5.1-alpha3"); - assert_match_all( + assert_match_all(r, &["0.5.1-alpha3"]); + assert_match_none( r, &[ - "0.5.1-alpha3", + "0.5.1-alpha1", + "0.5.2-alpha3", "0.5.1-alpha4", "0.5.1-beta", "0.5.1", "0.5.5", - ], - ); - assert_match_none( - r, - &[ - "0.5.1-alpha1", - "0.5.2-alpha3", "0.5.5-pre", "0.5.0-pre", "0.6.0", @@ -252,18 +262,19 @@ pub fn test_caret() { assert_match_none(r, &["2.9.0", "1.1.1"]); let ref r = req("^1.4.2-beta.5"); - assert_match_all( - r, - &["1.4.2", "1.4.3", "1.4.2-beta.5", "1.4.2-beta.6", "1.4.2-c"], - ); + assert_match_all(r, &["1.4.2-beta.5"]); assert_match_none( r, &[ "0.9.9", - "2.0.0", + "1.4.2", + "1.4.3", "1.4.2-alpha", "1.4.2-beta.4", + "1.4.2-beta.6", + "1.4.2-c", "1.4.3-beta.5", + "2.0.0", ], ); }