feat(tests): rework tests in numerous ways (#3059)

This commit is contained in:
ns6089 2024-08-22 23:48:24 +03:00 committed by GitHub
commit 764ce03520
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 316 additions and 446 deletions

View file

@ -2,74 +2,63 @@
* @file tests/unit/test_logging.cpp
* @brief Test src/logging.*.
*/
#include <fstream>
#include <src/logging.h>
#include <tests/conftest.cpp>
#include "../tests_common.h"
class LoggerInitTest: public virtual BaseTest, public ::testing::WithParamInterface<int> {
protected:
void
SetUp() override {
BaseTest::SetUp();
}
#include <fstream>
#include <random>
namespace {
std::array log_levels = {
std::tuple("verbose", &verbose),
std::tuple("debug", &debug),
std::tuple("info", &info),
std::tuple("warning", &warning),
std::tuple("error", &error),
std::tuple("fatal", &fatal),
};
constexpr auto log_file = "test_sunshine.log";
} // namespace
struct LogLevelsTest: testing::TestWithParam<decltype(log_levels)::value_type> {};
void
TearDown() override {
BaseTest::TearDown();
}
};
INSTANTIATE_TEST_SUITE_P(
LogLevel,
LoggerInitTest,
::testing::Values(
0,
1,
2,
3,
4,
5));
TEST_P(LoggerInitTest, InitLogging) {
int logLevel = GetParam();
std::string logFilePath = "test_log_" + std::to_string(logLevel) + ".log";
Logging,
LogLevelsTest,
testing::ValuesIn(log_levels),
[](const auto &info) { return std::string(std::get<0>(info.param)); });
// deinit the BaseTest logger
BaseTest::deinit_guard.reset();
TEST_P(LogLevelsTest, PutMessage) {
auto [label, plogger] = GetParam();
ASSERT_TRUE(plogger);
auto &logger = *plogger;
auto log_deinit = logging::init(logLevel, logFilePath);
if (!log_deinit) {
FAIL() << "Failed to initialize logging";
}
}
std::random_device rand_dev;
std::mt19937_64 rand_gen(rand_dev());
auto test_message = std::to_string(rand_gen()) + std::to_string(rand_gen());
BOOST_LOG(logger) << test_message;
TEST(LogFlushTest, CheckLogFile) {
// Write a log message
BOOST_LOG(info) << "Test message";
// Flush logger and search for the message in the log file
// Call log_flush
logging::log_flush();
// Check the contents of the log file
std::ifstream log_file("test.log");
std::string line;
std::ifstream input(log_file);
ASSERT_TRUE(input.is_open());
bool found = false;
while (std::getline(log_file, line)) {
if (line.find("Test message") != std::string::npos) {
found = true;
break;
for (std::string line; std::getline(input, line);) {
if (line.find(test_message) != std::string::npos) {
// Assume that logger may change the case of log level label
std::transform(line.begin(), line.end(), line.begin(),
[](char c) { return std::tolower(c); });
if (line.find(label) != std::string::npos) {
found = true;
break;
}
}
}
EXPECT_TRUE(found);
}
TEST(PrintHelpTest, CheckOutput) {
std::string name = "test";
logging::print_help(name.c_str());
std::string output = cout_buffer.str();
EXPECT_NE(output.find("Usage: " + name), std::string::npos);
EXPECT_NE(output.find("--help"), std::string::npos);
ASSERT_TRUE(found);
}