Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
index 85764af5f03f4d1dcf606ea401ee4ad255900f46..ebb3ee1f5fb67d5dce49bdaafc21b23baa943a41 100644 (file)
  * \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 "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/testasserts.h"
+#include "testutils/testfilemanager.h"
 
 namespace
 {
@@ -81,6 +84,7 @@ class MockModule : public gmx::CommandLineModuleInterface
         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::CommandLineHelpContext &context));
 
@@ -91,6 +95,11 @@ class MockModule : public gmx::CommandLineModuleInterface
         }
 
     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;
 
@@ -105,6 +114,8 @@ MockModule::MockModule(const char *name, const char *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)));
 }
@@ -133,17 +144,23 @@ class CommandLineModuleManagerTest : public ::testing::Test
 
         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, const char *realBinaryName)
 {
     manager_.reset();
-    programInfo_.reset(new gmx::ProgramInfo(args.argc(), args.argv()));
-    manager_.reset(new gmx::CommandLineModuleManager(realBinaryName, programInfo_.get()));
+    programContext_.reset(
+            new gmx::CommandLineProgramContext(args.argc(), args.argv()));
+    manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
+                                                     programContext_.get()));
     manager_->setQuiet(true);
 }
 
@@ -163,10 +180,32 @@ CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
     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[] = {
@@ -179,6 +218,7 @@ TEST_F(CommandLineModuleManagerTest, RunsModule)
     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;
@@ -282,6 +322,7 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
     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;
@@ -301,6 +342,7 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffi
     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;
@@ -320,6 +362,7 @@ TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
     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;