Skip to content

Commit 5b07061

Browse files
committed
Test TokenTrees' equality modulo Puncts' spacing
1 parent 4f415fc commit 5b07061

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

crates/hir-expand/src/fixup.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,31 @@ mod tests {
329329

330330
use super::reverse_fixups;
331331

332+
// The following three functions are only meant to check partial structural equivalence of
333+
// `TokenTree`s, see the last assertion in `check()`.
334+
fn check_leaf_eq(a: &tt::Leaf, b: &tt::Leaf) -> bool {
335+
match (a, b) {
336+
(tt::Leaf::Literal(a), tt::Leaf::Literal(b)) => a.text == b.text,
337+
(tt::Leaf::Punct(a), tt::Leaf::Punct(b)) => a.char == b.char,
338+
(tt::Leaf::Ident(a), tt::Leaf::Ident(b)) => a.text == b.text,
339+
_ => false,
340+
}
341+
}
342+
343+
fn check_subtree_eq(a: &tt::Subtree, b: &tt::Subtree) -> bool {
344+
a.delimiter.map(|it| it.kind) == b.delimiter.map(|it| it.kind)
345+
&& a.token_trees.len() == b.token_trees.len()
346+
&& a.token_trees.iter().zip(&b.token_trees).all(|(a, b)| check_tt_eq(a, b))
347+
}
348+
349+
fn check_tt_eq(a: &tt::TokenTree, b: &tt::TokenTree) -> bool {
350+
match (a, b) {
351+
(tt::TokenTree::Leaf(a), tt::TokenTree::Leaf(b)) => check_leaf_eq(a, b),
352+
(tt::TokenTree::Subtree(a), tt::TokenTree::Subtree(b)) => check_subtree_eq(a, b),
353+
_ => false,
354+
}
355+
}
356+
332357
#[track_caller]
333358
fn check(ra_fixture: &str, mut expect: Expect) {
334359
let parsed = syntax::SourceFile::parse(ra_fixture);
@@ -341,8 +366,7 @@ mod tests {
341366
fixups.append,
342367
);
343368

344-
let mut actual = tt.to_string();
345-
actual.push('\n');
369+
let actual = format!("{}\n", tt);
346370

347371
expect.indent(false);
348372
expect.assert_eq(&actual);
@@ -358,9 +382,12 @@ mod tests {
358382
reverse_fixups(&mut tt, &tmap, &fixups.undo_info);
359383

360384
// the fixed-up + reversed version should be equivalent to the original input
361-
// (but token IDs don't matter)
385+
// modulo token IDs and `Punct`s' spacing.
362386
let (original_as_tt, _) = mbe::syntax_node_to_token_tree(&parsed.syntax_node());
363-
assert_eq!(tt.to_string(), original_as_tt.to_string());
387+
assert!(
388+
check_subtree_eq(&tt, &original_as_tt),
389+
"different token tree: {tt:?}, {original_as_tt:?}"
390+
);
364391
}
365392

366393
#[test]
@@ -483,7 +510,6 @@ fn foo () {a . __ra_fixup}
483510
}
484511

485512
#[test]
486-
#[ignore]
487513
fn incomplete_field_expr_2() {
488514
check(
489515
r#"
@@ -492,13 +518,12 @@ fn foo() {
492518
}
493519
"#,
494520
expect![[r#"
495-
fn foo () {a .__ra_fixup ;}
521+
fn foo () {a . __ra_fixup ;}
496522
"#]],
497523
)
498524
}
499525

500526
#[test]
501-
#[ignore]
502527
fn incomplete_field_expr_3() {
503528
check(
504529
r#"
@@ -508,7 +533,7 @@ fn foo() {
508533
}
509534
"#,
510535
expect![[r#"
511-
fn foo () {a .__ra_fixup ; bar () ;}
536+
fn foo () {a . __ra_fixup ; bar () ;}
512537
"#]],
513538
)
514539
}

0 commit comments

Comments
 (0)