Split logical lines tests into one test per assertion (#4457)

This commit is contained in:
Charlie Marsh 2023-05-16 13:40:39 -04:00 committed by GitHub
parent d9c3f8e249
commit e5101e8eac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 113 deletions

View file

@ -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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = 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);
}
}

View file

@ -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<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = LogicalLines::from_tokens(&lxr, &locator)
.into_iter()
.map(|line| line.text_trimmed())
.map(ToString::to_string)
.collect();
let expected: Vec<String> = expected.iter().map(ToString::to_string).collect();
assert_eq!(actual, expected);
}
}