Merge branch 'master' into YearCalendar

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
Viktar Lukashonak
2023-01-23 18:42:32 +03:00
parent eb9d2d9c5d
commit ed31b20c26
57 changed files with 799 additions and 477 deletions

View File

@@ -2,7 +2,11 @@
#include <glibmm.h>
#include <catch2/catch_all.hpp>
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif
#include <thread>
#include <type_traits>

View File

@@ -1,6 +1,10 @@
#include "config.hpp"
#include <catch2/catch_all.hpp>
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif
TEST_CASE("Load simple config", "[config]") {
waybar::Config conf;

162
test/date.cpp Normal file
View File

@@ -0,0 +1,162 @@
#include "util/date.hpp"
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#else
#include <catch2/catch.hpp>
#endif
#ifndef SKIP
#define SKIP(...) \
WARN(__VA_ARGS__); \
return
#endif
using namespace std::literals::chrono_literals;
/*
* Check that the date/time formatter with locale and timezone support is working as expected.
*/
const date::zoned_time<std::chrono::seconds> TEST_TIME = date::zoned_time{
"UTC", date::local_days{date::Monday[1] / date::January / 2022} + 13h + 4min + 5s};
/*
* Check if the date formatted with LC_TIME=en_US is within expectations.
*
* The check expects Glibc output style and will fail with FreeBSD (different implementation)
* or musl (no implementation).
*/
static const bool LC_TIME_is_sane = []() {
try {
std::stringstream ss;
ss.imbue(std::locale("en_US.UTF-8"));
time_t t = 1641211200;
std::tm tm = *std::gmtime(&t);
ss << std::put_time(&tm, "%x %X");
return ss.str() == "01/03/2022 12:00:00 PM";
} catch (std::exception &) {
return false;
}
}();
TEST_CASE("Format UTC time", "[clock][util]") {
const auto loc = std::locale("C");
const auto tm = TEST_TIME;
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 13:04:05 2022 UTC");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
if (!LC_TIME_is_sane) {
SKIP("Locale support check failed, skip tests");
}
/* Test a few locales that are most likely to be present */
SECTION("US locale") {
try {
const auto loc = std::locale("en_US.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
} catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests");
}
}
SECTION("GB locale") {
try {
const auto loc = std::locale("en_GB.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 13:04:05"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "03/01/22 13:04:05");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
} catch (const std::runtime_error &) {
WARN("Locale en_GB not found, skip tests");
}
}
SECTION("Global locale") {
try {
const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
CHECK(fmt::format("{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM"));
CHECK(fmt::format("{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
CHECK(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
std::locale::global(loc);
} catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests");
}
}
}
TEST_CASE("Format zoned time", "[clock][util]") {
const auto loc = std::locale("C");
const auto tm = date::zoned_time{"America/New_York", TEST_TIME};
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 08:04:05 2022 EST");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
if (!LC_TIME_is_sane) {
SKIP("Locale support check failed, skip tests");
}
/* Test a few locales that are most likely to be present */
SECTION("US locale") {
try {
const auto loc = std::locale("en_US.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
} catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests");
}
}
SECTION("GB locale") {
try {
const auto loc = std::locale("en_GB.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "03/01/22 08:04:05");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
} catch (const std::runtime_error &) {
WARN("Locale en_GB not found, skip tests");
}
}
SECTION("Global locale") {
try {
const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
CHECK(fmt::format("{}", tm).empty()); // no format specified
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM"));
CHECK(fmt::format("{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
CHECK(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
std::locale::global(loc);
} catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests");
}
}
}

View File

@@ -3,8 +3,13 @@
#include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/spdlog.h>
#if __has_include(<catch2/catch_all.hpp>)
#include <catch2/catch_all.hpp>
#include <catch2/reporters/catch_reporter_tap.hpp>
#else
#include <catch2/catch.hpp>
#include <catch2/catch_reporter_tap.hpp>
#endif
#include <memory>
int main(int argc, char* argv[]) {
@@ -13,10 +18,16 @@ int main(int argc, char* argv[]) {
session.applyCommandLine(argc, argv);
const auto logger = spdlog::default_logger();
#if CATCH_VERSION_MAJOR >= 3
for (const auto& spec : session.config().getReporterSpecs()) {
if (spec.name() == "tap") {
const auto& reporter_name = spec.name();
#else
{
const auto& reporter_name = session.config().getReporterName();
#endif
if (reporter_name == "tap") {
spdlog::set_pattern("# [%l] %v");
} else if (spec.name() == "compact") {
} else if (reporter_name == "compact") {
logger->sinks().clear();
} else {
logger->sinks().assign({std::make_shared<spdlog::sinks::stderr_sink_st>()});

View File

@@ -15,7 +15,7 @@ test_src = files(
if tz_dep.found()
test_dep += tz_dep
test_src += files('waybar_time.cpp')
test_src += files('date.cpp')
endif
waybar_test = executable(

View File

@@ -1,90 +0,0 @@
#include "util/waybar_time.hpp"
#include <date/date.h>
#include <date/tz.h>
#include <catch2/catch_all.hpp>
#include <chrono>
#include <stdexcept>
using namespace std::literals::chrono_literals;
/*
* Check that the date/time formatter with locale and timezone support is working as expected.
*/
const date::zoned_time<std::chrono::seconds> TEST_TIME = date::make_zoned(
"UTC", date::local_days{date::Monday[1] / date::January / 2022} + 13h + 4min + 5s);
TEST_CASE("Format UTC time", "[clock][util]") {
waybar::waybar_time tm{std::locale("C"), TEST_TIME};
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE(fmt::format("{:%c %Z}", tm) == "Mon Jan 3 13:04:05 2022 UTC");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
/* Test a few locales that are most likely to be present */
SECTION("US locale") {
try {
tm.locale = std::locale("en_US");
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM"));
REQUIRE(fmt::format("{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
} catch (const std::runtime_error&) {
// locale not found; ignore
}
}
SECTION("GB locale") {
try {
tm.locale = std::locale("en_GB");
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 13:04:05"));
REQUIRE(fmt::format("{:%x %X}", tm) == "03/01/22 13:04:05");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
} catch (const std::runtime_error&) {
// locale not found; ignore
}
}
}
TEST_CASE("Format zoned time", "[clock][util]") {
waybar::waybar_time tm{std::locale("C"), date::make_zoned("America/New_York", TEST_TIME)};
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE(fmt::format("{:%c %Z}", tm) == "Mon Jan 3 08:04:05 2022 EST");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
/* Test a few locales that are most likely to be present */
SECTION("US locale") {
try {
tm.locale = std::locale("en_US");
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM"));
REQUIRE(fmt::format("{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
} catch (const std::runtime_error&) {
// locale not found; ignore
}
}
SECTION("GB locale") {
try {
tm.locale = std::locale("en_GB");
REQUIRE(fmt::format("{}", tm).empty()); // no format specified
REQUIRE_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05"));
REQUIRE(fmt::format("{:%x %X}", tm) == "03/01/22 08:04:05");
REQUIRE(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
} catch (const std::runtime_error&) {
// locale not found; ignore
}
}
}