cli_test.cxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // cli_test.cxx
  3. // cli-test
  4. //
  5. // Created by Sam Jaffe on 10/9/20.
  6. // Copyright © 2020 Sam Jaffe. All rights reserved.
  7. //
  8. #include "cli/cli.h"
  9. #include <gmock/gmock.h>
  10. #include <gtest/gtest.h>
  11. #if XCODE_UNIT_TEST
  12. // This is a hack to allow XCode to properly display failures when running
  13. // unit tests.
  14. #undef EXPECT_THAT
  15. #define EXPECT_THAT ASSERT_THAT
  16. #undef EXPECT_THROW
  17. #define EXPECT_THROW ASSERT_THROW
  18. #undef EXPECT_TRUE
  19. #define EXPECT_TRUE ASSERT_TRUE
  20. #undef EXPECT_FALSE
  21. #define EXPECT_FALSE ASSERT_FALSE
  22. #endif
  23. TEST(CliTest, EndsOnQuit) {
  24. std::stringstream input{R"(quit)"};
  25. cli::cli(input).run();
  26. }
  27. TEST(CliTest, CanInputNoArgsCommand) {
  28. bool was_hit{false};
  29. std::stringstream input{R"(act)"};
  30. cli::cli(input).register_callback("act", [&](){ was_hit = true; }).run();
  31. EXPECT_TRUE(was_hit);
  32. }
  33. TEST(CliTest, WillSkipCallbackIfNotEnoughArgs) {
  34. bool was_hit{false};
  35. std::stringstream input{R"(act)"};
  36. cli::cli(input).register_callback("act", [&](int){ was_hit = true; }).run();
  37. EXPECT_FALSE(was_hit);
  38. }
  39. struct example {};
  40. void cli_print(example const &) { throw example{}; }
  41. TEST(CliTest, QueryInvokesADLFunction) {
  42. std::stringstream input{R"(query)"};
  43. auto func = [](){ return example{}; };
  44. EXPECT_THROW(cli::cli(input).register_query("query", func).run(), example);
  45. }