Merge remote-tracking branch 'origin/release-4-6' into HEAD
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Tests gmx::CommandLineModuleManager.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_commandline
37  */
38 // For GMX_BINARY_SUFFIX
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <vector>
44
45 #include <gmock/gmock.h>
46
47 #include "gromacs/commandline/cmdlinemodule.h"
48 #include "gromacs/commandline/cmdlinemodulemanager.h"
49 #include "gromacs/utility/programinfo.h"
50
51 #include "testutils/cmdlinetest.h"
52 #include "testutils/mock_helptopic.h"
53
54 namespace
55 {
56
57 using gmx::test::CommandLine;
58 using gmx::test::MockHelpTopic;
59
60 /********************************************************************
61  * MockModule
62  */
63
64 /*! \internal \brief
65  * Mock implementation of gmx::CommandLineModuleInterface.
66  *
67  * \ingroup module_commandline
68  */
69 class MockModule : public gmx::CommandLineModuleInterface
70 {
71     public:
72         //! Creates a mock module with the given name and description.
73         MockModule(const char *name, const char *description);
74
75         virtual const char *name() const { return name_; }
76         virtual const char *shortDescription() const { return descr_; }
77
78         MOCK_METHOD2(run, int(int argc, char *argv[]));
79         MOCK_CONST_METHOD1(writeHelp, void(gmx::File *));
80
81     private:
82         const char             *name_;
83         const char             *descr_;
84 };
85
86 MockModule::MockModule(const char *name, const char *description)
87     : name_(name), descr_(description)
88 {
89 }
90
91 /********************************************************************
92  * Test fixture for the tests
93  */
94
95 class CommandLineModuleManagerTest : public ::testing::Test
96 {
97     public:
98         void initManager(const CommandLine &args);
99         MockModule &addModule(const char *name, const char *description);
100         MockHelpTopic &addHelpTopic(const char *name, const char *title);
101
102         gmx::CommandLineModuleManager &manager() { return *manager_; }
103
104     private:
105         boost::scoped_ptr<gmx::ProgramInfo>              programInfo_;
106         boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
107 };
108
109 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
110 {
111     manager_.reset();
112     programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
113     manager_.reset(new gmx::CommandLineModuleManager(*programInfo_));
114 }
115
116 MockModule &
117 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
118 {
119     MockModule *module = new MockModule(name, description);
120     manager().addModule(gmx::CommandLineModulePointer(module));
121     return *module;
122 }
123
124 MockHelpTopic &
125 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
126 {
127     MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
128     manager().addHelpTopic(gmx::HelpTopicPointer(topic));
129     return *topic;
130 }
131
132 /********************************************************************
133  * Actual tests
134  */
135
136 TEST_F(CommandLineModuleManagerTest, RunsModule)
137 {
138     const char *const cmdline[] = {
139         "g_test", "module", "-flag", "yes"
140     };
141     CommandLine args(CommandLine::create(cmdline));
142     initManager(args);
143     MockModule &mod1 = addModule("module", "First module");
144     addModule("other", "Second module");
145     using ::testing::_;
146     using ::testing::Args;
147     using ::testing::ElementsAreArray;
148     EXPECT_CALL(mod1, run(_, _))
149         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
150     int rc = 0;
151     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
152     ASSERT_EQ(0, rc);
153 }
154
155 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
156 {
157     const char *const cmdline[] = {
158         "g_test", "help", "module"
159     };
160     CommandLine args(CommandLine::create(cmdline));
161     initManager(args);
162     MockModule &mod1 = addModule("module", "First module");
163     addModule("other", "Second module");
164     using ::testing::_;
165     EXPECT_CALL(mod1, writeHelp(_));
166     int rc = 0;
167     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
168     ASSERT_EQ(0, rc);
169 }
170
171 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
172 {
173     const char *const cmdline[] = {
174         "g_test", "help", "topic"
175     };
176     CommandLine args(CommandLine::create(cmdline));
177     initManager(args);
178     addModule("module", "First module");
179     MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
180     using ::testing::_;
181     EXPECT_CALL(topic, writeHelp(_));
182     int rc = 0;
183     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
184     ASSERT_EQ(0, rc);
185 }
186
187 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
188 {
189     const char *const cmdline[] = {
190         "g_module", "-flag", "yes"
191     };
192     CommandLine args(CommandLine::create(cmdline));
193     initManager(args);
194     MockModule &mod1 = addModule("module", "First module");
195     addModule("other", "Second module");
196     using ::testing::_;
197     using ::testing::Args;
198     using ::testing::ElementsAreArray;
199     EXPECT_CALL(mod1, run(_, _))
200         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
201     int rc = 0;
202     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
203     ASSERT_EQ(0, rc);
204 }
205
206 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
207 {
208     const char *const cmdline[] = {
209         "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
210     };
211     CommandLine args(CommandLine::create(cmdline));
212     initManager(args);
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, run(_, _))
219         .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
220     int rc = 0;
221     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
222     ASSERT_EQ(0, rc);
223 }
224
225 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
226 {
227     const char *const cmdline[] = {
228         "g_test", "test", "-flag", "yes"
229     };
230     CommandLine args(CommandLine::create(cmdline));
231     initManager(args);
232     MockModule &mod1 = addModule("test", "Test module");
233     addModule("other", "Second module");
234     using ::testing::_;
235     using ::testing::Args;
236     using ::testing::ElementsAreArray;
237     EXPECT_CALL(mod1, run(_, _))
238         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
239     int rc = 0;
240     ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
241     ASSERT_EQ(0, rc);
242 }
243
244 } // namespace