Improve testutils infrastructure
authorMark Abraham <mark.j.abraham@gmail.com>
Tue, 10 Aug 2021 19:52:49 +0000 (19:52 +0000)
committerJoe Jordan <ejjordan12@gmail.com>
Tue, 10 Aug 2021 19:52:49 +0000 (19:52 +0000)
src/testutils/cmdlinetest.cpp
src/testutils/include/testutils/cmdlinetest.h
src/testutils/include/testutils/testfilemanager.h
src/testutils/testfilemanager.cpp
src/testutils/xvgtest.cpp

index bbea96a5a8a3659ae01c6a5a094290e79ff10614..786245e7ddc1399c08b3f90f9550b766565a979f 100644 (file)
@@ -374,6 +374,31 @@ void CommandLineTestHelper::setOutputFile(CommandLine*                args,
     impl_->outputFiles_.emplace_back(option, fullFilename, matcher.createFileMatcher());
 }
 
+void CommandLineTestHelper::setOutputFileWithGeneratedName(const char* filename,
+                                                           const ITextBlockMatcherSettings& matcher)
+{
+    setOutputFileWithGeneratedName(std::string(filename), TextFileMatch(matcher));
+}
+
+void CommandLineTestHelper::setOutputFileWithGeneratedName(std::string&& filename,
+                                                           const ITextBlockMatcherSettings& matcher)
+{
+    setOutputFileWithGeneratedName(std::move(filename), TextFileMatch(matcher));
+}
+
+void CommandLineTestHelper::setOutputFileWithGeneratedName(const char*                 filename,
+                                                           const IFileMatcherSettings& matcher)
+{
+    setOutputFileWithGeneratedName(std::string(filename), matcher);
+}
+
+void CommandLineTestHelper::setOutputFileWithGeneratedName(std::string&&               filename,
+                                                           const IFileMatcherSettings& matcher)
+{
+    impl_->outputFiles_.emplace_back(filename.c_str(), filename, matcher.createFileMatcher());
+    impl_->fileManager_.manageGeneratedOutputFile(std::move(filename));
+}
+
 void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const
 {
     if (!impl_->outputFiles_.empty())
@@ -461,6 +486,30 @@ void CommandLineTestBase::setOutputFile(const char*                 option,
     impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
 }
 
+void CommandLineTestBase::setOutputFileWithGeneratedName(const char*                      filename,
+                                                         const ITextBlockMatcherSettings& matcher)
+{
+    impl_->helper_.setOutputFileWithGeneratedName(std::string(filename), matcher);
+}
+
+void CommandLineTestBase::setOutputFileWithGeneratedName(std::string&&                    filename,
+                                                         const ITextBlockMatcherSettings& matcher)
+{
+    impl_->helper_.setOutputFileWithGeneratedName(std::move(filename), matcher);
+}
+
+void CommandLineTestBase::setOutputFileWithGeneratedName(const char*                 filename,
+                                                         const IFileMatcherSettings& matcher)
+{
+    impl_->helper_.setOutputFileWithGeneratedName(std::string(filename), matcher);
+}
+
+void CommandLineTestBase::setOutputFileWithGeneratedName(std::string&&               filename,
+                                                         const IFileMatcherSettings& matcher)
+{
+    impl_->helper_.setOutputFileWithGeneratedName(std::move(filename), matcher);
+}
+
 void CommandLineTestBase::setInputAndOutputFile(const char*                      option,
                                                 const char*                      filename,
                                                 const ITextBlockMatcherSettings& matcher)
index 3dfcd4eac38aa09272be0b050d5cac06482193aa..b8be83134adbac1c7523df003aed71696cb4520e 100644 (file)
@@ -333,6 +333,18 @@ public:
                        const char*                 option,
                        const char*                 filename,
                        const IFileMatcherSettings& matcher);
