* \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 =
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");
}
#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()
*/
* Tests for concatenateStrings()
*/
-typedef FormatTestBase ConcatenateStringsTest;
+typedef gmx::test::StringTestBase ConcatenateStringsTest;
TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
{
"Fourth string",
""
};
- checkFormatting(gmx::concatenateStrings(strings), "CombinedStrings");
+ checkText(gmx::concatenateStrings(strings), "CombinedStrings");
}
/********************************************************************
= "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)
{
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)
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();
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)
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");
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)
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
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 ()
--- /dev/null
+/*
+ *
+ * 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
--- /dev/null
+/*
+ *
+ * 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