2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015, 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
44 #include "gromacs/commandline/cmdlinemodulemanager.h"
48 #include <gmock/gmock.h>
50 #include "gromacs/commandline/cmdlinehelpcontext.h"
51 #include "gromacs/commandline/cmdlinemodule.h"
52 #include "gromacs/commandline/cmdlineoptionsmodule.h"
53 #include "gromacs/commandline/cmdlineprogramcontext.h"
54 #include "gromacs/options/basicoptions.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/utility/file.h"
58 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
59 #include "testutils/cmdlinetest.h"
60 #include "testutils/stringtest.h"
61 #include "testutils/testasserts.h"
62 #include "testutils/testfilemanager.h"
67 using gmx::test::CommandLine;
68 using gmx::test::MockHelpTopic;
71 * Helper method to disable nice() calls from CommandLineModuleManager.
73 * \ingroup module_commandline
75 void disableNice(gmx::CommandLineModuleSettings *settings)
77 settings->setDefaultNiceLevel(0);
80 /********************************************************************
85 * Mock implementation of gmx::CommandLineModuleInterface.
87 * \ingroup module_commandline
89 class MockModule : public gmx::CommandLineModuleInterface
92 //! Creates a mock module with the given name and description.
93 MockModule(const char *name, const char *description);
95 virtual const char *name() const { return name_; }
96 virtual const char *shortDescription() const { return descr_; }
98 MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
99 MOCK_METHOD2(run, int(int argc, char *argv[]));
100 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
102 //! Sets the expected display name for writeHelp() calls.
103 void setExpectedDisplayName(const char *expected)
105 expectedDisplayName_ = expected;
109 //! Checks the context passed to writeHelp().
110 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
114 std::string expectedDisplayName_;
117 MockModule::MockModule(const char *name, const char *description)
118 : name_(name), descr_(description)
121 using ::testing::Invoke;
122 ON_CALL(*this, init(_))
123 .WillByDefault(Invoke(&disableNice));
124 ON_CALL(*this, writeHelp(_))
125 .WillByDefault(Invoke(this, &MockModule::checkHelpContext));
128 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
130 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
132 gmx::TextLineWrapperSettings settings;
133 std::string moduleName =
134 context.writerContext().substituteMarkupAndWrapToString(
135 settings, "[THISMODULE]");
136 EXPECT_EQ(expectedDisplayName_, moduleName);
139 /********************************************************************
144 * Mock implementation of gmx::CommandLineOptionsModuleInterface.
146 * \ingroup module_commandline
148 class MockOptionsModule : public gmx::CommandLineOptionsModuleInterface
153 MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
154 MOCK_METHOD1(initOptions, void(gmx::Options *options));
155 MOCK_METHOD1(optionsFinished, void(gmx::Options *options));
156 MOCK_METHOD0(run, int());
159 MockOptionsModule::MockOptionsModule()
162 using ::testing::Invoke;
163 ON_CALL(*this, init(_))
164 .WillByDefault(Invoke(&disableNice));
167 /********************************************************************
168 * Test fixture for the tests
171 class CommandLineModuleManagerTest : public gmx::test::StringTestBase
174 void initManager(const CommandLine &args, const char *realBinaryName);
175 MockModule &addModule(const char *name, const char *description);
176 MockOptionsModule &addOptionsModule(const char *name, const char *description);
177 MockHelpTopic &addHelpTopic(const char *name, const char *title);
179 gmx::CommandLineModuleManager &manager() { return *manager_; }
181 void redirectManagerOutput()
183 manager_->setOutputRedirector(&initOutputRedirector(&fileManager_));
187 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
188 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
189 gmx::test::TestFileManager fileManager_;
192 void CommandLineModuleManagerTest::initManager(
193 const CommandLine &args, const char *realBinaryName)
196 programContext_.reset(
197 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
198 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
199 programContext_.get()));
200 manager_->setQuiet(true);
204 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
206 MockModule *module = new MockModule(name, description);
207 manager().addModule(gmx::CommandLineModulePointer(module));
212 CommandLineModuleManagerTest::addOptionsModule(const char *name, const char *description)
214 MockOptionsModule *module = new MockOptionsModule();
215 gmx::CommandLineOptionsModuleInterface::registerModule(
216 &manager(), name, description, module);
221 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
223 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
224 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
228 /********************************************************************
232 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
234 const char *const cmdline[] = {
237 CommandLine args(cmdline);
238 initManager(args, "test");
239 redirectManagerOutput();
240 addModule("module", "First module");
241 addModule("other", "Second module");
243 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
245 checkRedirectedOutputFiles();
248 TEST_F(CommandLineModuleManagerTest, RunsModule)
250 const char *const cmdline[] = {
251 "test", "module", "-flag", "yes"
253 CommandLine args(cmdline);
254 initManager(args, "test");
255 MockModule &mod1 = addModule("module", "First module");
256 addModule("other", "Second module");
258 using ::testing::Args;
259 using ::testing::ElementsAreArray;
260 EXPECT_CALL(mod1, init(_));
261 EXPECT_CALL(mod1, run(_, _))
262 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
264 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
268 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
270 const char *const cmdline[] = {
271 "test", "help", "module"
273 CommandLine args(cmdline);
274 initManager(args, "test");
275 MockModule &mod1 = addModule("module", "First module");
276 addModule("other", "Second module");
278 EXPECT_CALL(mod1, writeHelp(_));
279 mod1.setExpectedDisplayName("test module");
281 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
285 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
287 const char *const cmdline[] = {
288 "test", "module", "-h"
290 CommandLine args(cmdline);
291 initManager(args, "test");
292 MockModule &mod1 = addModule("module", "First module");
293 addModule("other", "Second module");
295 EXPECT_CALL(mod1, writeHelp(_));
296 mod1.setExpectedDisplayName("test module");
298 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
302 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
304 const char *const cmdline[] = {
307 CommandLine args(cmdline);
308 initManager(args, "g_module");
309 MockModule mod(NULL, NULL);
310 manager().setSingleModule(&mod);
312 EXPECT_CALL(mod, writeHelp(_));
313 mod.setExpectedDisplayName("g_module");
315 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
319 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
321 const char *const cmdline[] = {
322 "test", "help", "topic"
324 CommandLine args(cmdline);
325 initManager(args, "test");
326 addModule("module", "First module");
327 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
329 EXPECT_CALL(topic, writeHelp(_));
331 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
335 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
337 const char *const cmdline[] = {
338 "test", "test", "-flag", "yes"
340 CommandLine args(cmdline);
341 initManager(args, "test");
342 MockModule &mod1 = addModule("test", "Test module");
343 addModule("other", "Second module");
345 using ::testing::Args;
346 using ::testing::ElementsAreArray;
347 EXPECT_CALL(mod1, init(_));
348 EXPECT_CALL(mod1, run(_, _))
349 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
351 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
356 * Initializes Options for help export tests.
358 * \ingroup module_commandline
360 void initOptionsBasic(gmx::Options *options)
362 const char *const desc[] = {
363 "Sample description",
364 "for testing [THISMODULE]."
366 options->setDescription(desc);
367 options->addOption(gmx::IntegerOption("int"));
370 TEST_F(CommandLineModuleManagerTest, ExportsHelp)
372 const char *const cmdline[] = {
373 "test", "help", "-export", "rst"
375 // TODO: Find a more elegant solution, or get rid of the links.dat altogether.
376 gmx::File::writeFileFromString("links.dat", "");
377 CommandLine args(cmdline);
378 initManager(args, "test");
379 redirectManagerOutput();
380 MockOptionsModule &mod1 = addOptionsModule("module", "First module");
381 MockOptionsModule &mod2 = addOptionsModule("other", "Second module");
383 gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 1");
384 group.addModule("module");
387 gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 2");
388 group.addModule("other");
391 using ::testing::Invoke;
392 EXPECT_CALL(mod1, initOptions(_)).WillOnce(Invoke(&initOptionsBasic));
393 EXPECT_CALL(mod2, initOptions(_));
395 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
397 checkRedirectedOutputFiles();
398 std::remove("links.dat");