fix(test): correctly handle assertions on empty lines

Also fixes assertions for captures spanning on multiple lines.

Co-authored-by: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
This commit is contained in:
Amaan Qureshi 2024-09-24 11:43:45 -04:00
parent cc4378e751
commit d3c262a104

View file

@ -170,15 +170,17 @@ pub fn parse_position_comments(
}
// Adjust the row number in each assertion's position to refer to the line of
// code *above* the assertion. There can be multiple lines of assertion comments,
// so the positions may have to be decremented by more than one row.
// code *above* the assertion. There can be multiple lines of assertion comments and empty
// lines, so the positions may have to be decremented by more than one row.
let mut i = 0;
let lines = source.lines_with_terminator().collect::<Vec<_>>();
for assertion in &mut result {
loop {
let on_assertion_line = assertion_ranges[i..]
.iter()
.any(|(start, _)| start.row == assertion.position.row);
if on_assertion_line {
let on_empty_line = lines[assertion.position.row].len() <= assertion.position.column;
if on_assertion_line || on_empty_line {
assertion.position.row -= 1;
} else {
while i < assertion_ranges.len()
@ -206,11 +208,10 @@ pub fn assert_expected_captures(
let contents = fs::read_to_string(path)?;
let pairs = parse_position_comments(parser, language, contents.as_bytes())?;
for assertion in &pairs {
if let Some(found) = &infos.iter().find(|p| {
assertion.position.row == p.start.row
&& assertion.position >= p.start
&& assertion.position < p.end
}) {
if let Some(found) = &infos
.iter()
.find(|p| assertion.position >= p.start && assertion.position < p.end)
{
if assertion.expected_capture_name != found.name && found.name != "name" {
return Err(anyhow!(
"Assertion failed: at {}, found {}, expected {}",