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/cmdlineprogramcontext.h"
53 #include "gromacs/utility/file.h"
55 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/testasserts.h"
58 #include "testutils/testfilemanager.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_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
85 MOCK_METHOD2(run, int(int argc, char *argv[]));
86 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
88 //! Sets the expected display name for writeHelp() calls.
89 void setExpectedDisplayName(const char *expected)
91 expectedDisplayName_ = expected;
95 //! Disable nice() calls for tests.
96 void disableNice(gmx::CommandLineModuleSettings *settings)
98 settings->setDefaultNiceLevel(0);
100 //! Checks the context passed to writeHelp().
101 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
105 std::string expectedDisplayName_;
108 MockModule::MockModule(const char *name, const char *description)
109 : name_(name), descr_(description)
112 using ::testing::Invoke;
113 using ::testing::WithArg;
114 ON_CALL(*this, init(_))
115 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::disableNice)));
116 ON_CALL(*this, writeHelp(_))
117 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
120 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
122 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
124 gmx::TextLineWrapperSettings settings;
125 std::string moduleName =
126 context.writerContext().substituteMarkupAndWrapToString(
127 settings, "[THISMODULE]");
128 EXPECT_EQ(expectedDisplayName_, moduleName);
131 /********************************************************************
132 * Test fixture for the tests
135 class CommandLineModuleManagerTest : public ::testing::Test
138 void initManager(const CommandLine &args, const char *realBinaryName);
139 MockModule &addModule(const char *name, const char *description);
140 MockHelpTopic &addHelpTopic(const char *name, const char *title);
142 gmx::CommandLineModuleManager &manager() { return *manager_; }
144 void ignoreManagerOutput();
147 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
148 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
149 gmx::test::TestFileManager fileManager_;
150 boost::scoped_ptr<gmx::File> outputFile_;
153 void CommandLineModuleManagerTest::initManager(
154 const CommandLine &args, const char *realBinaryName)
157 programContext_.reset(
158 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
159 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
160 programContext_.get()));
161 manager_->setQuiet(true);
165 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
167 MockModule *module = new MockModule(name, description);
168 manager().addModule(gmx::CommandLineModulePointer(module));
173 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
175 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
176 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
180 void CommandLineModuleManagerTest::ignoreManagerOutput()
183 new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
184 manager().setOutputRedirect(outputFile_.get());
187 /********************************************************************
191 TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
193 const char *const cmdline[] = {
196 CommandLine args(cmdline);
197 initManager(args, "test");
198 ignoreManagerOutput();
199 addModule("module", "First module");
200 addModule("other", "Second module");
202 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
206 TEST_F(CommandLineModuleManagerTest, RunsModule)
208 const char *const cmdline[] = {
209 "test", "module", "-flag", "yes"
211 CommandLine args(cmdline);
212 initManager(args, "test");
213 MockModule &mod1 = addModule("module", "First module");
214 addModule("other", "Second module");
216 using ::testing::Args;
217 using ::testing::ElementsAreArray;
218 EXPECT_CALL(mod1, init(_));
219 EXPECT_CALL(mod1, run(_, _))
220 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
222 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
226 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
228 const char *const cmdline[] = {
229 "test", "help", "module"
231 CommandLine args(cmdline);
232 initManager(args, "test");
233 MockModule &mod1 = addModule("module", "First module");
234 addModule("other", "Second module");
236 EXPECT_CALL(mod1, writeHelp(_));
237 mod1.setExpectedDisplayName("test module");
239 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
243 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
245 const char *const cmdline[] = {
246 "test", "module", "-h"
248 CommandLine args(cmdline);
249 initManager(args, "test");
250 MockModule &mod1 = addModule("module", "First module");
251 addModule("other", "Second module");
253 EXPECT_CALL(mod1, writeHelp(_));
254 mod1.setExpectedDisplayName("test module");
256 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
260 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
262 const char *const cmdline[] = {
265 CommandLine args(cmdline);
266 initManager(args, "g_module");
267 MockModule mod(NULL, NULL);
268 manager().setSingleModule(&mod);
270 EXPECT_CALL(mod, writeHelp(_));
271 mod.setExpectedDisplayName("g_module");
273 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
277 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
279 const char *const cmdline[] = {
280 "test", "help", "topic"
282 CommandLine args(cmdline);
283 initManager(args, "test");
284 addModule("module", "First module");
285 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
287 EXPECT_CALL(topic, writeHelp(_));
289 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
293 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
295 const char *const cmdline[] = {
296 "test", "test", "-flag", "yes"
298 CommandLine args(cmdline);
299 initManager(args, "test");
300 MockModule &mod1 = addModule("test", "Test module");
301 addModule("other", "Second module");
303 using ::testing::Args;
304 using ::testing::ElementsAreArray;
305 EXPECT_CALL(mod1, init(_));
306 EXPECT_CALL(mod1, run(_, _))
307 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
309 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));