Skip to content

Commit

Permalink
LibTimeZone: Add a unit test for generated time zone data
Browse files Browse the repository at this point in the history
  • Loading branch information
trflynn89 authored and linusg committed Jan 10, 2022
1 parent ccce9e5 commit b493c2c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Meta/Lagom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=$<BOOL:${ENABLE_TIME_ZONE_DATABASE_DOWNLOAD}>)
endforeach()

# Unicode
file(GLOB LIBUNICODE_TEST_SOURCES CONFIGURE_DEPENDS "../../Tests/LibUnicode/*.cpp")
foreach(source ${LIBUNICODE_TEST_SOURCES})
Expand Down
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions Tests/LibTimeZone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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=$<BOOL:${ENABLE_TIME_ZONE_DATABASE_DOWNLOAD}>)
endforeach()
49 changes: 49 additions & 0 deletions Tests/LibTimeZone/TestTimeZone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2022, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibTest/TestCase.h>

#include <AK/StringView.h>
#include <LibTimeZone/TimeZone.h>

#if ENABLE_TIME_ZONE_DATA

# include <LibTimeZone/TimeZoneData.h>

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

0 comments on commit b493c2c

Please sign in to comment.