Skip to content

Commit

Permalink
Adding test and keeping validator API similar
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 24, 2017
1 parent 9616345 commit c8380d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ class Option : public OptionBase<Option> {
}

/// Adds a validator
Option *check(std::function<bool(std::string &)> validator) {
Option *check(std::function<bool(const std::string &)> validator) {
validators_.emplace_back(validator);
return this;
}

/// Adds a validator-like function that can change result
Option *transform(std::function<bool(std::string &)> validator) {
validators_.push_back(validator);
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace CLI {
/// @defgroup validator_group Validators
/// @brief Some validators that are provided
///
/// These are simple `bool(std::string)` validators that are useful.
/// These are simple `bool(std::string&)` validators that are useful.
/// @{

/// Check for an existing file
Expand Down
20 changes: 20 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,23 @@ TEST_F(TApp, SetWithDefaultsIC) {

EXPECT_THROW(run(), CLI::ConversionError);
}

// Added to test defaults on dual method
TEST_F(TApp, OrderedModifingValidators) {
std::vector<std::string> val;
auto m = app.add_option("-m", val);
m->transform([](std::string &x) {
x += "1";
return true;
});
m->transform([](std::string &x) {
x += "2";
return true;
});

args = {"-mone", "-mtwo"};

run();

EXPECT_EQ(val, std::vector<std::string>({"one12", "two12"}));
}

0 comments on commit c8380d6

Please sign in to comment.