Restructure CommandLineModuleManager tests
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 17 May 2015 04:07:41 +0000 (07:07 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Thu, 21 May 2015 17:33:46 +0000 (19:33 +0200)
Split tests from cmdlinemodulemanager.cpp to two files, one testing the
module manager itself, and the other the actual help output.
Extend the help tests slightly for better coverage (in particular for help
topic handling in the help output).

Move common functionality to cmdlinemodulemanagertest.*.

Change-Id: Ic709632deae21aa3ece2074a80de1e37d815d6e1

src/gromacs/commandline/tests/CMakeLists.txt
src/gromacs/commandline/tests/cmdlinehelpmodule.cpp [new file with mode: 0644]
src/gromacs/commandline/tests/cmdlinemodulemanager.cpp
src/gromacs/commandline/tests/cmdlinemodulemanagertest.cpp [new file with mode: 0644]
src/gromacs/commandline/tests/cmdlinemodulemanagertest.h [new file with mode: 0644]
src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_ExportsHelp.xml [moved from src/gromacs/commandline/tests/refdata/CommandLineModuleManagerTest_ExportsHelp.xml with 100% similarity]
src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_PrintsGeneralHelp.xml [moved from src/gromacs/commandline/tests/refdata/CommandLineModuleManagerTest_RunsGeneralHelp.xml with 97% similarity]
src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_PrintsHelpOnTopic.xml [new file with mode: 0644]
src/gromacs/onlinehelp/tests/mock_helptopic.h

index e9718d6b6b1ede8ae1a00995d30af529a6325b54..2e9be1a9086bf9c5ab148b9939adc5c765cae608 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+# Copyright (c) 2012,2013,2014,2015, 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.
@@ -52,8 +52,10 @@ if (UNIX)
 endif()
 
 gmx_add_unit_test(CommandLineUnitTests commandline-test
+                  cmdlinehelpmodule.cpp
                   cmdlinehelpwriter.cpp
                   cmdlinemodulemanager.cpp
+                  cmdlinemodulemanagertest.cpp
                   cmdlineparser.cpp
                   cmdlineprogramcontext.cpp
                   pargs.cpp
@@ -63,5 +65,6 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "XL")
     # This suppression stops a very verbose cascade of messages about the
     # mocks, which is probably a compiler issue.
     #   1540-2924 (W) Cannot pass an argument of non-POD class type "const gmx::CommandLineHelpContext" through ellipsis.
-    set_property(SOURCE cmdlinemodulemanager.cpp PROPERTY COMPILE_FLAGS "-qsuppress=1540-2924")
+    set_property(SOURCE cmdlinehelpmodule.cpp cmdlinemodulemanager.cpp cmdlinemodulemanagertest.cpp
+                 PROPERTY COMPILE_FLAGS "-qsuppress=1540-2924")
 endif()
diff --git a/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp b/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp
new file mode 100644 (file)
index 0000000..3a2af80
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2015, 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
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * 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.
+ *
+ * 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 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 research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * Tests gmx::CommandLineHelpModule through gmx::CommandLineModuleManager.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \ingroup module_commandline
+ */
+#include "gmxpre.h"
+
+#include <cstdio>
+
+#include <gmock/gmock.h>
+
+#include "gromacs/commandline/cmdlinemodulemanager.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/testasserts.h"
+
+#include "cmdlinemodulemanagertest.h"
+
+namespace
+{
+
+using gmx::test::CommandLine;
+using gmx::test::MockHelpTopic;
+using gmx::test::MockModule;
+using gmx::test::MockOptionsModule;
+
+//! Test fixture for the tests.
+typedef gmx::test::CommandLineModuleManagerTestBase CommandLineHelpModuleTest;
+
+TEST_F(CommandLineHelpModuleTest, PrintsGeneralHelp)
+{
+    const char *const cmdline[] = {
+        "test"
+    };
+    CommandLine       args(cmdline);
+    initManager(args, "test");
+    redirectManagerOutput();
+    addModule("module", "First module");
+    addModule("other", "Second module");
+    addHelpTopic("topic", "Test topic");
+    int rc = 0;
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_EQ(0, rc);
+    checkRedirectedOutputFiles();
+}
+
+TEST_F(CommandLineHelpModuleTest, PrintsHelpOnTopic)
+{
+    const char *const cmdline[] = {
+        "test", "help", "topic"
+    };
+    CommandLine       args(cmdline);
+    initManager(args, "test");
+    redirectManagerOutput();
+    addModule("module", "First module");
+    MockHelpTopic &topic = addHelpTopic("topic", "Test topic");
+    topic.addSubTopic("sub1", "Subtopic 1", "");
+    topic.addSubTopic("sub2", "Subtopic 2", "");
+    using ::testing::_;
+    EXPECT_CALL(topic, writeHelp(_));
+    int rc = 0;
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_EQ(0, rc);
+    checkRedirectedOutputFiles();
+}
+
+/*! \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(CommandLineHelpModuleTest, 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
index 6ff23d78ece228d7bc6da9adb16d54788189eeea..b32791630b2cf1dfd94477be03b2010019c5c4a3 100644 (file)
 
 #include "gromacs/commandline/cmdlinemodulemanager.h"
 
-#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"
+
+#include "cmdlinemodulemanagertest.h"
 
 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
- */
-
-/*! \internal \brief
- * Mock implementation of gmx::CommandLineModuleInterface.
- *
- * \ingroup module_commandline
- */
-class MockModule : public gmx::CommandLineModuleInterface
-{
-    public:
-        //! Creates a mock module with the given name and description.
-        MockModule(const char *name, const char *description);
-
-        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));
-
-        //! Sets the expected display name for writeHelp() calls.
-        void setExpectedDisplayName(const char *expected)
-        {
-            expectedDisplayName_ = expected;
-        }
-
-    private:
-        //! Checks the context passed to writeHelp().
-        void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
+using gmx::test::MockModule;
 
-        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;
-    ON_CALL(*this, init(_))
-        .WillByDefault(Invoke(&disableNice));
-    ON_CALL(*this, writeHelp(_))
-        .WillByDefault(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);
-}
-
-/********************************************************************
- * 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 gmx::test::StringTestBase
-{
-    public:
-        void initManager(const CommandLine &args, const char *realBinaryName);
-        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 redirectManagerOutput()
-        {
-            manager_->setOutputRedirector(&initOutputRedirector(&fileManager_));
-        }
-
-    private:
-        boost::scoped_ptr<gmx::CommandLineProgramContext> programContext_;
-        boost::scoped_ptr<gmx::CommandLineModuleManager>  manager_;
-        gmx::test::TestFileManager                        fileManager_;
-};
-
-void CommandLineModuleManagerTest::initManager(
-        const CommandLine &args, const char *realBinaryName)
-{
-    manager_.reset();
-    programContext_.reset(
-            new gmx::CommandLineProgramContext(args.argc(), args.argv()));
-    manager_.reset(new gmx::CommandLineModuleManager(realBinaryName,
-                                                     programContext_.get()));
-    manager_->setQuiet(true);
-}
-
-MockModule &
-CommandLineModuleManagerTest::addModule(const char *name, const char *description)
-{
-    MockModule *module = new MockModule(name, description);
-    manager().addModule(gmx::CommandLineModulePointer(module));
-    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)
-{
-    MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
-    manager().addHelpTopic(gmx::HelpTopicPointer(topic));
-    return *topic;
-}
-
-/********************************************************************
- * Actual tests
- */
-
-TEST_F(CommandLineModuleManagerTest, RunsGeneralHelp)
-{
-    const char *const cmdline[] = {
-        "test"
-    };
-    CommandLine       args(cmdline);
-    initManager(args, "test");
-    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 fixture for the tests.
+typedef gmx::test::CommandLineModuleManagerTestBase CommandLineModuleManagerTest;
 
 TEST_F(CommandLineModuleManagerTest, RunsModule)
 {
@@ -316,22 +130,6 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
     ASSERT_EQ(0, rc);
 }
 
-TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
-{
-    const char *const cmdline[] = {
-        "test", "help", "topic"
-    };
-    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_GMX(rc = manager().run(args.argc(), args.argv()));
-    ASSERT_EQ(0, rc);
-}
-
 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
 {
     const char *const cmdline[] = {
@@ -352,50 +150,4 @@ 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
diff --git a/src/gromacs/commandline/tests/cmdlinemodulemanagertest.cpp b/src/gromacs/commandline/tests/cmdlinemodulemanagertest.cpp
new file mode 100644 (file)
index 0000000..7281697
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2012,2013,2014,2015, 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
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * 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.
+ *
+ * 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 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 research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * Implements classes from cmdlinemodulemanagertest.h.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \ingroup module_commandline
+ */
+#include "gmxpre.h"
+
+#include "cmdlinemodulemanagertest.h"
+
+#include <string>
+
+#include <boost/scoped_ptr.hpp>
+#include <gmock/gmock.h>
+
+#include "gromacs/commandline/cmdlinehelpcontext.h"
+#include "gromacs/commandline/cmdlinemodule.h"
+#include "gromacs/commandline/cmdlinemodulemanager.h"
+#include "gromacs/commandline/cmdlineprogramcontext.h"
+#include "gromacs/utility/stringutil.h"
+
+#include "gromacs/onlinehelp/tests/mock_helptopic.h"
+#include "testutils/cmdlinetest.h"
+#include "testutils/testfilemanager.h"
+
+namespace gmx
+{
+namespace test
+{
+
+namespace
+{
+
+/*! \brief
+ * Helper method to disable nice() calls from CommandLineModuleManager.
+ *
+ * \ingroup module_commandline
+ */
+void disableNice(gmx::CommandLineModuleSettings *settings)
+{
+    settings->setDefaultNiceLevel(0);
+}
+
+}       // namespace
+
+/********************************************************************
+ * MockModule
+ */
+
+MockModule::MockModule(const char *name, const char *description)
+    : name_(name), descr_(description)
+{
+    using ::testing::_;
+    using ::testing::Invoke;
+    ON_CALL(*this, init(_))
+        .WillByDefault(Invoke(&disableNice));
+    ON_CALL(*this, writeHelp(_))
+        .WillByDefault(Invoke(this, &MockModule::checkHelpContext));
+}
+
+MockModule::~MockModule()
+{
+}
+
+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);
+}
+
+/********************************************************************
+ * MockOptionsModule
+ */
+
+MockOptionsModule::MockOptionsModule()
+{
+    using ::testing::_;
+    using ::testing::Invoke;
+    ON_CALL(*this, init(_))
+        .WillByDefault(Invoke(&disableNice));
+}
+
+MockOptionsModule::~MockOptionsModule()
+{
+}
+
+/********************************************************************
+ * Test fixture for the tests
+ */
+
+class CommandLineModuleManagerTestBase::Impl
+{
+    public:
+        boost::scoped_ptr<CommandLineProgramContext> programContext_;
+        boost::scoped_ptr<CommandLineModuleManager>  manager_;
+        TestFileManager                              fileManager_;
+};
+
+CommandLineModuleManagerTestBase::CommandLineModuleManagerTestBase()
+    : impl_(new Impl)
+{
+}
+
+CommandLineModuleManagerTestBase::~CommandLineModuleManagerTestBase()
+{
+}
+
+void CommandLineModuleManagerTestBase::initManager(
+        const CommandLine &args, const char *realBinaryName)
+{
+    impl_->manager_.reset();
+    impl_->programContext_.reset(
+            new gmx::CommandLineProgramContext(args.argc(), args.argv()));
+    impl_->manager_.reset(new gmx::CommandLineModuleManager(
+                                  realBinaryName, impl_->programContext_.get()));
+    impl_->manager_->setQuiet(true);
+}
+
+MockModule &
+CommandLineModuleManagerTestBase::addModule(const char *name, const char *description)
+{
+    MockModule *module = new MockModule(name, description);
+    manager().addModule(gmx::CommandLineModulePointer(module));
+    return *module;
+}
+
+MockOptionsModule &
+CommandLineModuleManagerTestBase::addOptionsModule(const char *name, const char *description)
+{
+    MockOptionsModule *module = new MockOptionsModule();
+    gmx::CommandLineOptionsModuleInterface::registerModule(
+            &manager(), name, description, module);
+    return *module;
+}
+
+MockHelpTopic &
+CommandLineModuleManagerTestBase::addHelpTopic(const char *name, const char *title)
+{
+    MockHelpTopic *topic = new MockHelpTopic(name, title, "Help text");
+    using ::testing::_;
+    using ::testing::Invoke;
+    ON_CALL(*topic, writeHelp(_))
+        .WillByDefault(Invoke(topic, &MockHelpTopic::writeHelpBase));
+    manager().addHelpTopic(gmx::HelpTopicPointer(topic));
+    return *topic;
+}
+
+CommandLineModuleManager &CommandLineModuleManagerTestBase::manager()
+{
+    return *impl_->manager_;
+}
+
+void CommandLineModuleManagerTestBase::redirectManagerOutput()
+{
+    impl_->manager_->setOutputRedirector(&initOutputRedirector(&impl_->fileManager_));
+}
+
+} // namespace test
+} // namespace gmx
diff --git a/src/gromacs/commandline/tests/cmdlinemodulemanagertest.h b/src/gromacs/commandline/tests/cmdlinemodulemanagertest.h
new file mode 100644 (file)
index 0000000..c156561
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2012,2013,2014,2015, 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
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * 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.
+ *
+ * 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 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 research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * Test fixture and helper classes for tests using gmx::CommandLineModuleManager.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \ingroup module_commandline
+ */
+#ifndef GMX_COMMANDLINE_CMDLINEMODULEMANAGERTEST_H
+#define GMX_COMMANDLINE_CMDLINEMODULEMANAGERTEST_H
+
+#include <string>
+
+#include <gmock/gmock.h>
+
+#include "gromacs/commandline/cmdlinehelpcontext.h"
+#include "gromacs/commandline/cmdlinemodule.h"
+#include "gromacs/commandline/cmdlineoptionsmodule.h"
+#include "gromacs/utility/classhelpers.h"
+
+#include "testutils/stringtest.h"
+
+namespace gmx
+{
+namespace test
+{
+
+class CommandLine;
+class MockHelpTopic;
+
+/*! \internal \brief
+ * Mock implementation of gmx::CommandLineModuleInterface.
+ *
+ * \ingroup module_commandline
+ */
+class MockModule : public gmx::CommandLineModuleInterface
+{
+    public:
+        //! Creates a mock module with the given name and description.
+        MockModule(const char *name, const char *description);
+        ~MockModule();
+
+        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));
+
+        //! Sets the expected display name for writeHelp() calls.
+        void setExpectedDisplayName(const char *expected)
+        {
+            expectedDisplayName_ = expected;
+        }
+
+    private:
+        //! Checks the context passed to writeHelp().
+        void checkHelpContext(const gmx::CommandLineHelpContext &context) const;
+
+        const char             *name_;
+        const char             *descr_;
+        std::string             expectedDisplayName_;
+};
+
+/*! \internal \brief
+ * Mock implementation of gmx::CommandLineOptionsModuleInterface.
+ *
+ * \ingroup module_commandline
+ */
+class MockOptionsModule : public gmx::CommandLineOptionsModuleInterface
+{
+    public:
+        MockOptionsModule();
+        ~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());
+};
+
+/*! \internal \brief
+ * Test fixture for tests using gmx::CommandLineModuleManager.
+ *
+ * \ingroup module_commandline
+ */
+class CommandLineModuleManagerTestBase : public gmx::test::StringTestBase
+{
+    public:
+        CommandLineModuleManagerTestBase();
+        ~CommandLineModuleManagerTestBase();
+
+        //! Creates the manager to run the given command line.
+        void initManager(const CommandLine &args, const char *realBinaryName);
+        //! Adds a mock module to the manager.
+        MockModule               &addModule(const char *name, const char *description);
+        //! Adds a mock module using gmx::Options to the manager.
+        MockOptionsModule        &addOptionsModule(const char *name, const char *description);
+        //! Adds a mock help topic to the manager.
+        MockHelpTopic            &addHelpTopic(const char *name, const char *title);
+
+        /*! \brief
+         * Returns the manager for this test.
+         *
+         * initManager() must have been called.
+         */
+        CommandLineModuleManager &manager();
+
+        /*! \brief
+         * Redirects all manager output to files.
+         *
+         * Can be used to silence tests that would otherwise print out
+         * something, and/or checkRedirectedFileContents() from the base class
+         * can be used to check the output.
+         *
+         * The manager is put into quiet mode by default, so the manager will
+         * only print out information if, e.g., help is explicitly requested.
+         */
+        void redirectManagerOutput();
+
+    private:
+        class Impl;
+
+        PrivateImplPointer<Impl> impl_;
+};
+
+} // namespace test
+} // namespace gmx
+
+#endif
similarity index 97%
rename from src/gromacs/commandline/tests/refdata/CommandLineModuleManagerTest_RunsGeneralHelp.xml
rename to src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_PrintsGeneralHelp.xml
index 54c0bfcc9f605c44b56d026320455e2546cf9512..4c8cf2a720dabc2551ee68181a9d9a11170145c4 100644 (file)
@@ -20,6 +20,7 @@ Other options:
 
 Additional help is available on the following topics:
     commands  List of available commands
