Skip to content

Commit

Permalink
test: add require_no_space tests
Browse files Browse the repository at this point in the history
Remove and modify some `require_equals` tests that are no longer valid.
  • Loading branch information
my4ng committed May 9, 2024
1 parent 60079b6 commit e7f3252
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 41 deletions.
115 changes: 115 additions & 0 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,121 @@ Options:
utils::assert_output(cmd, "ctest -h", ISSUE_1487, false);
}

#[test]
fn help_require_equals() {
static REQUIRE_EQUALS: &str = "\
Usage: myapp [OPTIONS]
Options:
--foo=<foo>
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.long("foo")
.require_equals(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_EQUALS, false);
}

#[test]
fn help_require_equals_short() {
static REQUIRE_EQUALS_SHORT: &str = "\
Usage: myapp [OPTIONS]
Options:
-f <foo>
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.short('f')
.require_equals(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_EQUALS_SHORT, false);
}

#[test]
fn help_require_no_space() {
static REQUIRE_NO_SPACE: &str = "\
Usage: myapp [OPTIONS]
Options:
-f<foo>
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.short('f')
.require_no_space(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_NO_SPACE, false);
}

#[test]
fn help_require_no_space_long() {
static REQUIRE_NO_SPACE_LONG: &str = "\
Usage: myapp [OPTIONS]
Options:
--foo <foo>
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.long("foo")
.require_no_space(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_NO_SPACE_LONG, false);
}

#[test]
fn help_require_equals_no_space() {
static REQUIRE_EQUALS_NO_SPACE: &str = "\
Usage: myapp [OPTIONS]
Options:
-f<foo>, --foo=<foo> This is foo
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.long("foo")
.short('f')
.help("This is foo")
.require_equals(true)
.require_no_space(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_EQUALS_NO_SPACE, false);
}

#[test]
fn help_require_equals_no_space_optional() {
static REQUIRE_EQUALS_NO_SPACE_OPTIONAL: &str = "\
Usage: myapp [OPTIONS]
Options:
-f[<foo>], --foo[=<foo>] This is foo
-h, --help Print help
";
let cmd = Command::new("myapp").arg(
Arg::new("foo")
.long("foo")
.short('f')
.help("This is foo")
.num_args(0..=1)
.require_equals(true)
.require_no_space(true)
.action(ArgAction::Set),
);
utils::assert_output(cmd, "myapp -h", REQUIRE_EQUALS_NO_SPACE_OPTIONAL, false);
}

#[cfg(debug_assertions)]
#[test]
#[should_panic = "Command::help_expected is enabled for the Command"]
Expand Down
139 changes: 139 additions & 0 deletions tests/builder/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,145 @@ fn require_equals_pass() {
assert!(res.is_ok(), "{}", res.unwrap_err());
}

#[test]
fn require_equals_short_unaffected() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_equals(true)
.short('c'),
)
.try_get_matches_from(vec!["prog", "-c", "file.conf"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
}

#[test]
fn require_equals_short_min_values_zero() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_equals(true)
.num_args(0..)
.short('c'),
)
.arg(Arg::new("cmd"))
.try_get_matches_from(vec!["prog", "-c", "cmd"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert_eq!(m.get_one::<String>("cfg").map(|v| v.as_str()), Some("cmd"));
assert_eq!(m.get_one::<String>("cmd"), None);
}

#[test]
fn require_no_space_fail() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c'),
)
.try_get_matches_from(vec!["prog", "-c", "file.conf"]);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::SpaceFound);
}

#[test]
#[cfg(feature = "error-context")]
fn require_no_space_fail_message() {
static SPACE_FOUND: &str = "error: no space is allowed when assigning values to '-c<cfg>'
Usage: prog [OPTIONS]
For more information, try '--help'.
";
let cmd = Command::new("prog").arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c'),
);
utils::assert_output(cmd, "prog -c file.conf", SPACE_FOUND, true);
}

#[test]
fn require_no_space_min_values_zero() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.num_args(0..)
.short('c'),
)
.arg(Arg::new("cmd"))
.try_get_matches_from(vec!["prog", "-c", "cmd"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("cfg"));
assert_eq!(m.get_one::<String>("cmd").map(|v| v.as_str()), Some("cmd"));
}

#[test]
fn require_no_space_empty_fail() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c'),
)
.try_get_matches_from(vec!["prog", "-c"]);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::SpaceFound);
}

#[test]
fn require_no_space_pass() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c'),
)
.try_get_matches_from(vec!["prog", "-cfile.conf"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
}

#[test]
fn require_no_space_equals_pass() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c'),
)
.try_get_matches_from(vec!["prog", "-c=file.conf"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
assert_eq!(
res.unwrap().get_one::<String>("cfg").map(|v| v.as_str()),
Some("=file.conf")
);
}

#[test]
fn require_no_space_long_unaffected() {
let res = Command::new("prog")
.arg(
Arg::new("cfg")
.action(ArgAction::Set)
.require_no_space(true)
.short('c')
.long("config"),
)
.try_get_matches_from(vec!["prog", "--config", "file.conf"]);
assert!(res.is_ok(), "{}", res.unwrap_err())
}

#[test]
fn stdin_char() {
let r = Command::new("opts")
Expand Down
Loading

0 comments on commit e7f3252

Please sign in to comment.