| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- //
- // action_test.cpp
- // program_args-test
- //
- // Created by Sam Jaffe on 2/13/22.
- //
- #include "program_args/arguments.h"
- #include "xcode_gtest_helper.h"
- using testing::NotNull;
- namespace program {
- struct ArgumentTestHelper {
- template <typename Impl> bool has_main(Impl const & args) const {
- return static_cast<bool>(args.main_callback);
- }
- template <typename Impl> int main(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);
- }
- program::detail::Any g_action;
- template <typename Args, typename Action>
- int typed_main(Args const &, Action const & action) {
- g_action = program::detail::Any(&action);
- 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');
- };
- class Push : public program::Arguments<Push> {
- public:
- using Arguments::Arguments;
- std::string remote = argument(0, "repository") = PROGRAM_DEFER(_remote());
- private:
- std::string _remote() const;
- };
- 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;
- std::string pwd = option('C');
- bool verbose = flag("verbose", 'v');
- Commit commit = action("commit");
- Checkout checkout = action("checkout");
- Push push = action("push");
- };
- std::string Push::_remote() const {
- if (Git const * git = Arguments::parent<Git>()) { return git->pwd; }
- return "";
- }
- 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"});
- EXPECT_THAT(git.commit.message, "this is a message");
- }
- TEST(ActionTest, CanFetchParentInfo) {
- Git git = parse<Git>({"", "-C", "./submodules/X", "push"});
- EXPECT_THAT(git.push.remote, "./submodules/X");
- }
|