Merge branch 'release-4-6' into master
[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 }
120
121 MockModule &
122 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
123 {
124     MockModule *module = new MockModule(name, description);
125     manager().addModule(gmx::CommandLineModulePointer(module));
126     return *module;
127 }
128
129 MockHelpTopic &
130 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
131 {
132     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
133     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
134     return *topic;
135 }
136
137 /********************************************************************
138  * Actual tests
139  */
140
141 TEST_F(CommandLineModuleManagerTest, RunsModule)
142 {
143     const char *const cmdline[] = {
144         "g_test", "module", "-flag", "yes"
145     };
146     CommandLine       args(CommandLine::create(cmdline));
147     initManager(args);
148     MockModule       &mod1 = addModule("module", "First module");
149     addModule("other", "Second module");
150     using ::testing::_;
151     using ::testing::Args;
152     using ::testing::ElementsAreArray;
153     EXPECT_CALL(mod1, run(_, _))
154         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
155     int rc = 0;
156     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
157     ASSERT_EQ(0, rc);
158 }
159
160 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
161 {
162     const char *const cmdline[] = {
163         "g_test", "help", "module"
164     };
165     CommandLine       args(CommandLine::create(cmdline));
166     initManager(args);
167     MockModule       &mod1 = addModule("module", "First module");
168     addModule("other", "Second module");
169     using ::testing::_;
170     EXPECT_CALL(mod1, writeHelp(_));
171     int rc = 0;
172     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
173     ASSERT_EQ(0, rc);
174 }
175
176 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
177 {
178     const char *const cmdline[] = {
179         "g_test", "help", "topic"
180     };
181     CommandLine       args(CommandLine::create(cmdline));
182     initManager(args);
183     addModule("module", "First module");
184     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
185     using ::testing::_;
186     EXPECT_CALL(topic, writeHelp(_));
187     int rc = 0;
188     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
189     ASSERT_EQ(0, rc);
190 }
191
192 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
193 {
194     const char *const cmdline[] = {
195         "g_module", "-flag", "yes"
196     };
197     CommandLine       args(CommandLine::create(cmdline));
198     initManager(args);
199     MockModule       &mod1 = addModule("module", "First module");
200     addModule("other", "Second module");
201     using ::testing::_;
202     using ::testing::Args;
203     using ::testing::ElementsAreArray;
204     EXPECT_CALL(mod1, run(_, _))
205         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
206     int rc = 0;
207     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
208     ASSERT_EQ(0, rc);
209 }
210
211 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
212 {
213     const char *const cmdline[] = {
214         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
215     };
216     CommandLine       args(CommandLine::create(cmdline));
217     initManager(args);
218     MockModule       &mod1 = addModule("module", "First module");
219     addModule("other", "Second module");
220     using ::testing::_;
221     using ::testing::Args;
222     using ::testing::ElementsAreArray;
223     EXPECT_CALL(mod1, run(_, _))
224         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
225     int rc = 0;
226     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
227     ASSERT_EQ(0, rc);
228 }
229
230 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
231 {
232     const char *const cmdline[] = {
233         "g_test", "test", "-flag", "yes"
234     };
235     CommandLine       args(CommandLine::create(cmdline));
236     initManager(args);
237     MockModule       &mod1 = addModule("test", "Test module");
238     addModule("other", "Second module");
239     using ::testing::_;
240     using ::testing::Args;
241     using ::testing::ElementsAreArray;
242     EXPECT_CALL(mod1, run(_, _))
243         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
244     int rc = 0;
245     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
246     ASSERT_EQ(0, rc);
247 }
248
249 } // namespace