mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
tests: adds tests to guard aginst issue 1105 and empty values in options
This commit is contained in:
parent
2fb758219c
commit
17a4d8a860
1 changed files with 63 additions and 14 deletions
|
@ -3,7 +3,7 @@ extern crate regex;
|
||||||
|
|
||||||
include!("../clap-test.rs");
|
include!("../clap-test.rs");
|
||||||
|
|
||||||
use clap::{App, Arg, ErrorKind};
|
use clap::{App, ArgMatches, Arg, ErrorKind};
|
||||||
|
|
||||||
#[cfg(feature = "suggestions")]
|
#[cfg(feature = "suggestions")]
|
||||||
static DYM: &'static str = "error: Found argument '--optio' which wasn't expected, or isn't valid in this context
|
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");
|
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::<Vec<_>>(),
|
|
||||||
["", "test"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn opts_using_long_equals() {
|
fn opts_using_long_equals() {
|
||||||
let r = App::new("opts")
|
let r = App::new("opts")
|
||||||
|
@ -410,3 +398,64 @@ fn issue_1047_min_zero_vals_default_val() {
|
||||||
assert_eq!(m.occurrences_of("del"), 1);
|
assert_eq!(m.occurrences_of("del"), 1);
|
||||||
assert_eq!(m.value_of("del"), Some("default"));
|
assert_eq!(m.value_of("del"), Some("default"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue_1105_setup(argv: Vec<&'static str>) -> Result<ArgMatches<'static>, 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(""));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue