2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012, 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/cmdlinemodule.h"
52 #include "gromacs/commandline/cmdlinemodulemanager.h"
53 #include "gromacs/utility/programinfo.h"
55 #include "testutils/cmdlinetest.h"
56 #include "testutils/mock_helptopic.h"
61 using gmx::test::CommandLine;
62 using gmx::test::MockHelpTopic;
64 /********************************************************************
69 * Mock implementation of gmx::CommandLineModuleInterface.
71 * \ingroup module_commandline
73 class MockModule : public gmx::CommandLineModuleInterface
76 //! Creates a mock module with the given name and description.
77 MockModule(const char *name, const char *description);
79 virtual const char *name() const { return name_; }
80 virtual const char *shortDescription() const { return descr_; }
82 MOCK_METHOD2(run, int(int argc, char *argv[]));
83 MOCK_CONST_METHOD1(writeHelp, void(const gmx::HelpWriterContext &context));
90 MockModule::MockModule(const char *name, const char *description)
91 : name_(name), descr_(description)
95 /********************************************************************
96 * Test fixture for the tests
99 class CommandLineModuleManagerTest : public ::testing::Test
102 void initManager(const CommandLine &args);
103 MockModule &addModule(const char *name, const char *description);
104 MockHelpTopic &addHelpTopic(const char *name, const char *title);
106 gmx::CommandLineModuleManager &manager() { return *manager_; }
109 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
110 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
113 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
116 programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
117 manager_.reset(new gmx::CommandLineModuleManager(*programInfo_));
121 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
123 MockModule *module = new MockModule(name, description);
124 manager().addModule(gmx::CommandLineModulePointer(module));
129 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
131 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
132 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
136 /********************************************************************
140 TEST_F(CommandLineModuleManagerTest, RunsModule)
142 const char *const cmdline[] = {
143 "g_test", "module", "-flag", "yes"
145 CommandLine args(CommandLine::create(cmdline));
147 MockModule &mod1 = addModule("module", "First module");
148 addModule("other", "Second module");
150 using ::testing::Args;
151 using ::testing::ElementsAreArray;
152 EXPECT_CALL(mod1, run(_, _))
153 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
155 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
159 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
161 const char *const cmdline[] = {
162 "g_test", "help", "module"
164 CommandLine args(CommandLine::create(cmdline));
166 MockModule &mod1 = addModule("module", "First module");
167 addModule("other", "Second module");
169 EXPECT_CALL(mod1, writeHelp(_));
171 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
175 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
177 const char *const cmdline[] = {
178 "g_test", "help", "topic"
180 CommandLine args(CommandLine::create(cmdline));
182 addModule("module", "First module");
183 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
185 EXPECT_CALL(topic, writeHelp(_));
187 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
191 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
193 const char *const cmdline[] = {
194 "g_module", "-flag", "yes"
196 CommandLine args(CommandLine::create(cmdline));
198 MockModule &mod1 = addModule("module", "First module");
199 addModule("other", "Second module");
201 using ::testing::Args;
202 using ::testing::ElementsAreArray;
203 EXPECT_CALL(mod1, run(_, _))
204 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
206 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
210 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
212 const char *const cmdline[] = {
213 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-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(rc = manager().run(args.argc(), args.argv()));
229 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
231 const char *const cmdline[] = {
232 "g_test", "test", "-flag", "yes"
234 CommandLine args(CommandLine::create(cmdline));
236 MockModule &mod1 = addModule("test", "Test 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() + 1, args.argc() - 1)));
244 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));