SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
probeTest.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include "probe.h"
3#include <iostream>
4#include <string>
5#include <vector>
6#include <set>
7#include <sstream>
8#include <regex>
9#include <source_location>
10#include "config.h"
11#include <chrono>
12#include "quill/LogMacros.h"
13
14std::string TEST_CONFIG = std::string(getenv("MESON_SOURCE_ROOT")) + "/tests/testsConfig.yaml";
15
16std::string getLastLine(const std::string& filename) {
17 std::ifstream file(filename);
18 std::string line, lastLine;
19
20 if (!file.is_open()) {
21 throw std::runtime_error("Could not open file");
22 }
23
24 while (std::getline(file, line)) {
25 lastLine = line;
26 }
27
28 return lastLine; // Returns the last non-empty line
29}
30
31std::string stripTimestamps(const std::string& logLine) {
32 std::regex logPattern(R"(\d+:\d+:\d+\.\d+\s+\[\d+\]\s+probeTest\.cpp:\d+\s+LOG_INFO\s+[A-Za-z]*\s+(.*))");
33 std::smatch match;
34 if (std::regex_match(logLine, match, logPattern) && match.size() > 1) {
35 return match[1].str(); // Extract log message after timestamp
36 }
37 return logLine; // Return as-is if pattern doesn't match
38}
39
40
41class probeTest : public ::testing::Test {};
42
43TEST_F(probeTest, DefaultConstructorTest) {
44 serif::config::Config::getInstance().loadConfig(TEST_CONFIG);
46}
47
48TEST_F(probeTest, waitTest) {
49 auto start = std::chrono::high_resolution_clock::now();
51 auto end = std::chrono::high_resolution_clock::now();
52 std::chrono::duration<double> elapsed = end - start;
53 EXPECT_LE(elapsed.count(), 1.1);
54}
55
56TEST_F(probeTest, getLoggerTest) {
57 serif::config::Config::getInstance().loadConfig(TEST_CONFIG);
59 const std::string loggerName = "testLog";
60 const std::string filename = "test.log";
61 quill::Logger* logger = logManager.newFileLogger(filename, loggerName);
62 EXPECT_NE(logger, nullptr);
63 LOG_INFO(logger, "This is a test message");
64 // Wait for the log to be written by calling getLastLine until it is non empty
65 std::string lastLine;
66 while (lastLine.empty()) {
67 lastLine = getLastLine("test.log");
68 }
69 EXPECT_EQ(stripTimestamps(lastLine), "This is a test message");
70}
71
72TEST_F(probeTest, newFileLoggerTest) {
73 serif::config::Config::getInstance().loadConfig(TEST_CONFIG);
75 const std::string loggerName = "newLog";
76 const std::string filename = "newLog.log";
77 quill::Logger* logger = logManager.newFileLogger(filename, loggerName);
78 EXPECT_NE(logger, nullptr);
79 LOG_INFO(logger, "This is a new test message");
80 // Wait for the log to be written by calling getLastLine until it is non empty
81 std::string lastLine;
82 while (lastLine.empty()) {
83 lastLine = getLastLine(filename);
84 }
85 EXPECT_EQ(stripTimestamps(lastLine), "This is a new test message");
86}
87
88TEST_F(probeTest, getLoggerNames) {
89 serif::config::Config::getInstance().loadConfig(TEST_CONFIG);
91 std::vector<std::string> loggerNames = logManager.getLoggerNames();
92 EXPECT_EQ(loggerNames.size(), 4);
93 EXPECT_EQ(loggerNames.at(0), "log");
94 EXPECT_EQ(loggerNames.at(1), "newLog");
95 EXPECT_EQ(loggerNames.at(2), "stdout");
96 EXPECT_EQ(loggerNames.at(3), "testLog");
97}
Class to manage logging operations.
Definition probe.h:79
quill::Logger * newFileLogger(const std::string &filename, const std::string &loggerName)
Create a new file logger.
Definition probe.cpp:245
static LogManager & getInstance()
Get the singleton instance of LogManager.
Definition probe.h:103
std::vector< std::string > getLoggerNames()
Get the names of all loggers.
Definition probe.cpp:227
std::string TEST_CONFIG
Definition eosTest.cpp:22
void wait(int seconds)
Wait for a specified number of seconds.
Definition probe.cpp:53
std::string stripTimestamps(const std::string &logLine)
Definition probeTest.cpp:31
std::string getLastLine(const std::string &filename)
Definition probeTest.cpp:16
TEST_F(probeTest, DefaultConstructorTest)
Definition probeTest.cpp:43