test(rust): prefer asserts to panics

This commit is contained in:
Amaan Qureshi 2025-09-22 21:07:33 -04:00 committed by Amaan Qureshi
parent b0cdab85fe
commit ce56465197
6 changed files with 42 additions and 43 deletions

View file

@ -481,7 +481,7 @@ fn test_highlighting_cancellation() {
// The initial `highlight` call, which eagerly parses the outer document, should not fail.
let mut highlighter = Highlighter::new();
let events = highlighter
let mut events = highlighter
.highlight(
&HTML_HIGHLIGHT,
source.as_bytes(),
@ -492,14 +492,18 @@ fn test_highlighting_cancellation() {
// Iterating the scopes should not panic. It should return an error once the
// cancellation is detected.
for event in events {
if let Err(e) = event {
assert_eq!(e, Error::Cancelled);
return;
let found_cancellation_error = events.any(|event| match event {
Ok(_) => false,
Err(Error::Cancelled) => true,
Err(Error::InvalidLanguage | Error::Unknown) => {
unreachable!("Unexpected error type while iterating events")
}
}
});
panic!("Expected an error while iterating highlighter");
assert!(
found_cancellation_error,
"Expected a cancellation error while iterating events"
);
}
#[test]

View file

@ -4186,12 +4186,9 @@ fn test_query_random() {
let pattern = pattern_ast.to_string();
let expected_matches = pattern_ast.matches_in_tree(&test_tree);
let query = match Query::new(&language, &pattern) {
Ok(query) => query,
Err(e) => {
panic!("failed to build query for pattern {pattern} - {e}. seed: {seed}");
}
};
let query = Query::new(&language, &pattern).unwrap_or_else(|e| {
panic!("failed to build query for pattern {pattern}. seed: {seed}\n{e}")
});
let mut actual_matches = Vec::new();
let mut match_iter = cursor.matches(
&query,

View file

@ -1,6 +1,7 @@
use std::{
ffi::{CStr, CString},
fs, ptr, slice, str,
sync::atomic::{AtomicUsize, Ordering},
};
use tree_sitter::Point;
@ -262,34 +263,34 @@ fn test_tags_ruby() {
#[test]
fn test_tags_cancellation() {
use std::sync::atomic::{AtomicUsize, Ordering};
allocations::record(|| {
// Large javascript document
let source = (0..500)
.map(|_| "/* hi */ class A { /* ok */ b() {} }\n")
.collect::<String>();
let source = "/* hi */ class A { /* ok */ b() {} }\n".repeat(500);
let cancellation_flag = AtomicUsize::new(0);
let language = get_language("javascript");
let tags_config = TagsConfiguration::new(language, JS_TAG_QUERY, "").unwrap();
let mut tag_context = TagsContext::new();
let tags = tag_context
.generate_tags(&tags_config, source.as_bytes(), Some(&cancellation_flag))
.unwrap();
for (i, tag) in tags.0.enumerate() {
let found_cancellation_error = tags.0.enumerate().any(|(i, tag)| {
if i == 150 {
cancellation_flag.store(1, Ordering::SeqCst);
}
if let Err(e) = tag {
assert_eq!(e, Error::Cancelled);
return;
match tag {
Ok(_) => false,
Err(Error::Cancelled) => true,
Err(e) => {
unreachable!("Unexpected error type while iterating tags: {e}")
}
}
}
});
panic!("Expected to halt tagging with an error");
assert!(
found_cancellation_error,
"Expected to halt tagging with a cancellation error"
);
});
}