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/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"
57 #include "testutils/testasserts.h"
62 using gmx::test::CommandLine;
63 using gmx::test::MockHelpTopic;
65 /********************************************************************
70 * Mock implementation of gmx::CommandLineModuleInterface.
72 * \ingroup module_commandline
74 class MockModule : public gmx::CommandLineModuleInterface
77 //! Creates a mock module with the given name and description.
78 MockModule(const char *name, const char *description);
80 virtual const char *name() const { return name_; }
81 virtual const char *shortDescription() const { return descr_; }
83 MOCK_METHOD2(run, int(int argc, char *argv[]));
84 MOCK_CONST_METHOD1(writeHelp, void(const gmx::HelpWriterContext &context));
91 MockModule::MockModule(const char *name, const char *description)
92 : name_(name), descr_(description)
96 /********************************************************************
97 * Test fixture for the tests
100 class CommandLineModuleManagerTest : public ::testing::Test
103 void initManager(const CommandLine &args);
104 MockModule &addModule(const char *name, const char *description);
105 MockHelpTopic &addHelpTopic(const char *name, const char *title);
107 gmx::CommandLineModuleManager &manager() { return *manager_; }
110 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
111 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
114 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
117 programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
118 manager_.reset(new gmx::CommandLineModuleManager(programInfo_.get()));
119 manager_->setQuiet(true);
123 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
125 MockModule *module = new MockModule(name, description);
126 manager().addModule(gmx::CommandLineModulePointer(module));
131 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
133 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
134 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
138 /********************************************************************
142 TEST_F(CommandLineModuleManagerTest, RunsModule)
144 const char *const cmdline[] = {
145 "g_test", "module", "-flag", "yes"
147 CommandLine args(CommandLine::create(cmdline));
149 MockModule &mod1 = addModule("module", "First module");
150 addModule("other", "Second module");
152 using ::testing::Args;
153 using ::testing::ElementsAreArray;
154 EXPECT_CALL(mod1, run(_, _))
155 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
157 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
161 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
163 const char *const cmdline[] = {
164 "g_test", "help", "module"
166 CommandLine args(CommandLine::create(cmdline));
168 MockModule &mod1 = addModule("module", "First module");
169 addModule("other", "Second module");
171 EXPECT_CALL(mod1, writeHelp(_));
173 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
177 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
179 const char *const cmdline[] = {
180 "g_test", "help", "topic"
182 CommandLine args(CommandLine::create(cmdline));
184 addModule("module", "First module");
185 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
187 EXPECT_CALL(topic, writeHelp(_));
189 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
193 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
195 const char *const cmdline[] = {
196 "g_module", "-flag", "yes"
198 CommandLine args(CommandLine::create(cmdline));
200 MockModule &mod1 = addModule("module", "First module");
201 addModule("other", "Second module");
203 using ::testing::Args;
204 using ::testing::ElementsAreArray;
205 EXPECT_CALL(mod1, run(_, _))
206 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
208 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
212 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
214 const char *const cmdline[] = {
215 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
217 CommandLine args(CommandLine::create(cmdline));
219 MockModule &mod1 = addModule("module", "First module");
220 addModule("other", "Second module");
222 using ::testing::Args;
223 using ::testing::ElementsAreArray;
224 EXPECT_CALL(mod1, run(_, _))
225 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
227 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
231 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
233 const char *const cmdline[] = {
234 "g_test", "test", "-flag", "yes"
236 CommandLine args(CommandLine::create(cmdline));
238 MockModule &mod1 = addModule("test", "Test module");
239 addModule("other", "Second module");
241 using ::testing::Args;
242 using ::testing::ElementsAreArray;
243 EXPECT_CALL(mod1, run(_, _))
244 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
246 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));