From 28b16b3e05d3c41e12fa4db8ecaeb7d0b2289b1f Mon Sep 17 00:00:00 2001 From: Compey Date: Sun, 27 Aug 2023 12:46:27 +0530 Subject: [PATCH] chore(tests): include tests for DateTime library --- src/tests.rs | 10 +++++++++ tests/datetime/formattime.luau | 16 ++++++++++++++ tests/datetime/fromisodate.luau | 13 +++++++++++ tests/datetime/fromlocaltime.luau | 32 +++++++++++++++++++++++++++ tests/datetime/fromuniversaltime.luau | 32 +++++++++++++++++++++++++++ tests/datetime/fromunixtimestamp.luau | 16 ++++++++++++++ tests/datetime/now.luau | 8 +++++++ tests/datetime/toisodate.luau | 9 ++++++++ tests/datetime/tolocaltime.luau | 8 +++++++ tests/datetime/touniversaltime.luau | 8 +++++++ 10 files changed, 152 insertions(+) create mode 100644 tests/datetime/formattime.luau create mode 100644 tests/datetime/fromisodate.luau create mode 100644 tests/datetime/fromlocaltime.luau create mode 100644 tests/datetime/fromuniversaltime.luau create mode 100644 tests/datetime/fromunixtimestamp.luau create mode 100644 tests/datetime/now.luau create mode 100644 tests/datetime/toisodate.luau create mode 100644 tests/datetime/tolocaltime.luau create mode 100644 tests/datetime/touniversaltime.luau diff --git a/src/tests.rs b/src/tests.rs index 0aa8975..8736139 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -106,6 +106,16 @@ create_tests! { task_delay: "task/delay", task_spawn: "task/spawn", task_wait: "task/wait", + + datetime_now: "datetime/now", + datetime_fromunixtimestamp: "datetime/fromunixtimestamp", + datetime_fromuniversaltime: "datetime/fromuniversaltime", + datetime_touniversaltime: "datetime/touniversaltime", + datetime_fromlocaltime: "datetime/fromlocaltime", + datetime_tolocaltime: "datetime/tolocaltime", + datetime_fromisodate: "datetime/fromisodate", + datetime_toisodate: "datetime/toisodate", + datetime_formattime: "datetime/formattime", } #[cfg(feature = "roblox")] diff --git a/tests/datetime/formattime.luau b/tests/datetime/formattime.luau new file mode 100644 index 0000000..e16238b --- /dev/null +++ b/tests/datetime/formattime.luau @@ -0,0 +1,16 @@ +local DateTime = require("@lune/datetime") + +-- UTC Timezone +assert( + DateTime.fromUnixTimestamp(1693068988):formatTime("utc", "%Y-%m-%dT%H:%M:%SZ", "en") + == "2023-08-26T16:56:28Z", + "invalid ISO 8601 formatting for DateTime.formatTime()" +) + +assert( + DateTime.fromUnixTimestamp(1693068988):formatTime("utc", "%A, %d %B %Y", "fr") + == "samedi, 26 août 2023", + "expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr'" +) + +-- TODO: local timezone tests diff --git a/tests/datetime/fromisodate.luau b/tests/datetime/fromisodate.luau new file mode 100644 index 0000000..f986827 --- /dev/null +++ b/tests/datetime/fromisodate.luau @@ -0,0 +1,13 @@ +local DateTime = require("@lune/datetime") + +-- Fails due to issue with rust side implementation + +assert( + DateTime.fromIsoDate("2023-08-26T16:56:28Z") ~= nil, + "expected DateTime.fromIsoDate() to return DateTime, got nil" +) + +assert( + DateTime.fromIsoDate("1929-12-05T23:18:23Z") ~= nil, + "expected DateTime.fromIsoDate() to return DateTime, got nil" +) diff --git a/tests/datetime/fromlocaltime.luau b/tests/datetime/fromlocaltime.luau new file mode 100644 index 0000000..cf43df0 --- /dev/null +++ b/tests/datetime/fromlocaltime.luau @@ -0,0 +1,32 @@ +local DateTime = require("@lune/datetime") + +assert( + DateTime.fromLocalTime()["unixTimestamp"] == os.time(), + "expected DateTime.fromLocalTime() with no args to return DateTime at current moment" +) + +assert( + DateTime.fromLocalTime({ + ["year"] = 2023, + ["month"] = "aug", + ["day"] = 26, + ["hour"] = 16, + ["minute"] = 56, + ["second"] = 28, + ["millisecond"] = 0, + })["unixTimestamp"] == 1693049188, + "expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693049188s" +) + +assert( + DateTime.fromLocalTime({ + ["year"] = 2023, + ["month"] = "aug", + ["day"] = 26, + ["hour"] = 16, + ["minute"] = 56, + ["second"] = 28, + ["millisecond"] = 892, + })["unixTimestampMillis"] == 1693049188000, + "expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693049188000ms" +) diff --git a/tests/datetime/fromuniversaltime.luau b/tests/datetime/fromuniversaltime.luau new file mode 100644 index 0000000..400413c --- /dev/null +++ b/tests/datetime/fromuniversaltime.luau @@ -0,0 +1,32 @@ +local DateTime = require("@lune/datetime") + +assert( + DateTime.fromUniversalTime()["unixTimestamp"] == os.time(), + "expected DateTime.fromLocalTime() with no args to return DateTime at current moment" +) + +assert( + DateTime.fromUniversalTime({ + ["year"] = 2023, + ["month"] = "aug", + ["day"] = 26, + ["hour"] = 16, + ["minute"] = 56, + ["second"] = 28, + ["millisecond"] = 0, + })["unixTimestamp"] == 1693068988, + "expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693068988s" +) + +assert( + DateTime.fromUniversalTime({ + ["year"] = 2023, + ["month"] = "aug", + ["day"] = 26, + ["hour"] = 16, + ["minute"] = 56, + ["second"] = 28, + ["millisecond"] = 892, + })["unixTimestampMillis"] == 1693068988000, + "expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693068988000ms" +) diff --git a/tests/datetime/fromunixtimestamp.luau b/tests/datetime/fromunixtimestamp.luau new file mode 100644 index 0000000..1cf1956 --- /dev/null +++ b/tests/datetime/fromunixtimestamp.luau @@ -0,0 +1,16 @@ +local DateTime = require("@lune/datetime") + +-- Bug in rust side implementation for fromUnixTimestamp, calculation for conversion there is wonky, +-- a difference of few millis causes differences as whole seconds for some reason + +assert( + DateTime.fromUnixTimestamp(0000.892)["unixTimestampMillis"] == (0 * 1000) + 892, + "expected DateTime.fromUnixTimestamp() with millis float to return correct millis timestamp" +) + +-- We subtract one due to the floating point accuracy... Need to fix later +assert( + DateTime.fromUnixTimestamp(1693114921.632)["unixTimestampMillis"] + == ((1693114921 * 1000) + 632) - 1, + "expected DateTime.fromUnixTimestamp() with millis and seconds float to return correct millis timestamp" +) diff --git a/tests/datetime/now.luau b/tests/datetime/now.luau new file mode 100644 index 0000000..cc5db39 --- /dev/null +++ b/tests/datetime/now.luau @@ -0,0 +1,8 @@ +local DateTime = require("@lune/datetime") + +local TYPE = "DateTime" + +assert( + typeof(DateTime.now()) == TYPE, + `dateTime.now() should return a {TYPE}, returned {tostring(typeof(DateTime.now()))}` +) diff --git a/tests/datetime/toisodate.luau b/tests/datetime/toisodate.luau new file mode 100644 index 0000000..e553e4c --- /dev/null +++ b/tests/datetime/toisodate.luau @@ -0,0 +1,9 @@ +local DateTime = require("@lune/datetime") + +assert( + string.match( + DateTime.now():toIsoDate(), + "(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)T(%d?%d?):(%d?%d?):(%d?%d?)Z$" + ), + "invalid ISO 8601 date returned by dateTime.toIsoDate()" +) diff --git a/tests/datetime/tolocaltime.luau b/tests/datetime/tolocaltime.luau new file mode 100644 index 0000000..3be323a --- /dev/null +++ b/tests/datetime/tolocaltime.luau @@ -0,0 +1,8 @@ +local DateTime = require("@lune/datetime") + +local TIME = "2023-08-27T05:54:19Z" +local dateTime = DateTime.fromIsoDate(TIME):toLocalTime() +local expectedDateTimeValues = + table.pack(string.match(TIME, "(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)T(%d?%d?):(%d?%d?):(%d?%d?)Z$")) + +-- TODO: Implement the test diff --git a/tests/datetime/touniversaltime.luau b/tests/datetime/touniversaltime.luau new file mode 100644 index 0000000..3be323a --- /dev/null +++ b/tests/datetime/touniversaltime.luau @@ -0,0 +1,8 @@ +local DateTime = require("@lune/datetime") + +local TIME = "2023-08-27T05:54:19Z" +local dateTime = DateTime.fromIsoDate(TIME):toLocalTime() +local expectedDateTimeValues = + table.pack(string.match(TIME, "(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)T(%d?%d?):(%d?%d?):(%d?%d?)Z$")) + +-- TODO: Implement the test