Some tests for 'gmx help -export rst'
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
index d680cfef6e435964c72e22101944ecf4d58055c3..6ff23d78ece228d7bc6da9adb16d54788189eeea 100644 (file)
 
 #include "gromacs/commandline/cmdlinemodulemanager.h"
 
-#include <vector>
+#include <cstdio>
 
 #include <gmock/gmock.h>
 
 #include "gromacs/commandline/cmdlinehelpcontext.h"
 #include "gromacs/commandline/cmdlinemodule.h"
+#include "gromacs/commandline/cmdlineoptionsmodule.h"
 #include "gromacs/commandline/cmdlineprogramcontext.h"
+#include "gromacs/options/basicoptions.h"
+#include "gromacs/options/options.h"
 #include "gromacs/utility/file.h"
 
 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
 #include "testutils/cmdlinetest.h"
+#include "testutils/stringtest.h"
 #include "testutils/testasserts.h"
 #include "testutils/testfilemanager.h"
 
@@ -63,6 +67,16 @@ namespace
 using gmx::test::CommandLine;
 using gmx::test::MockHelpTopic;
 
+/*! \brief
+ * Helper method to disable nice() calls from CommandLineModuleManager.
+ *
+ * \ingroup module_commandline
+ */
+void disableNice(gmx::CommandLineModuleSettings *settings)
+{
+    settings->setDefaultNiceLevel(0);
+}
+
 /********************************************************************
  * MockModule
  */
@@ -92,11 +106,6 @@ 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;
 
@@ -110,11 +119,10 @@ 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)));
+        .WillByDefault(Invoke(&disableNice));
     ON_CALL(*this, writeHelp(_))
-        .WillByDefault(WithArg<0>(Invoke(this, &MockModule::checkHelpContext)));
+        .WillByDefault(Invoke(this, &MockModule::checkHelpContext));
 }
 
 void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) const
@@ -128,26 +136,57 @@ void MockModule::checkHelpContext(const gmx::CommandLineHelpContext &context) co
     EXPECT_EQ(expectedDisplayName_, moduleName);
 }
 
+/********************************************************************
+ * MockOptionsModule
+ */
+
+/*! \internal \brief
+ * Mock implementation of gmx::CommandLineOptionsModuleInterface.
+ *
+ * \ingroup module_commandline
+ */
+class MockOptionsModule : public gmx::CommandLineOptionsModuleInterface
+{
+    public:
+        MockOptionsModule();
+
+        MOCK_METHOD1(init, void(gmx::CommandLineModuleSettings *settings));
+        MOCK_METHOD1(initOptions, void(gmx::Options *options));
+        MOCK_METHOD1(optionsFinished, void(gmx::Options *options));
+        MOCK_METHOD0(run, int());
+};
+
+MockOptionsModule::MockOptionsModule()
+{
+    using ::testing::_;
+    using ::testing::Invoke;
+    ON_CALL(*this, init(_))
+        .WillByDefault(Invoke(&disableNice));
+}
+
 /********************************************************************
  * Test fixture for the tests
  */
 
-class CommandLineModuleManagerTest : public ::testing::Test
+class CommandLineModuleManagerTest : public gmx::test::StringTestBase
 {
     public:
         void initManager(const CommandLine &args, const char *realBinaryName);
-        MockModule    &addModule(const char *name, const char *description);
-        MockHelpTopic &addHelpTopic(const char *name, const char *title);
+        MockModule        &addModule(const char *name, const char *description);
+        MockOptionsModule &addOptionsModule(const char *name, const char *description);
+        MockHelpTopic     &addHelpTopic(const char *name, const char *title);
 
         gmx::CommandLineModuleManager &manager() { return *manager_; }
 
-        void ignoreManagerOutput();
+        void redirectManagerOutput()
+        {
+            manager_->setOutputRedirector(&initOutputRedirector(&fileManager_));
+        }
 
     private:
         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(
@@ -169,6 +208,15 @@ CommandLineModuleManagerTest::addModule(const char *name, const char *descriptio
     return *module;
 }
 
+MockOptionsModule &
+CommandLineModuleManagerTest::addOptionsModule(const char *name, const char *description)
+{
+    MockOptionsModule *module = new MockOptionsModule();
+    gmx::CommandLineOptionsModuleInterface::registerModule(
+            &manager(), name, description, module);
+    return *module;
+}
+
 MockHelpTopic &
 CommandLineModuleManagerTest::addHelpTopic(const char *name, const char *title)
 {
@@ -177,13 +225,6 @@ 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
  */
@@ -195,12 +236,13 @@ TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
     };
     CommandLine       args(cmdline);
     initManager(args, "test");
-    ignoreManagerOutput();
+    redirectManagerOutput();
     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);
+    checkRedirectedOutputFiles();
 }
 
 TEST_F(CommandLineModuleManagerTest, RunsModule)
@@ -310,4 +352,50 @@ TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
     ASSERT_EQ(0, rc);
 }
 
+/*! \brief
+ * Initializes Options for help export tests.
+ *
+ * \ingroup module_commandline
+ */
+void initOptionsBasic(gmx::Options *options)
+{
+    const char *const desc[] = {
+        "Sample description",
+        "for testing [THISMODULE]."
+    };
+    options->setDescription(desc);
+    options->addOption(gmx::IntegerOption("int"));
+}
+
+TEST_F(CommandLineModuleManagerTest, ExportsHelp)
+{
+    const char *const cmdline[] = {
+        "test", "help", "-export", "rst"
+    };
+    // TODO: Find a more elegant solution, or get rid of the links.dat altogether.
+    gmx::File::writeFileFromString("links.dat", "");
+    CommandLine       args(cmdline);
+    initManager(args, "test");
+    redirectManagerOutput();
+    MockOptionsModule &mod1 = addOptionsModule("module", "First module");
+    MockOptionsModule &mod2 = addOptionsModule("other", "Second module");
+    {
+        gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 1");
+        group.addModule("module");
+    }
+    {
+        gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 2");
+        group.addModule("other");
+    }
+    using ::testing::_;
+    using ::testing::Invoke;
+    EXPECT_CALL(mod1, initOptions(_)).WillOnce(Invoke(&initOptionsBasic));
+    EXPECT_CALL(mod2, initOptions(_));
+    int rc = 0;
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_EQ(0, rc);
+    checkRedirectedOutputFiles();
+    std::remove("links.dat");
+}
+
 } // namespace