Uniform and less verbose startup for all binaries.
[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, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 // For GMX_BINARY_SUFFIX
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <vector>
48
49 #include <gmock/gmock.h>
50
51 #include "gromacs/commandline/cmdlinemodule.h"
52 #include "gromacs/commandline/cmdlinemodulemanager.h"
53 #include "gromacs/utility/programinfo.h"
54
55 #include "testutils/cmdlinetest.h"
56 #include "testutils/mock_helptopic.h"
57 #include "testutils/testasserts.h"
58
59 namespace
60 {
61
62 using gmx::test::CommandLine;
63 using gmx::test::MockHelpTopic;
64
65 /********************************************************************
66  * MockModule
67  */
68
69 /*! \internal \brief
70  * Mock implementation of gmx::CommandLineModuleInterface.
71  *
72  * \ingroup module_commandline
73  */
74 class MockModule : public gmx::CommandLineModuleInterface
75 {
76     public:
77         //! Creates a mock module with the given name and description.
78         MockModule(const char *name, const char *description);
79
80         virtual const char *name() const { return name_; }
81         virtual const char *shortDescription() const { return descr_; }
82
83         MOCK_METHOD2(run, int(int argc, char *argv[]));
84         MOCK_CONST_METHOD1(writeHelp, void(const gmx::HelpWriterContext &context));
85
86     private:
87         const char             *name_;
88         const char             *descr_;
89 };
90
91 MockModule::MockModule(const char *name, const char *description)
92     : name_(name), descr_(description)
93 {
94 }
95
96 /********************************************************************
97  * Test fixture for the tests
98  */
99
100 class CommandLineModuleManagerTest : public ::testing::Test
101 {
102     public:
103         void initManager(const CommandLine &args);
104         MockModule    &addModule(const char *name, const char *description);
105         MockHelpTopic &addHelpTopic(const char *name, const char *title);
106
107         gmx::CommandLineModuleManager &manager() { return *manager_; }
108
109     private:
110         boost::scoped_ptr<gmx::ProgramInfo>              programInfo_;
111         boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
112 };
113
114 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
115 {
116     manager_.reset();
117     programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
118     manager_.reset(new gmx::CommandLineModuleManager(*programInfo_));
119     manager_->setQuiet(true);
120 }
121
122 MockModule &
123 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
124 {
125     MockModule *module = new MockModule(name, description);
126     manager().addModule(gmx::CommandLineModulePointer(module));
127     return *module;
128 }
129
130 MockHelpTopic &
131 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
132 {
133     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
134     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
135     return *topic;
136 }
137
138 /********************************************************************
139  * Actual tests
140  */
141
142 TEST_F(CommandLineModuleManagerTest, RunsModule)
143 {
144     const char *const cmdline[] = {
145         "g_test", "module", "-flag", "yes"
146     };
147     CommandLine       args(CommandLine::create(cmdline));
148     initManager(args);
149     MockModule       &mod1 = addModule("module", "First module");
150     addModule("other", "Second module");
151     using ::testing::_;
152     using ::testing::Args;
153     using ::testing::ElementsAreArray;
154     EXPECT_CALL(mod1, run(_, _))
155         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
156     int rc = 0;
157     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
158     ASSERT_EQ(0, rc);
159 }
160
161 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
162 {
163     const char *const cmdline[] = {
164         "g_test", "help", "module"
165     };
166     CommandLine       args(CommandLine::create(cmdline));
167     initManager(args);
168     MockModule       &mod1 = addModule("module", "First module");
169     addModule("other", "Second module");
170     using ::testing::_;
171     EXPECT_CALL(mod1, writeHelp(_));
172     int rc = 0;
173     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
174     ASSERT_EQ(0, rc);
175 }
176
177 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
178 {
179     const char *const cmdline[] = {
180         "g_test", "help", "topic"
181     };
182     CommandLine       args(CommandLine::create(cmdline));
183     initManager(args);
184     addModule("module", "First module");
185     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
186     using ::testing::_;
187     EXPECT_CALL(topic, writeHelp(_));
188     int rc = 0;
189     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
190     ASSERT_EQ(0, rc);
191 }
192
193 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
194 {
195     const char *const cmdline[] = {
196         "g_module", "-flag", "yes"
197     };
198     CommandLine       args(CommandLine::create(cmdline));
199     initManager(args);
200     MockModule       &mod1 = addModule("module", "First module");
201     addModule("other", "Second module");
202     using ::testing::_;
203     using ::testing::Args;
204     using ::testing::ElementsAreArray;
205     EXPECT_CALL(mod1, run(_, _))
206         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
207     int rc = 0;
208     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
209     ASSERT_EQ(0, rc);
210 }
211
212 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
213 {
214     const char *const cmdline[] = {
215         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
216     };
217     CommandLine       args(CommandLine::create(cmdline));
218     initManager(args);
219     MockModule       &mod1 = addModule("module", "First module");
220     addModule("other", "Second module");
221     using ::testing::_;
222     using ::testing::Args;
223     using ::testing::ElementsAreArray;
224     EXPECT_CALL(mod1, run(_, _))
225         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
226     int rc = 0;
227     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
228     ASSERT_EQ(0, rc);
229 }
230
231 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
232 {
233     const char *const cmdline[] = {
234         "g_test", "test", "-flag", "yes"
235     };
236     CommandLine       args(CommandLine::create(cmdline));
237     initManager(args);
238     MockModule       &mod1 = addModule("test", "Test module");
239     addModule("other", "Second module");
240     using ::testing::_;
241     using ::testing::Args;
242     using ::testing::ElementsAreArray;
243     EXPECT_CALL(mod1, run(_, _))
244         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
245     int rc = 0;
246     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
247     ASSERT_EQ(0, rc);
248 }
249
250 } // namespace