From 7ee49ba8048c0d8a50a57d8315c2c4334ad8f27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ag=C3=BCero?= Date: Wed, 21 Aug 2024 21:09:25 +0200 Subject: [PATCH] Tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carlos Agüero --- src/Console.cc | 5 +- src/Console_TEST.cc | 109 ++++++++++++++++++++++---------------------- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/Console.cc b/src/Console.cc index 04bcfbc4..22c93f74 100644 --- a/src/Console.cc +++ b/src/Console.cc @@ -142,7 +142,6 @@ void Console::SetVerbosity(const int _level) return; } - verbosity = _level; switch (_level) { case 0: @@ -166,8 +165,10 @@ void Console::SetVerbosity(const int _level) default: Console::Root().RawLogger().log(spdlog::level::info, "Unknown verbosity level. Values should be between 0 and 5. Ignoring it"); - break; + return; } + + verbosity = _level; } ////////////////////////////////////////////////// diff --git a/src/Console_TEST.cc b/src/Console_TEST.cc index d2f38e66..fab45066 100644 --- a/src/Console_TEST.cc +++ b/src/Console_TEST.cc @@ -485,60 +485,61 @@ TEST_F(Console_TEST, ColorErr) EXPECT_TRUE(logContent.find(logString) != std::string::npos); } -// ///////////////////////////////////////////////// -// /// \brief Test Console::Verbosity -// TEST_F(Console_TEST, Verbosity) -// { -// EXPECT_EQ(common::Console::Verbosity(), 1); - -// common::Console::SetVerbosity(2); -// EXPECT_EQ(common::Console::Verbosity(), 2); - -// common::Console::SetVerbosity(-1); -// EXPECT_EQ(common::Console::Verbosity(), -1); -// } - -// ///////////////////////////////////////////////// -// /// \brief Test Console::Prefix -// TEST_F(Console_TEST, Prefix) -// { -// // Max verbosity -// common::Console::SetVerbosity(4); - -// // Path to log file -// auto path = common::uuid(); - -// gzLogInit(path, "test.log"); -// std::string logPath = common::joinPaths(path, "test.log"); - -// // Check default prefix -// EXPECT_EQ(common::Console::Prefix(), ""); - -// // Set new prefix -// common::Console::SetPrefix("**test** "); -// EXPECT_EQ(common::Console::Prefix(), "**test** "); - -// // Use the console -// gzerr << "error" << std::endl; -// gzwarn << "warning" << std::endl; -// gzmsg << "message" << std::endl; -// gzdbg << "debug" << std::endl; - -// common::Console::Root().RawLogger().flush(); - -// // Get the logged content -// std::string logContent = GetLogContent(logPath); - -// // Check -// EXPECT_TRUE(logContent.find("**test** [Err]") != std::string::npos); -// EXPECT_TRUE(logContent.find("**test** [Wrn]") != std::string::npos); -// EXPECT_TRUE(logContent.find("**test** [Msg]") != std::string::npos); -// EXPECT_TRUE(logContent.find("**test** [Dbg]") != std::string::npos); - -// // Reset -// common::Console::SetPrefix(""); -// EXPECT_EQ(common::Console::Prefix(), ""); -// } +///////////////////////////////////////////////// +/// \brief Test Console::Verbosity +TEST_F(Console_TEST, Verbosity) +{ + common::Console::SetVerbosity(2); + EXPECT_EQ(2, common::Console::Verbosity()); + + common::Console::SetVerbosity(-1); + EXPECT_EQ(2, common::Console::Verbosity()); + + common::Console::SetVerbosity(1000); + EXPECT_EQ(2, common::Console::Verbosity()); +} + +///////////////////////////////////////////////// +/// \brief Test Console::Prefix +TEST_F(Console_TEST, Prefix) +{ + // Max verbosity + common::Console::SetVerbosity(4); + + // Path to log file + auto path = common::uuid(); + + gzLogInit(path, "test.log"); + std::string logPath = common::joinPaths(path, "test.log"); + + // Check default prefix + EXPECT_EQ(common::Console::Prefix(), ""); + + // Set new prefix + common::Console::SetPrefix("**test** "); + EXPECT_EQ(common::Console::Prefix(), "**test** "); + + // Use the console + gzerr << "error" << std::endl; + gzwarn << "warning" << std::endl; + gzmsg << "message" << std::endl; + gzdbg << "debug" << std::endl; + + common::Console::Root().RawLogger().flush(); + + // Get the logged content + std::string logContent = GetLogContent(logPath); + + // Check + EXPECT_TRUE(logContent.find("**test** error") != std::string::npos); + EXPECT_TRUE(logContent.find("**test** warning") != std::string::npos); + EXPECT_TRUE(logContent.find("**test** message") != std::string::npos); + EXPECT_TRUE(logContent.find("**test** debug") != std::string::npos); + + // Reset + common::Console::SetPrefix(""); + EXPECT_EQ(common::Console::Prefix(), ""); +} ///////////////////////////////////////////////// /// \brief Test Console::LogDirectory