From ffec5f3c079eed66a39852c6ce6f9cda231b8f67 Mon Sep 17 00:00:00 2001 From: M4rFri Date: Fri, 5 Jul 2024 12:57:33 +0200 Subject: [PATCH] Added some unit tests --- tests/CMakeLists.txt | 1 + tests/test_attributes.cpp | 26 +++++++++++ tests/test_pattern_formatter.cpp | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 tests/test_attributes.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index acd02d02..5a4b2c6d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -51,6 +51,7 @@ set(SPDLOG_UTESTS_SOURCES test_no_source_location.cpp test_log_level.cpp test_include_sinks.cpp + test_attributes.cpp test_bin_to_hex.cpp test_errors.cpp) diff --git a/tests/test_attributes.cpp b/tests/test_attributes.cpp new file mode 100644 index 00000000..2c1f5977 --- /dev/null +++ b/tests/test_attributes.cpp @@ -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::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); +} \ No newline at end of file diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 85f2114b..c915a5ec 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -455,3 +455,80 @@ TEST_CASE("override need_localtime", "[pattern_formatter]") { 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(); + 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(); + 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(); + 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(); + 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); +}