From 17a4d8a860d9a418a28c5fd0b0190d76467443c4 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 13 Nov 2017 17:05:28 -0500 Subject: [PATCH] tests: adds tests to guard aginst issue 1105 and empty values in options --- tests/opts.rs | 77 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/tests/opts.rs b/tests/opts.rs index 8a2deee9..a9029ac9 100644 --- a/tests/opts.rs +++ b/tests/opts.rs @@ -3,7 +3,7 @@ extern crate regex; include!("../clap-test.rs"); -use clap::{App, Arg, ErrorKind}; +use clap::{App, ArgMatches, Arg, ErrorKind}; #[cfg(feature = "suggestions")] static DYM: &'static str = "error: Found argument '--optio' which wasn't expected, or isn't valid in this context @@ -194,18 +194,6 @@ fn opts_using_long_space() { assert_eq!(m.value_of("color").unwrap(), "other"); } -#[test] -fn opts_with_empty_values() { - let r = App::new("opts") - .arg_from_usage("--flag [flag]... 'some flag'") - .get_matches_from_safe(vec!["", "--flag", "", "test"]); - assert!(r.is_ok()); - let m = r.unwrap(); - assert!(m.is_present("flag")); - assert_eq!(m.values_of("flag").unwrap().collect::>(), - ["", "test"]); -} - #[test] fn opts_using_long_equals() { let r = App::new("opts") @@ -409,4 +397,65 @@ fn issue_1047_min_zero_vals_default_val() { .get_matches_from(vec!["foo", "-d"]); assert_eq!(m.occurrences_of("del"), 1); assert_eq!(m.value_of("del"), Some("default")); -} \ No newline at end of file +} + +fn issue_1105_setup(argv: Vec<&'static str>) -> Result, clap::Error> { + App::new("opts") + .arg_from_usage("-o, --option [opt] 'some option'") + .arg_from_usage("--flag 'some flag'") + .get_matches_from_safe(argv) +} + +#[test] +fn issue_1105_empty_value_long_fail() { + let r = issue_1105_setup(vec!["app", "--option", "--flag"]); + assert!(r.is_err()); + assert_eq!(r.unwrap_err().kind, ErrorKind::EmptyValue); +} + +#[test] +fn issue_1105_empty_value_long_explicit() { + let r = issue_1105_setup(vec!["app", "--option", ""]); + assert!(r.is_ok()); + let m = r.unwrap(); + assert_eq!(m.value_of("option"), Some("")); +} + +#[test] +fn issue_1105_empty_value_long_equals() { + let r = issue_1105_setup(vec!["app", "--option="]); + assert!(r.is_ok()); + let m = r.unwrap(); + assert_eq!(m.value_of("option"), Some("")); +} + +#[test] +fn issue_1105_empty_value_short_fail() { + let r = issue_1105_setup(vec!["app", "-o", "--flag"]); + assert!(r.is_err()); + assert_eq!(r.unwrap_err().kind, ErrorKind::EmptyValue); +} + +#[test] +fn issue_1105_empty_value_short_explicit() { + let r = issue_1105_setup(vec!["app", "-o", ""]); + assert!(r.is_ok()); + let m = r.unwrap(); + assert_eq!(m.value_of("option"), Some("")); +} + +#[test] +fn issue_1105_empty_value_short_equals() { + let r = issue_1105_setup(vec!["app", "-o="]); + assert!(r.is_ok()); + let m = r.unwrap(); + assert_eq!(m.value_of("option"), Some("")); +} + +#[test] +fn issue_1105_empty_value_short_explicit_no_space() { + let r = issue_1105_setup(vec!["app", "-o", ""]); + assert!(r.is_ok()); + let m = r.unwrap(); + assert_eq!(m.value_of("option"), Some("")); +}