Skip to content

Commit

Permalink
fix(assert): Allow stateful value parsers
Browse files Browse the repository at this point in the history
We'll need to re-evaluate how to solve #3202.

Fixes #4643
  • Loading branch information
epage committed Jul 18, 2023
1 parent 51abbe7 commit 4caa536
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 132 deletions.
51 changes: 0 additions & 51 deletions clap_builder/src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
);
}
}
}
32 changes: 0 additions & 32 deletions tests/builder/default_missing_vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
49 changes: 0 additions & 49 deletions tests/builder/default_vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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(
Expand Down

0 comments on commit 4caa536

Please sign in to comment.