/*
* 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.
*/
#include "gmxpre.h"
-#include "messagestringcollector.h"
+#include "message_string_collector.h"
#include <vector>
}
}
+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");
--- /dev/null
+/*
+ * 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