From 4caa536ca41c774cf441157f91234d9db4dcbd24 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 18 Jul 2023 14:11:56 -0500 Subject: [PATCH] fix(assert): Allow stateful value parsers We'll need to re-evaluate how to solve #3202. Fixes #4643 --- clap_builder/src/builder/debug_asserts.rs | 51 ----------------------- tests/builder/default_missing_vals.rs | 32 -------------- tests/builder/default_vals.rs | 49 ---------------------- 3 files changed, 132 deletions(-) diff --git a/clap_builder/src/builder/debug_asserts.rs b/clap_builder/src/builder/debug_asserts.rs index ef970cda..29008be9 100644 --- a/clap_builder/src/builder/debug_asserts.rs +++ b/clap_builder/src/builder/debug_asserts.rs @@ -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, -) { - 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 - ); - } - } -} diff --git a/tests/builder/default_missing_vals.rs b/tests/builder/default_missing_vals.rs index da34c28b..98efafcd 100644 --- a/tests/builder/default_missing_vals.rs +++ b/tests/builder/default_missing_vals.rs @@ -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") diff --git a/tests/builder/default_vals.rs b/tests/builder/default_vals.rs index e8ddf7c1..c10ddedb 100644 --- a/tests/builder/default_vals.rs +++ b/tests/builder/default_vals.rs @@ -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(