diff --git a/docs/tutorial.md b/docs/tutorial.md index 5a285ed..ee004f6 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -624,9 +624,9 @@ parser.add_optional_argument("method", "m").choices('a', 'b', 'c');
#### Value Actions - Functions that are called after parsing an argument's value. -Actions are represented as functions, which take the argument's value as an argument. The available action types are: +Actions are represented as standard C++ callables. The library automatically deduces the action's intent based on the function's signature. The available value action types are: -- `observe` actions | `void(const value_type&)` - applied to the parsed value. No value is returned - this action type is used to perform some logic on the parsed value without modifying it. +- **observe** actions | `void(const value_type&)` - applied to the parsed value. No value is returned. This is used to validate or log the parsed value without modifying it. ```cpp void is_valid_user_tag(const std::string& tag) { @@ -635,23 +635,10 @@ Actions are represented as functions, which take the argument's value as an argu } parser.add_optional_argument("user", "u") - .actions(is_valid_user_tag); + .actions(is_valid_user_tag); // The compiler automatically deduces this as an observer ``` -- `transform` actions | `value_type(const value_type&)` - applied to the parsed value. The returned value will be used to initialize the argument's value. - - ```cpp - std::string to_lower(std::string s) { - for (auto& c : s) - c = static_cast(std::tolower(c)); - return s; - } - - parser.add_optional_argument("key", "k") - .actions(to_lower); - ``` - -- `modify` actions | `void(value_type&)` - applied to the initialized value of an argument. +- **modify** actions | `void(value_type&)` - mutates the parsed value of an argument in-place. ```cpp void capitalize(std::string& s) { @@ -659,17 +646,18 @@ Actions are represented as functions, which take the argument's value as an argu } parser.add_optional_argument("name", "n") - .actions(capitalize); + .actions(capitalize); // The compiler automatically deduces this as a modifier ``` > [!TIP] > -> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reusability. You can pass multiple actions into a single `.actions(...)` call, e.g.: +> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reusability. You can pass multiple actions (both observers and modifiers) into a single `.actions(...)` call, e.g.: > ```cpp > parser.add_optional_argument("email") -> .actions( +> .actions( > validate_email_format, -> validate_active_user +> validate_active_user, +> normalize_lowercase // A modifier that runs after validation > ) > ``` @@ -839,7 +827,7 @@ parser.add_optional_argument("mode") Prints the parser's help message to the output stream and optionally exits with the given code. ```cpp - typename argon::action::on_flag::type print_help( + argon::action::util::state_callable_type print_help( const argon::argument_parser& parser, const std::optional exit_code = std::nullopt, std::ostream& os = std::cout @@ -847,11 +835,11 @@ parser.add_optional_argument("mode") ``` - `check_file_exists` | observe (value type: `std::string`) - + Throws if the provided file path does not exist. - + ```cpp - argon::action::util::callable_type check_file_exists() noexcept; + auto check_file_exists() noexcept; ``` - `gt` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic)) @@ -860,7 +848,7 @@ parser.add_optional_argument("mode") ```cpp template - argon::action::util::callable_type gt(const T lower_bound) noexcept; + auto gt(const T lower_bound) noexcept; ``` - `geq` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic)) @@ -869,7 +857,7 @@ parser.add_optional_argument("mode") ```cpp template - argon::action::util::callable_type geq(const T lower_bound) noexcept; + auto geq(const T lower_bound) noexcept; ``` - `lt` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic)) @@ -878,7 +866,7 @@ parser.add_optional_argument("mode") ```cpp template - argon::action::util::callable_type lt(const T upper_bound) noexcept; + auto lt(const T upper_bound) noexcept; ``` - `leq` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic)) @@ -887,7 +875,7 @@ parser.add_optional_argument("mode") ```cpp template - argon::action::util::callable_type leq(const T upper_bound) noexcept; + auto leq(const T upper_bound) noexcept; ``` - `within` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic)) @@ -895,10 +883,8 @@ parser.add_optional_argument("mode") Checks if the value is within the given interval. Bound inclusivity is customizable using template parameters. ```cpp - template - argon::action::util::callable_type within( - const T lower_bound, const T upper_bound - ) noexcept; + template + auto within(const T lower_bound, const T upper_bound) noexcept; ```
@@ -924,7 +910,7 @@ parser.default_arguments(); ```cpp // equivalent to: parser.add_positional_argument("input") - .actions(argon::action::check_file_exists()) + .actions(argon::action::check_file_exists()) .help("Input file path"); ``` @@ -959,18 +945,18 @@ parser.default_arguments(); - `o_input` and `o_multi_input`: ```cpp - // input - equivalent to: + // o_input - equivalent to: parser.add_optional_argument("input", "i") .required() .nargs(1) - .actions(argon::action::check_file_exists()) + .actions(argon::action::check_file_exists()) .help("Input file path"); - // multi_input - equivalent to: + // o_multi_input - equivalent to: parser.add_optional_argument("input", "i") .required() .nargs(argon::nargs::at_least(1)) - .actions(argon::action::check_file_exists()) + .actions(argon::action::check_file_exists()) .help("Input files paths"); ``` diff --git a/include/argon/action/predefined.hpp b/include/argon/action/predefined.hpp index b88202a..a30ff20 100644 --- a/include/argon/action/predefined.hpp +++ b/include/argon/action/predefined.hpp @@ -19,13 +19,13 @@ std::ostream& operator<<(std::ostream& os, const argument_parser&) noexcept; namespace action { -/// @brief A predefined negative flag action tag that discards the parsed values. +/// @brief A predefined state action tag that discards the parsed values. inline constexpr action::discard_parsed_t discard_parsed{}; -/// @brief A predefined negative flag action tag that discards all parsed, default, and implicit values. +/// @brief A predefined state action tag that discards all parsed, default, and implicit values. inline constexpr action::discard_all_t discard_all{}; -/// @brief A factory function returning a negative action tag that injects specific values. +/// @brief A factory function returning a state action tag that injects specific values. /// @param val The primary value to store. /// @param vals Additional variadic values to store. template @@ -41,7 +41,7 @@ inline action::store_t> store(V&& val, Vs&&... vals) noex // clang-format on } -/// @brief A factory function returning a negative action tag that injects a range of values. +/// @brief A factory function returning a state action tag that injects a range of values. /// @param range A range of values to store. template requires(not traits::c_argument_value_type>) @@ -64,7 +64,7 @@ inline action::store_t>> store /// @param parser Argument parser instance the help message of which will be printed. /// @param exit_code The exit code with which `std::exit` will be called (if not `std::nullopt`). /// @param os The output stream to which the help message will be printed. -inline typename argon::action::on_flag::type print_help( +inline util::state_callable_type print_help( const argument_parser& parser, const std::optional exit_code = std::nullopt, std::ostream& os = std::cout @@ -76,8 +76,8 @@ inline typename argon::action::on_flag::type print_help( }; } -/// @brief Returns an *observe* action which checks whether lower_bound file with the given name exists. -inline util::callable_type check_file_exists() noexcept { +/// @brief Returns a value action callable which checks whether a file with the given path exists. +inline auto check_file_exists() noexcept { return [](const std::string& file_path) { if (not std::filesystem::exists(file_path)) throw std::filesystem::filesystem_error( @@ -88,11 +88,11 @@ inline util::callable_type check_file_exists() noe }; } -/// @brief Returns an *observe* action which checks if a parsed value is greater than the given bound. +/// @brief Returns a value action callable which checks if a parsed value is greater than the given bound. /// @tparam T The *arithmetic* value type. /// @param lower_bound The exclusive lower bound to validate against. template -util::callable_type gt(const T lower_bound) noexcept { +auto gt(const T lower_bound) noexcept { return [lower_bound](const T& value) { if (not (value > lower_bound)) throw std::out_of_range( @@ -101,46 +101,46 @@ util::callable_type gt(const T lower_bound) noexcept { }; } -/// @brief Returns an *observe* action which checks if a parsed value is greater than or equal to the given bound. +/// @brief Returns a value action callable which checks if a parsed value is greater than or equal to the given bound. /// @tparam T The *arithmetic* value type. /// @param lower_bound The inclusive lower bound to validate against. template -util::callable_type geq(const T lower_bound) noexcept { +auto geq(const T lower_bound) noexcept { return [lower_bound](const T& value) { - if (! (value >= lower_bound)) + if (not (value >= lower_bound)) throw std::out_of_range( std::format("Value `{}` must be greater than or equal to `{}`!", value, lower_bound) ); }; } -/// @brief Returns an *observe* action which checks if a parsed value is less than the given bound. +/// @brief Returns a value action callable which checks if a parsed value is less than the given bound. /// @tparam T The *arithmetic* value type. -/// @param lower_bound The exclusive upper bound to validate against. +/// @param upper_bound The exclusive upper bound to validate against. template -util::callable_type lt(const T upper_bound) noexcept { +auto lt(const T upper_bound) noexcept { return [upper_bound](const T& value) { - if (! (value < upper_bound)) + if (not (value < upper_bound)) throw std::out_of_range( std::format("Value `{}` must be less than `{}`!", value, upper_bound) ); }; } -/// @brief Returns an *observe* action which checks if a parsed value is less than or equal to the given bound. +/// @brief Returns a value action callable which checks if a parsed value is less than or equal to the given bound. /// @tparam T The *arithmetic* value type. -/// @param lower_bound The inclusive upper bound to validate against. +/// @param upper_bound The inclusive upper bound to validate against. template -util::callable_type leq(const T upper_bound) noexcept { +auto leq(const T upper_bound) noexcept { return [upper_bound](const T& value) { - if (! (value <= upper_bound)) + if (not (value <= upper_bound)) throw std::out_of_range( std::format("Value `{}` must be less than or equal to `{}`!", value, upper_bound) ); }; } -/// @brief Returns an *observe* action which checks if a parsed value falls within the specified interval. +/// @brief Returns a value action callable which checks if a parsed value falls within the specified interval. /// /// The interval is defined by the given lower and upper bounds, with inclusivity controlled by the template parameters. /// @@ -149,11 +149,8 @@ util::callable_type leq(const T upper_bound) noexcept { /// @tparam RightInclusive Whether the upper bound is inclusive (`]`) or exclusive (`)`) - default: true. /// @param lower_bound The lower bound of the interval. /// @param upper_bound The upper bound of the interval. -/// @todo Add explicit inclusive/exclusive constants (perhaps in argon::traits) template -util::callable_type within( - const T lower_bound, const T upper_bound -) noexcept { +auto within(const T lower_bound, const T upper_bound) noexcept { return [lower_bound, upper_bound](const T& value) { constexpr char left_brace = LeftInclusive ? '[' : '('; constexpr char right_brace = RightInclusive ? ']' : ')'; diff --git a/include/argon/action/traits.hpp b/include/argon/action/traits.hpp index 3843aa1..81fde1d 100644 --- a/include/argon/action/traits.hpp +++ b/include/argon/action/traits.hpp @@ -9,38 +9,49 @@ #include "argon/action/types.hpp" -#include -#include +#include +#include namespace argon::traits { -/// @brief The concept is satisfied when `AS` is a valid *value* action action specifier. -/// @tparam AS The action specifier type. -/// @ingroup traits -template -concept c_value_action_specifier = - argon::traits::c_one_of; - -/// @brief The concept is satisfied when `AS` is a valid *on-flag* action action specifier. -/// @tparam AS The action specifier type. -/// @ingroup traits -template -concept c_flag_action_specifier = argon::traits::c_one_of; - -/// @brief The concept is satisfied when `AS` is a valid action action specifier. -/// @tparam AS The action specifier type. -/// @ingroup traits -template -concept c_action_specifier = c_value_action_specifier or std::same_as; +/// @brief Validates that `AC` is a valid action category tag (`action::value` or `action::on_flag`). +template +concept c_action_category = c_one_of; + +// --- Value Action Concepts --- + +/// @brief Validates that `F` is a valid *observe* callable for value type `T` (`void(const T&)`). +/// @note Safely rejects callables taking non-const `T&`. +template +concept c_observe_callable = + c_argument_value_type and std::invocable&, const T&> + and std::same_as&, const T&>, void>; + +/// @brief Validates that `F` is a valid *modify* callable for value type `T` (`void(T&)`). +/// @note Rejects callables taking `const T&` or `T` by value to prevent accidental no-op modifications. +template +concept c_modify_callable = + c_argument_value_type and std::invocable&, T&> + and (not std::invocable&, const T&>) + and std::same_as&, T&>, void>; + +/// @brief Validates that `F` is a valid value action callable (either observer or modifier) for value type `T`. +template +concept c_value_action = c_observe_callable or c_modify_callable; + +// --- State Action Concepts --- /// @brief Concept validating if a type is a predefined state action tag. template concept c_state_action_specifier = - c_one_of - or c_instantiation_of; + c_one_of, action::discard_parsed_t, action::discard_all_t> + or c_instantiation_of, action::store_t>; -/// @brief Concept validating if a type is a valid state action (a specifier tag or an invocable callable). +/// @brief Concept validating if a type is a valid state action (a specifier tag or a `void()` invocable callable). template -concept c_state_action = c_state_action_specifier> or std::invocable; +concept c_state_action = + c_state_action_specifier + or (std::invocable&> + and std::same_as&>, void>); } // namespace argon::traits diff --git a/include/argon/action/types.hpp b/include/argon/action/types.hpp index 3282426..5172ef3 100644 --- a/include/argon/action/types.hpp +++ b/include/argon/action/types.hpp @@ -3,16 +3,29 @@ // Licensed under the MIT License. See the LICENSE file in the project root for full license information. /// @file argon/action/types.hpp -/// @brief Defies the action specifier types. +/// @brief Defines the action category tags and state action specifiers. #pragma once #include "argon/traits.hpp" #include +#include namespace argon::action { +// --- Action Category Tags --- + +/// @brief Action category tag for *value* actions (executed sequentially when parsing a value). +struct value {}; + +/// @brief Action category tag for *on-flag* state actions (executed when an optional flag is encountered). +struct on_flag { + using type = std::function; +}; + +// --- Predefined State Action Specifiers --- + /// @brief Tag type used to discard an argument's parsed values. struct discard_parsed_t {}; @@ -25,41 +38,4 @@ struct store_t { std::vector values; }; -/// @brief An observing *value* action specifier. -/// -/// Represents an argument action which observes the parsed value and -/// performs some logic on it without modifying it. -struct observe { - template - using type = std::function; -}; - -/// @brief A transformating *value* action specifier. -/// -/// Represents an argument action which transforms the parsed value and -/// returns a new value with which the argument will be initialized. -struct transform { - template - using type = std::function; -}; - -/// @brief A modifying *value* action specifier. -/// -/// Represents an argument action which modifies the value of an -/// already initialized argument. -struct modify { - template - using type = std::function; -}; - -/// @brief An on-flag action specifier. -/// -/// Represents an action associated with an argument that performs -/// some logic, independently of the parsed value. -/// -/// @note An on-flag action is executed immediately after parsing the argument's flag and before parsing the following values. -struct on_flag { - using type = std::function; -}; - } // namespace argon::action diff --git a/include/argon/action/util.hpp b/include/argon/action/util.hpp index 458f60d..abceb0d 100644 --- a/include/argon/action/util.hpp +++ b/include/argon/action/util.hpp @@ -11,22 +11,22 @@ #include "argon/action/traits.hpp" +#include #include #include namespace argon::action::util { -/// @brief Template argument action callable type alias. +/// @brief State action callable type alias. /// @ingroup util -template -using callable_type = typename AS::template type; +using state_callable_type = typename action::on_flag::type; -/// @brief Template argument action callabla variant type alias. +/// @brief Variant type used to store different value action signatures. template using value_action_variant_type = std::variant< - callable_type, - callable_type, - callable_type>; + std::function, // observe + std::function // modify + >; /** * @brief A visitor structure used to apply *value* actions. @@ -39,25 +39,15 @@ struct apply_visitor { /** * @brief The call operator overload for the *observe* action type. - * @param action The *observe* action callable. */ - void operator()(const callable_type& action) const { + void operator()(const std::function& action) const { action(std::as_const(value)); } - /** - * @brief The call operator overload for the *transform* action type. - * @param action The *transform* action callable. - */ - void operator()(const callable_type& action) const { - value = action(std::as_const(value)); - } - /** * @brief The call operator overload for the *modify* action type. - * @param action The *modify* action callable. */ - void operator()(const callable_type& action) const { + void operator()(const std::function& action) const { action(value); } diff --git a/include/argon/argument.hpp b/include/argon/argument.hpp index f782732..876fa70 100644 --- a/include/argon/argument.hpp +++ b/include/argon/argument.hpp @@ -256,38 +256,35 @@ class argument : public typed_argument_base { /** * @brief Set multiple *value* actions for the argument. - * @tparam AS The action specifier type (e.g., `argon::action::observe`, `argon::action::transform`, `argon::action::modify`). - * @tparam Fs The types of the action callables. + * @tparam AC The action category type (defaults to `argon::action::value`). + * @tparam Fs The raw callable types. * @param value_actions A variadic list of callables to be executed sequentially after the argument's value is parsed. * @return Reference to the argument instance. * @note The method is enabled only if `value_type` is not `none_type`. */ - template + template argument& actions(Fs&&... value_actions) noexcept - requires(not traits::c_is_none) + requires(std::same_as and not traits::c_is_none) { - using callable_type = action::util::callable_type; - // clang-format off - (this->_value_actions.emplace_back(static_cast(std::forward(value_actions))), ...); - // clang-format on + (this->_add_value_action(std::forward(value_actions)), ...); return *this; } /** * @brief Set multiple *on-flag* actions for the argument. - * @tparam AS The action specifier type (must be `argon::action::on_flag`). + * @tparam AC The action category type (must be explicitly set to `argon::action::on_flag`). * @tparam Fs The types of the state action callables or predefined action tags. * @param state_actions A variadic list of predefined tags (e.g., `action::discard_parsed`, `action::store`) or generic invocable callables. * @return Reference to the argument instance. * @note The method is enabled only for optional arguments. * @note On-flag actions are executed immediately after parsing the argument's flag, before parsing any subsequent values. */ - template + template argument& actions(Fs&&... state_actions) - requires(type == argument_type::optional) + requires(std::same_as and type == argument_type::optional) { // clang-format off - (this->_flag_actions.emplace_back(this->_compile_state_action(std::forward(state_actions))), ...); + (this->_state_actions.emplace_back(this->_compile_state_action(std::forward(state_actions))), ...); // clang-format on return *this; } @@ -310,23 +307,24 @@ class argument : public typed_argument_base { if constexpr (sizeof...(Actions) == 0) { if constexpr (std::same_as) { - this->_negative_actions.emplace_back( + this->_neg_state_actions.emplace_back( this->_compile_state_action(action::discard_parsed) ); // Deduce the logical inverse based on the default value set by add_flag bool inverse_val = not this->_default_values.empty() ? this->_default_values.front() : false; - this->_negative_actions.emplace_back( + this->_neg_state_actions.emplace_back( this->_compile_state_action(action::store(inverse_val)) ); } else { - this->_negative_actions.emplace_back(this->_compile_state_action(action::discard_all - )); + this->_neg_state_actions.emplace_back( + this->_compile_state_action(action::discard_all) + ); } } else { - (this->_negative_actions.emplace_back( + (this->_neg_state_actions.emplace_back( this->_compile_state_action(std::forward(state_actions)) ), ...); @@ -550,7 +548,7 @@ class argument : public typed_argument_base { using value_action_type = action::util::value_action_variant_type; /// @brief The argument's state action type alias. - using state_action_type = std::function; + using state_action_type = action::util::state_callable_type; /// @brief The argument's value-argument-specific type alias. /// @tparam _T The actual type used if the argument's `value_type` is not `none_type`. @@ -626,7 +624,7 @@ class argument : public typed_argument_base { this->_ignore_predefined = false; if constexpr (type == argument_type::optional) { ++this->_count; - for (const auto& action : this->_flag_actions) + for (const auto& action : this->_state_actions) action(); } @@ -639,7 +637,7 @@ class argument : public typed_argument_base { this->_last_flag_sign = false; if constexpr (type == argument_type::optional) { ++this->_count; - for (const auto& action : this->_negative_actions) + for (const auto& action : this->_neg_state_actions) action(); } @@ -661,6 +659,12 @@ class argument : public typed_argument_base { if (this->_parsed_nvalues == 0ull and this->_has_predefined_values_impl()) return std::weak_ordering::equivalent; + // If the last interaction was a negative flag, the user explicitly bypassed values. + if constexpr (type == argument_type::optional) { + if (this->is_used() and not this->_last_flag_sign) + return std::weak_ordering::equivalent; + } + return this->_parsed_nvalues <=> this->_nargs_range; } @@ -710,12 +714,17 @@ class argument : public typed_argument_base { if (this->_ignore_predefined) return false; - if constexpr (type == argument_type::positional) + if constexpr (type == argument_type::positional) { return not this->_default_values.empty(); - else - return not this->_default_values.empty() - or (this->is_used() and this->_last_flag_sign and not this->_implicit_values.empty() - ); + } + else { + // If the user explicitly used the positive flag, check implicit values. + if (this->is_used() and this->_last_flag_sign) + return not this->_implicit_values.empty(); + + // Otherwise (unused, or negative flag), we check default values. + return not this->_default_values.empty(); + } } /** @@ -855,6 +864,20 @@ class argument : public typed_argument_base { // --- configuration --- + template F> + void _add_value_action(F&& f) { + if constexpr (traits::c_observe_callable) { + this->_value_actions.emplace_back( + std::in_place_type>, std::forward(f) + ); + } + else if constexpr (traits::c_modify_callable) { + this->_value_actions.emplace_back( + std::in_place_type>, std::forward(f) + ); + } + } + template state_action_type _compile_state_action(F&& action) { using action_t = std::remove_cvref_t; @@ -902,9 +925,9 @@ class argument : public typed_argument_base { [[no_unique_address]] value_arg_specific_type> _value_actions; ///< The argument's value actions collection. [[no_unique_address]] optional_specific_type> - _flag_actions; ///< The optional argument's flag actions collection. + _state_actions; ///< The optional argument's flag actions collection. [[no_unique_address]] optional_specific_type> - _negative_actions; ///< The action executed when the negative flag is used. + _neg_state_actions; ///< The action executed when the negative flag is used. bool _required : 1; ///< The argument's `required` attribute value. bool _suppress_arg_checks : 1 = diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index aceed01..4581730 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -1788,7 +1788,7 @@ inline void add_default_argument( switch (arg_discriminator) { case default_argument::p_input: arg_parser.add_positional_argument("input") - .actions(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input file path"); break; @@ -1814,7 +1814,7 @@ inline void add_default_argument( case default_argument::o_input: arg_parser.add_optional_argument("input", "i") .nargs(1ull) - .actions(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input file path"); break; @@ -1825,7 +1825,7 @@ inline void add_default_argument( case default_argument::o_multi_input: arg_parser.add_optional_argument("input", "i") .nargs(argon::nargs::at_least(1ull)) - .actions(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input files paths"); break; diff --git a/tests/source/test_optional_argument.cpp b/tests/source/test_optional_argument.cpp index 90f5c44..32d12d4 100644 --- a/tests/source/test_optional_argument.cpp +++ b/tests/source/test_optional_argument.cpp @@ -691,7 +691,7 @@ TEST_CASE_FIXTURE( } }; - sut.actions(is_power_of_two); + sut.actions(is_power_of_two); CHECK_THROWS_AS(set_value(sut, 3), std::runtime_error); @@ -700,18 +700,9 @@ TEST_CASE_FIXTURE( CHECK_EQ(sut.value(), valid_value); } - SUBCASE("transform action") { - const auto double_action = [](const sut_value_type& value) { return 2 * value; }; - sut.actions(double_action); - - set_value(sut, arbitrary_value); - - CHECK_EQ(sut.value(), double_action(arbitrary_value)); - } - SUBCASE("modify action") { const auto double_action = [](sut_value_type& value) { value *= 2; }; - sut.actions(double_action); + sut.actions(double_action); auto test_value = arbitrary_value; @@ -724,10 +715,12 @@ TEST_CASE_FIXTURE( SUBCASE("multiple mixed actions execute sequentially") { sut_value_type capture = 0; sut.actions([&]() { capture = 100; } - ) // Only executes on flag parsing - .actions([&](const sut_value_type& v) { capture += v; }) - .actions([](const sut_value_type& v) { return v + 1; }) - .actions([](sut_value_type& v) { v *= 2; }); + ) // Only executes on flag parsing.actions + .actions( + [&](const sut_value_type& v) { capture += v; }, + [](sut_value_type& v) { v += 1; }, + [](sut_value_type& v) { v *= 2; } + ); // Trigger flag mark_used(sut); diff --git a/tests/source/test_positional_argument.cpp b/tests/source/test_positional_argument.cpp index 8950bc7..9dea2c6 100644 --- a/tests/source/test_positional_argument.cpp +++ b/tests/source/test_positional_argument.cpp @@ -466,7 +466,7 @@ TEST_CASE_FIXTURE(argument_test_fixture, "set_value(any) should perform the spec } }; - sut.actions(is_power_of_two); + sut.actions(is_power_of_two); CHECK_THROWS_AS(set_value(sut, 3), std::runtime_error); @@ -475,18 +475,9 @@ TEST_CASE_FIXTURE(argument_test_fixture, "set_value(any) should perform the spec CHECK_EQ(sut.value(), valid_value); } - SUBCASE("transform action") { - const auto double_action = [](const sut_value_type& value) { return 2 * value; }; - sut.actions(double_action); - - set_value(sut, valid_value); - - CHECK_EQ(sut.value(), double_action(valid_value)); - } - SUBCASE("modify action") { const auto double_action = [](sut_value_type& value) { value *= 2; }; - sut.actions(double_action); + sut.actions(double_action); auto test_value = valid_value; @@ -498,10 +489,11 @@ TEST_CASE_FIXTURE(argument_test_fixture, "set_value(any) should perform the spec SUBCASE("multiple mixed actions execute sequentially") { sut_value_type capture = 0; - sut.actions([&](const sut_value_type& v) { capture += v; } - ).actions([](const sut_value_type& v) { - return v + 1; - }).actions([](sut_value_type& v) { v *= 2; }); + sut.actions( + [&capture](const sut_value_type& v) { capture += v; }, + [](sut_value_type& v) { v += 1; }, + [](sut_value_type& v) { v *= 2; } + ); // Initial value = 5 // 1. observe: capture += 5 (capture becomes 5)