Framework for help export from wrapper binary.
[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/cmdlinehelpcontext.h"
52 #include "gromacs/commandline/cmdlinemodule.h"
53 #include "gromacs/commandline/cmdlinemodulemanager.h"
54 #include "gromacs/utility/programinfo.h"
55
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/mock_helptopic.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     private:
88         const char             *name_;
89         const char             *descr_;
90 };
91
92 MockModule::MockModule(const char *name, const char *description)
93     : name_(name), descr_(description)
94 {
95 }
96
97 /********************************************************************
98  * Test fixture for the tests
99  */
100
101 class CommandLineModuleManagerTest : public ::testing::Test
102 {
103     public:
104         void initManager(const CommandLine &args);
105         MockModule    &addModule(const char *name, const char *description);
106         MockHelpTopic &addHelpTopic(const char *name, const char *title);
107
108         gmx::CommandLineModuleManager &manager() { return *manager_; }
109
110     private:
111         boost::scoped_ptr<gmx::ProgramInfo>              programInfo_;
112         boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
113 };
114
115 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
116 {
117     manager_.reset();
118     programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
119     manager_.reset(new gmx::CommandLineModuleManager(programInfo_.get()));
120     manager_->setQuiet(true);
121 }
122
123 MockModule &
124 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
125 {
126     MockModule *module = new MockModule(name, description);
127     manager().addModule(gmx::CommandLineModulePointer(module));
128     return *module;
129 }
130
131 MockHelpTopic &
132 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
133 {
134     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
135     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
136     return *topic;
137 }
138
139 /********************************************************************
140  * Actual tests
141  */
142
143 TEST_F(CommandLineModuleManagerTest, RunsModule)
144 {
145     const char *const cmdline[] = {
146         "g_test", "module", "-flag", "yes"
147     };
148     CommandLine       args(CommandLine::create(cmdline));
149     initManager(args);
150     MockModule       &mod1 = addModule("module", "First module");
151     addModule("other", "Second module");
152     using ::testing::_;
153     using ::testing::Args;
154     using ::testing::ElementsAreArray;
155     EXPECT_CALL(mod1, run(_, _))
156         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
157     int rc = 0;
158     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
159     ASSERT_EQ(0, rc);
160 }
161
162 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
163 {
164     const char *const cmdline[] = {
165         "g_test", "help", "module"
166     };
167     CommandLine       args(CommandLine::create(cmdline));
168     initManager(args);
169     MockModule       &mod1 = addModule("module", "First module");
170     addModule("other", "Second module");
171     using ::testing::_;
172     EXPECT_CALL(mod1, writeHelp(_));
173     int rc = 0;
174     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
175     ASSERT_EQ(0, rc);
176 }
177
178 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
179 {
180     const char *const cmdline[] = {
181         "g_test", "help", "topic"
182     };
183     CommandLine       args(CommandLine::create(cmdline));
184     initManager(args);
185     addModule("module", "First module");
186     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
187     using ::testing::_;
188     EXPECT_CALL(topic, writeHelp(_));
189     int rc = 0;
190     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
191     ASSERT_EQ(0, rc);
192 }
193
194 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
195 {
196     const char *const cmdline[] = {
197         "g_module", "-flag", "yes"
198     };
199     CommandLine       args(CommandLine::create(cmdline));
200     initManager(args);
201     MockModule       &mod1 = addModule("module", "First module");
202     addModule("other", "Second module");
203     using ::testing::_;
204     using ::testing::Args;
205     using ::testing::ElementsAreArray;
206     EXPECT_CALL(mod1, run(_, _))
207         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
208     int rc = 0;
209     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
210     ASSERT_EQ(0, rc);
211 }
212
213 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
214 {
215     const char *const cmdline[] = {
216         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
217     };
218     CommandLine       args(CommandLine::create(cmdline));
219     initManager(args);
220     MockModule       &mod1 = addModule("module", "First module");
221     addModule("other", "Second module");
222     using ::testing::_;
223     using ::testing::Args;
224     using ::testing::ElementsAreArray;
225     EXPECT_CALL(mod1, run(_, _))
226         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
227     int rc = 0;
228     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
229     ASSERT_EQ(0, rc);
230 }
231
232 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
233 {
234     const char *const cmdline[] = {
235         "g_test", "test", "-flag", "yes"
236     };
237     CommandLine       args(CommandLine::create(cmdline));
238     initManager(args);
239     MockModule       &mod1 = addModule("test", "Test module");
240     addModule("other", "Second module");
241     using ::testing::_;
242     using ::testing::Args;
243     using ::testing::ElementsAreArray;
244     EXPECT_CALL(mod1, run(_, _))
245         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
246     int rc = 0;
247     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
248     ASSERT_EQ(0, rc);
249 }
250
251 } // namespace