Merge branch 'release-4-6'
[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, 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 // 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/cmdlinehelpcontext.h"
52 #include "gromacs/commandline/cmdlinemodule.h"
53 #include "gromacs/commandline/cmdlinemodulemanager.h"
54 #include "gromacs/utility/programinfo.h"
55
56 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
57 #include "testutils/cmdlinetest.h"
58 #include "testutils/testasserts.h"
59
60 namespace
61 {
62
63 using gmx::test::CommandLine;
64 using gmx::test::MockHelpTopic;
65
66 /********************************************************************
67  * MockModule
68  */
69
70 /*! \internal \brief
71  * Mock implementation of gmx::CommandLineModuleInterface.
72  *
73  * \ingroup module_commandline
74  */
75 class MockModule : public gmx::CommandLineModuleInterface
76 {
77     public:
78         //! Creates a mock module with the given name and description.
79         MockModule(const char *name, const char *description);
80
81         virtual const char *name() const { return name_; }
82         virtual const char *shortDescription() const { return descr_; }
83
84         MOCK_METHOD2(run, int(int argc, char *argv[]));
85         MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
86
87         //! Sets the expected display name for writeHelp() calls.
88         void setExpectedDisplayName(const char *expected)
89         {
90             expectedDisplayName_ = expected;
91         }
92
93     private:
94         //! Checks the context passed to writeHelp().
95         void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
96
97         const char             *name_;
98         const char             *descr_;
99         std::string             expectedDisplayName_;
100 };
101
102 MockModule::MockModule(const char *name, const char *description)
103     : name_(name), descr_(description)
104 {
105     using ::testing::_;
106     using ::testing::Invoke;
107     using ::testing::WithArg;
108     ON_CALL(*this, writeHelp(_))
109         .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
110 }
111
112 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
113 {
114     EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
115
116     gmx::TextLineWrapperSettings settings;
117     std::string                  moduleName =
118         context.writerContext().substituteMarkupAndWrapToString(
119                 settings, "[THISMODULE]");
120     EXPECT_EQ(expectedDisplayName_, moduleName);
121 }
122
123 /********************************************************************
124  * Test fixture for the tests
125  */
126
127 class CommandLineModuleManagerTest : public ::testing::Test
128 {
129     public:
130         void initManager(const CommandLine &args, const char *realBinaryName);
131         MockModule    &addModule(const char *name, const char *description);
132         MockHelpTopic &addHelpTopic(const char *name, const char *title);
133
134         gmx::CommandLineModuleManager &manager() { return *manager_; }
135
136     private:
137         boost::scoped_ptr<gmx::ProgramInfo>              programInfo_;
138         boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
139 };
140
141 void CommandLineModuleManagerTest::initManager(
142         const CommandLine &args, const char *realBinaryName)
143 {
144     manager_.reset();
145     programInfo_.reset(new gmx::ProgramInfo(args.argc(), args.argv()));
146     manager_.reset(new gmx::CommandLineModuleManager(realBinaryName, programInfo_.get()));
147     manager_->setQuiet(true);
148 }
149
150 MockModule &
151 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
152 {
153     MockModule *module = new MockModule(name, description);
154     manager().addModule(gmx::CommandLineModulePointer(module));
155     return *module;
156 }
157
158 MockHelpTopic &
159 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
160 {
161     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
162     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
163     return *topic;
164 }
165
166 /********************************************************************
167  * Actual tests
168  */
169
170 TEST_F(CommandLineModuleManagerTest, RunsModule)
171 {
172     const char *const cmdline[] = {
173         "test", "module", "-flag", "yes"
174     };
175     CommandLine       args(cmdline);
176     initManager(args, "test");
177     MockModule       &mod1 = addModule("module", "First module");
178     addModule("other", "Second module");
179     using ::testing::_;
180     using ::testing::Args;
181     using ::testing::ElementsAreArray;
182     EXPECT_CALL(mod1, run(_, _))
183         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
184     int rc = 0;
185     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
186     ASSERT_EQ(0, rc);
187 }
188
189 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
190 {
191     const char *const cmdline[] = {
192         "test", "help", "module"
193     };
194     CommandLine       args(cmdline);
195     initManager(args, "test");
196     MockModule       &mod1 = addModule("module", "First module");
197     addModule("other", "Second module");
198     using ::testing::_;
199     EXPECT_CALL(mod1, writeHelp(_));
200     mod1.setExpectedDisplayName("test module");
201     int rc = 0;
202     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
203     ASSERT_EQ(0, rc);
204 }
205
206 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
207 {
208     const char *const cmdline[] = {
209         "test", "module", "-h"
210     };
211     CommandLine       args(cmdline);
212     initManager(args, "test");
213     MockModule       &mod1 = addModule("module", "First module");
214     addModule("other", "Second module");
215     using ::testing::_;
216     EXPECT_CALL(mod1, writeHelp(_));
217     mod1.setExpectedDisplayName("test module");
218     int rc = 0;
219     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
220     ASSERT_EQ(0, rc);
221 }
222
223 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
224 {
225     const char *const cmdline[] = {
226         "g_module", "-h"
227     };
228     CommandLine       args(cmdline);
229     initManager(args, "test");
230     MockModule       &mod1 = addModule("module", "First module");
231     addModule("other", "Second module");
232     using ::testing::_;
233     EXPECT_CALL(mod1, writeHelp(_));
234     mod1.setExpectedDisplayName("test module");
235     int rc = 0;
236     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
237     ASSERT_EQ(0, rc);
238 }
239
240 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
241 {
242     const char *const cmdline[] = {
243         "g_module", "-h"
244     };
245     CommandLine       args(cmdline);
246     initManager(args, "g_module");
247     MockModule        mod(NULL, NULL);
248     manager().setSingleModule(&mod);
249     using ::testing::_;
250     EXPECT_CALL(mod, writeHelp(_));
251     mod.setExpectedDisplayName("g_module");
252     int rc = 0;
253     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
254     ASSERT_EQ(0, rc);
255 }
256
257 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
258 {
259     const char *const cmdline[] = {
260         "test", "help", "topic"
261     };
262     CommandLine       args(cmdline);
263     initManager(args, "test");
264     addModule("module", "First module");
265     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
266     using ::testing::_;
267     EXPECT_CALL(topic, writeHelp(_));
268     int rc = 0;
269     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
270     ASSERT_EQ(0, rc);
271 }
272
273 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
274 {
275     const char *const cmdline[] = {
276         "g_module", "-flag", "yes"
277     };
278     CommandLine       args(cmdline);
279     initManager(args, "test");
280     MockModule       &mod1 = addModule("module", "First module");
281     addModule("other", "Second module");
282     using ::testing::_;
283     using ::testing::Args;
284     using ::testing::ElementsAreArray;
285     EXPECT_CALL(mod1, run(_, _))
286         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
287     int rc = 0;
288     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
289     ASSERT_EQ(0, rc);
290 }
291
292 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
293 {
294     const char *const cmdline[] = {
295         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
296     };
297     CommandLine       args(cmdline);
298     initManager(args, "test");
299     MockModule       &mod1 = addModule("module", "First module");
300     addModule("other", "Second module");
301     using ::testing::_;
302     using ::testing::Args;
303     using ::testing::ElementsAreArray;
304     EXPECT_CALL(mod1, run(_, _))
305         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
306     int rc = 0;
307     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
308     ASSERT_EQ(0, rc);
309 }
310
311 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
312 {
313     const char *const cmdline[] = {
314         "test", "test", "-flag", "yes"
315     };
316     CommandLine       args(cmdline);
317     initManager(args, "test");
318     MockModule       &mod1 = addModule("test", "Test module");
319     addModule("other", "Second module");
320     using ::testing::_;
321     using ::testing::Args;
322     using ::testing::ElementsAreArray;
323     EXPECT_CALL(mod1, run(_, _))
324         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
325     int rc = 0;
326     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
327     ASSERT_EQ(0, rc);
328 }
329
330 } // namespace