feat(cli): verify assertions for every carat in tests, not just the first one

Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
This commit is contained in:
Novus Nota 2024-11-29 05:25:49 +01:00 committed by GitHub
parent 473f0a1a4d
commit e445532a1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 40 additions and 19 deletions

View file

@ -62,6 +62,7 @@ pub struct CaptureInfo {
#[derive(Debug, PartialEq, Eq)]
pub struct Assertion {
pub position: Utf8Point,
pub length: usize,
pub negative: bool,
pub expected_capture_name: String,
}
@ -71,11 +72,13 @@ impl Assertion {
pub const fn new(
row: usize,
col: usize,
length: usize,
negative: bool,
expected_capture_name: String,
) -> Self {
Self {
position: Utf8Point::new(row, col),
length,
negative,
expected_capture_name,
}
@ -117,6 +120,7 @@ pub fn parse_position_comments(
let mut has_arrow = false;
let mut negative = false;
let mut arrow_end = 0;
let mut arrow_count = 1;
for (i, c) in text.char_indices() {
arrow_end = i + 1;
if c == '-' && has_left_caret {
@ -126,6 +130,14 @@ pub fn parse_position_comments(
if c == '^' {
has_arrow = true;
position.column += i;
// Continue counting remaining arrows and update their end column
for (_, c) in text[arrow_end..].char_indices() {
if c != '^' {
arrow_end += arrow_count - 1;
break;
}
arrow_count += 1;
}
break;
}
has_left_caret = c == '<';
@ -152,6 +164,7 @@ pub fn parse_position_comments(
assertion_ranges.push((node.start_position(), node.end_position()));
result.push(Assertion {
position: to_utf8_point(position, source),
length: arrow_count,
negative,
expected_capture_name: mat.as_str().to_string(),
});
@ -212,7 +225,8 @@ pub fn assert_expected_captures(
for assertion in &pairs {
if let Some(found) = &infos
.iter()
.find(|p| assertion.position >= p.start && assertion.position < p.end)
.find(|p| assertion.position >= p.start &&
(assertion.position.row < p.end.row || assertion.position.column + assertion.length - 1 < p.end.column))
{
if assertion.expected_capture_name != found.name && found.name != "name" {
return Err(anyhow!(
@ -224,9 +238,10 @@ pub fn assert_expected_captures(
}
} else {
return Err(anyhow!(
"Assertion failed: could not match {} at {}",
"Assertion failed: could not match {} at row {}, column {}",
assertion.expected_capture_name,
assertion.position
assertion.position.row,
assertion.position.column + assertion.length - 1,
));
}
}