chore(tests): include local formatTime test with custom locale

This commit is contained in:
Erica Marigold 2023-09-08 01:00:12 +05:30
parent 829494e304
commit a5efedcaad
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
2 changed files with 41 additions and 18 deletions

View file

@ -1,4 +1,5 @@
local DateTime = require("@lune/DateTime") local DateTime = require("@lune/DateTime")
local process = require("@lune/Process")
-- UTC Timezone -- UTC Timezone
assert( assert(
@ -10,20 +11,42 @@ assert(
assert( assert(
DateTime.fromUnixTimestamp(1693068988):formatTime("utc", "%A, %d %B %Y", "fr") DateTime.fromUnixTimestamp(1693068988):formatTime("utc", "%A, %d %B %Y", "fr")
== "samedi, 26 août 2023", == "samedi, 26 août 2023",
"expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr'" "expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr' (UTC)"
) )
local expectedTimeString = os.date("%Y-%m-%dT%H:%M:%SZ", 1694078954) local expectedTimeString = os.date("%Y-%m-%dT%H:%M:%SZ", 1694078954)
print(
expectedTimeString,
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
)
assert( assert(
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en") DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
== expectedTimeString, == expectedTimeString,
"invalid ISO 8601 formatting for DateTime.formatTime() (local)" "invalid ISO 8601 formatting for DateTime.formatTime() (local)"
) )
-- TODO: Locale tests, by using os.date() -- This test requires 'fr_FR.UTF-8 UTF-8' to be in /etc/locale.gen to pass
-- Local Timezone
assert(
DateTime.fromUnixTimestamp(1694078954):formatTime("local", "%Y-%m-%dT%H:%M:%SZ", "en")
== expectedTimeString,
"invalid ISO 8601 formatting for DateTime.formatTime() (local)"
)
local expectedLocalizedString
local dateCmd = process.spawn("bash", { "-c", "date +\"%A, %d %B %Y\" --date='@1693068988'" }, {
env = {
LC_ALL = "fr_FR.UTF-8 ",
},
})
if dateCmd.ok then
expectedLocalizedString = dateCmd.stdout
else
error("Failed to execute date command")
end
assert(
DateTime.fromUnixTimestamp(1693068988):formatTime("local", "%A, %d %B %Y", "fr")
== expectedLocalizedString:gsub("\n", ""),
"expected format specifier '%A, %d %B %Y' to return 'samedi, 26 août 2023' for locale 'fr' (local)"
)

View file

@ -42,11 +42,6 @@ export type DateTimeValues = {
millisecond: number, millisecond: number,
} }
export type DateTime = {
unixTimestamp: number,
unixTimestampMillis: number,
}
--[=[ --[=[
@class DateTime @class DateTime
@ -85,7 +80,10 @@ export type DateTime = {
DateTime.now():toUniversalTime() DateTime.now():toUniversalTime()
``` ```
]=] ]=]
local dateTime = {} local dateTime = {
unixTimestamp = 0,
unixTimestampMillis = 0,
}
--[=[ --[=[
@within DateTime @within DateTime
@ -94,7 +92,7 @@ local dateTime = {}
@return A DateTime instance @return A DateTime instance
]=] ]=]
function dateTime.now(): DateTime function dateTime.now(): typeof(dateTime)
return nil :: any return nil :: any
end end
@ -107,7 +105,7 @@ end
@param unixTimestamp The number of seconds or milliseconds (or both) since the Unix epoch. The fraction part of a float denotes the milliseconds. @param unixTimestamp The number of seconds or milliseconds (or both) since the Unix epoch. The fraction part of a float denotes the milliseconds.
@return A DateTime instance @return A DateTime instance
]=] ]=]
function dateTime.fromUnixTimestamp(unixTimestamp: number?): DateTime function dateTime.fromUnixTimestamp(unixTimestamp: number?): typeof(dateTime)
return nil :: any return nil :: any
end end
@ -126,7 +124,7 @@ end
@param dateTime Optional values for the dateTime instance, defaults to the current time @param dateTime Optional values for the dateTime instance, defaults to the current time
@return A DateTime instance @return A DateTime instance
]=] ]=]
function dateTime.fromUniversalTime(dateTime: DateTimeValues?): DateTime function dateTime.fromUniversalTime(dateTime: DateTimeValues?): typeof(dateTime)
return nil :: any return nil :: any
end end
@ -145,7 +143,7 @@ end
@param dateTime Optional values for the dateTime instance, defaults to the current time @param dateTime Optional values for the dateTime instance, defaults to the current time
@return A DateTime instance @return A DateTime instance
]=] ]=]
function dateTime.fromLocalTime(dateTime: DateTimeValues?): DateTime function dateTime.fromLocalTime(dateTime: DateTimeValues?): typeof(dateTime)
return nil :: any return nil :: any
end end
@ -162,7 +160,7 @@ end
@param isoDate An ISO 8601 formatted string @param isoDate An ISO 8601 formatted string
@return A DateTime instance @return A DateTime instance
]=] ]=]
function dateTime.fromIsoDate(iso_date: string): DateTime? function dateTime.fromIsoDate(iso_date: string): typeof(dateTime)?
return nil :: any return nil :: any
end end
@ -225,4 +223,6 @@ function dateTime:formatTime(timezone: Timezone, formatString: string, locale: L
return nil :: any return nil :: any
end end
export type DateTime = typeof(dateTime)
return dateTime return dateTime