Add shared test fixture for string tests.
authorTeemu Murtola <teemu.murtola@gmail.com>
Thu, 3 May 2012 04:19:38 +0000 (07:19 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Thu, 3 May 2012 04:32:50 +0000 (07:32 +0300)
There were already two sets of tests where the same functionality was
duplicated.  This makes it easier to add more of the same without
duplicating the code (and potentially improving all the tests in one
go in the future).

Change-Id: I81fdfde264cc9243a00c0dec0d24abcf9bbce1f1

src/gromacs/commandline/tests/cmdlinehelpwriter.cpp
src/gromacs/utility/tests/format.cpp
src/testutils/CMakeLists.txt
src/testutils/stringtest.cpp [new file with mode: 0644]
src/testutils/stringtest.h [new file with mode: 0644]

index f9317015e4958d032fb1a13844fd2e5333a5c20d..17a48d9452bfe5e88199d5ad8ec56b728ae78006 100644 (file)
@@ -41,7 +41,6 @@
  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
  * \ingroup module_commandline
  */
-#include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 
 #include "gromacs/legacyheaders/types/simple.h"
 #include "gromacs/selection/selectioncollection.h"
 #include "gromacs/utility/file.h"
 
-#include "testutils/refdata.h"
-#include "testutils/testoptions.h"
+#include "testutils/stringtest.h"
 
 namespace
 {
 
-using gmx::test::TestReferenceData;
-using gmx::test::TestReferenceChecker;
-
-class CommandLineHelpWriterTest : public ::testing::Test
+class CommandLineHelpWriterTest : public ::gmx::test::StringTestBase
 {
     public:
-        static void SetUpTestCase();
-
-        static bool                     s_bWriteToStdOut;
-
         CommandLineHelpWriterTest();
         ~CommandLineHelpWriterTest();
 
-        TestReferenceChecker &checker();
-
         void checkHelp(gmx::CommandLineHelpWriter *writer);
 
         std::string                     helpfile_;
-
-    private:
-        TestReferenceData       data_;
-        boost::scoped_ptr<TestReferenceChecker> checker_;
 };
 
-bool CommandLineHelpWriterTest::s_bWriteToStdOut = false;
-
-void CommandLineHelpWriterTest::SetUpTestCase()
-{
-    gmx::Options options(NULL, NULL);
-    options.addOption(gmx::BooleanOption("stdout").store(&s_bWriteToStdOut));
-    gmx::test::parseTestOptions(&options);
-}
-
 CommandLineHelpWriterTest::CommandLineHelpWriterTest()
 {
     const ::testing::TestInfo *test_info =
@@ -109,30 +85,13 @@ CommandLineHelpWriterTest::~CommandLineHelpWriterTest()
     std::remove(helpfile_.c_str());
 }
 
-TestReferenceChecker &CommandLineHelpWriterTest::checker()
-{
-    if (checker_.get() == NULL)
-    {
-        checker_.reset(new TestReferenceChecker(data_.rootChecker()));
-    }
-    return *checker_;
-}
-
 void CommandLineHelpWriterTest::checkHelp(gmx::CommandLineHelpWriter *writer)
 {
     gmx::File file(helpfile_, "w");
     writer->writeHelp(file.handle());
     file.close();
 
-    std::string text = gmx::File::readToString(helpfile_);
-    if (s_bWriteToStdOut)
-    {
-        printf("%s", text.c_str());
-    }
-    else
-    {
-        checker().checkStringBlock(text, "HelpText");
-    }
+    checkFileContents(helpfile_, "HelpText");
 }
 
 
index 70e1e377dbdf61421777ea6dc9ba543f06a866ed..424d215e3b8056257f339ba574ed0589ed49d048 100644 (file)
 #include <string>
 #include <vector>
 
-#include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 
-#include "gromacs/options/basicoptions.h"
-#include "gromacs/options/options.h"
 #include "gromacs/utility/format.h"
 
 #include "testutils/refdata.h"
-#include "testutils/testoptions.h"
+#include "testutils/stringtest.h"
 
 namespace
 {
 
-using gmx::test::TestReferenceData;
-using gmx::test::TestReferenceChecker;
-
-class FormatTestBase : public ::testing::Test
-{
-    public:
-        static void SetUpTestCase();
-
-        static bool             s_bWriteToStdOut;
-
-        TestReferenceChecker &checker();
-
-        void checkFormatting(const std::string &text, const char *id);
-
-    private:
-        TestReferenceData       data_;
-        boost::scoped_ptr<TestReferenceChecker> checker_;
-};
-
-bool FormatTestBase::s_bWriteToStdOut = false;
-
-void FormatTestBase::SetUpTestCase()
-{
-    gmx::Options options(NULL, NULL);
-    options.addOption(gmx::BooleanOption("stdout").store(&s_bWriteToStdOut));
-    gmx::test::parseTestOptions(&options);
-}
-
-TestReferenceChecker &FormatTestBase::checker()
-{
-    if (checker_.get() == NULL)
-    {
-        checker_.reset(new TestReferenceChecker(data_.rootChecker()));
-    }
-    return *checker_;
-}
-
-void FormatTestBase::checkFormatting(const std::string &text, const char *id)
-{
-    if (s_bWriteToStdOut)
-    {
-        printf("%s:\n", id);
-        printf("%s\n", text.c_str());
-    }
-    else
-    {
-        checker().checkStringBlock(text, id);
-    }
-}
-
 /********************************************************************
  * Tests for formatString()
  */
@@ -125,7 +72,7 @@ TEST(FormatStringTest, HandlesLongStrings)
  * Tests for concatenateStrings()
  */
 
-typedef FormatTestBase ConcatenateStringsTest;
+typedef gmx::test::StringTestBase ConcatenateStringsTest;
 
 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
 {
@@ -136,7 +83,7 @@ TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
         "Fourth string",
         ""
     };
-    checkFormatting(gmx::concatenateStrings(strings), "CombinedStrings");
+    checkText(gmx::concatenateStrings(strings), "CombinedStrings");
 }
 
 /********************************************************************
@@ -149,7 +96,7 @@ const char g_wrapTextLongWord[]
     = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
 const char g_wrapTextWhitespace[] = " A quick brown   fox jumps  \n over the lazy dog";
 
-typedef FormatTestBase TextLineWrapperTest;
+typedef gmx::test::StringTestBase TextLineWrapperTest;
 
 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
 {
@@ -204,25 +151,25 @@ TEST_F(TextLineWrapperTest, WrapsCorrectly)
     gmx::TextLineWrapper wrapper;
 
     wrapper.setLineLength(10);
-    checkFormatting(wrapper.wrapToString(g_wrapText), "WrappedAt10");
+    checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
     std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
     checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
     wrapper.setLineLength(13);
-    checkFormatting(wrapper.wrapToString(g_wrapText), "WrappedAt13");
+    checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
     wrapper.setLineLength(14);
-    checkFormatting(wrapper.wrapToString(g_wrapText), "WrappedAt14");
-    checkFormatting(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
+    checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
+    checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
 }
 
 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
 {
     gmx::TextLineWrapper wrapper;
 
-    checkFormatting(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
     wrapper.setLineLength(10);
-    checkFormatting(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
     wrapper.setLineLength(14);
-    checkFormatting(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
 }
 
 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
@@ -230,14 +177,14 @@ TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
     gmx::TextLineWrapper wrapper;
 
     wrapper.setLineLength(14);
-    checkFormatting(wrapper.wrapToString(g_wrapTextWhitespace), "WrappedAt14");
+    checkText(wrapper.wrapToString(g_wrapTextWhitespace), "WrappedAt14");
 }
 
 /********************************************************************
  * Tests for TextTableFormatter
  */
 
-class TextTableFormatterTest : public FormatTestBase
+class TextTableFormatterTest : public gmx::test::StringTestBase
 {
     public:
         TextTableFormatterTest();
@@ -260,7 +207,7 @@ TEST_F(TextTableFormatterTest, HandlesBasicCase)
     formatter_.addColumnLine(1, "bar");
     formatter_.addColumnLine(2, g_wrapText);
     formatter_.addColumnLine(3, g_wrapText2);
-    checkFormatting(formatter_.formatRow(), "FormattedTable");
+    checkText(formatter_.formatRow(), "FormattedTable");
 }
 
 TEST_F(TextTableFormatterTest, HandlesOverflowingLines)
@@ -270,14 +217,14 @@ TEST_F(TextTableFormatterTest, HandlesOverflowingLines)
     formatter_.addColumnLine(1, "barfoo");
     formatter_.addColumnLine(2, g_wrapText);
     formatter_.addColumnLine(3, g_wrapText2);
-    checkFormatting(formatter_.formatRow(), "FormattedTable");
+    checkText(formatter_.formatRow(), "FormattedTable");
     formatter_.clear();
     formatter_.addColumnLine(0, "foobar");
     formatter_.addColumnLine(1, "barfoo");
     formatter_.setColumnFirstLineOffset(1, 1);
     formatter_.addColumnLine(2, g_wrapText);
     formatter_.addColumnLine(3, g_wrapText2);
-    checkFormatting(formatter_.formatRow(), "FormattedRow2");
+    checkText(formatter_.formatRow(), "FormattedRow2");
     formatter_.clear();
     formatter_.addColumnLine(0, "foobar");
     formatter_.addColumnLine(1, "barfoo");
@@ -285,7 +232,7 @@ TEST_F(TextTableFormatterTest, HandlesOverflowingLines)
     formatter_.addColumnLine(2, g_wrapText);
     formatter_.setColumnFirstLineOffset(2, 2);
     formatter_.addColumnLine(3, g_wrapText2);
-    checkFormatting(formatter_.formatRow(), "FormattedRow3");
+    checkText(formatter_.formatRow(), "FormattedRow3");
 }
 
 TEST_F(TextTableFormatterTest, HandlesEmptyColumns)
@@ -294,20 +241,20 @@ TEST_F(TextTableFormatterTest, HandlesEmptyColumns)
     formatter_.addColumnLine(0, "foo");
     formatter_.addColumnLine(1, "bar");
     formatter_.addColumnLine(3, "Text");
-    checkFormatting(formatter_.formatRow(), "FormattedTable");
+    checkText(formatter_.formatRow(), "FormattedTable");
     formatter_.clear();
     formatter_.addColumnLine(0, "foo");
     formatter_.addColumnLine(1, "bar");
     formatter_.setColumnFirstLineOffset(2, 1);
     formatter_.addColumnLine(3, "Text");
-    checkFormatting(formatter_.formatRow(), "FormattedRow2");
+    checkText(formatter_.formatRow(), "FormattedRow2");
     formatter_.clear();
     formatter_.addColumnLine(0, "foo");
     formatter_.addColumnLine(1, "bar");
     formatter_.addColumnLine(2, "");
     formatter_.setColumnFirstLineOffset(2, 1);
     formatter_.addColumnLine(3, "Text");
-    checkFormatting(formatter_.formatRow(), "FormattedRow3");
+    checkText(formatter_.formatRow(), "FormattedRow3");
 }
 
 } // namespace
