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"
56 using gmx::test::CommandLine;
58 /********************************************************************
63 * Mock implementation of gmx::CommandLineModuleInterface.
65 * \ingroup module_commandline
67 class MockModule : public gmx::CommandLineModuleInterface
70 //! Creates a mock module with the given name and description.
71 MockModule(const char *name, const char *description);
73 virtual const char *name() const { return name_; }
74 virtual const char *shortDescription() const { return descr_; }
76 MOCK_METHOD2(run, int(int argc, char *argv[]));
83 MockModule::MockModule(const char *name, const char *description)
84 : name_(name), descr_(description)
88 /********************************************************************
89 * Test fixture for the tests
92 class CommandLineModuleManagerTest : public ::testing::Test
95 void initManager(const CommandLine &args);
96 MockModule &addModule(const char *name, const char *description);
98 gmx::CommandLineModuleManager &manager() { return *manager_; }
101 boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
102 boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
105 void CommandLineModuleManagerTest::initManager(const CommandLine &args)
108 programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
109 manager_.reset(new gmx::CommandLineModuleManager(*programInfo_));
113 CommandLineModuleManagerTest::addModule(const char *name, const char *description)
115 MockModule *module = new MockModule(name, description);
116 manager().addModule(gmx::CommandLineModulePointer(module));
120 /********************************************************************
124 TEST_F(CommandLineModuleManagerTest, RunsModule)
126 const char *const cmdline[] = {
127 "g_test", "module", "-flag", "yes"
129 CommandLine args(CommandLine::create(cmdline));
131 MockModule &mod1 = addModule("module", "First module");
132 addModule("other", "Second module");
134 using ::testing::Args;
135 using ::testing::ElementsAreArray;
136 EXPECT_CALL(mod1, run(_, _))
137 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
139 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
143 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
145 const char *const cmdline[] = {
146 "g_module", "-flag", "yes"
148 CommandLine args(CommandLine::create(cmdline));
150 MockModule &mod1 = addModule("module", "First module");
151 addModule("other", "Second module");
153 using ::testing::Args;
154 using ::testing::ElementsAreArray;
155 EXPECT_CALL(mod1, run(_, _))
156 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
158 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
162 TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffix)
164 const char *const cmdline[] = {
165 "/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
167 CommandLine args(CommandLine::create(cmdline));
169 MockModule &mod1 = addModule("module", "First module");
170 addModule("other", "Second module");
172 using ::testing::Args;
173 using ::testing::ElementsAreArray;
174 EXPECT_CALL(mod1, run(_, _))
175 .With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
177 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
181 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
183 const char *const cmdline[] = {
184 "g_test", "test", "-flag", "yes"
186 CommandLine args(CommandLine::create(cmdline));
188 MockModule &mod1 = addModule("test", "Test module");
189 addModule("other", "Second module");
191 using ::testing::Args;
192 using ::testing::ElementsAreArray;
193 EXPECT_CALL(mod1, run(_, _))
194 .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
196 ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));