chore(tests): finalize and implement missing tests

This commit is contained in:
Erica Marigold 2023-08-28 17:07:49 +05:30
parent e4a65e301b
commit c3e456f0ef
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
5 changed files with 61 additions and 18 deletions

View file

@ -148,7 +148,17 @@ impl<'lua> FromLua<'lua> for DateTime {
} }
} }
impl LuaUserData for DateTimeBuilder {} impl LuaUserData for DateTimeBuilder {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("year", |_, this| Ok(this.year));
fields.add_field_method_get("month", |_, this| Ok(this.month));
fields.add_field_method_get("day", |_, this| Ok(this.day));
fields.add_field_method_get("hour", |_, this| Ok(this.hour));
fields.add_field_method_get("minute", |_, this| Ok(this.minute));
fields.add_field_method_get("second", |_, this| Ok(this.second));
fields.add_field_method_get("millisecond", |_, this| Ok(this.millisecond));
}
}
impl<'lua> FromLua<'lua> for DateTimeBuilder { impl<'lua> FromLua<'lua> for DateTimeBuilder {
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
@ -176,6 +186,7 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder {
.with_hour(t.get("hour")?) .with_hour(t.get("hour")?)
.with_minute(t.get("minute")?) .with_minute(t.get("minute")?)
.with_second(t.get("second")?) .with_second(t.get("second")?)
.with_millisecond(t.get("millisecond")?)
// TODO: millisecond support // TODO: millisecond support
.build()), .build()),
_ => Err(LuaError::external( _ => Err(LuaError::external(

View file

@ -15,7 +15,7 @@ assert(
["second"] = 28, ["second"] = 28,
["millisecond"] = 0, ["millisecond"] = 0,
})["unixTimestamp"] == 1693049188, })["unixTimestamp"] == 1693049188,
"expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693049188s" "expected DateTime.fromLocalTime() with DateTimeValues arg to return 1693049188s"
) )
assert( assert(
@ -27,6 +27,6 @@ assert(
["minute"] = 56, ["minute"] = 56,
["second"] = 28, ["second"] = 28,
["millisecond"] = 892, ["millisecond"] = 892,
})["unixTimestampMillis"] == 1693049188000, })["unixTimestampMillis"] == 1693049188892,
"expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693049188000ms" "expected DateTime.fromLocalTime() with DateTimeValues arg with millis to return 1693049188892ms"
) )

View file

@ -2,7 +2,7 @@ local DateTime = require("@lune/datetime")
assert( assert(
DateTime.fromUniversalTime()["unixTimestamp"] == os.time(), DateTime.fromUniversalTime()["unixTimestamp"] == os.time(),
"expected DateTime.fromLocalTime() with no args to return DateTime at current moment" "expected DateTime.fromLocalTime() with no args to return DateTime at the current moment"
) )
assert( assert(
@ -15,7 +15,7 @@ assert(
["second"] = 28, ["second"] = 28,
["millisecond"] = 0, ["millisecond"] = 0,
})["unixTimestamp"] == 1693068988, })["unixTimestamp"] == 1693068988,
"expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693068988s" "expected DateTime.fromUniversalTime() with DateTimeValues arg to return 1693068988s"
) )
assert( assert(
@ -27,6 +27,6 @@ assert(
["minute"] = 56, ["minute"] = 56,
["second"] = 28, ["second"] = 28,
["millisecond"] = 892, ["millisecond"] = 892,
})["unixTimestampMillis"] == 1693068988000, })["unixTimestampMillis"] == 1693068988892,
"expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693068988000ms" "expected DateTime.fromUniversalTime() with DateTimeValues arg with millis to return 1693068988892ms"
) )

View file

@ -1,8 +1,24 @@
local DateTime = require("@lune/datetime") local DateTime = require("@lune/datetime")
local TIME = "2023-08-27T05:54:19Z" local dateTime = (DateTime.fromIsoDate("2023-08-27T05:54:19Z") :: DateTime.DateTime):toLocalTime()
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 local expectedDateTimeValues = table.pack(
string.match(
"2023-08-27T11:24:19Z",
"(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)T(%d?%d?):(%d?%d?):(%d?%d?)Z$"
)
)
local expectedYear = tonumber(expectedDateTimeValues[1])
local expectedMonth = tonumber(expectedDateTimeValues[2])
local expectedDay = tonumber(expectedDateTimeValues[3])
local expectedHour = tonumber(expectedDateTimeValues[4])
local expectedMinute = tonumber(expectedDateTimeValues[5])
local expectedSecond = tonumber(expectedDateTimeValues[6])
assert(dateTime.year == expectedYear, `expected {dateTime.year} == {expectedYear}`)
assert(dateTime.month == expectedMonth, `expected {dateTime.month} == {expectedMonth}`)
assert(dateTime.day == expectedDay, `expected {dateTime.day} == {expectedDay}`)
assert(dateTime.hour == expectedHour, `expected {dateTime.hour} == {expectedHour}`)
assert(dateTime.minute == expectedMinute, `expected {dateTime.minute} == {expectedMinute}`)
assert(dateTime.second == expectedSecond, `expected {dateTime.second} == {expectedSecond}`)

View file

@ -1,8 +1,24 @@
local DateTime = require("@lune/datetime") local DateTime = require("@lune/datetime")
local TIME = "2023-08-27T05:54:19Z" local dateTime = (DateTime.fromIsoDate("2023-08-27T05:54:19Z") :: DateTime.DateTime):toLocalTime()
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 local expectedDateTimeValues = table.pack(
string.match(
"2023-08-27T11:24:19Z",
"(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)T(%d?%d?):(%d?%d?):(%d?%d?)Z$"
)
)
local expectedYear = tonumber(expectedDateTimeValues[1])
local expectedMonth = tonumber(expectedDateTimeValues[2])
local expectedDay = tonumber(expectedDateTimeValues[3])
local expectedHour = tonumber(expectedDateTimeValues[4])
local expectedMinute = tonumber(expectedDateTimeValues[5])
local expectedSecond = tonumber(expectedDateTimeValues[6])
assert(dateTime.year == expectedYear, `expected {dateTime.year} == {expectedYear}`)
assert(dateTime.month == expectedMonth, `expected {dateTime.month} == {expectedMonth}`)
assert(dateTime.day == expectedDay, `expected {dateTime.day} == {expectedDay}`)
assert(dateTime.hour == expectedHour, `expected {dateTime.hour} == {expectedHour}`)
assert(dateTime.minute == expectedMinute, `expected {dateTime.minute} == {expectedMinute}`)
assert(dateTime.second == expectedSecond, `expected {dateTime.second} == {expectedSecond}`)