Add appendIf(..) method and tests for MessageStringCollector
authorArtem Zhmurov <zhmurov@gmail.com>
Thu, 13 May 2021 08:06:19 +0000 (11:06 +0300)
committerArtem Zhmurov <zhmurov@gmail.com>
Thu, 13 May 2021 08:34:36 +0000 (11:34 +0300)
The messages are usually added conditionally, which makes
it more natural to use msc.appendIf(conditional, message) as
compared to if(conditional) { msc.append(message); }.

Also adds tests for basic functionality of the class and
renames the files according to conventions.

Refs #3886

src/gromacs/mdlib/updategroups.cpp
src/gromacs/selection/selectionoption.cpp
src/gromacs/utility.h
src/gromacs/utility/message_string_collector.cpp [moved from src/gromacs/utility/messagestringcollector.cpp with 90% similarity]
src/gromacs/utility/message_string_collector.h [moved from src/gromacs/utility/messagestringcollector.h with 92% similarity]
src/gromacs/utility/tests/CMakeLists.txt
src/gromacs/utility/tests/message_string_collector.cpp [new file with mode: 0644]

index 64a0a724dd5c9cc80a2f163fc0a738dd585220ae..700276ea3401eaa19e5ad914af28a64166f9a62c 100644 (file)
@@ -59,7 +59,7 @@
 #include "gromacs/topology/topology.h"
 #include "gromacs/utility/listoflists.h"
 #include "gromacs/utility/logger.h"
-#include "gromacs/utility/messagestringcollector.h"
+#include "gromacs/utility/message_string_collector.h"
 
 namespace gmx
 {
index 76612b3ea3a87f17ba87de0de1e152d71b2cff77..fb2717662f33d9b4627345aa17be9d37ad3c4f4a 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 2010-2018, The GROMACS development team.
- * Copyright (c) 2019, by the GROMACS development team, led by
+ * Copyright (c) 2019,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.
@@ -52,7 +52,7 @@
 #include "gromacs/selection/selectionoptionmanager.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
-#include "gromacs/utility/messagestringcollector.h"
+#include "gromacs/utility/message_string_collector.h"
 
 #include "selectionfileoptionstorage.h"
 #include "selectionoptionstorage.h"
index f04c1c515ef6839aedac95762b1499919e9cf681..fce177d21882bd434b82167b536556416038c781 100644 (file)
  * Similarly, gmxomp.h removes the need to use conditional compilation for code
  * that needs to include omp.h for OpenMP functions.
  *
- * The header messagestringcollector.h declares a gmx::MessageStringCollector
+ * The header message_string_collector.h declares a gmx::MessageStringCollector
  * class for composing messages with context information.
  *
  * The header sysinfo.h declares gmx_getpid() for getting the current process
similarity index 90%
rename from src/gromacs/utility/messagestringcollector.cpp
rename to src/gromacs/utility/message_string_collector.cpp
index 306838627d585e145ca5354f9fa4e52e9411925e..a956bd904889a58146b43aa480f2010ee51786c8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2011,2012,2014,2016,2019, by the GROMACS development team, led by
+ * Copyright (c) 2011,2012,2014,2016,2019,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.
@@ -41,7 +41,7 @@
  */
 #include "gmxpre.h"
 
-#include "messagestringcollector.h"
+#include "message_string_collector.h"
 
 #include <vector>
 
@@ -101,6 +101,22 @@ void MessageStringCollector::append(const std::string& message)
     }
 }
 
