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_));
122 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
124 MockModule *module = new MockModule(name, description);
125 manager().addModule(gmx::CommandLineModulePointer(module));
130 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
132 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
133 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
137 /********************************************************************
141 TEST_F(CommandLineModuleManagerTest, RunsModule)
143 const char *const cmdline[] = {
144 "g_test", "module", "-flag", "yes"
146 CommandLine args(CommandLine::create(cmdline));
148 MockModule &mod1 = addModule("module", "First module");
149 addModule("other", "Second module");
151 using ::testing::Args;
152 using ::testing::ElementsAreArray;
153 EXPECT_CALL(mod1, run(_, _))
154 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
156 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
160 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
162 const char *const cmdline[] = {
163 "g_test", "help", "module"
165 CommandLine args(CommandLine::create(cmdline));
167 MockModule &mod1 = addModule("module", "First module");
168 addModule("other", "Second module");
170 EXPECT_CALL(mod1, writeHelp(_));
172 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
176 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
178 const char *const cmdline[] = {
179 "g_test", "help", "topic"
181 CommandLine args(CommandLine::create(cmdline));
183 addModule("module", "First module");
184 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
186 EXPECT_CALL(topic, writeHelp(_));
188 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
192 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
194 const char *const cmdline[] = {
195 "g_module", "-flag", "yes"
197 CommandLine args(CommandLine::create(cmdline));
199 MockModule &mod1 = addModule("module", "First module");
200 addModule("other", "Second module");
202 using ::testing::Args;
203 using ::testing::ElementsAreArray;
204 EXPECT_CALL(mod1, run(_, _))
205 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
207 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
211 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
213 const char *const cmdline[] = {
214 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
216 CommandLine args(CommandLine::create(cmdline));
218 MockModule &mod1 = addModule("module", "First module");
219 addModule("other", "Second module");
221 using ::testing::Args;
222 using ::testing::ElementsAreArray;
223 EXPECT_CALL(mod1, run(_, _))
224 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
226 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
230 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
232 const char *const cmdline[] = {
233 "g_test", "test", "-flag", "yes"
235 CommandLine args(CommandLine::create(cmdline));
237 MockModule &mod1 = addModule("test", "Test module");
238 addModule("other", "Second module");
240 using ::testing::Args;
241 using ::testing::ElementsAreArray;
242 EXPECT_CALL(mod1, run(_, _))
243 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
245 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));