Merge pull request #5017 from epage/assert

fix(assert): Allow stateful value parsers
This commit is contained in:
Ed Page 2023-07-18 15:00:29 -05:00 committed by GitHub
commit 6b80a72e50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 0 additions and 132 deletions

View file

@ -1,8 +1,5 @@
use std::cmp::Ordering;
use clap_lex::OsStrExt as _;
use crate::builder::OsStr;
use crate::builder::ValueRange;
use crate::mkeymap::KeyType;
use crate::util::FlatSet;
@ -809,20 +806,6 @@ fn assert_arg(arg: &Arg) {
}
assert_arg_flags(arg);
assert_defaults(arg, "default_value", arg.default_vals.iter());
assert_defaults(
arg,
"default_missing_value",
arg.default_missing_vals.iter(),
);
assert_defaults(
arg,
"default_value_if",
arg.default_vals_ifs
.iter()
.filter_map(|(_, _, default)| default.as_ref()),
);
}
fn assert_arg_flags(arg: &Arg) {
@ -854,37 +837,3 @@ fn assert_arg_flags(arg: &Arg) {
checker!(is_multiple_values_set requires is_takes_value_set);
checker!(is_ignore_case_set requires is_takes_value_set);
}
fn assert_defaults<'d>(
arg: &Arg,
field: &'static str,
defaults: impl IntoIterator<Item = &'d OsStr>,
) {
for default_os in defaults {
let value_parser = arg.get_value_parser();
let assert_cmd = Command::new("assert");
if let Some(val_delim) = arg.get_value_delimiter() {
let mut val_delim_buffer = [0; 4];
let val_delim = val_delim.encode_utf8(&mut val_delim_buffer);
for part in default_os.split(val_delim) {
if let Err(err) = value_parser.parse_ref(&assert_cmd, Some(arg), part) {
panic!(
"Argument `{}`'s {}={:?} failed validation: {}",
arg.get_id(),
field,
part.to_string_lossy(),
err
);
}
}
} else if let Err(err) = value_parser.parse_ref(&assert_cmd, Some(arg), default_os) {
panic!(
"Argument `{}`'s {}={:?} failed validation: {}",
arg.get_id(),
field,
default_os,
err
);
}
}
}

View file

@ -258,38 +258,6 @@ fn delimited_missing_value() {
);
}
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: invalid value 'value' for '[arg]'"]
fn default_missing_values_are_possible_values() {
use clap::{Arg, Command};
let _ = Command::new("test")
.arg(
Arg::new("arg")
.value_parser(["one", "two"])
.default_missing_value("value"),
)
.try_get_matches();
}
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: invalid value 'value' for '[arg]"]
fn default_missing_values_are_valid() {
use clap::{Arg, Command};
let _ = Command::new("test")
.arg(
Arg::new("arg")
.value_parser(clap::value_parser!(u32))
.default_missing_value("value"),
)
.try_get_matches();
}
#[test]
fn valid_index() {
let m = Command::new("df")

View file

@ -793,38 +793,6 @@ fn required_args_with_default_values() {
assert!(m.contains_id("arg"));
}
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_value=\"value\" failed validation: error: invalid value 'value' for '[arg]'"]
fn default_values_are_possible_values() {
use clap::{Arg, Command};
let _ = Command::new("test")
.arg(
Arg::new("arg")
.value_parser(["one", "two"])
.default_value("value"),
)
.try_get_matches();
}
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_value=\"one\" failed validation: error: invalid value 'one' for '[arg]"]
fn invalid_default_values() {
use clap::{Arg, Command};
let _ = Command::new("test")
.arg(
Arg::new("arg")
.value_parser(clap::value_parser!(u32))
.default_value("one"),
)
.try_get_matches();
}
#[test]
fn valid_delimited_default_values() {
use clap::{Arg, Command};
@ -839,23 +807,6 @@ fn valid_delimited_default_values() {
.debug_assert();
}
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_value=\"one\" failed validation: error: invalid value 'one' for '[arg]"]
fn invalid_delimited_default_values() {
use clap::{Arg, Command};
Command::new("test")
.arg(
Arg::new("arg")
.value_parser(clap::value_parser!(u32))
.value_delimiter(',')
.default_value("one,two"),
)
.debug_assert();
}
#[test]
fn with_value_delimiter() {
let cmd = Command::new("multiple_values").arg(