diff --git a/tests/format.cpp b/tests/test_misc.cpp similarity index 99% rename from tests/format.cpp rename to tests/test_misc.cpp index 0b2e1d6d..4f6ee72c 100644 --- a/tests/format.cpp +++ b/tests/test_misc.cpp @@ -39,7 +39,6 @@ TEST_CASE("basic_logging ", "[basic_logging]") //REQUIRE(log_info(some_logged_class("some_val")) == "some_val"); } - TEST_CASE("log_levels", "[log_levels]") { REQUIRE(log_info("Hello", spdlog::level::err) == ""); @@ -54,3 +53,6 @@ TEST_CASE("log_levels", "[log_levels]") + + + diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp new file mode 100644 index 00000000..473870e7 --- /dev/null +++ b/tests/test_pattern_formatter.cpp @@ -0,0 +1,68 @@ +#include "includes.h" + +// log to str and return it +static std::string log_to_str(const std::string& msg, std::shared_ptr formatter = nullptr) +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("pattern_tester", oss_sink); + oss_logger.set_level(spdlog::level::info); + if (formatter) oss_logger.set_formatter(formatter); + oss_logger.info(msg); + return oss.str(); +} +TEST_CASE("custom eol", "[pattern_formatter]") +{ + std::string msg = "Hello custom eol test"; + std::string eol = ";)"; + auto formatter = std::make_shared("%v", spdlog::pattern_time_type::local, ";)"); + + REQUIRE(log_to_str(msg, formatter) == msg + eol); +} + +TEST_CASE("empty format", "[pattern_formatter]") +{ + auto formatter = std::make_shared("", spdlog::pattern_time_type::local, ""); + REQUIRE(log_to_str("Some message", formatter) == ""); +} + +TEST_CASE("empty format2", "[pattern_formatter]") +{ + auto formatter = std::make_shared("", spdlog::pattern_time_type::local, "\n"); + REQUIRE(log_to_str("Some message", formatter) == "\n"); +} + +TEST_CASE("level", "[pattern_formatter]") +{ + auto formatter = std::make_shared("[%l] %v", spdlog::pattern_time_type::local, "\n"); + REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n"); +} + + +TEST_CASE("short level", "[pattern_formatter]") +{ + auto formatter = std::make_shared("[%L] %v", spdlog::pattern_time_type::local, "\n"); + REQUIRE(log_to_str("Some message", formatter) == "[I] Some message\n"); +} + +TEST_CASE("name", "[pattern_formatter]") +{ + auto formatter = std::make_shared("[%n] %v", spdlog::pattern_time_type::local, "\n"); + REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n"); +} + +TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") +{ + using namespace::std::chrono; + auto formatter = std::make_shared("%D %v", spdlog::pattern_time_type::local, "\n"); + auto now_tm = spdlog::details::os::localtime(); + std::stringstream oss; + oss << std::put_time(&now_tm, "%D") << " Some message\n"; + REQUIRE(log_to_str("Some message", formatter) == oss.str()); +} + + + + + + diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index ef9d78f9..286c6979 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -21,6 +21,7 @@ {59A07559-5F38-4DD6-A7FA-DB4153690B42} tests + 10.0.16299.0 @@ -128,10 +129,11 @@ - + + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index adc86e7b..d1fdf73d 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -18,9 +18,6 @@ Source Files - - Source Files - Source Files @@ -39,6 +36,12 @@ Source Files + + Source Files + + + Source Files +