Skip to content

Commit 07877db

Browse files
committed
yeast: Test panicking translation
Also updates the remaining tests to use the new infallible interface.
1 parent 2a90757 commit 07877db

1 file changed

Lines changed: 47 additions & 29 deletions

File tree

‎shared/yeast/tests/test.rs‎

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ fn with_passthrough_rules<C: Clone + 'static>(mut rules: Vec<Rule<C>>) -> Vec<Ru
4242
|ast, output, captures, source_range, user_ctx, translator| {
4343
let mut children = Vec::new();
4444
for child in captures.get_all("children") {
45-
children.extend(translator.translate(ast, output, user_ctx, child)?);
45+
children.extend(translator.translate(ast, output, user_ctx, child));
4646
}
4747
let kind = output
4848
.id_for_node_kind("program")
49-
.ok_or("program kind is not registered")?;
49+
.expect("program kind is not registered");
5050
let fields = BTreeMap::from([(CHILD_FIELD, children)]);
5151
let program = output.create_node_with_range(
5252
kind,
@@ -55,7 +55,7 @@ fn with_passthrough_rules<C: Clone + 'static>(mut rules: Vec<Rule<C>>) -> Vec<Ru
5555
true,
5656
source_range,
5757
);
58-
Ok(vec![program])
58+
vec![program]
5959
},
6060
),
6161
);
@@ -341,7 +341,7 @@ fn test_query_match() {
341341
);
342342

343343
let mut captures = yeast::captures::Captures::new();
344-
let matched = query.do_match(&ast, ast.get_root(), &mut captures).unwrap();
344+
let matched = query.do_match(&ast, ast.get_root(), &mut captures);
345345
assert!(matched);
346346
assert!(captures.get_var("left").is_ok());
347347
assert!(captures.get_var("right").is_ok());
@@ -411,7 +411,7 @@ fn test_query_no_match() {
411411
);
412412

413413
let mut captures = yeast::captures::Captures::new();
414-
let matched = query.do_match(&ast, ast.get_root(), &mut captures).unwrap();
414+
let matched = query.do_match(&ast, ast.get_root(), &mut captures);
415415
assert!(!matched);
416416
}
417417

