Commit 3cf5156b5e4dd194181eb4cccae94bc4441b0379
Committed by
Henry Schreiner
1 parent
02548a64
Adding tests for help print
Showing
2 changed files
with
58 additions
and
4 deletions
include/CLI/App.hpp
| ... | ... | @@ -727,20 +727,20 @@ class App { |
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | /// Print a nice error message and return the exit code |
| 730 | - int exit(const Error &e) const { | |
| 730 | + int exit(const Error &e, std::ostream &out = std::cout, std::ostream &err = std::cerr) const { | |
| 731 | 731 | |
| 732 | 732 | /// Avoid printing anything if this is a CLI::RuntimeError |
| 733 | 733 | if(dynamic_cast<const CLI::RuntimeError *>(&e) != nullptr) |
| 734 | 734 | return e.get_exit_code(); |
| 735 | 735 | |
| 736 | 736 | if(dynamic_cast<const CLI::CallForHelp *>(&e) != nullptr) { |
| 737 | - std::cout << help(); | |
| 737 | + out << help(); | |
| 738 | 738 | return e.get_exit_code(); |
| 739 | 739 | } |
| 740 | 740 | |
| 741 | 741 | if(e.exit_code != static_cast<int>(ExitCodes::Success)) { |
| 742 | 742 | if(failure_message_) |
| 743 | - std::cerr << failure_message_(this, e) << std::flush; | |
| 743 | + err << failure_message_(this, e) << std::flush; | |
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | return e.get_exit_code(); |
| ... | ... | @@ -1428,17 +1428,20 @@ class App { |
| 1428 | 1428 | }; |
| 1429 | 1429 | |
| 1430 | 1430 | namespace FailureMessage { |
| 1431 | + | |
| 1431 | 1432 | inline std::string simple(const App *app, const Error &e) { |
| 1432 | 1433 | std::string header = std::string("ERROR: ") + e.what() + "\n"; |
| 1433 | 1434 | if(app->get_help_ptr() != nullptr) |
| 1434 | - header += "Run with " + app->get_help_ptr()->single_name() + " for more help\n"; | |
| 1435 | + header += "Run with " + app->get_help_ptr()->single_name() + " for more information.\n"; | |
| 1435 | 1436 | return header; |
| 1436 | 1437 | }; |
| 1438 | + | |
| 1437 | 1439 | inline std::string help(const App *app, const Error &e) { |
| 1438 | 1440 | std::string header = std::string("ERROR: ") + e.what() + "\n"; |
| 1439 | 1441 | header += app->help(); |
| 1440 | 1442 | return header; |
| 1441 | 1443 | }; |
| 1444 | + | |
| 1442 | 1445 | } // namespace FailureMessage |
| 1443 | 1446 | |
| 1444 | 1447 | namespace detail { | ... | ... |
tests/HelpTest.cpp
| ... | ... | @@ -386,3 +386,54 @@ TEST(Exit, ExitCodes) { |
| 386 | 386 | EXPECT_EQ(42, app.exit(CLI::RuntimeError(42))); |
| 387 | 387 | EXPECT_EQ(1, app.exit(CLI::RuntimeError())); // Not sure if a default here is a good thing |
| 388 | 388 | } |
| 389 | + | |
| 390 | +struct CapturedHelp : public ::testing::Test { | |
| 391 | + CLI::App app{"My Test Program"}; | |
| 392 | + std::stringstream out; | |
| 393 | + std::stringstream err; | |
| 394 | + | |
| 395 | + int run(const CLI::Error &e) { return app.exit(e, out, err); } | |
| 396 | + | |
| 397 | + void reset() { | |
| 398 | + out.clear(); | |
| 399 | + err.clear(); | |
| 400 | + } | |
| 401 | +}; | |
| 402 | + | |
| 403 | +TEST_F(CapturedHelp, Sucessful) { | |
| 404 | + EXPECT_EQ(run(CLI::Success()), 0); | |
| 405 | + EXPECT_EQ(out.str(), ""); | |
| 406 | + EXPECT_EQ(err.str(), ""); | |
| 407 | +} | |
| 408 | + | |
| 409 | +TEST_F(CapturedHelp, JustAnError) { | |
| 410 | + EXPECT_EQ(run(CLI::RuntimeError(42)), 42); | |
| 411 | + EXPECT_EQ(out.str(), ""); | |
| 412 | + EXPECT_EQ(err.str(), ""); | |
| 413 | +} | |
| 414 | + | |
| 415 | +TEST_F(CapturedHelp, CallForHelp) { | |
| 416 | + EXPECT_EQ(run(CLI::CallForHelp()), 0); | |
| 417 | + EXPECT_EQ(out.str(), app.help()); | |
| 418 | + EXPECT_EQ(err.str(), ""); | |
| 419 | +} | |
| 420 | + | |
| 421 | +TEST_F(CapturedHelp, NormalError) { | |
| 422 | + EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast<int>(CLI::ExitCodes::Extras)); | |
| 423 | + EXPECT_EQ(out.str(), ""); | |
| 424 | + EXPECT_THAT(err.str(), HasSubstr("for more information")); | |
| 425 | + EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError")); | |
| 426 | + EXPECT_THAT(err.str(), HasSubstr("Thing")); | |
| 427 | + EXPECT_THAT(err.str(), Not(HasSubstr("Usage"))); | |
| 428 | +} | |
| 429 | + | |
| 430 | +TEST_F(CapturedHelp, RepacedError) { | |
| 431 | + app.set_failure_message(CLI::FailureMessage::help); | |
| 432 | + | |
| 433 | + EXPECT_EQ(run(CLI::ExtrasError("Thing")), static_cast<int>(CLI::ExitCodes::Extras)); | |
| 434 | + EXPECT_EQ(out.str(), ""); | |
| 435 | + EXPECT_THAT(err.str(), Not(HasSubstr("for more information"))); | |
| 436 | + EXPECT_THAT(err.str(), HasSubstr("ERROR: ExtrasError")); | |
| 437 | + EXPECT_THAT(err.str(), HasSubstr("Thing")); | |
| 438 | + EXPECT_THAT(err.str(), HasSubstr("Usage")); | |
| 439 | +} | ... | ... |