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