@@ -436,7 +436,7 @@ fn test_query_skips_extras_in_positional_match() {
436436
// the comment that sits between them.
437437
let query = yeast::query!((array (_) @a (_) @b));
438438
let mut captures = yeast::captures::Captures::new();
439-
let matched = query.do_match(&ast, array_id, &mut captures).unwrap();
439+
let matched = query.do_match(&ast, array_id, &mut captures);
440440
assert!(matched);
441441
assert_eq!(
442442
ast.get_node(captures.get_var("a").unwrap())
@@ -492,7 +492,7 @@ fn test_query_repeated_capture() {
492492
let assignment_id = cursor.node_id();
493493

494494
let mut captures = yeast::captures::Captures::new();
495-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
495+
let matched = query.do_match(&ast, assignment_id, &mut captures);
496496
assert!(matched);
497497
assert_eq!(captures.get_all("names").len(), 3);
498498
}
@@ -515,7 +515,7 @@ fn test_capture_unnamed_node_parenthesized() {
515515
let assignment_id = cursor.node_id();
516516

517517
let mut captures = yeast::captures::Captures::new();
518-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
518+
let matched = query.do_match(&ast, assignment_id, &mut captures);
519519
assert!(matched);
520520
let op_id = captures.get_var("op").unwrap();
521521
let op_node = ast.get_node(op_id).unwrap();
@@ -536,7 +536,7 @@ fn test_capture_bare_underscore_repeated() {
536536
let assignment_id = cursor.node_id();
537537

538538
let mut captures = yeast::captures::Captures::new();
539-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
539+
let matched = query.do_match(&ast, assignment_id, &mut captures);
540540
assert!(matched);
541541

542542
let all = captures.get_all("all");
@@ -563,7 +563,7 @@ fn test_capture_unnamed_node_bare_literal() {
563563
let assignment_id = cursor.node_id();
564564

565565
let mut captures = yeast::captures::Captures::new();
566-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
566+
let matched = query.do_match(&ast, assignment_id, &mut captures);
567567
assert!(matched);
568568
let op_id = captures.get_var("op").unwrap();
569569
let op_node = ast.get_node(op_id).unwrap();
@@ -587,9 +587,7 @@ fn test_bare_underscore_matches_unnamed() {
587587
// child is the unnamed `=`).
588588
let query_named = yeast::query!((assignment (_) @any));
589589
let mut captures = yeast::captures::Captures::new();
590-
let matched = query_named
591-
.do_match(&ast, assignment_id, &mut captures)
592-
.unwrap();
590+
let matched = query_named.do_match(&ast, assignment_id, &mut captures);
593591
assert!(
594592
!matched,
595593
"(_) should skip the unnamed `=` and fail to match"
@@ -599,9 +597,7 @@ fn test_bare_underscore_matches_unnamed() {
599597
// unnamed `=` token.
600598
let query_any = yeast::query!((assignment _ @any));
601599
let mut captures = yeast::captures::Captures::new();
602-
let matched = query_any
603-
.do_match(&ast, assignment_id, &mut captures)
604-
.unwrap();
600+
let matched = query_any.do_match(&ast, assignment_id, &mut captures);
605601
assert!(matched, "_ should match the unnamed `=`");
606602
let any_node = ast.get_node(captures.get_var("any").unwrap()).unwrap();
607603
assert_eq!(any_node.kind_name(), "=");
@@ -625,7 +621,7 @@ fn test_bare_forms_in_field_position() {
625621
// first child of `left` happens to be named.
626622
let query = yeast::query!((assignment left: _ @lhs));
627623
let mut captures = yeast::captures::Captures::new();
628-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
624+
let matched = query.do_match(&ast, assignment_id, &mut captures);
629625
assert!(matched);
630626
assert_eq!(
631627
ast.get_node(captures.get_var("lhs").unwrap())
@@ -637,7 +633,7 @@ fn test_bare_forms_in_field_position() {
637633
// Bare literal in field position. Equivalent to `("=") @op`.
638634
let query = yeast::query!((assignment child: "=" @op));
639635
let mut captures = yeast::captures::Captures::new();
640-
let matched = query.do_match(&ast, assignment_id, &mut captures).unwrap();
636+
let matched = query.do_match(&ast, assignment_id, &mut captures);
641637
assert!(matched);
642638
let op = ast.get_node(captures.get_var("op").unwrap()).unwrap();
643639
assert_eq!(op.kind_name(), "=");
@@ -665,7 +661,7 @@ fn test_forward_scan_finds_unnamed_token_late() {
665661

666662
let query = yeast::query!((do ("end") @kw));
667663
let mut captures = yeast::captures::Captures::new();
668-
let matched = query.do_match(&ast, do_id, &mut captures).unwrap();
664+
let matched = query.do_match(&ast, do_id, &mut captures);
669665
assert!(matched, "forward-scan should find the `end` keyword");
670666
let kw = ast.get_node(captures.get_var("kw").unwrap()).unwrap();
671667
assert_eq!(kw.kind_name(), "end");
@@ -690,7 +686,7 @@ fn test_forward_scan_preserves_order() {
690686

691687
let query = yeast::query!((do ("end") @first ("do") @second));
692688
let mut captures = yeast::captures::Captures::new();
693-
let matched = query.do_match(&ast, do_id, &mut captures).unwrap();
689+
let matched = query.do_match(&ast, do_id, &mut captures);
694690
assert!(!matched, "scan must not go backwards");
695691
}
696692

@@ -1389,16 +1385,38 @@ fn test_translation_phase_errors_when_no_rule_matches() {
13891385
let err = runner
13901386
.run("x = 1")
13911387
.expect_err("expected translation to error on unmatched node");
1392-
assert!(
1393-
err.contains("Phase `translate`"),
1394-
"error should name the phase, got: {err}"
1395-
);
13961388
assert!(
13971389
err.contains("no rule matched") && err.contains("integer"),
13981390
"error should describe the unmatched node kind, got: {err}"
13991391
);
14001392
}
14011393

1394+
#[test]
1395+
fn test_translation_phase_catches_panics() {
1396+
fn panic_during_translation() -> Vec<yeast::OutputId> {
1397+
panic!("deliberate translation panic")
1398+
}
1399+
1400+
let schema = yeast::node_types_yaml::schema_from_yaml(OUTPUT_SCHEMA_YAML).unwrap();
1401+
let rules: Vec<Rule> = vec![yeast::rule!(
1402+
(integer)
1403+
=>
1404+
integer {
1405+
panic_during_translation()
1406+
}
1407+
)];
1408+
let phases = [Phase::new("translate", rules)];
1409+
let runner: Runner = Runner::with_schema_no_language(&schema, &phases);
1410+
1411+
let err = runner
1412+
.run_from_ast(synthetic_integer_ast("1"))
1413+
.expect_err("expected translation panic to become an error");
1414+
assert_eq!(
1415+
err,
1416+
"YEAST translation panicked: deliberate translation panic"
1417+
);
1418+
}
1419+
14021420
#[test]
14031421
fn test_guard_runs_before_capture_translation() {
14041422
let lang: tree_sitter::Language = tree_sitter_ruby::LANGUAGE.into();
@@ -1448,7 +1466,7 @@ fn test_transform_context_propagates_to_nested_guard_without_mutating_caller() {
14481466
=>
14491467
program {
14501468
ctx.enabled = true;
1451-
let stmt = ctx.translate(stmt)?;
1469+
let stmt = ctx.translate(stmt);
14521470
tree!((program stmt: {stmt}))
14531471
}
14541472
),
@@ -1611,7 +1629,7 @@ fn test_raw_capture_marker() {
16111629
// string "x" (source_text inherits the input range), so the dump
16121630
// wouldn't change here. The companion test
16131631
// `test_raw_capture_marker_explicit_translate` exercises the
1614-
// stronger property that `ctx.translate(raw_lhs)?` succeeds and
1632+
// stronger property that `ctx.translate(raw_lhs)` succeeds and
16151633
// produces the translated `(identifier "ID")`.
16161634
assert_dump_eq(
16171635
&dump,
@@ -1646,7 +1664,7 @@ fn test_raw_capture_marker_explicit_translate() {
16461664
(assignment left: (_) @@raw_lhs right: (_) @rhs)
16471665
=>
16481666
call {
1649-
let translated_lhs = ctx.translate(raw_lhs)?;
1667+
let translated_lhs = ctx.translate(raw_lhs);
16501668
tree!((call
16511669
method: {translated_lhs}
16521670
receiver: {rhs}))
@@ -2144,10 +2162,10 @@ fn test_explicit_recursive_translation_keeps_nested_rule_location() {
21442162
(call method: (identifier) @@name)
21452163
=>
21462164
identifier {
2147-
ctx.translate(name)?
2165+
ctx.translate(name)
21482166
.into_iter()
21492167
.next()
2150-
.ok_or("identifier translation produced no result")?
2168+
.expect("identifier translation produced no result")
21512169
}
21522170
);
21532171
let translate_identifier: Rule = rule!((identifier) @@identifier => (identifier #{identifier}));

0 commit comments

Comments
 (0)