Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
index 17a848ff0de40c54613ef63c8ec9da4ea86325e2..ebb3ee1f5fb67d5dce49bdaafc21b23baa943a41 100644 (file)
@@ -1,32 +1,36 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * 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.
  *
- *                 G   R   O   M   A   C   S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
  *
- *          GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
  * \brief
  * \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
 {
@@ -75,17 +84,51 @@ 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::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);
 }
 
 /********************************************************************
@@ -95,22 +138,30 @@ MockModule::MockModule(const char *name, const char *description)
 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_));
+    programContext_.reset(
+            new gmx::CommandLineProgramContext(args.argc(), args.argv()));
+    manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
+                                                     programContext_.get()));
+    manager_->setQuiet(true);
 }
 
 MockModule &
@@ -129,58 +180,133 @@ 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[] = {
-        "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;
-    ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
     ASSERT_EQ(0, rc);
 }
 
 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
 {
     const char *const cmdline[] = {
-        "g_test", "help", "module"
+        "test", "help", "module"
     };
-    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, RunsModuleHelpWithDashH)
+{
+    const char *const cmdline[] = {
+        "test", "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, 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(rc = manager().run(args.argc(), args.argv()));
+    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::_;
     EXPECT_CALL(topic, writeHelp(_));
     int rc = 0;
-    ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
     ASSERT_EQ(0, rc);
 }
 
@@ -189,17 +315,18 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryName)
     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;
-    ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
     ASSERT_EQ(0, rc);
 }
 
@@ -208,36 +335,38 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleBasedOnBinaryNameWithPathAndSuffi
     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;
-    ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
     ASSERT_EQ(0, rc);
 }
 
 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;
-    ASSERT_NO_THROW(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
     ASSERT_EQ(0, rc);
 }