From d3c262a1043480775e0283fd0fcb2ac876d01306 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Tue, 24 Sep 2024 11:43:45 -0400 Subject: [PATCH] fix(test): correctly handle assertions on empty lines Also fixes assertions for captures spanning on multiple lines. Co-authored-by: Thomas Vigouroux --- cli/src/query_testing.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cli/src/query_testing.rs b/cli/src/query_testing.rs index 87c08d23..2791d815 100644 --- a/cli/src/query_testing.rs +++ b/cli/src/query_testing.rs @@ -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::>(); 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 {}",