+    /*! \brief As for \c setOutputFile() but does not create an option
+     *
+     * This method is useful when a tool generates a series of output
+     * files by modifying a common base name. Call this method with
+     * every file name that is expected to be generated by the tool. */
+    void setOutputFileWithGeneratedName(const char* filename, const ITextBlockMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(std::string&& filename, const ITextBlockMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(const char* filename, const IFileMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(std::string&& filename, const IFileMatcherSettings& matcher);
 
     /*! \brief
      * Checks output files added with setOutputFile() against reference
@@ -415,17 +427,28 @@ public:
                               const char*                        extension,
                               const ArrayRef<const char* const>& contents);
     /*! \brief
-     * Sets an output file parameter and adds it to the set of tested files.
+     * Sets an output file whose name is passed via an option and adds it to the set of tested files.
      *
      * \see CommandLineTestHelper::setOutputFile()
      */
     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()
-     */
+    //! \copydoc setOutputFile(const char *, const ITextBlockMatcherSettings &)
     void setOutputFile(const char* option, const char* filename, const IFileMatcherSettings& matcher);
+    /*! \brief
+     * Sets an output file whose name is generated by the tool and adds it to the set of tested files.
+     *
+     * This method is useful when a tool generates a series of output
+     * files by modifying a common base name. Call this method with
+     * every file name that is expected to be generated by the tool.
+     *
+     * \see CommandLineTestHelper::setOutputFileWithGeneratedName() */
+    void setOutputFileWithGeneratedName(const char* filename, const ITextBlockMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(std::string&& filename, const ITextBlockMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(const char* filename, const IFileMatcherSettings& matcher);
+    //! \copydoc setOutputFileWithGeneratedName(const char *, const ITextBlockMatcherSettings &)
+    void setOutputFileWithGeneratedName(std::string&& filename, const IFileMatcherSettings& matcher);
     /*! \brief
      * Sets a file parameter that is used for input and modified as output. The input file
      * is copied to a temporary file that is used as input and can be modified.
index d9b8e7c2b1b3aef2467de7d9536ce48011bf7db1..eeaa07fe7f8630c4aefbc589e71b47227cd82114 100644 (file)
@@ -114,6 +114,23 @@ public:
     //! \copydoc TestFileManager::getTemporaryFilePath(const char *)
     std::string getTemporaryFilePath(const std::string& suffix);
 
+    /*! \brief
+     * Manage cleaning up this output file whose name was generated
+     *
+     * \param[in] filename  Filename relative to the working directory
+     *                        (which is generally not the output directory)
+     *
+     * This method should only be called from within a Google Test
+     * test. It lets the TestFileManager know to attempt to clean up
+     * the named file. That file should normally be one whose name is
+     * generated by the tool under test. For an output file whose name
+     * is specified by e.g. an input command-line option, pass the
+     * return value of getTemporaryFilePath() to that option.
+     */
+    void manageGeneratedOutputFile(const char* filename);
+    //! \copydoc TestFileManager::manageGeneratedOutputFile(const char *)
+    void manageGeneratedOutputFile(std::string&& filename);
+
     /*! \brief Returns the path to the output temporary directory
      * for tests which use this TestFileManager object.
      *
index 99748ff77fbc39c64e18e54ffb71a357e6b16c02..29b077fa1b9abcf08309cea01c28414aa9d085de 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -173,18 +173,27 @@ std::string TestFileManager::getTemporaryFilePath(const std::string& suffix)
     return getTemporaryFilePath(suffix.c_str());
 }
 
+void TestFileManager::manageGeneratedOutputFile(const char* filename)
+{
+    manageGeneratedOutputFile(std::string(filename));
+}
+void TestFileManager::manageGeneratedOutputFile(std::string&& filename)
+{
+    impl_->files_.insert(std::move(filename));
+}
+
 std::string TestFileManager::getTestSpecificFileNameRoot()
 {
     const ::testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
     std::string                filenameRoot;
     if (test_info)
     {
-        filenameRoot = std::string(test_info->test_case_name()) + "_" + test_info->name();
+        filenameRoot = std::string(test_info->test_suite_name()) + "_" + test_info->name();
     }
     else
     {
-        const ::testing::TestCase* test_case_info =
-                ::testing::UnitTest::GetInstance()->current_test_case();
+        const ::testing::TestSuite* test_case_info =
+                ::testing::UnitTest::GetInstance()->current_test_suite();
         filenameRoot = std::string(test_case_info->name());
     }
     std::replace(filenameRoot.begin(), filenameRoot.end(), '/', '_');
index 437f046a0f6a3c53b77be597bd5a4941e9ee86dc..321b1d905e6da7aba2602c82a12c904f128b8da7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, 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.
@@ -112,6 +112,14 @@ void checkXvgFile(TextInputStream* input, TestReferenceChecker* checker, const X
         {
             continue;
         }
+        // Ignore ampersand dataset separators (for now)
+        // Later, when we need to test code that writes multiple
+        // datasets, we might want to introduce that new concept
+        // to this testing code.
+        if (startsWith(line, "&"))
+        {
+            continue;
+        }
         if (startsWith(line, "@"))
         {
             if (isRelevantXvgCommand(line))