Repository: gabime/spdlog
File: tests/test_pattern_formatter.cpp (C++)

Code before:
TEST_CASE("level", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") ==
            "[info] Some message\n");
}

TEST_CASE("short level", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") ==
            "[I] Some message\n");
}

TEST_CASE("name", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") ==
            "[pattern_tester] Some message\n");
}

TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
    auto now_tm = spdlog::details::os::localtime();
    std::stringstream oss;
    oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << std::setw(2)
        << now_tm.tm_mday << "/" << std::setw(2) << (now_tm.tm_year + 1900) % 1000
        << " Some message\n";
    REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") ==
            oss.str());
}

// see test_timezone.cpp for actutal UTC offset calculation tests
TEST_CASE("UTC offset", "[pattern_formatter]") {
    using namespace std::chrono_literals;
    const auto now = std::chrono::system_clock::now();
    std::string result =
        log_to_str_with_time(now, "Some message", "%z", spdlog::pattern_time_type::local, "\n");

#ifndef SPDLOG_NO_TZ_OFFSET
    // Match format: +HH:MM or -HH:MM
    std::regex re(R"([+-]\d{2}:[0-5]\d\n)");
    REQUIRE(std::regex_match(result, re));
#else
    REQUIRE(result == "+??:??\n");
#endif
}

TEST_CASE("color range test1", "[pattern_formatter]") {
    auto formatter = std::make_shared<spdlog::pattern_formatter>(
        "%^%v%$", spdlog::pattern_time_type::local, "\n");

    memory_buf_t buf;
    spdlog::fmt_lib::format_to(std::back_inserter(buf), "Hello");
    memory_buf_t formatted;
    std::string logger_name = "test";
    spdlog::details::log_msg msg(logger_name, spdlog::level::info,
                                 spdlog::string_view_t(buf.data(), buf.size()));

Review comment (suggestion):
Corrected spelling of 'actutal' to 'actual'.
```suggestion
// see test_timezone.cpp for actual UTC offset calculation tests
```

Code after:
TEST_CASE("level", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") ==
            "[info] Some message\n");
}

TEST_CASE("short level", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") ==
            "[I] Some message\n");
}

TEST_CASE("name", "[pattern_formatter]") {
    REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") ==
            "[pattern_tester] Some message\n");
}

TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
    auto now_tm = spdlog::details::os::localtime();
    std::stringstream oss;
    oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << std::setw(2)
        << now_tm.tm_mday << "/" << std::setw(2) << (now_tm.tm_year + 1900) % 1000
        << " Some message\n";
    REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") ==
            oss.str());
}

// see test_timezone.cpp for actual UTC offset calculation tests
TEST_CASE("UTC offset", "[pattern_formatter]") {
    using namespace std::chrono_literals;
    const auto now = std::chrono::system_clock::now();
    std::string result =
        log_to_str_with_time(now, "Some message", "%z", spdlog::pattern_time_type::local, "\n");

#ifndef SPDLOG_NO_TZ_OFFSET
    // Match format: +HH:MM or -HH:MM
    std::regex re(R"([+-]\d{2}:[0-5]\d\n)");
    REQUIRE(std::regex_match(result, re));
#else
    REQUIRE(result == "+??:??\n");
#endif
}

TEST_CASE("color range test1", "[pattern_formatter]") {
    auto formatter = std::make_shared<spdlog::pattern_formatter>(
        "%^%v%$", spdlog::pattern_time_type::local, "\n");

    memory_buf_t buf;
    spdlog::fmt_lib::format_to(std::back_inserter(buf), "Hello");
    memory_buf_t formatted;
    std::string logger_name = "test";
    spdlog::details::log_msg msg(logger_name, spdlog::level::info,
                                 spdlog::string_view_t(buf.data(), buf.size()));