diff --git a/crates/ruff/src/checkers/logical_lines.rs b/crates/ruff/src/checkers/logical_lines.rs index 6e32b87306..81fada3f46 100644 --- a/crates/ruff/src/checkers/logical_lines.rs +++ b/crates/ruff/src/checkers/logical_lines.rs @@ -154,116 +154,3 @@ impl<'a> LogicalLinesContext<'a> { } } } - -#[cfg(test)] -mod tests { - use rustpython_parser::lexer::LexResult; - use rustpython_parser::{lexer, Mode}; - - use crate::rules::pycodestyle::rules::logical_lines::LogicalLines; - use ruff_python_ast::source_code::Locator; - - #[test] - fn split_logical_lines() { - let contents = r#" -x = 1 -y = 2 -z = x + 1"# - .trim(); - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec![ - "x = 1".to_string(), - "y = 2".to_string(), - "z = x + 1".to_string(), - ]; - assert_eq!(actual, expected); - - let contents = r#" -x = [ - 1, - 2, - 3, -] -y = 2 -z = x + 1"# - .trim(); - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec![ - "x = [\n 1,\n 2,\n 3,\n]".to_string(), - "y = 2".to_string(), - "z = x + 1".to_string(), - ]; - assert_eq!(actual, expected); - - let contents = "x = 'abc'"; - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec!["x = 'abc'".to_string()]; - assert_eq!(actual, expected); - - let contents = r#" -def f(): - x = 1 -f()"# - .trim(); - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec!["def f():", "x = 1", "f()"]; - assert_eq!(actual, expected); - - let contents = r#" -def f(): - """Docstring goes here.""" - # Comment goes here. - x = 1 -f()"# - .trim(); - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec![ - "def f():", - "\"\"\"Docstring goes here.\"\"\"", - "", - "x = 1", - "f()", - ]; - assert_eq!(actual, expected); - - let contents = r#" -if False: - - print() -"# - .trim(); - let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); - let locator = Locator::new(contents); - let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) - .into_iter() - .map(|line| line.text_trimmed().to_string()) - .collect(); - let expected = vec!["if False:", "print()", ""]; - assert_eq!(actual, expected); - } -} diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs index f6f4e94d04..f8f153132c 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs @@ -535,3 +535,103 @@ struct Line { tokens_start: u32, tokens_end: u32, } + +#[cfg(test)] +mod tests { + use rustpython_parser::lexer::LexResult; + use rustpython_parser::{lexer, Mode}; + + use ruff_python_ast::source_code::Locator; + + use super::LogicalLines; + + #[test] + fn multi_line() { + assert_logical_lines( + r#" +x = 1 +y = 2 +z = x + 1"# + .trim(), + &["x = 1", "y = 2", "z = x + 1"], + ); + } + + #[test] + fn indented() { + assert_logical_lines( + r#" +x = [ + 1, + 2, + 3, +] +y = 2 +z = x + 1"# + .trim(), + &["x = [\n 1,\n 2,\n 3,\n]", "y = 2", "z = x + 1"], + ); + } + + #[test] + fn string_assignment() { + assert_logical_lines("x = 'abc'".trim(), &["x = 'abc'"]); + } + + #[test] + fn function_definition() { + assert_logical_lines( + r#" +def f(): + x = 1 +f()"# + .trim(), + &["def f():", "x = 1", "f()"], + ); + } + + #[test] + fn trivia() { + assert_logical_lines( + r#" +def f(): + """Docstring goes here.""" + # Comment goes here. + x = 1 +f()"# + .trim(), + &[ + "def f():", + "\"\"\"Docstring goes here.\"\"\"", + "", + "x = 1", + "f()", + ], + ); + } + + #[test] + fn empty_line() { + assert_logical_lines( + r#" +if False: + + print() +"# + .trim(), + &["if False:", "print()", ""], + ); + } + + fn assert_logical_lines(contents: &str, expected: &[&str]) { + let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); + let locator = Locator::new(contents); + let actual: Vec = LogicalLines::from_tokens(&lxr, &locator) + .into_iter() + .map(|line| line.text_trimmed()) + .map(ToString::to_string) + .collect(); + let expected: Vec = expected.iter().map(ToString::to_string).collect(); + assert_eq!(actual, expected); + } +}