clap/tests/derive/default_value.rs

178 lines
4.8 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
use clap::{CommandFactory, Parser};
use crate::utils;
#[test]
fn default_value() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(default_value = "3")]
arg: i32,
}
test(derive): Provide better error info `Parser::parse_from` will call `exit` on failure and we don't just lose backtrace information but we don't even know which of the tests running in parallel panicked. I ran into this when experimenting with `clap_derive` and I couldn't tell what actually failed. So let's switch to `Parse::try_parse_from`. Errors went from: ``` test option_option ... ok error: Found argument 'bar' which wasn't expected, or isn't valid in this context USAGE: clap_derive [OPTIONS] For more information try --help error: test failed, to rerun pass '--test arg_enum' ``` To: ``` test option_option ... ok test variant_with_defined_casing ... ok test skip_variant ... ok test default_value ... ok test vector ... FAILED test option_vector ... ok failures: ---- vector stdout ---- thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto , pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), (" \n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: vector test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass '--test arg_enum' ```
2021-10-30 14:55:50 +00:00
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: 3]"));
}
#[test]
fn default_value_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(default_value_t = 3)]
arg: i32,
}
test(derive): Provide better error info `Parser::parse_from` will call `exit` on failure and we don't just lose backtrace information but we don't even know which of the tests running in parallel panicked. I ran into this when experimenting with `clap_derive` and I couldn't tell what actually failed. So let's switch to `Parse::try_parse_from`. Errors went from: ``` test option_option ... ok error: Found argument 'bar' which wasn't expected, or isn't valid in this context USAGE: clap_derive [OPTIONS] For more information try --help error: test failed, to rerun pass '--test arg_enum' ``` To: ``` test option_option ... ok test variant_with_defined_casing ... ok test skip_variant ... ok test default_value ... ok test vector ... FAILED test option_vector ... ok failures: ---- vector stdout ---- thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto , pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), (" \n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: vector test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass '--test arg_enum' ```
2021-10-30 14:55:50 +00:00
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: 3]"));
}
#[test]
fn auto_default_value_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(default_value_t)]
arg: i32,
}
test(derive): Provide better error info `Parser::parse_from` will call `exit` on failure and we don't just lose backtrace information but we don't even know which of the tests running in parallel panicked. I ran into this when experimenting with `clap_derive` and I couldn't tell what actually failed. So let's switch to `Parse::try_parse_from`. Errors went from: ``` test option_option ... ok error: Found argument 'bar' which wasn't expected, or isn't valid in this context USAGE: clap_derive [OPTIONS] For more information try --help error: test failed, to rerun pass '--test arg_enum' ``` To: ``` test option_option ... ok test variant_with_defined_casing ... ok test skip_variant ... ok test default_value ... ok test vector ... FAILED test option_vector ... ok failures: ---- vector stdout ---- thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto , pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), (" \n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: vector test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass '--test arg_enum' ```
2021-10-30 14:55:50 +00:00
assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: 0]"));
}
#[test]
fn default_values_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(default_values_t = vec![1, 2, 3])]
arg1: Vec<i32>,
#[clap(long, default_values_t = &[4, 5, 6])]
arg2: Vec<i32>,
#[clap(long, default_values_t = [7, 8, 9])]
arg3: Vec<i32>,
#[clap(long, default_values_t = 10..=12)]
arg4: Vec<i32>,
#[clap(long, default_values_t = vec!["hello".to_string(), "world".to_string()])]
arg5: Vec<String>,
#[clap(long, default_values_t = &vec!["foo".to_string(), "bar".to_string()])]
arg6: Vec<String>,
}
assert_eq!(
Opt {
arg1: vec![1, 2, 3],
arg2: vec![4, 5, 6],
arg3: vec![7, 8, 9],
arg4: vec![10, 11, 12],
arg5: vec!["hello".to_string(), "world".to_string()],
arg6: vec!["foo".to_string(), "bar".to_string()],
},
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![1],
arg2: vec![4, 5, 6],
arg3: vec![7, 8, 9],
arg4: vec![10, 11, 12],
arg5: vec!["hello".to_string(), "world".to_string()],
arg6: vec!["foo".to_string(), "bar".to_string()],
},
Opt::try_parse_from(&["test", "1"]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![1, 2, 3],
arg2: vec![4, 5, 6],
arg3: vec![7, 8, 9],
arg4: vec![42, 15],
arg5: vec!["baz".to_string()],
arg6: vec!["foo".to_string(), "bar".to_string()],
},
Opt::try_parse_from(&["test", "--arg4", "42", "--arg4", "15", "--arg5", "baz"]).unwrap()
);
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: 1 2 3]"));
}
#[test]
fn default_value_os_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(default_value_os_t = PathBuf::from("abc.def"))]
arg: PathBuf,
}
assert_eq!(
Opt {
arg: PathBuf::from("abc.def")
},
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt {
arg: PathBuf::from("ghi")
},
Opt::try_parse_from(&["test", "ghi"]).unwrap()
);
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: abc.def]"));
}
#[test]
fn default_values_os_t() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(
default_values_os_t = vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")]
)]
arg1: Vec<PathBuf>,
#[clap(
long,
default_values_os_t = &[PathBuf::from("bar.baz")]
)]
arg2: Vec<PathBuf>,
}
assert_eq!(
Opt {
arg1: vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")],
arg2: vec![PathBuf::from("bar.baz")]
},
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt {
arg1: vec![PathBuf::from("ghi")],
arg2: vec![PathBuf::from("baz.bar"), PathBuf::from("foo.bar")]
},
Opt::try_parse_from(&["test", "ghi", "--arg2", "baz.bar", "--arg2", "foo.bar"]).unwrap()
);
let help = utils::get_long_help::<Opt>();
assert!(help.contains("[default: abc.def 123.foo]"));
}
#[test]
fn detect_os_variant() {
#![allow(unused_parens)] // needed for `as_ref` call
#[derive(clap::Parser)]
pub struct Options {
#[clap(default_value_os = ("123".as_ref()))]
x: String,
}
Options::command().debug_assert();
}