From b493c2ca902569fbde6151d3953ac06863c75c7c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 10 Jan 2022 12:22:47 -0500 Subject: [PATCH] LibTimeZone: Add a unit test for generated time zone data --- Meta/Lagom/CMakeLists.txt | 9 ++++++ Tests/CMakeLists.txt | 1 + Tests/LibTimeZone/CMakeLists.txt | 10 ++++++ Tests/LibTimeZone/TestTimeZone.cpp | 49 ++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 Tests/LibTimeZone/CMakeLists.txt create mode 100644 Tests/LibTimeZone/TestTimeZone.cpp diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index ed9581ac8489dd..fcc5d15a67416c 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -576,6 +576,15 @@ if (BUILD_LAGOM) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../Tests/LibTLS) endforeach() + # TimeZone + file(GLOB LIBTIMEZONE_TEST_SOURCES CONFIGURE_DEPENDS "../../Tests/LibTimeZone/*.cpp") + foreach(source ${LIBTIMEZONE_TEST_SOURCES}) + lagom_test(${source} LIBS LagomTimeZone) + + get_filename_component(target "${source}" NAME_WLE) + target_compile_definitions("${target}_lagom" PRIVATE ENABLE_TIME_ZONE_DATA=$) + endforeach() + # Unicode file(GLOB LIBUNICODE_TEST_SOURCES CONFIGURE_DEPENDS "../../Tests/LibUnicode/*.cpp") foreach(source ${LIBUNICODE_TEST_SOURCES}) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 9164ffb885ee6f..b7b2951385d3ff 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -17,6 +17,7 @@ add_subdirectory(LibRegex) add_subdirectory(LibSQL) add_subdirectory(LibTest) add_subdirectory(LibThreading) +add_subdirectory(LibTimeZone) add_subdirectory(LibUnicode) add_subdirectory(LibWasm) add_subdirectory(LibWeb) diff --git a/Tests/LibTimeZone/CMakeLists.txt b/Tests/LibTimeZone/CMakeLists.txt new file mode 100644 index 00000000000000..26f88bf4cecc7a --- /dev/null +++ b/Tests/LibTimeZone/CMakeLists.txt @@ -0,0 +1,10 @@ +set(TEST_SOURCES + TestTimeZone.cpp +) + +foreach(source IN LISTS TEST_SOURCES) + serenity_test("${source}" LibTimeZone LIBS LibTimeZone) + + get_filename_component(target "${source}" NAME_WLE) + target_compile_definitions("${target}" PRIVATE ENABLE_TIME_ZONE_DATA=$) +endforeach() diff --git a/Tests/LibTimeZone/TestTimeZone.cpp b/Tests/LibTimeZone/TestTimeZone.cpp new file mode 100644 index 00000000000000..15a411526760e1 --- /dev/null +++ b/Tests/LibTimeZone/TestTimeZone.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include + +#if ENABLE_TIME_ZONE_DATA + +# include + +TEST_CASE(time_zone_from_string) +{ + EXPECT_EQ(TimeZone::time_zone_from_string("America/New_York"sv), TimeZone::TimeZone::America_New_York); + EXPECT_EQ(TimeZone::time_zone_from_string("Europe/Paris"sv), TimeZone::TimeZone::Europe_Paris); + EXPECT_EQ(TimeZone::time_zone_from_string("Etc/GMT+2"sv), TimeZone::TimeZone::Etc_GMT_Ahead_2); + EXPECT_EQ(TimeZone::time_zone_from_string("Etc/GMT-5"sv), TimeZone::TimeZone::Etc_GMT_Behind_5); + + EXPECT(!TimeZone::time_zone_from_string("I don't exist"sv).has_value()); +} + +TEST_CASE(time_zone_from_string_link) +{ + auto test_link = [](auto tz1, auto tz2) { + auto result1 = TimeZone::time_zone_from_string(tz1); + EXPECT(result1.has_value()); + + auto result2 = TimeZone::time_zone_from_string(tz2); + EXPECT(result2.has_value()); + + EXPECT_EQ(*result1, *result2); + }; + + test_link("America/New_York"sv, "US/Eastern"sv); + + test_link("Etc/GMT"sv, "GMT"sv); + test_link("Etc/GMT+0"sv, "GMT"sv); + test_link("Etc/GMT-0"sv, "GMT"sv); + + test_link("Etc/UTC"sv, "UTC"sv); + test_link("Etc/Universal"sv, "UTC"sv); + test_link("Universal"sv, "UTC"sv); +} + +#endif