set(TESTUTILS_SOURCES
cmdlinetest.cpp
conftest.cpp
+ filematchers.cpp
interactivetest.cpp
loggertest.cpp
mpi-printer.cpp
#include "gromacs/commandline/cmdlineoptionsmodule.h"
#include "gromacs/commandline/cmdlineprogramcontext.h"
#include "gromacs/utility/arrayref.h"
-#include "gromacs/utility/filestream.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/strconvert.h"
#include "gromacs/utility/stringstream.h"
#include "gromacs/utility/textreader.h"
#include "gromacs/utility/textwriter.h"
+#include "testutils/filematchers.h"
#include "testutils/refdata.h"
#include "testutils/testfilemanager.h"
-#include "testutils/textblockmatchers.h"
namespace gmx
{
struct OutputFileInfo
{
OutputFileInfo(const char *option, const std::string &path,
- TextBlockMatcherPointer matcher)
+ FileMatcherPointer matcher)
: option(option), path(path), matcher(move(matcher))
{
}
- OutputFileInfo(OutputFileInfo &&other)
- : option(std::move(other.option)), path(std::move(other.path)),
- matcher(std::move(other.matcher))
- {
- }
-
- OutputFileInfo &operator=(OutputFileInfo &&other)
- {
- option = std::move(other.option);
- path = std::move(other.path);
- matcher = std::move(other.matcher);
- return *this;
- }
std::string option;
std::string path;
- TextBlockMatcherPointer matcher;
+ FileMatcherPointer matcher;
};
typedef std::vector<OutputFileInfo> OutputFileList;
void CommandLineTestHelper::setOutputFile(
CommandLine *args, const char *option, const char *filename,
const ITextBlockMatcherSettings &matcher)
+{
+ setOutputFile(args, option, filename, TextFileMatch(matcher));
+}
+
+void CommandLineTestHelper::setOutputFile(
+ CommandLine *args, const char *option, const char *filename,
+ const IFileMatcherSettings &matcher)
{
std::string suffix(filename);
if (startsWith(filename, "."))
}
std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(suffix);
args->addOption(option, fullFilename);
- impl_->outputFiles_.emplace_back(option, fullFilename, matcher.createMatcher());
+ impl_->outputFiles_.emplace_back(option, fullFilename, matcher.createFileMatcher());
}
void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const
TestReferenceChecker outputChecker(
checker.checkCompound("OutputFiles", "Files"));
Impl::OutputFileList::const_iterator outfile;
- for (outfile = impl_->outputFiles_.begin();
- outfile != impl_->outputFiles_.end();
- ++outfile)
+ for (const auto &outfile : impl_->outputFiles_)
{
TestReferenceChecker fileChecker(
- outputChecker.checkCompound("File", outfile->option.c_str()));
- TextInputFile stream(outfile->path);
- outfile->matcher->checkStream(&stream, &fileChecker);
- stream.close();
+ outputChecker.checkCompound("File", outfile.option.c_str()));
+ outfile.matcher->checkFile(outfile.path, &fileChecker);
}
}
}
impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
}
+void CommandLineTestBase::setOutputFile(
+ const char *option, const char *filename,
+ const IFileMatcherSettings &matcher)
+{
+ impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
+}
+
CommandLine &CommandLineTestBase::commandLine()
{
return impl_->cmdline_;
namespace test
{
+class IFileMatcherSettings;
class ITextBlockMatcherSettings;
class TestFileManager;
class TestReferenceChecker;
*
* If the output file is needed to trigger some computation, or is
* unconditionally produced by the code under test, but the contents
- * are not interesting for the test, use NoTextMatch as the matcher.
+ * are not interesting for the test, use NoContentsMatch as the matcher.
+ * Note that the existence of the output file is still verified.
*/
void setOutputFile(CommandLine *args, const char *option,
const char *filename,
const ITextBlockMatcherSettings &matcher);
+ //! \copydoc setOutputFile(CommandLine *, const char *, const char *, const ITextBlockMatcherSettings &)
+ void setOutputFile(CommandLine *args, const char *option,
+ const char *filename,
+ const IFileMatcherSettings &matcher);
/*! \brief
* Checks output files added with setOutputFile() against reference
*/
void setOutputFile(const char *option, const char *filename,
const ITextBlockMatcherSettings &matcher);
+ /*! \brief
+ * Sets an output file parameter and adds it to the set of tested files.
+ *
+ * \see CommandLineTestHelper::setOutputFile()
+ */
+ void setOutputFile(const char *option, const char *filename,
+ const IFileMatcherSettings &matcher);
/*! \brief
* Returns the internal CommandLine object used to construct the
--- /dev/null
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2017, 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 filematchers.h.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \ingroup module_testutils
+ */
+#include "gmxpre.h"
+
+#include "filematchers.h"
+
+#include "gromacs/utility/filestream.h"
+
+#include "testutils/textblockmatchers.h"
+
+namespace gmx
+{
+namespace test
+{
+
+namespace
+{
+
+class TextFileMatcher : public IFileMatcher
+{
+ public:
+ explicit TextFileMatcher(TextBlockMatcherPointer matcher)
+ : matcher_(std::move(matcher))
+ {
+ }
+
+ virtual void checkFile(const std::string &path,
+ TestReferenceChecker *checker)
+ {
+ TextInputFile stream(path);
+ matcher_->checkStream(&stream, checker);
+ stream.close();
+ }
+
+ private:
+ TextBlockMatcherPointer matcher_;
+};
+
+} // namespace
+
+IFileMatcher::~IFileMatcher()
+{
+}
+
+IFileMatcherSettings::~IFileMatcherSettings()
+{
+}
+
+FileMatcherPointer TextFileMatch::createFileMatcher() const
+{
+ return FileMatcherPointer(new TextFileMatcher(streamSettings_.createMatcher()));
+}
+
+FileMatcherPointer NoContentsMatch::createFileMatcher() const
+{
+ return TextFileMatch(NoTextMatch()).createFileMatcher();
+}
+
+} // namespace test
+} // namespace gmx
--- /dev/null
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2017, 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.
+ */
+/*! \libinternal \file
+ * \brief
+ * Declares utility classes for testing file contents.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+#ifndef GMX_TESTUTILS_FILEMATCHERS_H
+#define GMX_TESTUTILS_FILEMATCHERS_H
+
+#include <memory>
+#include <string>
+
+namespace gmx
+{
+namespace test
+{
+
+class ITextBlockMatcherSettings;
+class TestReferenceChecker;
+
+/*! \libinternal \brief
+ * Represents a file matcher, matching file contents against reference (or
+ * other) data.
+ *
+ * Typical pattern of declaring such matchers is to
+ * - Create a factory that implements IFileMatcherSettings,
+ * - Make that factory provide any necessary parameters that the matcher needs,
+ * using a "named parameter" idiom (see XvgMatch for an example), and
+ * - Make the factory create and return an instance of an internal
+ * implementation class that implements IFileMatcher and provides
+ * the actual matching logic.
+ *
+ * Any method that then wants to accept a matcher can accept a
+ * IFileMatcherSettings.
+ *
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+class IFileMatcher
+{
+ public:
+ virtual ~IFileMatcher();
+
+ /*! \brief
+ * Matches contents of a file.
+ *
+ * \param path Path to the file to match.
+ * \param checker Checker to use for matching.
+ *
+ * The method can change the state of the provided checker (e.g., by
+ * changing the default tolerance).
+ * The caller is responsible of providing a checker where such state
+ * changes do not matter.
+ */
+ virtual void checkFile(const std::string &path,
+ TestReferenceChecker *checker) = 0;
+};
+
+//! Smart pointer for managing a IFileMatcher.
+typedef std::unique_ptr<IFileMatcher> FileMatcherPointer;
+
+/*! \libinternal \brief
+ * Represents a factory for creating a file matcher.
+ *
+ * See derived classes for available matchers. Each derived class represents
+ * one type of matcher (see IFileMatcher), and provides any methods
+ * necessary to pass parameters to such a matcher. Methods that accept a
+ * matcher can then take in this interface, and call createFileMatcher() to use
+ * the matcher that the caller of the method specifies.
+ *
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+class IFileMatcherSettings
+{
+ public:
+ //! Factory method that constructs the matcher after parameters are set.
+ virtual FileMatcherPointer createFileMatcher() const = 0;
+
+ protected:
+ virtual ~IFileMatcherSettings();
+};
+
+/*! \libinternal \brief
+ * Use a ITextStreamMatcher for matching the contents.
+ *
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+class TextFileMatch : public IFileMatcherSettings
+{
+ public:
+ //! Creates a matcher to match contents with given text matcher.
+ explicit TextFileMatch(const ITextBlockMatcherSettings &streamSettings)
+ : streamSettings_(streamSettings)
+ {
+ }
+
+ virtual FileMatcherPointer createFileMatcher() const;
+
+ private:
+ const ITextBlockMatcherSettings &streamSettings_;
+};
+
+/*! \libinternal \brief
+ * Do not check the contents of the file.
+ *
+ * \inlibraryapi
+ * \ingroup module_testutils
+ */
+class NoContentsMatch : public IFileMatcherSettings
+{
+ public:
+ virtual FileMatcherPointer createFileMatcher() const;
+};
+
+} // namespace test
+} // namespace gmx
+
+#endif