action_test.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // action_test.cpp
  3. // program_args-test
  4. //
  5. // Created by Sam Jaffe on 2/13/22.
  6. //
  7. #include "program_args/arguments.h"
  8. #include "xcode_gtest_helper.h"
  9. using testing::NotNull;
  10. namespace program {
  11. struct ArgumentTestHelper {
  12. template <typename Impl> bool has_main(Impl const & args) const {
  13. return static_cast<bool>(args.main_callback);
  14. }
  15. template <typename Impl> int main(Impl const & args) const {
  16. return args.main_callback(args);
  17. }
  18. };
  19. }
  20. template <typename T, size_t N> static T parse(char const * const (&argv)[N]) {
  21. return T(N, argv);
  22. }
  23. program::detail::Any g_action;
  24. template <typename Args, typename Action>
  25. int typed_main(Args const &, Action const & action) {
  26. g_action = program::detail::Any(&action);
  27. return 0;
  28. }
  29. struct Checkout : program::Arguments<Checkout> {
  30. using Arguments::Arguments;
  31. std::string commitish = argument(0, "commit-ish");
  32. };
  33. struct Commit : program::Arguments<Commit> {
  34. using Arguments::Arguments;
  35. std::string message = option("message", 'm');
  36. };
  37. class Push : public program::Arguments<Push> {
  38. public:
  39. using Arguments::Arguments;
  40. std::string remote = argument(0, "repository") = PROGRAM_DEFER(_remote());
  41. private:
  42. std::string _remote() const;
  43. };
  44. struct Bad1 : program::Arguments<Bad1> {
  45. using Arguments::Arguments;
  46. Commit commit = action("commit");
  47. std::string arg0 = argument(0, "arg0");
  48. };
  49. struct Bad2 : program::Arguments<Bad2> {
  50. using Arguments::Arguments;
  51. std::string arg0 = argument(0, "arg0");
  52. Commit commit = action("commit");
  53. };
  54. struct Git : program::Arguments<Git> {
  55. using Arguments::Arguments;
  56. std::string pwd = option('C');
  57. bool verbose = flag("verbose", 'v');
  58. Commit commit = action("commit");
  59. Checkout checkout = action("checkout");
  60. Push push = action("push");
  61. };
  62. std::string Push::_remote() const {
  63. if (Git const * git = Arguments::parent<Git>()) { return git->pwd; }
  64. return "";
  65. }
  66. TEST(ActionTest, CannotMixActionAndArgument) {
  67. EXPECT_THROW(Bad1(), program::ArgumentMixingError);
  68. EXPECT_THROW(Bad2(), program::ArgumentMixingError);
  69. }
  70. TEST(ActionTest, CanRunWithMultipleActions) { EXPECT_NO_THROW(Git()); }
  71. TEST(ActionTest, ActionIsRouted) {
  72. Git git = parse<Git>({"", "-v", "commit", "-m", "this is a message"});
  73. EXPECT_THAT(git.commit.message, "this is a message");
  74. }
  75. TEST(ActionTest, CanFetchParentInfo) {
  76. Git git = parse<Git>({"", "-C", "./submodules/X", "push"});
  77. EXPECT_THAT(git.push.remote, "./submodules/X");
  78. }