2024-06-05 18:38:19 +01:00
|
|
|
local process = require("@lune/process")
|
2024-06-05 19:18:23 +01:00
|
|
|
local regex = require("@lune/regex")
|
2024-06-05 18:38:19 +01:00
|
|
|
local roblox = require("@lune/roblox")
|
2023-05-14 21:16:58 +01:00
|
|
|
local stdio = require("@lune/stdio")
|
|
|
|
|
2024-06-05 18:38:19 +01:00
|
|
|
local function assertFormatting(errorMessage: string, formatted: string, expected: string)
|
|
|
|
if formatted ~= expected then
|
|
|
|
stdio.ewrite(string.format("%s\nExpected: %s\nGot: %s", errorMessage, expected, formatted))
|
|
|
|
process.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-05 19:18:23 +01:00
|
|
|
local function assertContains(errorMessage: string, haystack: string, needle: string)
|
|
|
|
if string.find(haystack, needle) == nil then
|
|
|
|
stdio.ewrite(string.format("%s\nHaystack: %s\nNeedle: %s", errorMessage, needle, haystack))
|
|
|
|
process.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-05 18:38:19 +01:00
|
|
|
assertFormatting(
|
|
|
|
"Should add a single space between arguments",
|
|
|
|
stdio.format("Hello", "world", "!"),
|
|
|
|
"Hello world !"
|
2023-01-21 06:37:31 +00:00
|
|
|
)
|
|
|
|
|
2024-06-05 18:38:19 +01:00
|
|
|
assertFormatting(
|
|
|
|
"Should format tables in a sorted manner",
|
|
|
|
stdio.format({ A = "A", B = "B", C = "C" }),
|
|
|
|
'{\n A = "A",\n B = "B",\n C = "C",\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertFormatting(
|
|
|
|
"Should format tables properly with single values",
|
|
|
|
stdio.format({ Hello = "World" }),
|
|
|
|
'{\n Hello = "World",\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertFormatting(
|
|
|
|
"Should format tables properly with multiple values",
|
|
|
|
stdio.format({ Hello = "World", Hello2 = "Value" }),
|
|
|
|
'{\n Hello = "World",\n Hello2 = "Value",\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertFormatting(
|
|
|
|
"Should simplify array-like tables and not format keys",
|
|
|
|
stdio.format({ "Hello", "World" }),
|
|
|
|
'{\n "Hello",\n "World",\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertFormatting(
|
|
|
|
"Should still format numeric keys for mixed tables",
|
|
|
|
stdio.format({ "Hello", "World", Hello = "World" }),
|
|
|
|
'{\n [1] = "Hello",\n [2] = "World",\n Hello = "World",\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
local userdatas = {
|
|
|
|
Foo = newproxy(false),
|
2024-06-05 19:18:23 +01:00
|
|
|
Bar = regex.new("TEST"),
|
|
|
|
Baz = (roblox :: any).Vector3.new(1, 2, 3),
|
2024-06-05 18:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assertFormatting(
|
2024-06-05 19:18:23 +01:00
|
|
|
"Should format userdatas as generic 'userdata' if unknown",
|
2024-06-05 18:38:19 +01:00
|
|
|
stdio.format(userdatas.Foo),
|
|
|
|
"<userdata>"
|
|
|
|
)
|
|
|
|
|
2024-06-05 19:18:23 +01:00
|
|
|
assertContains(
|
|
|
|
"Should format userdatas with their type if they have a __type metafield",
|
2024-06-05 18:38:19 +01:00
|
|
|
stdio.format(userdatas.Bar),
|
2024-06-05 19:18:23 +01:00
|
|
|
"Regex"
|
|
|
|
)
|
|
|
|
|
|
|
|
assertContains(
|
|
|
|
"Should format userdatas with their type even if they have a __tostring metamethod",
|
|
|
|
stdio.format(userdatas.Baz),
|
|
|
|
"Vector3"
|
|
|
|
)
|
|
|
|
|
|
|
|
assertContains(
|
|
|
|
"Should format userdatas with their tostringed value if they have a __tostring metamethod",
|
|
|
|
stdio.format(userdatas.Baz),
|
|
|
|
"1, 2, 3"
|
2024-06-05 18:38:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assertFormatting(
|
2024-06-05 19:18:23 +01:00
|
|
|
"Should format userdatas properly in tables",
|
2024-06-05 18:38:19 +01:00
|
|
|
stdio.format(userdatas),
|
2024-06-05 19:18:23 +01:00
|
|
|
"{\n Bar = <Regex(TEST)>,\n Baz = <Vector3(1, 2, 3)>,\n Foo = <userdata>,\n}"
|
2023-01-21 06:37:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
local nested = {
|
|
|
|
Oh = {
|
|
|
|
No = {
|
|
|
|
TooMuch = {
|
|
|
|
Nesting = {
|
|
|
|
"Will not print",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-05 19:18:23 +01:00
|
|
|
assertContains(
|
|
|
|
"Should print 4 levels of nested tables before cutting off",
|
|
|
|
stdio.format(nested),
|
|
|
|
"Nesting = { ... }"
|
2023-01-21 06:37:31 +00:00
|
|
|
)
|
2024-06-05 19:18:23 +01:00
|
|
|
|
|
|
|
local _, errorMessage = pcall(function()
|
|
|
|
local function innerInnerFn()
|
|
|
|
process.spawn("PROGRAM_THAT_DOES_NOT_EXIST")
|
|
|
|
end
|
|
|
|
local function innerFn()
|
|
|
|
innerInnerFn()
|
|
|
|
end
|
|
|
|
innerFn()
|
|
|
|
end)
|
|
|
|
|
|
|
|
stdio.ewrite(typeof(errorMessage))
|
|
|
|
|
|
|
|
assertContains("Should format errors similarly to userdata", stdio.format(errorMessage), "<LuaErr")
|
|
|
|
assertContains("Should format errors with stack begins", stdio.format(errorMessage), "Stack Begin")
|
|
|
|
assertContains("Should format errors with stack ends", stdio.format(errorMessage), "Stack End")
|