index bb234a0d34161b759512e5f2000c50d8e27fb20e..54abcc2a04f7987e7398527b89471f4d6b492595 100644 (file)
@@ -2,7 +2,7 @@ set(TESTUTILS_HAVE_REFDATA FALSE)
 set(COMMON_SOURCES datapath.cpp refdata-common.cpp testoptions.cpp)
 if (GMX_USE_GTEST AND LIBXML2_FOUND)
     include_directories(${GTEST_INCLUDE_DIRS})
-    list(APPEND COMMON_SOURCES refdata.cpp)
+    list(APPEND COMMON_SOURCES refdata.cpp stringtest.cpp)
     set(TESTUTILS_HAVE_REFDATA TRUE)
     add_definitions(-DTESTUTILS_HAVE_REFDATA)
 endif ()
diff --git a/src/testutils/stringtest.cpp b/src/testutils/stringtest.cpp
new file mode 100644 (file)
index 0000000..695deda
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ *
+ *                This source code is part of
+ *
+ *                 G   R   O   M   A   C   S
+ *
+ *          GROningen MAchine for Chemical Simulations
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * 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
+ */
+/*! \internal \file
+ * \brief
+ * Implements gmx::test::StringTestBase.
+ *
+ * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \ingroup module_testutils
+ */
+#include "testutils/stringtest.h"
+
+#include "gromacs/options/basicoptions.h"
+#include "gromacs/options/options.h"
+#include "gromacs/utility/file.h"
+#include "testutils/testoptions.h"
+
+namespace gmx
+{
+namespace test
+{
+
+bool StringTestBase::s_bWriteToStdOut = false;
+
+void StringTestBase::SetUpTestCase()
+{
+    Options options(NULL, NULL);
+    options.addOption(BooleanOption("stdout").store(&s_bWriteToStdOut));
+    parseTestOptions(&options);
+}
+
+StringTestBase::StringTestBase()
+{
+}
+
+StringTestBase::~StringTestBase()
+{
+}
+
+TestReferenceChecker &
+StringTestBase::checker()
+{
+    if (checker_.get() == NULL)
+    {
+        checker_.reset(new TestReferenceChecker(data_.rootChecker()));
+    }
+    return *checker_;
+}
+
+void
+StringTestBase::checkText(const std::string &text, const char *id)
+{
+    if (s_bWriteToStdOut)
+    {
+        printf("%s:\n", id);
+        printf("%s[END]\n", text.c_str());
+    }
+    else
+    {
+        checker().checkStringBlock(text, id);
+    }
+}
+
+void
+StringTestBase::checkFileContents(const std::string &filename, const char *id)
+{
+    std::string text = File::readToString(filename);
+    checkText(text, id);
+}
+
+} // namespace test
+} // namespace gmx
diff --git a/src/testutils/stringtest.h b/src/testutils/stringtest.h
new file mode 100644 (file)
index 0000000..a85d107
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ *
+ *                This source code is part of
+ *
+ *                 G   R   O   M   A   C   S
+ *
+ *          GROningen MAchine for Chemical Simulations
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * 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
+ */
+/*! \libinternal \file
+ * \brief
+ * Declares gmx::test::StringTestBase.
+ *
+ * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+#ifndef GMX_TESTUTILS_STRINGTEST_H
+#define GMX_TESTUTILS_STRINGTEST_H
+
+#include <string>
+
+#include <boost/scoped_ptr.hpp>
+#include <gtest/gtest.h>
+
+#include "refdata.h"
+
+namespace gmx
+{
+namespace test
+{
+
+/*! \libinternal \brief
+ * Test fixture for tests that check string formatting.
+ *
+ * For development, tests that use this fixture as their base can be run with a
+ * '-stdout' command-line option to print out the tested strings to stdout.
+ * If this flag is not given, they check the strings using the XML reference
+ * framework (see TestReferenceData).
+ *
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+class StringTestBase : public ::testing::Test
+{
+    public:
+        //! Static fixture setup to parse command-line options.
+        static void SetUpTestCase();
+
+        StringTestBase();
+        ~StringTestBase();
+
+        /*! \brief
+         * Returns the root checker for this test's reference data.
+         *
+         * Can be used to perform custom checks against reference data (e.g.,
+         * if the test needs to check some other values than plain strings.
+         */
+        TestReferenceChecker &checker();
+
+        /*! \brief
+         * Check a string.
+         *
+         * \param[in] text  String to check.
+         * \param[in] id    Unique (within a single test) id for the string.
+         */
+        void checkText(const std::string &text, const char *id);
+        /*! \brief
+         * Check contents of a file as a single string.
+         *
+         * \param[in] filename  Name of the file to check.
+         * \param[in] id        Unique (within a single test) id for the string.
+         *
+         * Provided for convenience.  Reads the contents of \p filename into a
+         * single string and calls checkText().
+         */
+        void checkFileContents(const std::string &filename, const char *id);
+
+    private:
+        static bool             s_bWriteToStdOut;
+
+        TestReferenceData       data_;
+        boost::scoped_ptr<TestReferenceChecker> checker_;
+};
+
+} // namespace test
+} // namespace gmx
+
+#endif