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