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