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_METHOD2(run, int(int argc, char *argv[]));
87 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
89 //! Sets the expected display name for writeHelp() calls.
90 void setExpectedDisplayName(const char *expected)
92 expectedDisplayName_ = expected;
96 //! Checks the context passed to writeHelp().
97 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
101 std::string expectedDisplayName_;
104 MockModule::MockModule(const char *name, const char *description)
105 : name_(name), descr_(description)
108 using ::testing::Invoke;
109 using ::testing::WithArg;
110 ON_CALL(*this, writeHelp(_))
111 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
114 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
116 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
118 gmx::TextLineWrapperSettings settings;
119 std::string moduleName =
120 context.writerContext().substituteMarkupAndWrapToString(
121 settings, "[THISMODULE]");
122 EXPECT_EQ(expectedDisplayName_, moduleName);
125 /********************************************************************
126 * Test fixture for the tests
129 class CommandLineModuleManagerTest : public ::testing::Test
132 void initManager(const CommandLine &args, const char *realBinaryName);
133 MockModule &addModule(const char *name, const char *description);
134 MockHelpTopic &addHelpTopic(const char *name, const char *title);
136 gmx::CommandLineModuleManager &manager() { return *manager_; }
138 void ignoreManagerOutput();
141 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
142 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
143 gmx::test::TestFileManager fileManager_;
144 boost::scoped_ptr<gmx::File> outputFile_;
147 void CommandLineModuleManagerTest::initManager(
148 const CommandLine &args, const char *realBinaryName)
151 programContext_.reset(
152 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
153 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
154 programContext_.get()));
155 manager_->setQuiet(true);
159 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
161 MockModule *module = new MockModule(name, description);
162 manager().addModule(gmx::CommandLineModulePointer(module));
167 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
169 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
170 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
174 void CommandLineModuleManagerTest::ignoreManagerOutput()
177 new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
178 manager().setOutputRedirect(outputFile_.get());
181 /********************************************************************
185 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
187 const char *const cmdline[] = {
190 CommandLine args(cmdline);
191 initManager(args, "test");
192 ignoreManagerOutput();
193 addModule("module", "First module");
194 addModule("other", "Second module");
196 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
200 TEST_F(CommandLineModuleManagerTest, RunsModule)
202 const char *const cmdline[] = {
203 "test", "module", "-flag", "yes"
205 CommandLine args(cmdline);
206 initManager(args, "test");
207 MockModule &mod1 = addModule("module", "First module");
208 addModule("other", "Second module");
210 using ::testing::Args;
211 using ::testing::ElementsAreArray;
212 EXPECT_CALL(mod1, run(_, _))
213 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
215 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
219 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
221 const char *const cmdline[] = {
222 "test", "help", "module"
224 CommandLine args(cmdline);
225 initManager(args, "test");
226 MockModule &mod1 = addModule("module", "First module");
227 addModule("other", "Second module");
229 EXPECT_CALL(mod1, writeHelp(_));
230 mod1.setExpectedDisplayName("test module");
232 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
236 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
238 const char *const cmdline[] = {
239 "test", "module", "-h"
241 CommandLine args(cmdline);
242 initManager(args, "test");
243 MockModule &mod1 = addModule("module", "First module");
244 addModule("other", "Second module");
246 EXPECT_CALL(mod1, writeHelp(_));
247 mod1.setExpectedDisplayName("test module");
249 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
253 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
255 const char *const cmdline[] = {
258 CommandLine args(cmdline);
259 initManager(args, "test");
260 MockModule &mod1 = addModule("module", "First module");
261 addModule("other", "Second module");
263 EXPECT_CALL(mod1, writeHelp(_));
264 mod1.setExpectedDisplayName("test module");
266 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
270 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
272 const char *const cmdline[] = {
275 CommandLine args(cmdline);
276 initManager(args, "g_module");
277 MockModule mod(NULL, NULL);
278 manager().setSingleModule(&mod);
280 EXPECT_CALL(mod, writeHelp(_));
281 mod.setExpectedDisplayName("g_module");
283 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
287 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
289 const char *const cmdline[] = {
290 "test", "help", "topic"
292 CommandLine args(cmdline);
293 initManager(args, "test");
294 addModule("module", "First module");
295 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
297 EXPECT_CALL(topic, writeHelp(_));
299 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
303 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
305 const char *const cmdline[] = {
306 "g_module", "-flag", "yes"
308 CommandLine args(cmdline);
309 initManager(args, "test");
310 MockModule &mod1 = addModule("module", "First module");
311 addModule("other", "Second module");
313 using ::testing::Args;
314 using ::testing::ElementsAreArray;
315 EXPECT_CALL(mod1, run(_, _))
316 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
318 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
322 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
324 const char *const cmdline[] = {
325 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
327 CommandLine args(cmdline);
328 initManager(args, "test");
329 MockModule &mod1 = addModule("module", "First module");
330 addModule("other", "Second module");
332 using ::testing::Args;
333 using ::testing::ElementsAreArray;
334 EXPECT_CALL(mod1, run(_, _))
335 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
337 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
341 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
343 const char *const cmdline[] = {
344 "test", "test", "-flag", "yes"
346 CommandLine args(cmdline);
347 initManager(args, "test");
348 MockModule &mod1 = addModule("test", "Test module");
349 addModule("other", "Second module");
351 using ::testing::Args;
352 using ::testing::ElementsAreArray;
353 EXPECT_CALL(mod1, run(_, _))
354 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
356 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));