From ce5646519786f3ad5e616dbeea150ba19b61e2b3 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 22 Sep 2025 21:07:33 -0400 Subject: [PATCH] test(rust): prefer `assert`s to `panic`s --- crates/cli/src/tests/highlight_test.rs | 18 ++++++++----- crates/cli/src/tests/query_test.rs | 9 +++---- crates/cli/src/tests/tags_test.rs | 27 ++++++++++--------- .../src/prepare_grammar/extract_tokens.rs | 15 +++++------ .../src/prepare_grammar/intern_symbols.rs | 7 +++-- .../src/prepare_grammar/process_inlines.rs | 9 +++---- 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/crates/cli/src/tests/highlight_test.rs b/crates/cli/src/tests/highlight_test.rs index 76c43f43..88209bfe 100644 --- a/crates/cli/src/tests/highlight_test.rs +++ b/crates/cli/src/tests/highlight_test.rs @@ -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] diff --git a/crates/cli/src/tests/query_test.rs b/crates/cli/src/tests/query_test.rs index 835138ce..2fff0525 100644 --- a/crates/cli/src/tests/query_test.rs +++ b/crates/cli/src/tests/query_test.rs @@ -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, diff --git a/crates/cli/src/tests/tags_test.rs b/crates/cli/src/tests/tags_test.rs index 232a01dc..0c9f7111 100644 --- a/crates/cli/src/tests/tags_test.rs +++ b/crates/cli/src/tests/tags_test.rs @@ -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::(); - + 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" + ); }); } diff --git a/crates/generate/src/prepare_grammar/extract_tokens.rs b/crates/generate/src/prepare_grammar/extract_tokens.rs index cb40ce5a..b74a8e84 100644 --- a/crates/generate/src/prepare_grammar/extract_tokens.rs +++ b/crates/generate/src/prepare_grammar/extract_tokens.rs @@ -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] diff --git a/crates/generate/src/prepare_grammar/intern_symbols.rs b/crates/generate/src/prepare_grammar/intern_symbols.rs index 010967b8..241e6e99 100644 --- a/crates/generate/src/prepare_grammar/intern_symbols.rs +++ b/crates/generate/src/prepare_grammar/intern_symbols.rs @@ -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) -> InputGrammar { diff --git a/crates/generate/src/prepare_grammar/process_inlines.rs b/crates/generate/src/prepare_grammar/process_inlines.rs index 085e6732..f20e182d 100644 --- a/crates/generate/src/prepare_grammar/process_inlines.rs +++ b/crates/generate/src/prepare_grammar/process_inlines.rs @@ -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",); } }