+void MessageStringCollector::appendIf(bool condition, const char* message)
+{
+    if (condition)
+    {
+        append(std::string(message));
+    }
+}
+
+void MessageStringCollector::appendIf(bool condition, const std::string& message)
+{
+    if (condition)
+    {
+        append(message);
+    }
+}
+
 void MessageStringCollector::finishContext()
 {
     GMX_RELEASE_ASSERT(!impl_->contexts_.empty(), "finishContext() called without context");
similarity index 92%
rename from src/gromacs/utility/messagestringcollector.h
rename to src/gromacs/utility/message_string_collector.h
index 8b1595fd4330ea6a170789ba9c015b2a5e75309d..a75851d7aaac0f282cbf53cd94fa6755fea1138b 100644 (file)
@@ -40,8 +40,8 @@
  * \inlibraryapi
  * \ingroup module_utility
  */
-#ifndef GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
-#define GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
+#ifndef GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H
+#define GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H
 
 #include <memory>
 #include <string>
@@ -87,6 +87,14 @@ public:
      * Adds a new message.
      */
     void append(const std::string& message);
+    /*! \brief
+     * Adds a new message if the condition is satisfied..
+     */
+    void appendIf(bool condition, const char* message);
+    /*! \brief
+     * Adds a new message if the condition is satisfied.
+     */
+    void appendIf(bool condition, const std::string& message);
     /*! \brief
      * Ends a context started with startContext().
      *
@@ -177,4 +185,4 @@ private:
 
 } // namespace gmx
 
-#endif
+#endif // GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H
index 7532e3c008214976783aaf3fef819efb2da2f415..142d4c19848f44455940b7bee976c16a93c01e33 100644 (file)
@@ -48,6 +48,7 @@ gmx_add_unit_test(UtilityUnitTests utility-test
         listoflists.cpp
         logger.cpp
         mdmodulesnotifier.cpp
+        message_string_collector.cpp
         path.cpp
         physicalnodecommunicator.cpp
         range.cpp
diff --git a/src/gromacs/utility/tests/message_string_collector.cpp b/src/gromacs/utility/tests/message_string_collector.cpp
new file mode 100644 (file)
index 0000000..fd7acef
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 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.
+ *
+ * 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 for MessageStringCollector.
+ *
+ * \author Artem Zhmurov <zhmurov@gmail.com>
+ * \ingroup module_utility
+ */
+#include "gmxpre.h"
+
+#include "gromacs/utility/message_string_collector.h"
+
+#include <gtest/gtest.h>
+
+namespace gmx
+{
+
+namespace
+{
+
+TEST(MessageStringCollectorTest, CanAddAndClearMessagesNoContext)
+{
+    MessageStringCollector messages;
+
+    EXPECT_TRUE(messages.isEmpty());
+
+    messages.append("Message1");
+
+    EXPECT_FALSE(messages.isEmpty());
+
+    messages.append("Message2");
+
+    EXPECT_FALSE(messages.isEmpty());
+
+    messages.clear();
+
+    EXPECT_TRUE(messages.isEmpty());
+}
+
+TEST(MessageStringCollectorTest, CanAddAndClearMessagesWithContext)
+{
+    MessageStringCollector messages;
+
+    messages.startContext("Context1");
+
+    EXPECT_TRUE(messages.isEmpty());
+
+    messages.append("Message1");
+
+    EXPECT_FALSE(messages.isEmpty());
+
+    messages.append("Message1");
+
+    EXPECT_FALSE(messages.isEmpty());
+
+    messages.finishContext();
+
+    messages.clear();
+
+    EXPECT_TRUE(messages.isEmpty());
+}
+
+TEST(MessageStringCollectorTest, CanAddStringMessages)
+{
+    std::string context1 = "Context1";
+    std::string message1 = "Message1";
+
+    MessageStringCollector messagesChar;
+    MessageStringCollector messagesString;
+
+    messagesChar.startContext(context1.c_str());
+    messagesChar.append(message1.c_str());
+    messagesChar.finishContext();
+
+    messagesString.startContext(context1);
+    messagesString.append(message1);
+    messagesString.finishContext();
+
+    EXPECT_EQ(messagesChar.toString(), messagesString.toString());
+}
+
+TEST(MessageStringCollectorTest, CanAddCharMessagesConditionally)
+{
+    std::string context1     = "Context1";
+    std::string message1     = "Message1";
+    std::string message2     = "Message2";
+    bool        conditional1 = true;
+    bool        conditional2 = false;
+
+    MessageStringCollector messagesDirect;
+    MessageStringCollector messagesConditional;
+
+    messagesDirect.startContext(context1);
+    if (conditional1)
+    {
+        messagesDirect.append(message1.c_str());
+    }
+
+    if (conditional2)
+    {
+        messagesDirect.append(message2.c_str());
+    }
+
+    messagesDirect.finishContext();
+
+    messagesConditional.startContext(context1);
+    messagesConditional.appendIf(conditional1, message1.c_str());
+    messagesConditional.appendIf(conditional2, message2.c_str());
+    messagesConditional.finishContext();
+
+    EXPECT_EQ(messagesDirect.toString(), messagesConditional.toString());
+}
+
+TEST(MessageStringCollectorTest, CanAddStringMessagesConditionally)
+{
+    std::string context1     = "Context1";
+    std::string message1     = "Message1";
+    std::string message2     = "Message2";
+    bool        conditional1 = true;
+    bool        conditional2 = false;
+
+    MessageStringCollector messagesDirect;
+    MessageStringCollector messagesConditional;
+
+    messagesDirect.startContext(context1);
+    if (conditional1)
+    {
+        messagesDirect.append(message1);
+    }
+
+    if (conditional2)
+    {
+        messagesDirect.append(message2);
+    }
+
+    messagesDirect.finishContext();
+
+    messagesConditional.startContext(context1);
+    messagesConditional.appendIf(conditional1, message1);
+    messagesConditional.appendIf(conditional2, message2);
+    messagesConditional.finishContext();
+
+    EXPECT_EQ(messagesDirect.toString(), messagesConditional.toString());
+}
+
+} // namespace
+
+} // namespace gmx