2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
37 * Tests gmx::CommandLineModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
42 // For GMX_BINARY_SUFFIX
49 #include <gmock/gmock.h>
51 #include "gromacs/commandline/cmdlinehelpcontext.h"
52 #include "gromacs/commandline/cmdlinemodule.h"
53 #include "gromacs/commandline/cmdlinemodulemanager.h"
54 #include "gromacs/utility/programinfo.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/mock_helptopic.h"
58 #include "testutils/testasserts.h"
63 using gmx::test::CommandLine;
64 using gmx::test::MockHelpTopic;
66 /********************************************************************
71 * Mock implementation of gmx::CommandLineModuleInterface.
73 * \ingroup module_commandline
75 class MockModule : public gmx::CommandLineModuleInterface
78 //! Creates a mock module with the given name and description.
79 MockModule(const char *name, const char *description);
81 virtual const char *name() const { return name_; }
82 virtual const char *shortDescription() const { return descr_; }
84 MOCK_METHOD2(run, int(int argc, char *argv[]));
85 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
92 MockModule::MockModule(const char *name, const char *description)
93 : name_(name), descr_(description)
97 /********************************************************************
98 * Test fixture for the tests
101 class CommandLineModuleManagerTest : public ::testing::Test
104 void initManager(const CommandLine &args);
105 MockModule &addModule(const char *name, const char *description);
106 MockHelpTopic &addHelpTopic(const char *name, const char *title);
108 gmx::CommandLineModuleManager &manager() { return *manager_; }
111 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
112 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
115 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
118 programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
119 manager_.reset(new gmx::CommandLineModuleManager(programInfo_.get()));
120 manager_->setQuiet(true);
124 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
126 MockModule *module = new MockModule(name, description);
127 manager().addModule(gmx::CommandLineModulePointer(module));
132 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
134 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
135 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
139 /********************************************************************
143 TEST_F(CommandLineModuleManagerTest, RunsModule)
145 const char *const cmdline[] = {
146 "g_test", "module", "-flag", "yes"
148 CommandLine args(CommandLine::create(cmdline));
150 MockModule &mod1 = addModule("module", "First module");
151 addModule("other", "Second module");
153 using ::testing::Args;
154 using ::testing::ElementsAreArray;
155 EXPECT_CALL(mod1, run(_, _))
156 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
158 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
162 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
164 const char *const cmdline[] = {
165 "g_test", "help", "module"
167 CommandLine args(CommandLine::create(cmdline));
169 MockModule &mod1 = addModule("module", "First module");
170 addModule("other", "Second module");
172 EXPECT_CALL(mod1, writeHelp(_));
174 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
178 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
180 const char *const cmdline[] = {
181 "g_test", "module", "-h"
183 CommandLine args(CommandLine::create(cmdline));
185 MockModule &mod1 = addModule("module", "First module");
186 addModule("other", "Second module");
188 EXPECT_CALL(mod1, writeHelp(_));
190 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
194 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
196 const char *const cmdline[] = {
197 "g_test", "help", "topic"
199 CommandLine args(CommandLine::create(cmdline));
201 addModule("module", "First module");
202 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
204 EXPECT_CALL(topic, writeHelp(_));
206 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
210 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
212 const char *const cmdline[] = {
213 "g_module", "-flag", "yes"
215 CommandLine args(CommandLine::create(cmdline));
217 MockModule &mod1 = addModule("module", "First module");
218 addModule("other", "Second module");
220 using ::testing::Args;
221 using ::testing::ElementsAreArray;
222 EXPECT_CALL(mod1, run(_, _))
223 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
225 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
229 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
231 const char *const cmdline[] = {
232 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
234 CommandLine args(CommandLine::create(cmdline));
236 MockModule &mod1 = addModule("module", "First module");
237 addModule("other", "Second module");
239 using ::testing::Args;
240 using ::testing::ElementsAreArray;
241 EXPECT_CALL(mod1, run(_, _))
242 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
244 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
248 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
250 const char *const cmdline[] = {
251 "g_test", "test", "-flag", "yes"
253 CommandLine args(CommandLine::create(cmdline));
255 MockModule &mod1 = addModule("test", "Test module");
256 addModule("other", "Second module");
258 using ::testing::Args;
259 using ::testing::ElementsAreArray;
260 EXPECT_CALL(mod1, run(_, _))
261 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
263 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));