Added no source location tests and fixed source location tests

This commit is contained in:
gabime 2024-11-29 12:54:53 +02:00
parent 3fc14822ef
commit 9c1b76fe72
4 changed files with 41 additions and 13 deletions

View File

@ -50,6 +50,7 @@ set(SPDLOG_UTESTS_SOURCES
test_circular_q.cpp
test_ringbuffer_sink.cpp
test_source_location.cpp
test_no_source_location.cpp
test_log_level.cpp
test_include_sinks.cpp)

View File

@ -0,0 +1,22 @@
#ifndef SPDLOG_NO_SOURCE_LOC
#define SPDLOG_NO_SOURCE_LOC
#endif
#include "includes.h"
#include "test_sink.h"
// no source location should appear in the log message
TEST_CASE("test_no_source_location", "[source_location]") {
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
auto logger = std::make_shared<spdlog::logger>("test", test_sink);
logger->set_pattern("%s:%#:%! %v");
// test with no source location with parameters
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello {}", "no source location");
REQUIRE(test_sink->lines().size() == 1);
REQUIRE(test_sink->lines()[0] == ":: Hello no source location");
// test with no source location without parameters
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello");
REQUIRE(test_sink->lines().size() == 2);
REQUIRE(test_sink->lines()[1] == ":: Hello");
}

View File

@ -12,6 +12,7 @@
#include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/details/os.h"
namespace spdlog {
namespace sinks {

View File

@ -1,21 +1,25 @@
#ifndef SPDLOG_NO_SOURCE_LOC
#ifdef SPDLOG_NO_SOURCE_LOC
#undef SPDLOG_NO_SOURCE_LOC
#endif
#include "includes.h"
#include "spdlog/sinks/ostream_sink.h"
#include "test_sink.h"
using spdlog::details::os::default_eol;
// test with source location
TEST_CASE("test_source_location", "[source_location]") {
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_st>(oss);
auto oss_logger = std::make_shared<spdlog::logger>("oss", oss_sink);
//spdlog::logger oss_logger("oss", oss_sink);
oss_logger->set_pattern("%s:%# %v");
SPDLOG_LOGGER_CALL(oss_logger, spdlog::level::info, "Hello {}", "source location");
REQUIRE(oss.str() == std::string("test_source_location.cpp:17 Hello source location") + default_eol);
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
auto logger = std::make_shared<spdlog::logger>("test", test_sink);
logger->set_pattern("%s:%# %v");
// test with source location with parameters
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello {}", "source location");
REQUIRE(test_sink->lines().size() == 1);
REQUIRE(test_sink->lines()[0] == "test_source_location.cpp:14 Hello source location");
// test with source location without parameters
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello");
REQUIRE(test_sink->lines().size() == 2);
REQUIRE(test_sink->lines()[1] == "test_source_location.cpp:18 Hello");
}
#endif
//REQUIRE(oss.str() == std::string("test_source_location.cpp:20 Hello source location") + default_eol);