-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow empty value by default, introduce NoEquals Error
- Loading branch information
Showing
9 changed files
with
179 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
mod utils; | ||
|
||
use clap::{App, Arg, ArgSettings, ErrorKind}; | ||
|
||
#[test] | ||
fn empty_values() { | ||
let m = App::new("config") | ||
.arg(Arg::new("config").long("config").takes_value(true)) | ||
.get_matches_from(&["config", "--config", ""]); | ||
assert_eq!(m.value_of("config"), Some("")); | ||
} | ||
|
||
#[test] | ||
fn empty_values_with_equals() { | ||
let m = App::new("config") | ||
.arg(Arg::new("config").long("config").takes_value(true)) | ||
.get_matches_from(&["config", "--config="]); | ||
assert_eq!(m.value_of("config"), Some("")); | ||
|
||
let m = App::new("config") | ||
.arg(Arg::new("config").short('c').takes_value(true)) | ||
.get_matches_from(&["config", "-c="]); | ||
assert_eq!(m.value_of("config"), Some("")) | ||
} | ||
|
||
#[test] | ||
fn no_empty_values() { | ||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.long("config") | ||
.takes_value(true) | ||
.forbid_empty_values(true), | ||
) | ||
.try_get_matches_from(&["config", "--config", ""]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue); | ||
|
||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.short('c') | ||
.takes_value(true) | ||
.forbid_empty_values(true), | ||
) | ||
.try_get_matches_from(&["config", "-c", ""]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue) | ||
} | ||
|
||
#[test] | ||
fn no_empty_values_with_equals() { | ||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.long("config") | ||
.takes_value(true) | ||
.setting(ArgSettings::NoEmptyValues), | ||
) | ||
.try_get_matches_from(&["config", "--config="]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue); | ||
|
||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.short('c') | ||
.takes_value(true) | ||
.setting(ArgSettings::NoEmptyValues), | ||
) | ||
.try_get_matches_from(&["config", "-c="]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue); | ||
} | ||
|
||
#[test] | ||
fn no_empty_values_without_equals() { | ||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.long("config") | ||
.takes_value(true) | ||
.setting(ArgSettings::NoEmptyValues), | ||
) | ||
.try_get_matches_from(&["config", "--config"]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue); | ||
|
||
let m = App::new("config") | ||
.arg( | ||
Arg::new("config") | ||
.short('c') | ||
.takes_value(true) | ||
.setting(ArgSettings::NoEmptyValues), | ||
) | ||
.try_get_matches_from(&["config", "-c"]); | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::EmptyValue) | ||
} | ||
|
||
#[test] | ||
fn no_empty_values_without_equals_but_requires_equals() { | ||
let app = App::new("config").arg( | ||
Arg::new("config") | ||
.long("config") | ||
.takes_value(true) | ||
.setting(ArgSettings::NoEmptyValues) | ||
.setting(ArgSettings::RequireEquals), | ||
); | ||
let m = app.clone().try_get_matches_from(&["config", "--config"]); | ||
// Should error on no equals rather than empty value. | ||
assert!(m.is_err()); | ||
assert_eq!(m.unwrap_err().kind, ErrorKind::NoEquals); | ||
|
||
static NO_EUQALS_ERROR: &str = | ||
"error: Equal sign is needed when assigning values to '--config=<config>'. | ||
USAGE: | ||
config [OPTIONS] | ||
For more information try --help"; | ||
|
||
assert!(utils::compare_output( | ||
app, | ||
"config --config", | ||
NO_EUQALS_ERROR, | ||
true | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters