Skip to content

Commit

Permalink
Added no source location tests and fixed source location tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Nov 29, 2024
1 parent 3fc1482 commit 9c1b76f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_no_source_location.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions tests/test_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 17 additions & 13 deletions tests/test_source_location.cpp
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 9c1b76f

Please sign in to comment.