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
45 #include "gromacs/commandline/cmdlinemodulemanager.h"
51 #include <gmock/gmock.h>
53 #include "gromacs/commandline/cmdlinehelpcontext.h"
54 #include "gromacs/commandline/cmdlinemodule.h"
55 #include "gromacs/commandline/cmdlineprogramcontext.h"
56 #include "gromacs/utility/file.h"
58 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
59 #include "testutils/cmdlinetest.h"
60 #include "testutils/testasserts.h"
61 #include "testutils/testfilemanager.h"
66 using gmx::test::CommandLine;
67 using gmx::test::MockHelpTopic;
69 /********************************************************************
74 * Mock implementation of gmx::CommandLineModuleInterface.
76 * \ingroup module_commandline
78 class MockModule : public gmx::CommandLineModuleInterface
81 //! Creates a mock module with the given name and description.
82 MockModule(const char *name, const char *description);
84 virtual const char *name() const { return name_; }
85 virtual const char *shortDescription() const { return descr_; }
87 MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
88 MOCK_METHOD2(run, int(int argc, char *argv[]));
89 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
91 //! Sets the expected display name for writeHelp() calls.
92 void setExpectedDisplayName(const char *expected)
94 expectedDisplayName_ = expected;
98 //! Disable nice() calls for tests.
99 void disableNice(gmx::CommandLineModuleSettings *settings)
101 settings->setDefaultNiceLevel(0);
103 //! Checks the context passed to writeHelp().
104 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
108 std::string expectedDisplayName_;
111 MockModule::MockModule(const char *name, const char *description)
112 : name_(name), descr_(description)
115 using ::testing::Invoke;
116 using ::testing::WithArg;
117 ON_CALL(*this, init(_))
118 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::disableNice)));
119 ON_CALL(*this, writeHelp(_))
120 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
123 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
125 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
127 gmx::TextLineWrapperSettings settings;
128 std::string moduleName =
129 context.writerContext().substituteMarkupAndWrapToString(
130 settings, "[THISMODULE]");
131 EXPECT_EQ(expectedDisplayName_, moduleName);
134 /********************************************************************
135 * Test fixture for the tests
138 class CommandLineModuleManagerTest : public ::testing::Test
141 void initManager(const CommandLine &args, const char *realBinaryName);
142 MockModule &addModule(const char *name, const char *description);
143 MockHelpTopic &addHelpTopic(const char *name, const char *title);
145 gmx::CommandLineModuleManager &manager() { return *manager_; }
147 void ignoreManagerOutput();
150 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
151 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
152 gmx::test::TestFileManager fileManager_;
153 boost::scoped_ptr<gmx::File> outputFile_;
156 void CommandLineModuleManagerTest::initManager(
157 const CommandLine &args, const char *realBinaryName)
160 programContext_.reset(
161 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
162 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
163 programContext_.get()));
164 manager_->setQuiet(true);
168 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
170 MockModule *module = new MockModule(name, description);
171 manager().addModule(gmx::CommandLineModulePointer(module));
176 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
178 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
179 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
183 void CommandLineModuleManagerTest::ignoreManagerOutput()
186 new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
187 manager().setOutputRedirect(outputFile_.get());
190 /********************************************************************
194 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
196 const char *const cmdline[] = {
199 CommandLine args(cmdline);
200 initManager(args, "test");
201 ignoreManagerOutput();
202 addModule("module", "First module");
203 addModule("other", "Second module");
205 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
209 TEST_F(CommandLineModuleManagerTest, RunsModule)
211 const char *const cmdline[] = {
212 "test", "module", "-flag", "yes"
214 CommandLine args(cmdline);
215 initManager(args, "test");
216 MockModule &mod1 = addModule("module", "First module");
217 addModule("other", "Second module");
219 using ::testing::Args;
220 using ::testing::ElementsAreArray;
221 EXPECT_CALL(mod1, init(_));
222 EXPECT_CALL(mod1, run(_, _))
223 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
225 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
229 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
231 const char *const cmdline[] = {
232 "test", "help", "module"
234 CommandLine args(cmdline);
235 initManager(args, "test");
236 MockModule &mod1 = addModule("module", "First module");
237 addModule("other", "Second module");
239 EXPECT_CALL(mod1, writeHelp(_));
240 mod1.setExpectedDisplayName("test module");
242 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
246 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
248 const char *const cmdline[] = {
249 "test", "module", "-h"
251 CommandLine args(cmdline);
252 initManager(args, "test");
253 MockModule &mod1 = addModule("module", "First module");
254 addModule("other", "Second module");
256 EXPECT_CALL(mod1, writeHelp(_));
257 mod1.setExpectedDisplayName("test module");
259 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
263 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
265 const char *const cmdline[] = {
268 CommandLine args(cmdline);
269 initManager(args, "test");
270 MockModule &mod1 = addModule("module", "First module");
271 addModule("other", "Second module");
273 EXPECT_CALL(mod1, writeHelp(_));
274 mod1.setExpectedDisplayName("test module");
276 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
280 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
282 const char *const cmdline[] = {
285 CommandLine args(cmdline);
286 initManager(args, "g_module");
287 MockModule mod(NULL, NULL);
288 manager().setSingleModule(&mod);
290 EXPECT_CALL(mod, writeHelp(_));
291 mod.setExpectedDisplayName("g_module");
293 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
297 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
299 const char *const cmdline[] = {
300 "test", "help", "topic"
302 CommandLine args(cmdline);
303 initManager(args, "test");
304 addModule("module", "First module");
305 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
307 EXPECT_CALL(topic, writeHelp(_));
309 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
313 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
315 const char *const cmdline[] = {
316 "g_module", "-flag", "yes"
318 CommandLine args(cmdline);
319 initManager(args, "test");
320 MockModule &mod1 = addModule("module", "First module");
321 addModule("other", "Second module");
323 using ::testing::Args;
324 using ::testing::ElementsAreArray;
325 EXPECT_CALL(mod1, init(_));
326 EXPECT_CALL(mod1, run(_, _))
327 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
329 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
333 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
335 const char *const cmdline[] = {
336 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
338 CommandLine args(cmdline);
339 initManager(args, "test");
340 MockModule &mod1 = addModule("module", "First module");
341 addModule("other", "Second module");
343 using ::testing::Args;
344 using ::testing::ElementsAreArray;
345 EXPECT_CALL(mod1, init(_));
346 EXPECT_CALL(mod1, run(_, _))
347 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
349 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
353 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
355 const char *const cmdline[] = {
356 "test", "test", "-flag", "yes"
358 CommandLine args(cmdline);
359 initManager(args, "test");
360 MockModule &mod1 = addModule("test", "Test module");
361 addModule("other", "Second module");
363 using ::testing::Args;
364 using ::testing::ElementsAreArray;
365 EXPECT_CALL(mod1, init(_));
366 EXPECT_CALL(mod1, run(_, _))
367 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
369 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));