From 99644004a8461edaf7471f994e0df58a9d3cdc36 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Wed, 28 Feb 2024 21:02:29 +0100 Subject: [PATCH] Add some tests for logging #44 --- Tests/Log/FakeLogTarget.h | 40 +++++++++++++++++ Tests/Log/logError.cpp | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 Tests/Log/FakeLogTarget.h create mode 100644 Tests/Log/logError.cpp diff --git a/Tests/Log/FakeLogTarget.h b/Tests/Log/FakeLogTarget.h new file mode 100644 index 0000000..6e71ad9 --- /dev/null +++ b/Tests/Log/FakeLogTarget.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021-2024 by Geoffrey Merck F4FXL / KC3FRA + * + * 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. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include + +#include "LogTarget.h" + +class CFakeLogTarget : public CLogTarget +{ +public: + CFakeLogTarget(LOG_SEVERITY logLevel) : + CLogTarget(logLevel) + { + + } + + virtual void printLogInt(const std::string& msg) + { + m_messages.push_back(msg); + std::cout << msg; + } + + std::vector m_messages; +}; \ No newline at end of file diff --git a/Tests/Log/logError.cpp b/Tests/Log/logError.cpp new file mode 100644 index 0000000..feda3a5 --- /dev/null +++ b/Tests/Log/logError.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2021-2024 by Geoffrey Merck F4FXL / KC3FRA + * + * 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. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include + +#include "Log.h" +#include "LogSeverity.h" +#include "FakeLogTarget.h" + +using ::testing::EndsWith; + +namespace LogErrorTests +{ + class Log_logError: public ::testing::Test { + protected: + CFakeLogTarget * m_logTarget; + + void SetUp() override + { + m_logTarget = new CFakeLogTarget(LOG_ERROR); + CLog::addTarget((CLogTarget *)m_logTarget); + } + + void TearDown() override + { + CLog::finalise(); + } + }; + + TEST_F(Log_logError, OneMessage) { + CLog::logError("One Message"); + + EXPECT_EQ(1, m_logTarget->m_messages.size()) << "There should be one message in the log."; + EXPECT_THAT(m_logTarget->m_messages[0].c_str(), EndsWith("[ERROR ] One Message\n")); + } + + TEST_F(Log_logError, WrongLevel) { + CLog::logDebug("One Message"); + + EXPECT_EQ(0, m_logTarget->m_messages.size()) << "There should be no message in the log."; + } + + TEST_F(Log_logError, TwoMessage) { + CLog::logError("One Message"); + CLog::logError("Two Message"); + + EXPECT_EQ(2, m_logTarget->m_messages.size()) << "There should be exactly two message in the log."; + EXPECT_THAT(m_logTarget->m_messages[0].c_str(), EndsWith("[ERROR ] One Message\n")); + EXPECT_THAT(m_logTarget->m_messages[1].c_str(), EndsWith("[ERROR ] Two Message\n")); + } + + TEST_F(Log_logError, ThreeIdenticalMessage) { + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + + EXPECT_EQ(1, m_logTarget->m_messages.size()) << "There should be one message in the log."; + EXPECT_THAT(m_logTarget->m_messages[0].c_str(), EndsWith("[ERROR ] One Message\n")); + } + + TEST_F(Log_logError, ThreeIdenticalMessageOneDifferent) { + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("One Message"); + CLog::logError("Another Message"); + + EXPECT_EQ(3, m_logTarget->m_messages.size()) << "There should be two message in the log."; + EXPECT_THAT(m_logTarget->m_messages[0].c_str(), EndsWith("[ERROR ] One Message\n")); + EXPECT_THAT(m_logTarget->m_messages[1].c_str(), EndsWith("[ERROR ] Previous message repeated 8 times\n")); + EXPECT_THAT(m_logTarget->m_messages[2].c_str(), EndsWith("[ERROR ] Another Message\n")); + } +} \ No newline at end of file