Move a few defines to buildinfo.h
[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,2015, 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 #include "gmxpre.h"
43
44 #include "gromacs/commandline/cmdlinemodulemanager.h"
45
46 #include <vector>
47
48 #include <gmock/gmock.h>
49
50 #include "gromacs/commandline/cmdlinehelpcontext.h"
51 #include "gromacs/commandline/cmdlinemodule.h"
52 #include "gromacs/commandline/cmdlineprogramcontext.h"
53 #include "gromacs/utility/file.h"
54
55 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/testasserts.h"
58 #include "testutils/testfilemanager.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_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
85         MOCK_METHOD2(run, int(int argc, char *argv[]));
86         MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
87
88         //! Sets the expected display name for writeHelp() calls.
89         void setExpectedDisplayName(const char *expected)
90         {
91             expectedDisplayName_ = expected;
92         }
93
94     private:
95         //! Disable nice() calls for tests.
96         void disableNice(gmx::CommandLineModuleSettings *settings)
97         {
98             settings->setDefaultNiceLevel(0);
99         }
100         //! Checks the context passed to writeHelp().
101         void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
102
103         const char             *name_;
104         const char             *descr_;
105         std::string             expectedDisplayName_;
106 };
107
108 MockModule::MockModule(const char *name, const char *description)
109     : name_(name), descr_(description)
110 {
111     using ::testing::_;
112     using ::testing::Invoke;
113     using ::testing::WithArg;
114     ON_CALL(*this, init(_))
115         .WillByDefault(WithArg<0>(Invoke(this, &MockModule::disableNice)));
116     ON_CALL(*this, writeHelp(_))
117         .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
118 }
119
120 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
121 {
122     EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
123
124     gmx::TextLineWrapperSettings settings;
125     std::string                  moduleName =
126         context.writerContext().substituteMarkupAndWrapToString(
127                 settings, "[THISMODULE]");
128     EXPECT_EQ(expectedDisplayName_, moduleName);
129 }
130
131 /********************************************************************
132  * Test fixture for the tests
133  */
134
135 class CommandLineModuleManagerTest : public ::testing::Test
136 {
137     public:
138         void initManager(const CommandLine &args, const char *realBinaryName);
139         MockModule    &addModule(const char *name, const char *description);
140         MockHelpTopic &addHelpTopic(const char *name, const char *title);
141
142         gmx::CommandLineModuleManager &manager() { return *manager_; }
143
144         void ignoreManagerOutput();
145
146     private:
147         boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
148         boost::scoped_ptr<gmx::CommandLineModuleManager>  manager_;
149         gmx::test::TestFileManager                        fileManager_;
150         boost::scoped_ptr<gmx::File>                      outputFile_;
151 };
152
153 void CommandLineModuleManagerTest::initManager(
154         const CommandLine &args, const char *realBinaryName)
155 {
156     manager_.reset();
157     programContext_.reset(
158             new gmx::CommandLineProgramContext(args.argc(), args.argv()));
159     manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
160                                                      programContext_.get()));
161     manager_->setQuiet(true);
162 }
163
164 MockModule &
165 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
166 {
167     MockModule *module = new MockModule(name, description);
168     manager().addModule(gmx::CommandLineModulePointer(module));
169     return *module;
170 }
171
172 MockHelpTopic &
173 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
174 {
175     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
176     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
177     return *topic;
178 }
179
180 void CommandLineModuleManagerTest::ignoreManagerOutput()
181 {
182     outputFile_.reset(
183             new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
184     manager().setOutputRedirect(outputFile_.get());
185 }
186
187 /********************************************************************
188  * Actual tests
189  */
190
191 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
192 {
193     const char *const cmdline[] = {
194         "test"
195     };
196     CommandLine       args(cmdline);
197     initManager(args, "test");
198     ignoreManagerOutput();
199     addModule("module", "First module");
200     addModule("other", "Second module");
201     int rc = 0;
202     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
203     ASSERT_EQ(0, rc);
204 }
205
206 TEST_F(CommandLineModuleManagerTest, RunsModule)
207 {
208     const char *const cmdline[] = {
209         "test", "module", "-flag", "yes"
210     };
211     CommandLine       args(cmdline);
212     initManager(args, "test");
213     MockModule       &mod1 = addModule("module", "First module");
214     addModule("other", "Second module");
215     using ::testing::_;
216     using ::testing::Args;
217     using ::testing::ElementsAreArray;
218     EXPECT_CALL(mod1, init(_));
219     EXPECT_CALL(mod1, run(_, _))
220         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
221     int rc = 0;
222     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
223     ASSERT_EQ(0, rc);
224 }
225
226 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
227 {
228     const char *const cmdline[] = {
229         "test", "help", "module"
230     };
231     CommandLine       args(cmdline);
232     initManager(args, "test");
233     MockModule       &mod1 = addModule("module", "First module");
234     addModule("other", "Second module");
235     using ::testing::_;
236     EXPECT_CALL(mod1, writeHelp(_));
237     mod1.setExpectedDisplayName("test module");
238     int rc = 0;
239     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
240     ASSERT_EQ(0, rc);
241 }
242
243 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
244 {
245     const char *const cmdline[] = {
246         "test", "module", "-h"
247     };
248     CommandLine       args(cmdline);
249     initManager(args, "test");
250     MockModule       &mod1 = addModule("module", "First module");
251     addModule("other", "Second module");
252     using ::testing::_;
253     EXPECT_CALL(mod1, writeHelp(_));
254     mod1.setExpectedDisplayName("test module");
255     int rc = 0;
256     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
257     ASSERT_EQ(0, rc);
258 }
259
260 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
261 {
262     const char *const cmdline[] = {
263         "g_module", "-h"
264     };
265     CommandLine       args(cmdline);
266     initManager(args, "g_module");
267     MockModule        mod(NULL, NULL);
268     manager().setSingleModule(&mod);
269     using ::testing::_;
270     EXPECT_CALL(mod, writeHelp(_));
271     mod.setExpectedDisplayName("g_module");
272     int rc = 0;
273     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
274     ASSERT_EQ(0, rc);
275 }
276
277 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
278 {
279     const char *const cmdline[] = {
280         "test", "help", "topic"
281     };
282     CommandLine       args(cmdline);
283     initManager(args, "test");
284     addModule("module", "First module");
285     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
286     using ::testing::_;
287     EXPECT_CALL(topic, writeHelp(_));
288     int rc = 0;
289     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
290     ASSERT_EQ(0, rc);
291 }
292
293 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
294 {
295     const char *const cmdline[] = {
296         "test", "test", "-flag", "yes"
297     };
298     CommandLine       args(cmdline);
299     initManager(args, "test");
300     MockModule       &mod1 = addModule("test", "Test module");
301     addModule("other", "Second module");
302     using ::testing::_;
303     using ::testing::Args;
304     using ::testing::ElementsAreArray;
305     EXPECT_CALL(mod1, init(_));
306     EXPECT_CALL(mod1, run(_, _))
307         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
308     int rc = 0;
309     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
310     ASSERT_EQ(0, rc);
311 }
312
313 } // namespace