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"
56 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
57 #include "testutils/cmdlinetest.h"
58 #include "testutils/testasserts.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_METHOD2(run, int(int argc, char *argv[]));
85 MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
87 //! Sets the expected display name for writeHelp() calls.
88 void setExpectedDisplayName(const char *expected)
90 expectedDisplayName_ = expected;
94 //! Checks the context passed to writeHelp().
95 void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
99 std::string expectedDisplayName_;
102 MockModule::MockModule(const char *name, const char *description)
103 : name_(name), descr_(description)
106 using ::testing::Invoke;
107 using ::testing::WithArg;
108 ON_CALL(*this, writeHelp(_))
109 .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
112 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
114 EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
116 gmx::TextLineWrapperSettings settings;
117 std::string moduleName =
118 context.writerContext().substituteMarkupAndWrapToString(
119 settings, "[THISMODULE]");
120 EXPECT_EQ(expectedDisplayName_, moduleName);
123 /********************************************************************
124 * Test fixture for the tests
127 class CommandLineModuleManagerTest : public ::testing::Test
130 void initManager(const CommandLine &args, const char *realBinaryName);
131 MockModule &addModule(const char *name, const char *description);
132 MockHelpTopic &addHelpTopic(const char *name, const char *title);
134 gmx::CommandLineModuleManager &manager() { return *manager_; }
137 boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
138 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
141 void CommandLineModuleManagerTest::initManager(
142 const CommandLine &args, const char *realBinaryName)
145 programContext_.reset(
146 new gmx::CommandLineProgramContext(args.argc(), args.argv()));
147 manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
148 programContext_.get()));
149 manager_->setQuiet(true);
153 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
155 MockModule *module = new MockModule(name, description);
156 manager().addModule(gmx::CommandLineModulePointer(module));
161 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
163 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
164 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
168 /********************************************************************
172 TEST_F(CommandLineModuleManagerTest, RunsModule)
174 const char *const cmdline[] = {
175 "test", "module", "-flag", "yes"
177 CommandLine args(cmdline);
178 initManager(args, "test");
179 MockModule &mod1 = addModule("module", "First module");
180 addModule("other", "Second module");
182 using ::testing::Args;
183 using ::testing::ElementsAreArray;
184 EXPECT_CALL(mod1, run(_, _))
185 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
187 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
191 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
193 const char *const cmdline[] = {
194 "test", "help", "module"
196 CommandLine args(cmdline);
197 initManager(args, "test");
198 MockModule &mod1 = addModule("module", "First module");
199 addModule("other", "Second module");
201 EXPECT_CALL(mod1, writeHelp(_));
202 mod1.setExpectedDisplayName("test module");
204 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
208 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
210 const char *const cmdline[] = {
211 "test", "module", "-h"
213 CommandLine args(cmdline);
214 initManager(args, "test");
215 MockModule &mod1 = addModule("module", "First module");
216 addModule("other", "Second module");
218 EXPECT_CALL(mod1, writeHelp(_));
219 mod1.setExpectedDisplayName("test module");
221 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
225 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
227 const char *const cmdline[] = {
230 CommandLine args(cmdline);
231 initManager(args, "test");
232 MockModule &mod1 = addModule("module", "First module");
233 addModule("other", "Second module");
235 EXPECT_CALL(mod1, writeHelp(_));
236 mod1.setExpectedDisplayName("test module");
238 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
242 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
244 const char *const cmdline[] = {
247 CommandLine args(cmdline);
248 initManager(args, "g_module");
249 MockModule mod(NULL, NULL);
250 manager().setSingleModule(&mod);
252 EXPECT_CALL(mod, writeHelp(_));
253 mod.setExpectedDisplayName("g_module");
255 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
259 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
261 const char *const cmdline[] = {
262 "test", "help", "topic"
264 CommandLine args(cmdline);
265 initManager(args, "test");
266 addModule("module", "First module");
267 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
269 EXPECT_CALL(topic, writeHelp(_));
271 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
275 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
277 const char *const cmdline[] = {
278 "g_module", "-flag", "yes"
280 CommandLine args(cmdline);
281 initManager(args, "test");
282 MockModule &mod1 = addModule("module", "First module");
283 addModule("other", "Second module");
285 using ::testing::Args;
286 using ::testing::ElementsAreArray;
287 EXPECT_CALL(mod1, run(_, _))
288 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
290 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
294 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
296 const char *const cmdline[] = {
297 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
299 CommandLine args(cmdline);
300 initManager(args, "test");
301 MockModule &mod1 = addModule("module", "First module");
302 addModule("other", "Second module");
304 using ::testing::Args;
305 using ::testing::ElementsAreArray;
306 EXPECT_CALL(mod1, run(_, _))
307 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
309 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
313 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
315 const char *const cmdline[] = {
316 "test", "test", "-flag", "yes"
318 CommandLine args(cmdline);
319 initManager(args, "test");
320 MockModule &mod1 = addModule("test", "Test module");
321 addModule("other", "Second module");
323 using ::testing::Args;
324 using ::testing::ElementsAreArray;
325 EXPECT_CALL(mod1, run(_, _))
326 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
328 ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));