Commit 9216dcc3 authored by Scott Abbey's avatar Scott Abbey

Add tests

parent ffcc1117
......@@ -188,3 +188,369 @@ pub fn parse(range: &str) -> Result<VersionReq, String> {
predicates: vec![predicate],
})
}
#[cfg(test)]
mod tests {
use super::*;
use range;
use version::Identifier;
#[test]
fn test_parsing_default() {
let r = range::parse("1.0.0").unwrap();
assert_eq!(Predicate {
op: Op::Compatible,
major: 1,
minor: Some(0),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_exact_01() {
let r = range::parse("=1.0.0").unwrap();
assert_eq!(Predicate {
op: Op::Ex,
major: 1,
minor: Some(0),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_exact_02() {
let r = range::parse("=0.9.0").unwrap();
assert_eq!(Predicate {
op: Op::Ex,
major: 0,
minor: Some(9),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_exact_03() {
let r = range::parse("=0.1.0-beta2.a").unwrap();
assert_eq!(Predicate {
op: Op::Ex,
major: 0,
minor: Some(1),
patch: Some(0),
pre: vec![Identifier::AlphaNumeric(String::from("beta2")),
Identifier::AlphaNumeric(String::from("a"))],
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_greater_than() {
let r = range::parse("> 1.0.0").unwrap();
assert_eq!(Predicate {
op: Op::Gt,
major: 1,
minor: Some(0),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_greater_than_01() {
let r = range::parse(">= 1.0.0").unwrap();
assert_eq!(Predicate {
op: Op::GtEq,
major: 1,
minor: Some(0),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_greater_than_02() {
let r = range::parse(">= 2.1.0-alpha2").unwrap();
assert_eq!(Predicate {
op: Op::GtEq,
major: 2,
minor: Some(1),
patch: Some(0),
pre: vec![Identifier::AlphaNumeric(String::from("alpha2"))],
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_less_than() {
let r = range::parse("< 1.0.0").unwrap();
assert_eq!(Predicate {
op: Op::Lt,
major: 1,
minor: Some(0),
patch: Some(0),
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_less_than_eq() {
let r = range::parse("<= 2.1.0-alpha2").unwrap();
assert_eq!(Predicate {
op: Op::LtEq,
major: 2,
minor: Some(1),
patch: Some(0),
pre: vec![Identifier::AlphaNumeric(String::from("alpha2"))],
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_tilde() {
let r = range::parse("~1").unwrap();
assert_eq!(Predicate {
op: Op::Tilde,
major: 1,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
pub fn test_parsing_compatible() {
let r = range::parse("^0").unwrap();
assert_eq!(Predicate {
op: Op::Compatible,
major: 0,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_blank() {
let r = range::parse("").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Major),
major: 0,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_wildcard() {
let r = range::parse("*").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Major),
major: 0,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_x() {
let r = range::parse("x").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Major),
major: 0,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_capital_x() {
let r = range::parse("X").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Major),
major: 0,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_minor_wildcard_star() {
let r = range::parse("1.*").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Minor),
major: 1,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_minor_wildcard_x() {
let r = range::parse("1.x").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Minor),
major: 1,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_minor_wildcard_capital_x() {
let r = range::parse("1.X").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Minor),
major: 1,
minor: None,
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_patch_wildcard_star() {
let r = range::parse("1.2.*").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Patch),
major: 1,
minor: Some(2),
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_patch_wildcard_x() {
let r = range::parse("1.2.x").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Patch),
major: 1,
minor: Some(2),
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
#[test]
fn test_parsing_patch_wildcard_capital_x() {
let r = range::parse("1.2.X").unwrap();
assert_eq!(Predicate {
op: Op::Wildcard(WildcardVersion::Patch),
major: 1,
minor: Some(2),
patch: None,
pre: Vec::new(),
},
r.predicates[0]
);
}
// #[test]
// pub fn test_multiple() {
// let r = req("> 0.0.9, <= 2.5.3");
// assert_eq!(r.to_string(), "> 0.0.9, <= 2.5.3".to_string());
// assert_match(&r, &["0.0.10", "1.0.0", "2.5.3"]);
// assert_not_match(&r, &["0.0.8", "2.5.4"]);
//
// let r = req("0.3.0, 0.4.0");
// assert_eq!(r.to_string(), "^0.3.0, ^0.4.0".to_string());
// assert_not_match(&r, &["0.0.8", "0.3.0", "0.4.0"]);
//
// let r = req("<= 0.2.0, >= 0.5.0");
// assert_eq!(r.to_string(), "<= 0.2.0, >= 0.5.0".to_string());
// assert_not_match(&r, &["0.0.8", "0.3.0", "0.5.1"]);
//
// let r = req("0.1.0, 0.1.4, 0.1.6");
// assert_eq!(r.to_string(), "^0.1.0, ^0.1.4, ^0.1.6".to_string());
// assert_match(&r, &["0.1.6", "0.1.9"]);
// assert_not_match(&r, &["0.1.0", "0.1.4", "0.2.0"]);
//
// assert!(VersionReq::parse("> 0.1.0,").is_err());
// assert!(VersionReq::parse("> 0.3.0, ,").is_err());
//
// let r = req(">=0.5.1-alpha3, <0.6");
// assert_eq!(r.to_string(), ">= 0.5.1-alpha3, < 0.6".to_string());
// assert_match(&r,
// &["0.5.1-alpha3", "0.5.1-alpha4", "0.5.1-beta", "0.5.1", "0.5.5"]);
// assert_not_match(&r,
// &["0.5.1-alpha1", "0.5.2-alpha3", "0.5.5-pre", "0.5.0-pre"]);
// assert_not_match(&r, &["0.6.0", "0.6.0-pre"]);
// }
#[test]
pub fn test_parse_errors() {
assert!(range::parse("\0").is_err());
assert!(range::parse(">= >= 0.0.2").is_err());
assert!(range::parse(">== 0.0.2").is_err());
assert!(range::parse("a.0.0").is_err());
assert!(range::parse("1.0.0-").is_err());
assert!(range::parse(">=").is_err());
}
}
......@@ -79,13 +79,14 @@ pub fn parse(version: &str) -> Result<Version, String> {
#[cfg(test)]
mod tests {
use version;
use super::*;
#[test]
fn parse_empty() {
let version = "";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), "empty string incorrectly considered a valid parse");
}
......@@ -94,7 +95,7 @@ mod tests {
fn parse_blank() {
let version = " ";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), "blank string incorrectly considered a valid parse");
}
......@@ -103,7 +104,7 @@ mod tests {
fn parse_no_minor_patch() {
let version = "1";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
}
......@@ -112,7 +113,7 @@ mod tests {
fn parse_no_patch() {
let version = "1.2";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
}
......@@ -121,7 +122,7 @@ mod tests {
fn parse_empty_pre() {
let version = "1.2.3-";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
}
......@@ -130,16 +131,16 @@ mod tests {
fn parse_letters() {
let version = "a.b.c";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
}
#[test]
fn parse_version_with_letters() {
fn parse_with_letters() {
let version = "1.2.3 a.b.c";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
}
......@@ -148,7 +149,7 @@ mod tests {
fn parse_basic_version() {
let version = "1.2.3";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
assert_eq!(1, parsed.major);
assert_eq!(2, parsed.minor);
......@@ -159,7 +160,7 @@ mod tests {
fn parse_trims_input() {
let version = " 1.2.3 ";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
assert_eq!(1, parsed.major);
assert_eq!(2, parsed.minor);
......@@ -167,57 +168,57 @@ mod tests {
}
#[test]
fn parse_version_no_major_leading_zeroes() {
fn parse_no_major_leading_zeroes() {
let version = "01.0.0";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), "01 incorrectly considered a valid major version");
}
#[test]
fn parse_version_no_minor_leading_zeroes() {
fn parse_no_minor_leading_zeroes() {
let version = "0.01.0";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), "01 incorrectly considered a valid minor version");
}
#[test]
fn parse_version_no_patch_leading_zeroes() {
fn parse_no_patch_leading_zeroes() {
let version = "0.0.01";
let parsed = parse_version(version);
let parsed = version::parse(version);
assert!(parsed.is_err(), "01 incorrectly considered a valid patch version");
}
#[test]
fn parse_version_basic_prerelease() {
fn parse_basic_prerelease() {
let version = "1.2.3-pre";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::AlphaNumeric(String::from("pre"))]);
assert_eq!(expected_pre, parsed.pre);
}
#[test]
fn parse_version_prerelease_alphanumeric() {
fn parse_prerelease_alphanumeric() {
let version = "1.2.3-alpha1";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::AlphaNumeric(String::from("alpha1"))]);
assert_eq!(expected_pre, parsed.pre);
}
#[test]
fn parse_version_prerelease_zero() {
fn parse_prerelease_zero() {
let version = "1.2.3-pre.0";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::AlphaNumeric(String::from("pre")),
Identifier::Numeric(0)]);
......@@ -225,30 +226,30 @@ mod tests {
}
#[test]
fn parse_version_basic_build() {
fn parse_basic_build() {
let version = "1.2.3+build";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_build = Some(vec![Identifier::AlphaNumeric(String::from("build"))]);
assert_eq!(expected_build, parsed.build);
}
#[test]
fn parse_version_build_alphanumeric() {
fn parse_build_alphanumeric() {
let version = "1.2.3+build5";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_build = Some(vec![Identifier::AlphaNumeric(String::from("build5"))]);
assert_eq!(expected_build, parsed.build);
}
#[test]
fn parse_version_pre_and_build() {
fn parse_pre_and_build() {
let version = "1.2.3-alpha1+build5";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::AlphaNumeric(String::from("alpha1"))]);
assert_eq!(expected_pre, parsed.pre);
......@@ -258,10 +259,10 @@ mod tests {
}
#[test]
fn parse_version_complex_metadata_01() {
fn parse_complex_metadata_01() {
let version = "1.2.3-1.alpha1.9+build5.7.3aedf ";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::Numeric(1),
Identifier::AlphaNumeric(String::from("alpha1")),
......@@ -275,10 +276,10 @@ mod tests {
}
#[test]
fn parse_version_complex_metadata_02() {
fn parse_complex_metadata_02() {
let version = "0.4.0-beta.1+0851523";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
let expected_pre = Some(vec![Identifier::AlphaNumeric(String::from("beta")),
Identifier::Numeric(1)]);
......@@ -292,7 +293,7 @@ mod tests {
fn parse_regression_01() {
let version = "0.0.0-WIP";
let parsed = parse_version(version).unwrap();
let parsed = version::parse(version).unwrap();
assert_eq!(0, parsed.major);
assert_eq!(0, parsed.minor);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment