Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests gmx::CommandLineModuleManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/commandline/cmdlinemodulemanager.h"
45
46 #include <gmock/gmock.h>
47
48 #include "testutils/cmdlinetest.h"
49 #include "testutils/testasserts.h"
50
51 #include "cmdlinemodulemanagertest.h"
52
53 namespace
54 {
55
56 using gmx::test::CommandLine;
57 using gmx::test::MockModule;
58
59 //! Test fixture for the tests.
60 typedef gmx::test::CommandLineModuleManagerTestBase CommandLineModuleManagerTest;
61
62 TEST_F(CommandLineModuleManagerTest, RunsModule)
63 {
64     const char* const cmdline[] = { "test", "module", "-flag", "yes" };
65     CommandLine       args(cmdline);
66     initManager(args, "test");
67     MockModule& mod1 = addModule("module", "First module");
68     addModule("other", "Second module");
69     using ::testing::_;
70     using ::testing::Args;
71     using ::testing::ElementsAreArray;
72     EXPECT_CALL(mod1, init(_));
73     EXPECT_CALL(mod1, run(_, _)).With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
74     int rc = 0;
75     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
76     ASSERT_EQ(0, rc);
77 }
78
79 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
80 {
81     const char* const cmdline[] = { "test", "help", "module" };
82     CommandLine       args(cmdline);
83     initManager(args, "test");
84     MockModule& mod1 = addModule("module", "First module");
85     addModule("other", "Second module");
86     using ::testing::_;
87     EXPECT_CALL(mod1, writeHelp(_));
88     mod1.setExpectedDisplayName("test module");
89     int rc = 0;
90     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
91     ASSERT_EQ(0, rc);
92 }
93
94 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpAfterQuiet)
95 {
96     const char* const cmdline[] = { "test", "-quiet", "help", "module" };
97     CommandLine       args(cmdline);
98     initManager(args, "test");
99     MockModule& mod1 = addModule("module", "First module");
100     addModule("other", "Second module");
101     using ::testing::_;
102     EXPECT_CALL(mod1, writeHelp(_));
103     mod1.setExpectedDisplayName("test module");
104     int rc = 0;
105     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
106     ASSERT_EQ(0, rc);
107 }
108
109 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
110 {
111     const char* const cmdline[] = { "test", "module", "-h" };
112     CommandLine       args(cmdline);
113     initManager(args, "test");
114     MockModule& mod1 = addModule("module", "First module");
115     addModule("other", "Second module");
116     using ::testing::_;
117     EXPECT_CALL(mod1, writeHelp(_));
118     mod1.setExpectedDisplayName("test module");
119     int rc = 0;
120     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
121     ASSERT_EQ(0, rc);
122 }
123
124 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
125 {
126     const char* const cmdline[] = { "g_module", "-h" };
127     CommandLine       args(cmdline);
128     initManager(args, "g_module");
129     MockModule mod(nullptr, nullptr);
130     manager().setSingleModule(&mod);
131     using ::testing::_;
132     EXPECT_CALL(mod, writeHelp(_));
133     mod.setExpectedDisplayName("g_module");
134     int rc = 0;
135     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
136     ASSERT_EQ(0, rc);
137 }
138
139 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
140 {
141     const char* const cmdline[] = { "test", "test", "-flag", "yes" };
142     CommandLine       args(cmdline);
143     initManager(args, "test");
144     MockModule& mod1 = addModule("test", "Test module");
145     addModule("other", "Second module");
146     using ::testing::_;
147     using ::testing::Args;
148     using ::testing::ElementsAreArray;
149     EXPECT_CALL(mod1, init(_));
150     EXPECT_CALL(mod1, run(_, _)).With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
151     int rc = 0;
152     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
153     ASSERT_EQ(0, rc);
154 }
155
156 } // namespace