| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // 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 <typename Impl> bool has_main(Arguments<Impl> const &args) const {
- return static_cast<bool>(args.main_callback);
- }
- template <typename Impl> int main(Arguments<Impl> const &args) const {
- return args.main_callback(args);
- }
- };
- }
- template <typename T, size_t N> static T parse(char const * const (&argv)[N]) {
- return T(N, argv);
- }
- std::string g_type_name;
- template <typename Args, typename Action>
- int typed_main(Args const &, Action const &) {
- g_type_name = typeid(Action).name();
- return 0;
- }
- struct Checkout : program::Arguments<Checkout> {
- using Arguments::Arguments;
- std::string commitish = argument(0, "commit-ish");
- };
- struct Commit : program::Arguments<Commit> {
- using Arguments::Arguments;
- std::string message = option("message", 'm');
- };
- struct Bad1 : program::Arguments<Bad1> {
- using Arguments::Arguments;
- Commit commit = action("commit");
- std::string arg0 = argument(0, "arg0");
- };
- struct Bad2 : program::Arguments<Bad2> {
- using Arguments::Arguments;
- std::string arg0 = argument(0, "arg0");
- Commit commit = action("commit");
- };
- struct Git : program::Arguments<Git> {
- using 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<Git>({"", "-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());
- }
|