2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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));
87 //! Sets the expected display name for writeHelp() calls.
88 void setExpectedDisplayName(const char *expected)
90 expectedDisplayName_ = expected;
94 //! Checks the context passed to writeHelp().
95 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
99 std::string expectedDisplayName_;
102 MockModule::MockModule(const char *name, const char *description)
103 : name_(name), descr_(description)
106 using ::testing::Invoke;
107 using ::testing::WithArg;
108 ON_CALL(*this, writeHelp(_))
109 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
112 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
114 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
116 gmx::TextLineWrapperSettings settings;
117 std::string moduleName =
118 context.writerContext().substituteMarkupAndWrapToString(
119 settings, "[THISMODULE]");
120 EXPECT_EQ(expectedDisplayName_, moduleName);
123 /********************************************************************
124 * Test fixture for the tests
127 class CommandLineModuleManagerTest : public ::testing::Test
130 void initManager(const CommandLine &args, const char *realBinaryName);
131 MockModule &addModule(const char *name, const char *description);
132 MockHelpTopic &addHelpTopic(const char *name, const char *title);
134 gmx::CommandLineModuleManager &manager() { return *manager_; }
137 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
138 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
141 void CommandLineModuleManagerTest::initManager(
142 const CommandLine &args, const char *realBinaryName)
145 programInfo_.reset(new gmx::ProgramInfo(realBinaryName, args.argc(), args.argv()));
146 manager_.reset(new gmx::CommandLineModuleManager(programInfo_.get()));
147 manager_->setQuiet(true);
151 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
153 MockModule *module = new MockModule(name, description);
154 manager().addModule(gmx::CommandLineModulePointer(module));
159 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
161 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
162 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
166 /********************************************************************
170 TEST_F(CommandLineModuleManagerTest, RunsModule)
172 const char *const cmdline[] = {
173 "test", "module", "-flag", "yes"
175 CommandLine args(CommandLine::create(cmdline));
176 initManager(args, "test");
177 MockModule &mod1 = addModule("module", "First module");
178 addModule("other", "Second module");
180 using ::testing::Args;
181 using ::testing::ElementsAreArray;
182 EXPECT_CALL(mod1, run(_, _))
183 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
185 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
189 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
191 const char *const cmdline[] = {
192 "test", "help", "module"
194 CommandLine args(CommandLine::create(cmdline));
195 initManager(args, "test");
196 MockModule &mod1 = addModule("module", "First module");
197 addModule("other", "Second module");
199 EXPECT_CALL(mod1, writeHelp(_));
200 mod1.setExpectedDisplayName("test module");
202 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
206 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
208 const char *const cmdline[] = {
209 "test", "module", "-h"
211 CommandLine args(CommandLine::create(cmdline));
212 initManager(args, "test");
213 MockModule &mod1 = addModule("module", "First module");
214 addModule("other", "Second module");
216 EXPECT_CALL(mod1, writeHelp(_));
217 mod1.setExpectedDisplayName("test module");
219 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
223 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
225 const char *const cmdline[] = {
228 CommandLine args(CommandLine::create(cmdline));
229 initManager(args, "test");
230 MockModule &mod1 = addModule("module", "First module");
231 addModule("other", "Second module");
233 EXPECT_CALL(mod1, writeHelp(_));
234 mod1.setExpectedDisplayName("test module");
236 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
240 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
242 const char *const cmdline[] = {
245 CommandLine args(CommandLine::create(cmdline));
246 initManager(args, "g_module");
247 MockModule mod(NULL, NULL);
248 manager().setSingleModule(&mod);
250 EXPECT_CALL(mod, writeHelp(_));
251 mod.setExpectedDisplayName("g_module");
253 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
257 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
259 const char *const cmdline[] = {
260 "test", "help", "topic"
262 CommandLine args(CommandLine::create(cmdline));
263 initManager(args, "test");
264 addModule("module", "First module");
265 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
267 EXPECT_CALL(topic, writeHelp(_));
269 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
273 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
275 const char *const cmdline[] = {
276 "g_module", "-flag", "yes"
278 CommandLine args(CommandLine::create(cmdline));
279 initManager(args, "test");
280 MockModule &mod1 = addModule("module", "First module");
281 addModule("other", "Second module");
283 using ::testing::Args;
284 using ::testing::ElementsAreArray;
285 EXPECT_CALL(mod1, run(_, _))
286 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
288 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
292 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
294 const char *const cmdline[] = {
295 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
297 CommandLine args(CommandLine::create(cmdline));
298 initManager(args, "test");
299 MockModule &mod1 = addModule("module", "First module");
300 addModule("other", "Second module");
302 using ::testing::Args;
303 using ::testing::ElementsAreArray;
304 EXPECT_CALL(mod1, run(_, _))
305 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
307 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
311 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
313 const char *const cmdline[] = {
314 "test", "test", "-flag", "yes"
316 CommandLine args(CommandLine::create(cmdline));
317 initManager(args, "test");
318 MockModule &mod1 = addModule("test", "Test module");
319 addModule("other", "Second module");
321 using ::testing::Args;
322 using ::testing::ElementsAreArray;
323 EXPECT_CALL(mod1, run(_, _))
324 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
326 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));