mirror of
https://github.com/gabime/spdlog.git
synced 2025-03-04 13:15:48 +08:00
Added some unit tests
This commit is contained in:
parent
c3dac2fa5d
commit
ffec5f3c07
@ -51,6 +51,7 @@ set(SPDLOG_UTESTS_SOURCES
|
|||||||
test_no_source_location.cpp
|
test_no_source_location.cpp
|
||||||
test_log_level.cpp
|
test_log_level.cpp
|
||||||
test_include_sinks.cpp
|
test_include_sinks.cpp
|
||||||
|
test_attributes.cpp
|
||||||
test_bin_to_hex.cpp
|
test_bin_to_hex.cpp
|
||||||
test_errors.cpp)
|
test_errors.cpp)
|
||||||
|
|
||||||
|
26
tests/test_attributes.cpp
Normal file
26
tests/test_attributes.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include "includes.h"
|
||||||
|
#include "spdlog/mdc.h"
|
||||||
|
#include "spdlog/sinks/stdout_sinks.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
#include "test_sink.h"
|
||||||
|
|
||||||
|
TEST_CASE("Attribute test") {
|
||||||
|
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
||||||
|
spdlog::logger log_a("log_a", test_sink);
|
||||||
|
spdlog::logger log_b("log_b", test_sink);
|
||||||
|
log_a.set_pattern("[%n] [%*]");
|
||||||
|
log_b.set_pattern("[%n] [%*]");
|
||||||
|
|
||||||
|
log_a.push_attribute("my_key", "my_value");
|
||||||
|
|
||||||
|
log_a.info("Hello");
|
||||||
|
log_b.info("Hello");
|
||||||
|
|
||||||
|
auto expected_log_a = spdlog::fmt_lib::format("[log_a] [my_key:my_value]");
|
||||||
|
auto expected_log_b = spdlog::fmt_lib::format("[log_b] []");
|
||||||
|
|
||||||
|
auto lines = test_sink->lines();
|
||||||
|
REQUIRE(lines.size() == 2);
|
||||||
|
REQUIRE(lines[0] == expected_log_a);
|
||||||
|
REQUIRE(lines[1] == expected_log_b);
|
||||||
|
}
|
@ -455,3 +455,80 @@ TEST_CASE("override need_localtime", "[pattern_formatter]") {
|
|||||||
REQUIRE(to_string_view(formatted) == oss.str());
|
REQUIRE(to_string_view(formatted) == oss.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("attribute formatter test-1", "[pattern_formatter]") {
|
||||||
|
spdlog::log_attributes attributes;
|
||||||
|
attributes.put("attribute_key_1", "attribute_value_1");
|
||||||
|
attributes.put("attribute_key_2", "attribute_value_2");
|
||||||
|
|
||||||
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||||
|
formatter->set_pattern("[%n] [%l] [%*] %v");
|
||||||
|
|
||||||
|
memory_buf_t formatted;
|
||||||
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message", attributes);
|
||||||
|
formatter->format(msg, formatted);
|
||||||
|
|
||||||
|
auto expected = spdlog::fmt_lib::format(
|
||||||
|
"[logger-name] [info] [attribute_key_1:attribute_value_1 attribute_key_2:attribute_value_2] some message{}",
|
||||||
|
spdlog::details::os::default_eol);
|
||||||
|
REQUIRE(to_string_view(formatted) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("attribute formatter value update", "[pattern_formatter]") {
|
||||||
|
spdlog::log_attributes attributes;
|
||||||
|
attributes.put("attribute_key_1", "attribute_value_1");
|
||||||
|
attributes.put("attribute_key_2", "attribute_value_2");
|
||||||
|
|
||||||
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||||
|
formatter->set_pattern("[%n] [%l] [%*] %v");
|
||||||
|
|
||||||
|
memory_buf_t formatted_1;
|
||||||
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message", attributes);
|
||||||
|
formatter->format(msg, formatted_1);
|
||||||
|
|
||||||
|
auto expected = spdlog::fmt_lib::format(
|
||||||
|
"[logger-name] [info] [attribute_key_1:attribute_value_1 attribute_key_2:attribute_value_2] some message{}",
|
||||||
|
spdlog::details::os::default_eol);
|
||||||
|
|
||||||
|
REQUIRE(to_string_view(formatted_1) == expected);
|
||||||
|
|
||||||
|
msg.attributes.put("attribute_key_1", "new_attribute_value_1");
|
||||||
|
memory_buf_t formatted_2;
|
||||||
|
formatter->format(msg, formatted_2);
|
||||||
|
expected = spdlog::fmt_lib::format(
|
||||||
|
"[logger-name] [info] [attribute_key_1:new_attribute_value_1 attribute_key_2:attribute_value_2] some message{}",
|
||||||
|
spdlog::details::os::default_eol);
|
||||||
|
|
||||||
|
REQUIRE(to_string_view(formatted_2) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("attribute remove key", "[pattern_formatter]") {
|
||||||
|
spdlog::log_attributes attributes;
|
||||||
|
attributes.put("attribute_key_1", "attribute_value_1");
|
||||||
|
attributes.put("attribute_key_2", "attribute_value_2");
|
||||||
|
attributes.remove("attribute_key_1");
|
||||||
|
|
||||||
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||||
|
formatter->set_pattern("[%n] [%l] [%*] %v");
|
||||||
|
|
||||||
|
memory_buf_t formatted;
|
||||||
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message", attributes);
|
||||||
|
formatter->format(msg, formatted);
|
||||||
|
|
||||||
|
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [attribute_key_2:attribute_value_2] some message{}",
|
||||||
|
spdlog::details::os::default_eol);
|
||||||
|
REQUIRE(to_string_view(formatted) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("attribute empty", "[pattern_formatter]") {
|
||||||
|
spdlog::log_attributes attributes;
|
||||||
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||||
|
formatter->set_pattern("[%n] [%l] [%*] %v");
|
||||||
|
|
||||||
|
memory_buf_t formatted;
|
||||||
|
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message", attributes);
|
||||||
|
formatter->format(msg, formatted);
|
||||||
|
|
||||||
|
auto expected = spdlog::fmt_lib::format("[logger-name] [info] [] some message{}", spdlog::details::os::default_eol);
|
||||||
|
REQUIRE(to_string_view(formatted) == expected);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user