// // action_test.cpp // program_args-test // // Created by Sam Jaffe on 2/13/22. // #include "program_args/arguments.h" #include "xcode_gtest_helper.h" namespace program { struct ArgumentTestHelper { template bool has_main(Arguments const &args) const { return static_cast(args.main_callback); } template int main(Arguments const &args) const { return args.main_callback(args); } }; } template static T parse(char const * const (&argv)[N]) { return T(N, argv); } std::string g_type_name; template int typed_main(Args const &, Action const &) { g_type_name = typeid(Action).name(); return 0; } struct Checkout : program::Arguments { using program::Arguments::Arguments; std::string commitish = argument(0, "commit-ish"); }; struct Commit : program::Arguments { using program::Arguments::Arguments; std::string message = option("message", 'm'); }; struct Bad1 : program::Arguments { using program::Arguments::Arguments; Commit commit = action("commit"); std::string arg0 = argument(0, "arg0"); }; struct Bad2 : program::Arguments { using program::Arguments::Arguments; std::string arg0 = argument(0, "arg0"); Commit commit = action("commit"); }; struct Git : program::Arguments { using program::Arguments::Arguments; bool verbose = flag("verbose", 'v'); Commit commit = action("commit"); Checkout checkout = action("checkout"); }; TEST(ActionTest, CannotMixActionAndArgument) { EXPECT_THROW(Bad1(), program::ArgumentMixingError); EXPECT_THROW(Bad2(), program::ArgumentMixingError); } TEST(ActionTest, CanRunWithMultipleActions) { EXPECT_NO_THROW(Git()); } TEST(ActionTest, ActionIsRouted) { Git git = parse({"", "-v", "commit", "-m", "this is a message"}); program::ArgumentTestHelper helper; EXPECT_TRUE(helper.has_main(git)); helper.main(git); EXPECT_THAT(g_type_name, typeid(Commit).name()); }