3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Tests gmx::CommandLineModuleManager.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_commandline
38 // For GMX_BINARY_SUFFIX
45 #include <gmock/gmock.h>
47 #include "gromacs/commandline/cmdlinemodule.h"
48 #include "gromacs/commandline/cmdlinemodulemanager.h"
49 #include "gromacs/utility/programinfo.h"
51 #include "testutils/cmdlinetest.h"
52 #include "testutils/mock_helptopic.h"
57 using gmx::test::CommandLine;
58 using gmx::test::MockHelpTopic;
60 /********************************************************************
65 * Mock implementation of gmx::CommandLineModuleInterface.
67 * \ingroup module_commandline
69 class MockModule : public gmx::CommandLineModuleInterface
72 //! Creates a mock module with the given name and description.
73 MockModule(const char *name, const char *description);
75 virtual const char *name() const { return name_; }
76 virtual const char *shortDescription() const { return descr_; }
78 MOCK_METHOD2(run, int(int argc, char *argv[]));
79 MOCK_CONST_METHOD1(writeHelp, void(const gmx::HelpWriterContext &context));
86 MockModule::MockModule(const char *name, const char *description)
87 : name_(name), descr_(description)
91 /********************************************************************
92 * Test fixture for the tests
95 class CommandLineModuleManagerTest : public ::testing::Test
98 void initManager(const CommandLine &args);
99 MockModule &addModule(const char *name, const char *description);
100 MockHelpTopic &addHelpTopic(const char *name, const char *title);
102 gmx::CommandLineModuleManager &manager() { return *manager_; }
105 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
106 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
109 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
112 programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
113 manager_.reset(new gmx::CommandLineModuleManager(*programInfo_));
117 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
119 MockModule *module = new MockModule(name, description);
120 manager().addModule(gmx::CommandLineModulePointer(module));
125 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
127 MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
128 manager().addHelpTopic(gmx::HelpTopicPointer(topic));
132 /********************************************************************
136 TEST_F(CommandLineModuleManagerTest, RunsModule)
138 const char *const cmdline[] = {
139 "g_test", "module", "-flag", "yes"
141 CommandLine args(CommandLine::create(cmdline));
143 MockModule &mod1 = addModule("module", "First module");
144 addModule("other", "Second module");
146 using ::testing::Args;
147 using ::testing::ElementsAreArray;
148 EXPECT_CALL(mod1, run(_, _))
149 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
151 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
155 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
157 const char *const cmdline[] = {
158 "g_test", "help", "module"
160 CommandLine args(CommandLine::create(cmdline));
162 MockModule &mod1 = addModule("module", "First module");
163 addModule("other", "Second module");
165 EXPECT_CALL(mod1, writeHelp(_));
167 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
171 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
173 const char *const cmdline[] = {
174 "g_test", "help", "topic"
176 CommandLine args(CommandLine::create(cmdline));
178 addModule("module", "First module");
179 MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
181 EXPECT_CALL(topic, writeHelp(_));
183 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
187 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
189 const char *const cmdline[] = {
190 "g_module", "-flag", "yes"
192 CommandLine args(CommandLine::create(cmdline));
194 MockModule &mod1 = addModule("module", "First module");
195 addModule("other", "Second module");
197 using ::testing::Args;
198 using ::testing::ElementsAreArray;
199 EXPECT_CALL(mod1, run(_, _))
200 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
202 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
206 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
208 const char *const cmdline[] = {
209 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
211 CommandLine args(CommandLine::create(cmdline));
213 MockModule &mod1 = addModule("module", "First module");
214 addModule("other", "Second module");
216 using ::testing::Args;
217 using ::testing::ElementsAreArray;
218 EXPECT_CALL(mod1, run(_, _))
219 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
221 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
225 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
227 const char *const cmdline[] = {
228 "g_test", "test", "-flag", "yes"
230 CommandLine args(CommandLine::create(cmdline));
232 MockModule &mod1 = addModule("test", "Test module");
233 addModule("other", "Second module");
235 using ::testing::Args;
236 using ::testing::ElementsAreArray;
237 EXPECT_CALL(mod1, run(_, _))
238 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
240 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));