Implement test suite for console

This commit is contained in:
Filip Tibell 2023-01-21 01:37:31 -05:00
parent b64990490d
commit 761580ad84
No known key found for this signature in database
7 changed files with 87 additions and 15 deletions

View file

@ -4,7 +4,7 @@ declare console: {
resetColor: () -> (),
setColor: (color: "black" | "red" | "green" | "yellow" | "blue" | "purple" | "cyan" | "white") -> (),
resetStyle: () -> (),
setStyle: (color: "bold" | "dim") -> (),
setStyle: (style: "bold" | "dim") -> (),
format: (...any) -> (string),
log: (...any) -> (),
info: (...any) -> (),

View file

@ -91,6 +91,9 @@ mod tests {
}
run_tests! {
console_format: "console/format",
console_set_color: "console/set_color",
console_set_style: "console/set_style",
process_args: "process/args",
process_env: "process/env",
process_spawn: "process/spawn",

View file

@ -9,19 +9,21 @@ const MAX_FORMAT_DEPTH: usize = 4;
const INDENT: &str = " ";
pub const COLOR_RESET: &str = "\x1B[0m";
pub const COLOR_BLACK: &str = "\x1B[30m";
pub const COLOR_RED: &str = "\x1B[31m";
pub const COLOR_GREEN: &str = "\x1B[32m";
pub const COLOR_YELLOW: &str = "\x1B[33m";
pub const COLOR_BLUE: &str = "\x1B[34m";
pub const COLOR_PURPLE: &str = "\x1B[35m";
pub const COLOR_CYAN: &str = "\x1B[36m";
pub const COLOR_WHITE: &str = "\x1B[37m";
// TODO: Use some crate for this instead
pub const STYLE_RESET: &str = "\x1B[22m";
pub const STYLE_BOLD: &str = "\x1B[1m";
pub const STYLE_DIM: &str = "\x1B[2m";
pub const COLOR_RESET: &str = if cfg!(test) { "" } else { "\x1B[0m" };
pub const COLOR_BLACK: &str = if cfg!(test) { "" } else { "\x1B[30m" };
pub const COLOR_RED: &str = if cfg!(test) { "" } else { "\x1B[31m" };
pub const COLOR_GREEN: &str = if cfg!(test) { "" } else { "\x1B[32m" };
pub const COLOR_YELLOW: &str = if cfg!(test) { "" } else { "\x1B[33m" };
pub const COLOR_BLUE: &str = if cfg!(test) { "" } else { "\x1B[34m" };
pub const COLOR_PURPLE: &str = if cfg!(test) { "" } else { "\x1B[35m" };
pub const COLOR_CYAN: &str = if cfg!(test) { "" } else { "\x1B[36m" };
pub const COLOR_WHITE: &str = if cfg!(test) { "" } else { "\x1B[37m" };
pub const STYLE_RESET: &str = if cfg!(test) { "" } else { "\x1B[22m" };
pub const STYLE_BOLD: &str = if cfg!(test) { "" } else { "\x1B[1m" };
pub const STYLE_DIM: &str = if cfg!(test) { "" } else { "\x1B[2m" };
pub fn flush_stdout() -> mlua::Result<()> {
io::stdout().flush().map_err(mlua::Error::external)

View file

@ -0,0 +1,26 @@
assert(
console.format("Hello", "world", "!") == "Hello world !",
"Format should add a single space between arguments"
)
assert(
console.format({ Hello = "World" }) == '{\n Hello = "World",\n}',
"Format should print out proper tables"
)
local nested = {
Oh = {
No = {
TooMuch = {
Nesting = {
"Will not print",
},
},
},
},
}
assert(
string.find(console.format(nested), "Nesting = { ... }", 1, true) ~= nil,
"Format should print 4 levels of nested tables before cutting off"
)

View file

@ -0,0 +1,15 @@
local VALID_COLORS = { "black", "red", "green", "yellow", "blue", "purple", "cyan", "white" }
local INVALID_COLORS = { "", "gray", "grass", "red?", "super red", " ", "none" }
for _, color in VALID_COLORS do
if not pcall(console.setColor, color :: any) then
error(string.format("Setting color failed for color '%s'", color))
end
end
for _, color in INVALID_COLORS do
if pcall(console.setColor, color :: any) then
console.resetColor()
error(string.format("Setting color should have failed for color '%s' but succeeded", color))
end
end

View file

@ -0,0 +1,15 @@
local VALID_STYLES = { "bold", "dim" }
local INVALID_STYLES = { "", "*bold*", "dimm", "megabright", "cheerful", "sad", " " }
for _, style in VALID_STYLES do
if not pcall(console.setStyle, style :: any) then
error(string.format("Setting style failed for style '%s'", style))
end
end
for _, style in INVALID_STYLES do
if pcall(console.setStyle, style :: any) then
console.resetStyle()
error(string.format("Setting style should have failed for style '%s' but succeeded", style))
end
end

View file

@ -3,10 +3,21 @@ assert(#process.args > 0, "No process arguments found")
assert(process.args[1] == "Foo", "Invalid first argument to process")
assert(process.args[2] == "Bar", "Invalid second argument to process")
local success = pcall(function()
local success, message = pcall(function()
process.args[1] = "abc"
end)
assert(not success, "Trying to set process arguments should throw an error")
assert(
success == false and type(message) == "string" and #message > 0,
"Trying to set process arguments should throw an error with a message"
)
assert(
string.find(message, "read") ~= nil,
"Setting process args error message should mention that they are read-only"
)
assert(
string.find(message, "only") ~= nil,
"Setting process args error message should mention that they are read-only"
)
local foundValue = false
for _, _ in process.args do