From 0e8abbeb79759000b1dd993087c0965d56a2757d Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sat, 23 Dec 2023 15:48:01 +0300 Subject: [PATCH] Add tests for new space after function name option --- src/editorconfig.rs | 35 +++++++ tests/test_spaces_after_functions.rs | 134 +++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 tests/test_spaces_after_functions.rs diff --git a/src/editorconfig.rs b/src/editorconfig.rs index c215d289..cdd2320a 100644 --- a/src/editorconfig.rs +++ b/src/editorconfig.rs @@ -333,6 +333,41 @@ mod tests { assert_eq!(config.call_parentheses, CallParenType::None); } + #[test] + fn test_space_after_functions_always() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("space_after_functions", "Always"); + let config = Config::from(&properties); + assert_eq!(config.space_after_functions, SpaceAfterFunctions::Always); + } + + #[test] + fn test_space_after_functions_definitions() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("space_after_functions", "Definitions"); + let config = Config::from(&properties); + assert_eq!( + config.space_after_functions, + SpaceAfterFunctions::Definitions + ); + } + + #[test] + fn test_space_after_functions_calls() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("space_after_functions", "Calls"); + let config = Config::from(&properties); + assert_eq!(config.space_after_functions, SpaceAfterFunctions::Calls); + } + + #[test] + fn test_space_after_functions_never() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("space_after_functions", "Never"); + let config = Config::from(&properties); + assert_eq!(config.space_after_functions, SpaceAfterFunctions::Never); + } + #[test] fn test_collapse_simple_statement_never() { let mut properties = Properties::new(); diff --git a/tests/test_spaces_after_functions.rs b/tests/test_spaces_after_functions.rs new file mode 100644 index 00000000..c332f3b5 --- /dev/null +++ b/tests/test_spaces_after_functions.rs @@ -0,0 +1,134 @@ +use stylua_lib::{format_code, Config, OutputVerification, SpaceAfterFunctions}; + +fn format(input: &str, space_after_functions: SpaceAfterFunctions) -> String { + format_code( + input, + Config { + space_after_functions, + ..Config::default() + }, + None, + OutputVerification::None, + ) + .unwrap() +} + +const STARTINGCODE: &str = r###" +local foo = function() end +local function bar () end +function baz() end +a = {} +function a:b () end +function a.c () end +function qiz () return function () end end +foo() +bar () +baz() +a:b () +a.c () +qiz()() +"###; + +#[test] +fn test_never_space_after_functions() { + insta::assert_snapshot!( + format(STARTINGCODE, + SpaceAfterFunctions::Never + ), + @r###" +local foo = function() end +local function bar() end +function baz() end +a = {} +function a:b() end +function a.c() end +function qiz() + return function() end +end +foo() +bar() +baz() +a:b() +a.c() +qiz()() + "### + ); +} + +#[test] +fn test_space_after_function_definitions() { + insta::assert_snapshot!( + format(STARTINGCODE, + SpaceAfterFunctions::Definitions + ), + @r###" +local foo = function () end +local function bar () end +function baz () end +a = {} +function a:b () end +function a.c () end +function qiz () + return function () end +end +foo() +bar() +baz() +a:b() +a.c() +qiz()() + "### + ); +} + +#[test] +fn test_space_after_function_calls() { + insta::assert_snapshot!( + format(STARTINGCODE, + SpaceAfterFunctions::Calls + ), + @r###" +local foo = function() end +local function bar() end +function baz() end +a = {} +function a:b() end +function a.c() end +function qiz() + return function() end +end +foo () +bar () +baz () +a:b () +a.c () +qiz () () + "### + ); +} + +#[test] +fn test_always_space_after_functions() { + insta::assert_snapshot!( + format(STARTINGCODE, + SpaceAfterFunctions::Always + ), + @r###" +local foo = function () end +local function bar () end +function baz () end +a = {} +function a:b () end +function a.c () end +function qiz () + return function () end +end +foo () +bar () +baz () +a:b () +a.c () +qiz () () + "### + ); +}