2022-02-14 22:09:38 +00:00
|
|
|
use clap::{CommandFactory, Parser};
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
use crate::utils;
|
2019-12-29 15:53:12 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-07-28 13:53:32 +00:00
|
|
|
fn default_value() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2019-12-29 15:53:12 +00:00
|
|
|
struct Opt {
|
2021-07-28 13:53:32 +00:00
|
|
|
#[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());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
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());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
struct Opt {
|
|
|
|
#[clap(default_value_t)]
|
2019-12-29 15:53:12 +00:00
|
|
|
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());
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2019-12-29 15:53:12 +00:00
|
|
|
assert!(help.contains("[default: 0]"));
|
|
|
|
}
|
2021-12-13 22:19:12 +00:00
|
|
|
|
|
|
|
#[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,
|
|
|
|
}
|
2022-02-14 22:04:07 +00:00
|
|
|
Options::command().debug_assert();
|
2021-12-13 22:19:12 +00:00
|
|
|
}
|