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"
);
});
}

View file

@ -591,14 +591,13 @@ mod test {
]);
grammar.external_tokens = vec![Variable::named("rule_1", Rule::non_terminal(1))];
match extract_tokens(grammar) {
Err(e) => {
assert_eq!(e.to_string(), "Rule 'rule_1' cannot be used as both an external token and a non-terminal rule");
}
_ => {
panic!("Expected an error but got no error");
}
}
let result = extract_tokens(grammar);
assert!(result.is_err(), "Expected an error but got no error");
let err = result.err().unwrap();
assert_eq!(
err.to_string(),
"Rule 'rule_1' cannot be used as both an external token and a non-terminal rule"
);
}
#[test]

View file

@ -279,10 +279,9 @@ mod tests {
fn test_grammar_with_undefined_symbols() {
let result = intern_symbols(&build_grammar(vec![Variable::named("x", Rule::named("y"))]));
match result {
Err(e) => assert_eq!(e.to_string(), "Undefined symbol `y`"),
_ => panic!("Expected an error but got none"),
}
assert!(result.is_err(), "Expected an error but got none");
let e = result.err().unwrap();
assert_eq!(e.to_string(), "Undefined symbol `y`");
}
fn build_grammar(variables: Vec<Variable>) -> InputGrammar {

View file

@ -549,10 +549,9 @@ mod tests {
..Default::default()
};
if let Err(error) = process_inlines(&grammar, &lexical_grammar) {
assert_eq!(error.to_string(), "Token `something` cannot be inlined");
} else {
panic!("expected an error, but got none");
}
let result = process_inlines(&grammar, &lexical_grammar);
assert!(result.is_err(), "expected an error, but got none");
let err = result.err().unwrap();
assert_eq!(err.to_string(), "Token `something` cannot be inlined",);
}
}