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