fix(derive): Don't assume use clap::ArgEnum

The error was when doing `#[clap(arg_enum, default_value_t = ...)]`.

Good example of why we should minimize `use`, at least in tests
(besides reducing merge conflicts, code churn, etc).
This commit is contained in:
Ed Page 2022-01-10 15:41:58 -06:00
parent c5ace9aaa6
commit 3326a11be0
2 changed files with 35 additions and 25 deletions

View file

@ -469,7 +469,7 @@ impl Attrs {
quote_spanned!(ident.span()=> { quote_spanned!(ident.span()=> {
{ {
let val: #ty = #val; let val: #ty = #val;
val.to_possible_value().unwrap().get_name() clap::ArgEnum::to_possible_value(&val).unwrap().get_name()
} }
}) })
} else { } else {

View file

@ -7,11 +7,11 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use clap::{ArgEnum, Parser, PossibleValue}; use clap::Parser;
#[test] #[test]
fn basic() { fn basic() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,
@ -40,7 +40,7 @@ fn basic() {
#[test] #[test]
fn default_value() { fn default_value() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,
@ -80,7 +80,7 @@ fn default_value() {
#[test] #[test]
fn multi_word_is_renamed_kebab() { fn multi_word_is_renamed_kebab() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
enum ArgChoice { enum ArgChoice {
FooBar, FooBar,
@ -110,7 +110,7 @@ fn multi_word_is_renamed_kebab() {
#[test] #[test]
fn variant_with_defined_casing() { fn variant_with_defined_casing() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
#[clap(rename_all = "screaming_snake")] #[clap(rename_all = "screaming_snake")]
FooBar, FooBar,
@ -133,7 +133,7 @@ fn variant_with_defined_casing() {
#[test] #[test]
fn casing_is_propagated_from_parent() { fn casing_is_propagated_from_parent() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
#[clap(rename_all = "screaming_snake")] #[clap(rename_all = "screaming_snake")]
enum ArgChoice { enum ArgChoice {
FooBar, FooBar,
@ -156,7 +156,7 @@ fn casing_is_propagated_from_parent() {
#[test] #[test]
fn casing_propagation_is_overridden() { fn casing_propagation_is_overridden() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
#[clap(rename_all = "screaming_snake")] #[clap(rename_all = "screaming_snake")]
enum ArgChoice { enum ArgChoice {
#[clap(rename_all = "camel")] #[clap(rename_all = "camel")]
@ -181,7 +181,7 @@ fn casing_propagation_is_overridden() {
#[test] #[test]
fn ignore_case() { fn ignore_case() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
} }
@ -208,7 +208,7 @@ fn ignore_case() {
#[test] #[test]
fn ignore_case_set_to_false() { fn ignore_case_set_to_false() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
} }
@ -230,7 +230,7 @@ fn ignore_case_set_to_false() {
#[test] #[test]
fn alias() { fn alias() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
#[clap(alias = "TOTP")] #[clap(alias = "TOTP")]
Totp, Totp,
@ -258,7 +258,7 @@ fn alias() {
#[test] #[test]
fn multiple_alias() { fn multiple_alias() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
#[clap(alias = "TOTP", alias = "t")] #[clap(alias = "TOTP", alias = "t")]
Totp, Totp,
@ -292,7 +292,7 @@ fn multiple_alias() {
#[test] #[test]
fn skip_variant() { fn skip_variant() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
#[allow(dead_code)] // silence warning about `Baz` being unused #[allow(dead_code)] // silence warning about `Baz` being unused
enum ArgChoice { enum ArgChoice {
Foo, Foo,
@ -302,31 +302,41 @@ fn skip_variant() {
} }
assert_eq!( assert_eq!(
ArgChoice::value_variants() <ArgChoice as clap::ArgEnum>::value_variants()
.iter() .iter()
.map(ArgEnum::to_possible_value) .map(clap::ArgEnum::to_possible_value)
.map(Option::unwrap) .map(Option::unwrap)
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
vec![PossibleValue::new("foo"), PossibleValue::new("bar")] vec![
clap::PossibleValue::new("foo"),
clap::PossibleValue::new("bar")
]
); );
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok()); {
assert!(ArgChoice::from_str("baz", true).is_err()); use clap::ArgEnum;
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok());
assert!(ArgChoice::from_str("baz", true).is_err());
}
} }
#[test] #[test]
fn from_str_invalid() { fn from_str_invalid() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
} }
assert!(ArgChoice::from_str("bar", true).is_err()); {
use clap::ArgEnum;
assert!(ArgChoice::from_str("bar", true).is_err());
}
} }
#[test] #[test]
fn option_type() { fn option_type() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,
@ -356,7 +366,7 @@ fn option_type() {
#[test] #[test]
fn option_option_type() { fn option_option_type() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,
@ -390,7 +400,7 @@ fn option_option_type() {
#[test] #[test]
fn vec_type() { fn vec_type() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,
@ -420,7 +430,7 @@ fn vec_type() {
#[test] #[test]
fn option_vec_type() { fn option_vec_type() {
#[derive(ArgEnum, PartialEq, Debug, Clone)] #[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
enum ArgChoice { enum ArgChoice {
Foo, Foo,
Bar, Bar,