/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2012,2013, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
* GROMACS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* \ingroup module_commandline
*/
// For GMX_BINARY_SUFFIX
-#ifdef HAVE_CONFIG_H
+#include "gmxpre.h"
+
+#include "gromacs/commandline/cmdlinemodulemanager.h"
+
#include "config.h"
-#endif
#include <vector>
#include <gmock/gmock.h>
+#include "gromacs/commandline/cmdlinehelpcontext.h"
#include "gromacs/commandline/cmdlinemodule.h"
-#include "gromacs/commandline/cmdlinemodulemanager.h"
-#include "gromacs/utility/programinfo.h"
+#include "gromacs/commandline/cmdlineprogramcontext.h"
+#include "gromacs/utility/file.h"
+#include "gromacs/onlinehelp/tests/mock_helptopic.h"
#include "testutils/cmdlinetest.h"
-#include "testutils/mock_helptopic.h"
#include "testutils/testasserts.h"
+#include "testutils/testfilemanager.h"
namespace
{
virtual const char *name() const { return name_; }
virtual const char *shortDescription() const { return descr_; }
+ MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
MOCK_METHOD2(run, int(int argc, char *argv[]));
- MOCK_CONST_METHOD1(writeHelp, void(const gmx::HelpWriterContext &context));
+ MOCK_CONST_METHOD1(writeHelp, void(const gmx::CommandLineHelpContext &context));
+
+ //! Sets the expected display name for writeHelp() calls.
+ void setExpectedDisplayName(const char *expected)
+ {
+ expectedDisplayName_ = expected;
+ }
private:
+ //! Disable nice() calls for tests.
+ void disableNice(gmx::CommandLineModuleSettings *settings)
+ {
+ settings->setDefaultNiceLevel(0);
+ }
+ //! Checks the context passed to writeHelp().
+ void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
+
const char *name_;
const char *descr_;
+ std::string expectedDisplayName_;
};
MockModule::MockModule(const char *name, const char *description)
: name_(name), descr_(description)
{
+ using ::testing::_;
+ using ::testing::Invoke;
+ using ::testing::WithArg;
+ ON_CALL(*this, init(_))
+ .WillByDefault(WithArg<0>(Invoke(this, &MockModule::disableNice)));
+ ON_CALL(*this, writeHelp(_))
+ .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
+}
+
+void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
+{
+ EXPECT_EQ(expectedDisplayName_, context.moduleDisplayName());
+
+ gmx::TextLineWrapperSettings settings;
+ std::string moduleName =
+ context.writerContext().substituteMarkupAndWrapToString(
+ settings, "[THISMODULE]");
+ EXPECT_EQ(expectedDisplayName_, moduleName);
}
/********************************************************************
class CommandLineModuleManagerTest : public ::testing::Test
{
public:
- void initManager(const CommandLine &args);
+ void initManager(const CommandLine &args, const char *realBinaryName);
MockModule &addModule(const char *name, const char *description);
MockHelpTopic &addHelpTopic(const char *name, const char *title);
gmx::CommandLineModuleManager &manager() { return *manager_; }
+ void ignoreManagerOutput();
+
private:
- boost::scoped_ptr<gmx::ProgramInfo> programInfo_;
- boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
+ boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
+ boost::scoped_ptr<gmx::CommandLineModuleManager> manager_;
+ gmx::test::TestFileManager fileManager_;
+ boost::scoped_ptr<gmx::File> outputFile_;
};
-void CommandLineModuleManagerTest::initManager(const CommandLine &args)
+void CommandLineModuleManagerTest::initManager(
+ const CommandLine &args, const char *realBinaryName)
{
manager_.reset();
- programInfo_.reset(new gmx::ProgramInfo("g_test", args.argc(), args.argv()));
- manager_.reset(new gmx::CommandLineModuleManager(programInfo_.get()));
+ programContext_.reset(
+ new gmx::CommandLineProgramContext(args.argc(), args.argv()));
+ manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
+ programContext_.get()));
manager_->setQuiet(true);
}
return *topic;
}
+void CommandLineModuleManagerTest::ignoreManagerOutput()
+{
+ outputFile_.reset(
+ new gmx::File(fileManager_.getTemporaryFilePath("out.txt"), "w"));
+ manager().setOutputRedirect(outputFile_.get());
+}
+
/********************************************************************
* Actual tests
*/
+TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
+{
+ const char *const cmdline[] = {
+ "test"
+ };
+ CommandLine args(cmdline);
+ initManager(args, "test");
+ ignoreManagerOutput();
+ addModule("module", "First module");
+ addModule("other", "Second module");
+ int rc = 0;
+ ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+ ASSERT_EQ(0, rc);
+}
+
TEST_F(CommandLineModuleManagerTest, RunsModule)
{
const char *const cmdline[] = {
- "g_test", "module", "-flag", "yes"
+ "test", "module", "-flag", "yes"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
MockModule &mod1 = addModule("module", "First module");
addModule("other", "Second module");
using ::testing::_;
using ::testing::Args;
using ::testing::ElementsAreArray;
+ EXPECT_CALL(mod1, init(_));
EXPECT_CALL(mod1, run(_, _))
.With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
int rc = 0;
TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
{
const char *const cmdline[] = {
- "g_test", "help", "module"
+ "test", "help", "module"
+ };
+ CommandLine args(cmdline);
+ initManager(args, "test");
+ MockModule &mod1 = addModule("module", "First module");
+ addModule("other", "Second module");
+ using ::testing::_;
+ EXPECT_CALL(mod1, writeHelp(_));
+ mod1.setExpectedDisplayName("test module");
+ int rc = 0;
+ ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+ ASSERT_EQ(0, rc);
+}
+
+TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
+{
+ const char *const cmdline[] = {
+ "test", "module", "-h"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
MockModule &mod1 = addModule("module", "First module");
addModule("other", "Second module");
using ::testing::_;
EXPECT_CALL(mod1, writeHelp(_));
+ mod1.setExpectedDisplayName("test module");
+ int rc = 0;
+ ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+ ASSERT_EQ(0, rc);
+}
+
+TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSymLink)
+{
+ const char *const cmdline[] = {
+ "g_module", "-h"
+ };
+ CommandLine args(cmdline);
+ initManager(args, "test");
+ MockModule &mod1 = addModule("module", "First module");
+ addModule("other", "Second module");
+ using ::testing::_;
+ EXPECT_CALL(mod1, writeHelp(_));
+ mod1.setExpectedDisplayName("test module");
+ int rc = 0;
+ ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+ ASSERT_EQ(0, rc);
+}
+
+TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
+{
+ const char *const cmdline[] = {
+ "g_module", "-h"
+ };
+ CommandLine args(cmdline);
+ initManager(args, "g_module");
+ MockModule mod(NULL, NULL);
+ manager().setSingleModule(&mod);
+ using ::testing::_;
+ EXPECT_CALL(mod, writeHelp(_));
+ mod.setExpectedDisplayName("g_module");
int rc = 0;
ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
ASSERT_EQ(0, rc);
TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
{
const char *const cmdline[] = {
- "g_test", "help", "topic"
+ "test", "help", "topic"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
addModule("module", "First module");
MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
using ::testing::_;
const char *const cmdline[] = {
"g_module", "-flag", "yes"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
MockModule &mod1 = addModule("module", "First module");
addModule("other", "Second module");
using ::testing::_;
using ::testing::Args;
using ::testing::ElementsAreArray;
+ EXPECT_CALL(mod1, init(_));
EXPECT_CALL(mod1, run(_, _))
.With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
int rc = 0;
const char *const cmdline[] = {
"/usr/local/gromacs/bin/g_module" GMX_BINARY_SUFFIX ".exe", "-flag", "yes"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
MockModule &mod1 = addModule("module", "First module");
addModule("other", "Second module");
using ::testing::_;
using ::testing::Args;
using ::testing::ElementsAreArray;
+ EXPECT_CALL(mod1, init(_));
EXPECT_CALL(mod1, run(_, _))
.With(Args<1, 0>(ElementsAreArray(args.argv(), args.argc())));
int rc = 0;
TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
{
const char *const cmdline[] = {
- "g_test", "test", "-flag", "yes"
+ "test", "test", "-flag", "yes"
};
- CommandLine args(CommandLine::create(cmdline));
- initManager(args);
+ CommandLine args(cmdline);
+ initManager(args, "test");
MockModule &mod1 = addModule("test", "Test module");
addModule("other", "Second module");
using ::testing::_;
using ::testing::Args;
using ::testing::ElementsAreArray;
+ EXPECT_CALL(mod1, init(_));
EXPECT_CALL(mod1, run(_, _))
.With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
int rc = 0;