mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
chore(tests): include tests for DateTime library
This commit is contained in:
parent
c46ae61d34
commit
28b16b3e05
10 changed files with 152 additions and 0 deletions
10
src/tests.rs
10
src/tests.rs
|
@ -106,6 +106,16 @@ create_tests! {
|
||||||
task_delay: "task/delay",
|
task_delay: "task/delay",
|
||||||
task_spawn: "task/spawn",
|
task_spawn: "task/spawn",
|
||||||
task_wait: "task/wait",
|
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")]
|
#[cfg(feature = "roblox")]
|
||||||
|
|
16
tests/datetime/formattime.luau
Normal file
16
tests/datetime/formattime.luau
Normal 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
|
13
tests/datetime/fromisodate.luau
Normal file
13
tests/datetime/fromisodate.luau
Normal 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"
|
||||||
|
)
|
32
tests/datetime/fromlocaltime.luau
Normal file
32
tests/datetime/fromlocaltime.luau
Normal 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"
|
||||||
|
)
|
32
tests/datetime/fromuniversaltime.luau
Normal file
32
tests/datetime/fromuniversaltime.luau
Normal 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"
|
||||||
|
)
|
16
tests/datetime/fromunixtimestamp.luau
Normal file
16
tests/datetime/fromunixtimestamp.luau
Normal 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
8
tests/datetime/now.luau
Normal 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()))}`
|
||||||
|
)
|
9
tests/datetime/toisodate.luau
Normal file
9
tests/datetime/toisodate.luau
Normal 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()"
|
||||||
|
)
|
8
tests/datetime/tolocaltime.luau
Normal file
8
tests/datetime/tolocaltime.luau
Normal 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
|
8
tests/datetime/touniversaltime.luau
Normal file
8
tests/datetime/touniversaltime.luau
Normal 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
|
Loading…
Add table
Reference in a new issue