cli_test.cxx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_TRUE
  17. #define EXPECT_TRUE ASSERT_TRUE
  18. #undef EXPECT_FALSE
  19. #define EXPECT_FALSE ASSERT_FALSE
  20. #endif
  21. TEST(CliTest, EndsOnQuit) {
  22. std::stringstream input{R"(quit)"};
  23. cli::cli(input).run();
  24. }
  25. TEST(CliTest, CanInputNoArgsCommand) {
  26. bool was_hit{false};
  27. std::stringstream input{R"(act)"};
  28. cli::cli(input).register_callback("act", [&](){ was_hit = true; }).run();
  29. EXPECT_TRUE(was_hit);
  30. }
  31. TEST(CliTest, WillSkipCallbackIfNotEnoughArgs) {
  32. bool was_hit{false};
  33. std::stringstream input{R"(act)"};
  34. cli::cli(input).register_callback("act", [&](int){ was_hit = true; }).run();
  35. EXPECT_FALSE(was_hit);
  36. }
  37. struct example {};
  38. void cli_print(example const &) { throw example{}; }
  39. TEST(CliTest, QueryInvokesADLFunction) {
  40. std::stringstream input{R"(query)"};
  41. auto func = [](){ return example{}; };
  42. EXPECT_THROW(cli::cli(input).register_query("query", func).run(), example);
  43. }