+    topic     Test topic
 To access the help, use 'commandline-test help <topic>'.
 For help on a command, use 'commandline-test help <command>'.
 ]]></String>
diff --git a/src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_PrintsHelpOnTopic.xml b/src/gromacs/commandline/tests/refdata/CommandLineHelpModuleTest_PrintsHelpOnTopic.xml
new file mode 100644 (file)
index 0000000..2acb786
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <String Name="&lt;stdout&gt;"><![CDATA[
+TEST TOPIC
+
+Help text
+
+Available subtopics:
+    sub1  Subtopic 1
+    sub2  Subtopic 2
+]]></String>
+</ReferenceData>
index f0b0b61d7b70dafe934ad13c02483e136ec43239..649ff921c3e07dbaa531e432f48a314c20eab16c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2014, by the GROMACS development team, led by
+ * Copyright (c) 2012,2014,2015, 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.
@@ -72,6 +72,18 @@ class MockHelpTopic : public AbstractCompositeHelpTopic
                                    const char *text);
         using AbstractCompositeHelpTopic::addSubTopic;
 
+        /*! \brief
+         * Calls base class writeHelp() method.
+         *
+         * This provides the possibility for the mock to do the actual help
+         * writing.  The caller must set this as the writeHelp() action, as
+         * most tests do not want to get the output.
+         */
+        void writeHelpBase(const HelpWriterContext &context)
+        {
+            AbstractCompositeHelpTopic::writeHelp(context);
+        }
+
     private:
         virtual std::string helpText() const;