2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, 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/commandline/cmdlineprogramcontext.h"
55 #include "gromacs/utility/file.h"
57 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
58 #include "testutils/cmdlinetest.h"
59 #include "testutils/testasserts.h"
60 #include "testutils/testfilemanager.h"
65 using gmx::test::CommandLine;
66 using gmx::test::MockHelpTopic;
68 /********************************************************************
73 * Mock implementation of gmx::CommandLineModuleInterface.
75 * \ingroup module_commandline
77 class MockModule : public gmx::CommandLineModuleInterface
80 //! Creates a mock module with the given name and description.
81 MockModule(const char *name, const char *description);
83 virtual const char *name() const { return name_; }
84 virtual const char *shortDescription() const { return descr_; }
86 MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
87 MOCK_METHOD2(run, int(int argc, char *argv[]));
88 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
90 //! Sets the expected display name for writeHelp() calls.
91 void setExpectedDisplayName(const char *expected)
93 expectedDisplayName_ = expected;
97 //! Disable nice() calls for tests.
98 void disableNice(gmx::CommandLineModuleSettings *settings)
100 settings->setDefaultNiceLevel(0);
102 //! Checks the context passed to writeHelp().
103 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
107 std::string expectedDisplayName_;
110 MockModule::MockModule(const char *name, const char *description)
111 : name_(name), descr_(description)
114 using ::testing::Invoke;
115 using ::testing::WithArg;
116 ON_CALL(*this, init(_))
117 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::disableNice)));
118 ON_CALL(*this, writeHelp(_))
119 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
122 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
124 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
126 gmx::TextLineWrapperSettings settings;
127 std::string moduleName =
128 context.writerContext().substituteMarkupAndWrapToString(
129 settings, "[THISMODULE]");
130 EXPECT_EQ(expectedDisplayName_, moduleName);
133 /********************************************************************
134 * Test fixture for the tests
137 class CommandLineModuleManagerTest : public ::testing::Test
140 void initManager(const CommandLine &args, const char *realBinaryName);
141 MockModule &addModule(const char *name, const char *description);
142 MockHelpTopic &addHelpTopic(const char *name, const char *title);
144 gmx::CommandLineModuleManager &manager() { return *manager_; }
146 void ignoreManagerOutput();
149 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
150 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
151 gmx::test::TestFileManager fileManager_;
152 boost::scoped_ptr<gmx::File> outputFile_;
155 void CommandLineModuleManagerTest::initManager(
156 const CommandLine &args, const char *realBinaryName)
159 programContext_.reset(
160 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
161 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
162 programContext_.get()));
163 manager_->setQuiet(true);
167 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
169 MockModule *module = new MockModule(name, description);
170 manager().addModule(gmx::CommandLineModulePointer(module));
175 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
177 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
178 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
182 void CommandLineModuleManagerTest::ignoreManagerOutput()
185 new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
186 manager().setOutputRedirect(outputFile_.get());
189 /********************************************************************
193 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
195 const char *const cmdline[] = {
198 CommandLine args(cmdline);
199 initManager(args, "test");
200 ignoreManagerOutput();
201 addModule("module", "First module");
202 addModule("other", "Second module");
204 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
208 TEST_F(CommandLineModuleManagerTest, RunsModule)
210 const char *const cmdline[] = {
211 "test", "module", "-flag", "yes"
213 CommandLine args(cmdline);
214 initManager(args, "test");
215 MockModule &mod1 = addModule("module", "First module");
216 addModule("other", "Second module");
218 using ::testing::Args;
219 using ::testing::ElementsAreArray;
220 EXPECT_CALL(mod1, init(_));
221 EXPECT_CALL(mod1, run(_, _))
222 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
224 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
228 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
230 const char *const cmdline[] = {
231 "test", "help", "module"
233 CommandLine args(cmdline);
234 initManager(args, "test");
235 MockModule &mod1 = addModule("module", "First module");
236 addModule("other", "Second module");
238 EXPECT_CALL(mod1, writeHelp(_));
239 mod1.setExpectedDisplayName("test module");
241 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
245 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
247 const char *const cmdline[] = {
248 "test", "module", "-h"
250 CommandLine args(cmdline);
251 initManager(args, "test");
252 MockModule &mod1 = addModule("module", "First module");
253 addModule("other", "Second module");
255 EXPECT_CALL(mod1, writeHelp(_));
256 mod1.setExpectedDisplayName("test module");
258 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
262 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
264 const char *const cmdline[] = {
267 CommandLine args(cmdline);
268 initManager(args, "test");
269 MockModule &mod1 = addModule("module", "First module");
270 addModule("other", "Second module");
272 EXPECT_CALL(mod1, writeHelp(_));
273 mod1.setExpectedDisplayName("test module");
275 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
279 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
281 const char *const cmdline[] = {
284 CommandLine args(cmdline);
285 initManager(args, "g_module");
286 MockModule mod(NULL, NULL);
287 manager().setSingleModule(&mod);
289 EXPECT_CALL(mod, writeHelp(_));
290 mod.setExpectedDisplayName("g_module");
292 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
296 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
298 const char *const cmdline[] = {
299 "test", "help", "topic"
301 CommandLine args(cmdline);
302 initManager(args, "test");
303 addModule("module", "First module");
304 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
306 EXPECT_CALL(topic, writeHelp(_));
308 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
312 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
314 const char *const cmdline[] = {
315 "g_module", "-flag", "yes"
317 CommandLine args(cmdline);
318 initManager(args, "test");
319 MockModule &mod1 = addModule("module", "First module");
320 addModule("other", "Second module");
322 using ::testing::Args;
323 using ::testing::ElementsAreArray;
324 EXPECT_CALL(mod1, init(_));
325 EXPECT_CALL(mod1, run(_, _))
326 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
328 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
332 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
334 const char *const cmdline[] = {
335 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
337 CommandLine args(cmdline);
338 initManager(args, "test");
339 MockModule &mod1 = addModule("module", "First module");
340 addModule("other", "Second module");
342 using ::testing::Args;
343 using ::testing::ElementsAreArray;
344 EXPECT_CALL(mod1, init(_));
345 EXPECT_CALL(mod1, run(_, _))
346 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
348 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
352 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
354 const char *const cmdline[] = {
355 "test", "test", "-flag", "yes"
357 CommandLine args(cmdline);
358 initManager(args, "test");
359 MockModule &mod1 = addModule("test", "Test module");
360 addModule("other", "Second module");
362 using ::testing::Args;
363 using ::testing::ElementsAreArray;
364 EXPECT_CALL(mod1, init(_));
365 EXPECT_CALL(mod1, run(_, _))
366 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
368 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));