Add simple test for syntax_node_to_token_tree_censored

This commit is contained in:
Lukas Wirth 2021-08-25 19:22:22 +02:00
parent d6134b6802
commit 1195cb50c2
4 changed files with 46 additions and 3 deletions

1
Cargo.lock generated
View file

@ -888,6 +888,7 @@ name = "mbe"
version = "0.0.0"
dependencies = [
"cov-mark",
"expect-test",
"log",
"parser",
"profile",

View file

@ -13,6 +13,7 @@ cov-mark = "2.0.0-pre.1"
rustc-hash = "1.1.0"
smallvec = "1.2.0"
log = "0.4.8"
expect-test = "1.1"
syntax = { path = "../syntax", version = "0.0.0" }
parser = { path = "../parser", version = "0.0.0" }

View file

@ -15,14 +15,14 @@ use tt::buffer::{Cursor, TokenBuffer};
use crate::{subtree_source::SubtreeTokenSource, tt_iter::TtIter};
use crate::{ExpandError, TokenMap};
/// Convert the syntax node to a `TokenTree` with the censored nodes excluded (what macro
/// Convert the syntax node to a `TokenTree` (what macro
/// will consume).
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
syntax_node_to_token_tree_censored(node, None)
}
/// Convert the syntax node to a `TokenTree` with the censored nodes excluded (what macro
/// will consume).
/// Convert the syntax node to a `TokenTree` (what macro will consume)
/// with the censored range excluded.
pub fn syntax_node_to_token_tree_censored(
node: &SyntaxNode,
censor: Option<TextRange>,

View file

@ -228,3 +228,44 @@ fn debug_dump_ignore_spaces(node: &syntax::SyntaxNode) -> String {
buf
}
#[test]
fn test_node_to_tt_censor() {
use syntax::ast::{AttrsOwner, ModuleItemOwner};
let source = r##"
#[attr0]
#[attr1]
#[attr2]
struct Struct {
field: ()
}
"##;
let source_file = ast::SourceFile::parse(&source).ok().unwrap();
let item = source_file.items().next().unwrap();
let attr = item.attrs().nth(1).unwrap();
let (tt, _) =
syntax_node_to_token_tree_censored(item.syntax(), Some(attr.syntax().text_range()));
expect_test::expect![[r##"# [attr0] # [attr2] struct Struct {field : ()}"##]]
.assert_eq(&tt.to_string());
let source = r##"
#[derive(Derive0)]
#[derive(Derive1)]
#[derive(Derive2)]
struct Struct {
field: ()
}
"##;
let source_file = ast::SourceFile::parse(&source).ok().unwrap();
let item = source_file.items().next().unwrap();
let attr = item.attrs().nth(1).unwrap();
let (tt, _) = syntax_node_to_token_tree_censored(
item.syntax(),
Some(attr.syntax().text_range().cover_offset(0.into())),
);
expect_test::expect![[r##"# [derive (Derive2)] struct Struct {field : ()}"##]]
.assert_eq(&tt.to_string());
}