Rename ProgramInfo to CommandLineProgramContext
[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/commandline/cmdlineprogramcontext.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::CommandLineProgramContext> programContext_;
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     programContext_.reset(
146             new gmx::CommandLineProgramContext(args.argc(), args.argv()));
147     manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
148                                                      programContext_.get()));
149     manager_->setQuiet(true);
150 }
151
152 MockModule &
153 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
154 {
155     MockModule *module = new MockModule(name, description);
156     manager().addModule(gmx::CommandLineModulePointer(module));
157     return *module;
158 }
159
160 MockHelpTopic &
161 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
162 {
163     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
164     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
165     return *topic;
166 }
167
168 /********************************************************************
169  * Actual tests
170  */
171
172 TEST_F(CommandLineModuleManagerTest, RunsModule)
173 {
174     const char *const cmdline[] = {
175         "test", "module", "-flag", "yes"
176     };
177     CommandLine       args(cmdline);
178     initManager(args, "test");
179     MockModule       &mod1 = addModule("module", "First module");
180     addModule("other", "Second module");
181     using ::testing::_;
182     using ::testing::Args;
183     using ::testing::ElementsAreArray;
184     EXPECT_CALL(mod1, run(_, _))
185         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
186     int rc = 0;
187     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
188     ASSERT_EQ(0, rc);
189 }
190
191 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
192 {
193     const char *const cmdline[] = {
194         "test", "help", "module"
195     };
196     CommandLine       args(cmdline);
197     initManager(args, "test");
198     MockModule       &mod1 = addModule("module", "First module");
199     addModule("other", "Second module");
200     using ::testing::_;
201     EXPECT_CALL(mod1, writeHelp(_));
202     mod1.setExpectedDisplayName("test module");
203     int rc = 0;
204     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
205     ASSERT_EQ(0, rc);
206 }
207
208 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
209 {
210     const char *const cmdline[] = {
211         "test", "module", "-h"
212     };
213     CommandLine       args(cmdline);
214     initManager(args, "test");
215     MockModule       &mod1 = addModule("module", "First module");
216     addModule("other", "Second module");
217     using ::testing::_;
218     EXPECT_CALL(mod1, writeHelp(_));
219     mod1.setExpectedDisplayName("test module");
220     int rc = 0;
221     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
222     ASSERT_EQ(0, rc);
223 }
224
225 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
226 {
227     const char *const cmdline[] = {
228         "g_module", "-h"
229     };
230     CommandLine       args(cmdline);
231     initManager(args, "test");
232     MockModule       &mod1 = addModule("module", "First module");
233     addModule("other", "Second module");
234     using ::testing::_;
235     EXPECT_CALL(mod1, writeHelp(_));
236     mod1.setExpectedDisplayName("test module");
237     int rc = 0;
238     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
239     ASSERT_EQ(0, rc);
240 }
241
242 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
243 {
244     const char *const cmdline[] = {
245         "g_module", "-h"
246     };
247     CommandLine       args(cmdline);
248     initManager(args, "g_module");
249     MockModule        mod(NULL, NULL);
250     manager().setSingleModule(&mod);
251     using ::testing::_;
252     EXPECT_CALL(mod, writeHelp(_));
253     mod.setExpectedDisplayName("g_module");
254     int rc = 0;
255     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
256     ASSERT_EQ(0, rc);
257 }
258
259 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
260 {
261     const char *const cmdline[] = {
262         "test", "help", "topic"
263     };
264     CommandLine       args(cmdline);
265     initManager(args, "test");
266     addModule("module", "First module");
267     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
268     using ::testing::_;
269     EXPECT_CALL(topic, writeHelp(_));
270     int rc = 0;
271     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
272     ASSERT_EQ(0, rc);
273 }
274
275 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
276 {
277     const char *const cmdline[] = {
278         "g_module", "-flag", "yes"
279     };
280     CommandLine       args(cmdline);
281     initManager(args, "test");
282     MockModule       &mod1 = addModule("module", "First module");
283     addModule("other", "Second module");
284     using ::testing::_;
285     using ::testing::Args;
286     using ::testing::ElementsAreArray;
287     EXPECT_CALL(mod1, run(_, _))
288         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
289     int rc = 0;
290     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
291     ASSERT_EQ(0, rc);
292 }
293
294 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
295 {
296     const char *const cmdline[] = {
297         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
298     };
299     CommandLine       args(cmdline);
300     initManager(args, "test");
301     MockModule       &mod1 = addModule("module", "First module");
302     addModule("other", "Second module");
303     using ::testing::_;
304     using ::testing::Args;
305     using ::testing::ElementsAreArray;
306     EXPECT_CALL(mod1, run(_, _))
307         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
308     int rc = 0;
309     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
310     ASSERT_EQ(0, rc);
311 }
312
313 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
314 {
315     const char *const cmdline[] = {
316         "test", "test", "-flag", "yes"
317     };
318     CommandLine       args(cmdline);
319     initManager(args, "test");
320     MockModule       &mod1 = addModule("test", "Test module");
321     addModule("other", "Second module");
322     using ::testing::_;
323     using ::testing::Args;
324     using ::testing::ElementsAreArray;
325     EXPECT_CALL(mod1, run(_, _))
326         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
327     int rc = 0;
328     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
329     ASSERT_EQ(0, rc);
330 }
331
332 } // namespace