Skip to content

Commit

Permalink
Adding test, fixing option name to single name in message
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 25, 2017
1 parent 5b4013a commit 34ffa37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class Option : public OptionBase<Option> {
for(const std::function<std::string(std::string &)> &vali : validators_) {
std::string err_msg = vali(result);
if(!err_msg.empty())
throw ValidationError(get_name() + ": " + err_msg);
throw ValidationError(single_name() + ": " + err_msg);
}
}

Expand Down
25 changes: 23 additions & 2 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,8 @@ TEST_F(TApp, SetWithDefaultsIC) {
EXPECT_THROW(run(), CLI::ConversionError);
}

// Added to test defaults on dual method
TEST_F(TApp, OrderedModifingValidators) {
// Added to test ->transform
TEST_F(TApp, OrderedModifingTransforms) {
std::vector<std::string> val;
auto m = app.add_option("-m", val);
m->transform([](std::string x) { return x + "1"; });
Expand All @@ -1185,3 +1185,24 @@ TEST_F(TApp, OrderedModifingValidators) {

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

TEST_F(TApp, ThrowingTransform) {
std::string val;
auto m = app.add_option("-m,--mess", val);
m->transform([](std::string x) -> std::string { throw CLI::ValidationError("My Message"); });

EXPECT_NO_THROW(run());
app.reset();

args = {"-mone"};

ASSERT_THROW(run(), CLI::ValidationError);

app.reset();

try {
run();
} catch(const CLI::ValidationError &e) {
EXPECT_EQ(e.what(), std::string("--mess: My Message"));
}
}

0 comments on commit 34ffa37

Please sign in to comment.