diff --git a/luneTypes.d.luau b/luneTypes.d.luau index 2cbcc61..e796a87 100644 --- a/luneTypes.d.luau +++ b/luneTypes.d.luau @@ -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) -> (), diff --git a/src/lib/lib.rs b/src/lib/lib.rs index f098c8c..cc69f2a 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -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", diff --git a/src/lib/utils/formatting.rs b/src/lib/utils/formatting.rs index 3afb521..b601482 100644 --- a/src/lib/utils/formatting.rs +++ b/src/lib/utils/formatting.rs @@ -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) diff --git a/src/tests/console/format.luau b/src/tests/console/format.luau new file mode 100644 index 0000000..b5957b2 --- /dev/null +++ b/src/tests/console/format.luau @@ -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" +) diff --git a/src/tests/console/set_color.luau b/src/tests/console/set_color.luau new file mode 100644 index 0000000..af0dca5 --- /dev/null +++ b/src/tests/console/set_color.luau @@ -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 diff --git a/src/tests/console/set_style.luau b/src/tests/console/set_style.luau new file mode 100644 index 0000000..78d65e1 --- /dev/null +++ b/src/tests/console/set_style.luau @@ -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 diff --git a/src/tests/process/args.luau b/src/tests/process/args.luau index a0c1dba..5ee7ef6 100644 --- a/src/tests/process/args.luau +++ b/src/tests/process/args.luau @@ -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