2821: test(derive): Port structopt flatten coverage r=epage a=epage



2822: feat(derive): Add support for lower/upper in rename_all r=epage a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
bors[bot] 2021-10-07 16:02:20 +00:00 committed by GitHub
commit 6c2daef7a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 10 deletions

View file

@ -80,6 +80,10 @@ pub enum CasingStyle {
ScreamingSnake,
/// Keep all letters lowercase and indicate word boundaries with underscores.
Snake,
/// Keep all letters lowercase and remove word boundaries.
Lower,
/// Keep all letters uppercase and remove word boundaries.
Upper,
/// Use the original attribute name defined in the code.
Verbatim,
}
@ -206,6 +210,8 @@ impl CasingStyle {
"pascal" | "pascalcase" => cs(Pascal),
"screamingsnake" | "screamingsnakecase" => cs(ScreamingSnake),
"snake" | "snakecase" => cs(Snake),
"lower" | "lowercase" => cs(Lower),
"upper" | "uppercase" => cs(Upper),
"verbatim" | "verbatimcase" => cs(Verbatim),
s => abort!(name, "unsupported casing: `{}`", s),
}
@ -226,6 +232,8 @@ impl Name {
Camel => s.to_mixed_case(),
ScreamingSnake => s.to_shouty_snake_case(),
Snake => s.to_snake_case(),
Lower => s.to_snake_case().replace("_", ""),
Upper => s.to_shouty_snake_case().replace("_", ""),
Verbatim => s,
};
quote_spanned!(ident.span()=> #s)
@ -246,6 +254,8 @@ impl Name {
Camel => s.to_mixed_case(),
ScreamingSnake => s.to_shouty_snake_case(),
Snake => s.to_snake_case(),
Lower => s.to_snake_case(),
Upper => s.to_shouty_snake_case(),
Verbatim => s,
};

View file

@ -288,3 +288,31 @@ fn test_rename_all_is_propagation_can_be_overridden() {
Opt::parse_from(&["test", "SECOND_VARIANT", "--foo-option"])
);
}
#[test]
fn test_lower_is_renamed() {
#[derive(Clap, Debug, PartialEq)]
struct Opt {
#[clap(rename_all = "lower", long)]
foo_option: bool,
}
assert_eq!(
Opt { foo_option: true },
Opt::parse_from(&["test", "--foooption"])
);
}
#[test]
fn test_upper_is_renamed() {
#[derive(Clap, Debug, PartialEq)]
struct Opt {
#[clap(rename_all = "upper", long)]
foo_option: bool,
}
assert_eq!(
Opt { foo_option: true },
Opt::parse_from(&["test", "--FOOOPTION"])
);
}

View file

@ -155,21 +155,29 @@ fn update_subcommands_with_flatten() {
#[test]
fn flatten_with_doc_comment() {
#[derive(Clap, Debug)]
struct DaemonOpts {
#[clap(short)]
user: String,
#[clap(short)]
group: String,
#[derive(Clap, PartialEq, Debug)]
struct Common {
/// This is an arg. Arg means "argument". Command line argument.
arg: i32,
}
#[derive(Clap, Debug)]
#[clap(name = "basic")]
#[derive(Clap, PartialEq, Debug)]
struct Opt {
/// A very important doc comment I just can't leave out!
/// The very important comment that clippy had me put here.
/// It knows better.
#[clap(flatten)]
opts: DaemonOpts,
common: Common,
}
assert_eq!(
Opt {
common: Common { arg: 42 }
},
Opt::parse_from(&["test", "42"])
);
let help = utils::get_help::<Opt>();
assert!(help.contains("This is an arg."));
assert!(!help.contains("The very important"));
}
#[test]