chore(tests): include tests for DateTime library

This commit is contained in:
Erica Marigold 2023-08-27 12:46:27 +05:30
parent c46ae61d34
commit 28b16b3e05
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
10 changed files with 152 additions and 0 deletions

View file

@ -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")]

View file

@ -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

View file

@ -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"
)

View file

@ -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"
)

View file

@ -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"
)

View file

@ -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"
)

8
tests/datetime/now.luau Normal file
View file

@ -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()))}`
)

View file

@ -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()"
)

View file

@ -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

View file

@ -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