From c3e456f0ef11ed23e22981a28f147123ca58d482 Mon Sep 17 00:00:00 2001 From: Compey Date: Mon, 28 Aug 2023 17:07:49 +0530 Subject: [PATCH] chore(tests): finalize and implement missing tests --- src/lune/builtins/datetime/mod.rs | 13 ++++++++++++- tests/datetime/fromlocaltime.luau | 6 +++--- tests/datetime/fromuniversaltime.luau | 8 ++++---- tests/datetime/tolocaltime.luau | 26 +++++++++++++++++++++----- tests/datetime/touniversaltime.luau | 26 +++++++++++++++++++++----- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/lune/builtins/datetime/mod.rs b/src/lune/builtins/datetime/mod.rs index f81fa7d..320d239 100644 --- a/src/lune/builtins/datetime/mod.rs +++ b/src/lune/builtins/datetime/mod.rs @@ -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 { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult { @@ -176,6 +186,7 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder { .with_hour(t.get("hour")?) .with_minute(t.get("minute")?) .with_second(t.get("second")?) + .with_millisecond(t.get("millisecond")?) // TODO: millisecond support .build()), _ => Err(LuaError::external( diff --git a/tests/datetime/fromlocaltime.luau b/tests/datetime/fromlocaltime.luau index cf43df0..0260cfd 100644 --- a/tests/datetime/fromlocaltime.luau +++ b/tests/datetime/fromlocaltime.luau @@ -15,7 +15,7 @@ assert( ["second"] = 28, ["millisecond"] = 0, })["unixTimestamp"] == 1693049188, - "expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693049188s" + "expected DateTime.fromLocalTime() with DateTimeValues arg to return 1693049188s" ) assert( @@ -27,6 +27,6 @@ assert( ["minute"] = 56, ["second"] = 28, ["millisecond"] = 892, - })["unixTimestampMillis"] == 1693049188000, - "expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693049188000ms" + })["unixTimestampMillis"] == 1693049188892, + "expected DateTime.fromLocalTime() with DateTimeValues arg with millis to return 1693049188892ms" ) diff --git a/tests/datetime/fromuniversaltime.luau b/tests/datetime/fromuniversaltime.luau index 400413c..a56f8d4 100644 --- a/tests/datetime/fromuniversaltime.luau +++ b/tests/datetime/fromuniversaltime.luau @@ -2,7 +2,7 @@ local DateTime = require("@lune/datetime") assert( 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( @@ -15,7 +15,7 @@ assert( ["second"] = 28, ["millisecond"] = 0, })["unixTimestamp"] == 1693068988, - "expected DateTime.fromLocalTime() to return DateTime with timestamp to return 1693068988s" + "expected DateTime.fromUniversalTime() with DateTimeValues arg to return 1693068988s" ) assert( @@ -27,6 +27,6 @@ assert( ["minute"] = 56, ["second"] = 28, ["millisecond"] = 892, - })["unixTimestampMillis"] == 1693068988000, - "expected DateTime.fromLocalTime() with float millis arg timestamp to return 1693068988000ms" + })["unixTimestampMillis"] == 1693068988892, + "expected DateTime.fromUniversalTime() with DateTimeValues arg with millis to return 1693068988892ms" ) diff --git a/tests/datetime/tolocaltime.luau b/tests/datetime/tolocaltime.luau index 3be323a..6792db9 100644 --- a/tests/datetime/tolocaltime.luau +++ b/tests/datetime/tolocaltime.luau @@ -1,8 +1,24 @@ 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$")) +local dateTime = (DateTime.fromIsoDate("2023-08-27T05:54:19Z") :: DateTime.DateTime):toLocalTime() --- 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}`) diff --git a/tests/datetime/touniversaltime.luau b/tests/datetime/touniversaltime.luau index 3be323a..6792db9 100644 --- a/tests/datetime/touniversaltime.luau +++ b/tests/datetime/touniversaltime.luau @@ -1,8 +1,24 @@ 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$")) +local dateTime = (DateTime.fromIsoDate("2023-08-27T05:54:19Z") :: DateTime.DateTime):toLocalTime() --- 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}`)