From dcfab0596199bd058b5e2771544583715c0947ae Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Sat, 21 Jun 2025 08:17:56 -0400 Subject: [PATCH] test(tests): brought testing framework for logging from SERiF --- meson.build | 1 + subprojects/gtest.wrap | 16 +++++++ tests/logging/loggingTest.cpp | 82 +++++++++++++++++++++++++++++++++++ tests/logging/meson.build | 23 ++++++++++ tests/meson.build | 7 +++ 5 files changed, 129 insertions(+) create mode 100644 subprojects/gtest.wrap create mode 100644 tests/logging/loggingTest.cpp create mode 100644 tests/logging/meson.build create mode 100644 tests/meson.build diff --git a/meson.build b/meson.build index 55ebf7c..e5d7190 100644 --- a/meson.build +++ b/meson.build @@ -24,6 +24,7 @@ project('liblogging', 'cpp', version: '0.0.1a', default_options: ['cpp_std=c++23 add_project_arguments('-fvisibility=default', language: 'cpp') subdir('build-config') subdir('src') +subdir('tests') pkg = import('pkgconfig') pkg.generate( diff --git a/subprojects/gtest.wrap b/subprojects/gtest.wrap new file mode 100644 index 0000000..7b1e69e --- /dev/null +++ b/subprojects/gtest.wrap @@ -0,0 +1,16 @@ +[wrap-file] +directory = googletest-1.15.2 +source_url = https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz +source_filename = gtest-1.15.2.tar.gz +source_hash = 7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926 +patch_filename = gtest_1.15.2-2_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.15.2-2/get_patch +patch_hash = 641a16b33c96cd32a593537bc30eb7d853c5cc361fa1ee96884f0e2fca21e2d3 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.15.2-2/gtest-1.15.2.tar.gz +wrapdb_version = 1.15.2-2 + +[provide] +gtest = gtest_dep +gtest_main = gtest_main_dep +gmock = gmock_dep +gmock_main = gmock_main_dep diff --git a/tests/logging/loggingTest.cpp b/tests/logging/loggingTest.cpp new file mode 100644 index 0000000..8d13f34 --- /dev/null +++ b/tests/logging/loggingTest.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "quill/LogMacros.h" + +#include "logging.h" + +std::string getLastLine(const std::string& filename) { + std::ifstream file(filename); + std::string line, lastLine; + + if (!file.is_open()) { + throw std::runtime_error("Could not open file"); + } + + while (std::getline(file, line)) { + lastLine = line; + } + + return lastLine; // Returns the last non-empty line +} + +std::string stripTimestamps(const std::string& logLine) { + std::regex logPattern(R"(\d+:\d+:\d+\.\d+\s+\[\d+\]\s+loggingTest\.cpp:\d+\s+LOG_INFO\s+[A-Za-z]*\s+(.*))"); + std::smatch match; + if (std::regex_match(logLine, match, logPattern) && match.size() > 1) { + return match[1].str(); // Extract log message after timestamp + } + return logLine; // Return as-is if pattern doesn't match +} + + +class loggingTest : public ::testing::Test {}; + +TEST_F(loggingTest, DefaultConstructorTest) { + EXPECT_NO_THROW(fourdst::logging::LogManager::getInstance()); +} + +TEST_F(loggingTest, getLoggerTest) { + fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance(); + const std::string loggerName = "testLog"; + const std::string filename = "test.log"; + quill::Logger* logger = logManager.newFileLogger(filename, loggerName); + EXPECT_NE(logger, nullptr); + LOG_INFO(logger, "This is a test message"); + // Wait for the log to be written by calling getLastLine until it is non empty + std::string lastLine; + while (lastLine.empty()) { + lastLine = getLastLine("test.log"); + } + EXPECT_EQ(stripTimestamps(lastLine), "This is a test message"); +} + +TEST_F(loggingTest, newFileLoggerTest) { + fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance(); + const std::string loggerName = "newLog"; + const std::string filename = "newLog.log"; + quill::Logger* logger = logManager.newFileLogger(filename, loggerName); + EXPECT_NE(logger, nullptr); + LOG_INFO(logger, "This is a new test message"); + // Wait for the log to be written by calling getLastLine until it is non empty + std::string lastLine; + while (lastLine.empty()) { + lastLine = getLastLine(filename); + } + EXPECT_EQ(stripTimestamps(lastLine), "This is a new test message"); +} + +TEST_F(loggingTest, getLoggerNames) { + fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance(); + std::vector loggerNames = logManager.getLoggerNames(); + EXPECT_EQ(loggerNames.size(), 4); + EXPECT_EQ(loggerNames.at(0), "log"); + EXPECT_EQ(loggerNames.at(1), "newLog"); + EXPECT_EQ(loggerNames.at(2), "stdout"); + EXPECT_EQ(loggerNames.at(3), "testLog"); +} diff --git a/tests/logging/meson.build b/tests/logging/meson.build new file mode 100644 index 0000000..df9b02e --- /dev/null +++ b/tests/logging/meson.build @@ -0,0 +1,23 @@ +# Test files for dobj +test_sources = [ + 'loggingTest.cpp', +] + +foreach test_file : test_sources + exe_name = test_file.split('.')[0] + message('Building test: ' + exe_name) + + # Create an executable target for each test + test_exe = executable( + exe_name, + test_file, + dependencies: [gtest_dep, logging_dep, quill_dep, gtest_main], + install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly + ) + + # Add the executable as a test + test( + exe_name, + test_exe, + env: ['MESON_SOURCE_ROOT=' + meson.project_source_root(), 'MESON_BUILD_ROOT=' + meson.project_build_root()]) +endforeach diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..f9cfdb1 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,7 @@ +# Google Test dependency +gtest_dep = dependency('gtest', main: true, required : true) +gtest_main = dependency('gtest_main', required: true) +gtest_nomain_dep = dependency('gtest', main: false, required : true) + +# Subdirectories for unit and integration tests +subdir('logging')