mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Test TokenTree
s' equality modulo Punct
s' spacing
This commit is contained in:
parent
4f415fc348
commit
5b07061011
1 changed files with 33 additions and 8 deletions
|
@ -329,6 +329,31 @@ mod tests {
|
||||||
|
|
||||||
use super::reverse_fixups;
|
use super::reverse_fixups;
|
||||||
|
|
||||||
|
// The following three functions are only meant to check partial structural equivalence of
|
||||||
|
// `TokenTree`s, see the last assertion in `check()`.
|
||||||
|
fn check_leaf_eq(a: &tt::Leaf, b: &tt::Leaf) -> bool {
|
||||||
|
match (a, b) {
|
||||||
|
(tt::Leaf::Literal(a), tt::Leaf::Literal(b)) => a.text == b.text,
|
||||||
|
(tt::Leaf::Punct(a), tt::Leaf::Punct(b)) => a.char == b.char,
|
||||||
|
(tt::Leaf::Ident(a), tt::Leaf::Ident(b)) => a.text == b.text,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_subtree_eq(a: &tt::Subtree, b: &tt::Subtree) -> bool {
|
||||||
|
a.delimiter.map(|it| it.kind) == b.delimiter.map(|it| it.kind)
|
||||||
|
&& a.token_trees.len() == b.token_trees.len()
|
||||||
|
&& a.token_trees.iter().zip(&b.token_trees).all(|(a, b)| check_tt_eq(a, b))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_tt_eq(a: &tt::TokenTree, b: &tt::TokenTree) -> bool {
|
||||||
|
match (a, b) {
|
||||||
|
(tt::TokenTree::Leaf(a), tt::TokenTree::Leaf(b)) => check_leaf_eq(a, b),
|
||||||
|
(tt::TokenTree::Subtree(a), tt::TokenTree::Subtree(b)) => check_subtree_eq(a, b),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn check(ra_fixture: &str, mut expect: Expect) {
|
fn check(ra_fixture: &str, mut expect: Expect) {
|
||||||
let parsed = syntax::SourceFile::parse(ra_fixture);
|
let parsed = syntax::SourceFile::parse(ra_fixture);
|
||||||
|
@ -341,8 +366,7 @@ mod tests {
|
||||||
fixups.append,
|
fixups.append,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut actual = tt.to_string();
|
let actual = format!("{}\n", tt);
|
||||||
actual.push('\n');
|
|
||||||
|
|
||||||
expect.indent(false);
|
expect.indent(false);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
|
@ -358,9 +382,12 @@ mod tests {
|
||||||
reverse_fixups(&mut tt, &tmap, &fixups.undo_info);
|
reverse_fixups(&mut tt, &tmap, &fixups.undo_info);
|
||||||
|
|
||||||
// the fixed-up + reversed version should be equivalent to the original input
|
// the fixed-up + reversed version should be equivalent to the original input
|
||||||
// (but token IDs don't matter)
|
// modulo token IDs and `Punct`s' spacing.
|
||||||
let (original_as_tt, _) = mbe::syntax_node_to_token_tree(&parsed.syntax_node());
|
let (original_as_tt, _) = mbe::syntax_node_to_token_tree(&parsed.syntax_node());
|
||||||
assert_eq!(tt.to_string(), original_as_tt.to_string());
|
assert!(
|
||||||
|
check_subtree_eq(&tt, &original_as_tt),
|
||||||
|
"different token tree: {tt:?}, {original_as_tt:?}"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -483,7 +510,6 @@ fn foo () {a . __ra_fixup}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn incomplete_field_expr_2() {
|
fn incomplete_field_expr_2() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
|
@ -498,7 +524,6 @@ fn foo () {a .__ra_fixup ;}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn incomplete_field_expr_3() {
|
fn incomplete_field_expr_3() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue