Skip to content

Commit

Permalink
Merge pull request #3821 from epage/test
Browse files Browse the repository at this point in the history
Follow up to #3815
  • Loading branch information
epage committed Jun 13, 2022
2 parents c8b45f7 + 1d053e9 commit d25b805
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ _gated behind `unstable-v4`_
- Leading dashes in `Arg::long` are no longer allowed (#3691)
- Verify `required` is not used with conditional required settings (#3660)
- Disallow more `value_names` than `number_of_values` (#2695)
- Don't correct argument id in `default_value_ifs_os`(#3815)
- *(assert)* Always enforce that version is specified when the `ArgAction::Version` is used
- *(assert)* Add missing `#[track_caller]`s to make it easier to debug asserts
- *(help)* Use `Command::display_name` in the help title rather than `Command::bin_name`
Expand Down
43 changes: 1 addition & 42 deletions tests/builder/arg_settings.rs
@@ -1,7 +1,5 @@
#![allow(deprecated)]

use clap::{Arg, ArgSettings, Command};
use std::ffi::OsStr;
use clap::{Arg, ArgSettings};

#[test]
fn setting() {
Expand Down Expand Up @@ -44,42 +42,3 @@ fn unset_setting_bitor() {
assert!(!m.is_hide_set(), "{:#?}", m);
assert!(!m.is_last_set(), "{:#?}", m);
}

#[test]
fn default_value_ifs_os() {
let cmd = Command::new("my_cargo")
.arg(
Arg::new("flag")
.long("flag")
.allow_invalid_utf8(true)
.takes_value(true),
)
.arg(
Arg::new("other")
.long("other")
.allow_invalid_utf8(true)
.default_value_ifs_os(&[(
"flag",
Some("标记2").map(OsStr::new),
Some("flag=标记2").map(OsStr::new),
)]),
);
let result = cmd.try_get_matches_from([
OsStr::new("my_cargo"),
OsStr::new("--flag"),
OsStr::new("标记2"),
]);
assert!(result.is_ok());
match result {
Ok(arg_matches) => {
assert_eq!(arg_matches.value_of_os("flag"), Some(OsStr::new("标记2")));
assert_eq!(
arg_matches.value_of_os("other"),
Some(OsStr::new("flag=标记2")),
);
}
Err(e) => {
println!("{}", e.to_string());
}
}
}
37 changes: 36 additions & 1 deletion tests/builder/default_vals.rs
@@ -1,5 +1,8 @@
use std::ffi::OsStr;
use std::ffi::OsString;

use super::utils;
use clap::{arg, error::ErrorKind, Arg, Command};
use clap::{arg, error::ErrorKind, value_parser, Arg, Command};

#[test]
fn opts() {
Expand Down Expand Up @@ -596,6 +599,38 @@ fn default_ifs_arg_present_order() {
);
}

#[test]
fn default_value_ifs_os() {
let cmd = Command::new("my_cargo")
.arg(
Arg::new("flag")
.long("flag")
.value_parser(value_parser!(OsString))
.takes_value(true),
)
.arg(
Arg::new("other")
.long("other")
.value_parser(value_parser!(OsString))
.default_value_ifs_os(&[(
"flag",
Some(OsStr::new("标记2")),
Some(OsStr::new("flag=标记2")),
)]),
);
let result = cmd.try_get_matches_from(["my_cargo", "--flag", "标记2"]);
assert!(result.is_ok(), "{}", result.unwrap_err());
let m = result.unwrap();
assert_eq!(
m.get_one::<OsString>("flag").map(OsString::as_os_str),
Some(OsStr::new("标记2"))
);
assert_eq!(
m.get_one::<OsString>("other").map(OsString::as_os_str),
Some(OsStr::new("flag=标记2")),
);
}

// Interaction with requires

#[test]
Expand Down

0 comments on commit d25b805

Please sign in to comment.