mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
Update expand macro tests
This commit is contained in:
parent
14bf5bb7ee
commit
bebbbb61a7
1 changed files with 119 additions and 130 deletions
|
@ -2,7 +2,9 @@ use hir::Semantics;
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::{find_node_at_offset, SyntaxRewriter},
|
algo::{find_node_at_offset, SyntaxRewriter},
|
||||||
ast, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, T,
|
ast, AstNode, NodeOrToken, SyntaxKind,
|
||||||
|
SyntaxKind::*,
|
||||||
|
SyntaxNode, WalkEvent, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::FilePosition;
|
use crate::FilePosition;
|
||||||
|
@ -65,8 +67,6 @@ fn expand_macro_recur(
|
||||||
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
||||||
// which are pretty unreadable at the moment.
|
// which are pretty unreadable at the moment.
|
||||||
fn insert_whitespaces(syn: SyntaxNode) -> String {
|
fn insert_whitespaces(syn: SyntaxNode) -> String {
|
||||||
use SyntaxKind::*;
|
|
||||||
|
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
let mut token_iter = syn
|
let mut token_iter = syn
|
||||||
.preorder_with_tokens()
|
.preorder_with_tokens()
|
||||||
|
@ -120,75 +120,68 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use insta::assert_snapshot;
|
use expect::{expect, Expect};
|
||||||
|
|
||||||
use crate::mock_analysis::analysis_and_position;
|
use crate::mock_analysis::analysis_and_position;
|
||||||
|
|
||||||
use super::*;
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
|
let (analysis, pos) = analysis_and_position(ra_fixture);
|
||||||
fn check_expand_macro(fixture: &str) -> ExpandedMacro {
|
let expansion = analysis.expand_macro(pos).unwrap().unwrap();
|
||||||
let (analysis, pos) = analysis_and_position(fixture);
|
let actual = format!("{}\n{}", expansion.name, expansion.expansion);
|
||||||
analysis.expand_macro(pos).unwrap().unwrap()
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_recursive_expansion() {
|
fn macro_expand_recursive_expansion() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
macro_rules! bar {
|
||||||
macro_rules! bar {
|
|
||||||
() => { fn b() {} }
|
() => { fn b() {} }
|
||||||
}
|
}
|
||||||
macro_rules! foo {
|
macro_rules! foo {
|
||||||
() => { bar!(); }
|
() => { bar!(); }
|
||||||
}
|
}
|
||||||
macro_rules! baz {
|
macro_rules! baz {
|
||||||
() => { foo!(); }
|
() => { foo!(); }
|
||||||
}
|
}
|
||||||
f<|>oo!();
|
f<|>oo!();
|
||||||
"#,
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
foo
|
||||||
|
fn b(){}
|
||||||
|
"#]],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(res.name, "foo");
|
|
||||||
assert_snapshot!(res.expansion, @r###"
|
|
||||||
fn b(){}
|
|
||||||
"###);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_multiple_lines() {
|
fn macro_expand_multiple_lines() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
macro_rules! foo {
|
||||||
macro_rules! foo {
|
|
||||||
() => {
|
() => {
|
||||||
fn some_thing() -> u32 {
|
fn some_thing() -> u32 {
|
||||||
let a = 0;
|
let a = 0;
|
||||||
a + 10
|
a + 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f<|>oo!();
|
f<|>oo!();
|
||||||
"#,
|
"#,
|
||||||
);
|
expect![[r#"
|
||||||
|
foo
|
||||||
assert_eq!(res.name, "foo");
|
fn some_thing() -> u32 {
|
||||||
assert_snapshot!(res.expansion, @r###"
|
|
||||||
fn some_thing() -> u32 {
|
|
||||||
let a = 0;
|
let a = 0;
|
||||||
a+10
|
a+10
|
||||||
}
|
}"#]],
|
||||||
"###);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_match_ast() {
|
fn macro_expand_match_ast() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
macro_rules! match_ast {
|
||||||
macro_rules! match_ast {
|
|
||||||
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
||||||
|
|
||||||
(match ($node:expr) {
|
(match ($node:expr) {
|
||||||
$( ast::$ast:ident($it:ident) => $res:block, )*
|
$( ast::$ast:ident($it:ident) => $res:block, )*
|
||||||
_ => $catch_all:expr $(,)?
|
_ => $catch_all:expr $(,)?
|
||||||
|
@ -196,9 +189,9 @@ fn some_thing() -> u32 {
|
||||||
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
||||||
{ $catch_all }
|
{ $catch_all }
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mat<|>ch_ast! {
|
mat<|>ch_ast! {
|
||||||
match container {
|
match container {
|
||||||
ast::TraitDef(it) => {},
|
ast::TraitDef(it) => {},
|
||||||
|
@ -206,13 +199,11 @@ fn some_thing() -> u32 {
|
||||||
_ => { continue },
|
_ => { continue },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
expect![[r#"
|
||||||
|
match_ast
|
||||||
assert_eq!(res.name, "match_ast");
|
{
|
||||||
assert_snapshot!(res.expansion, @r###"
|
|
||||||
{
|
|
||||||
if let Some(it) = ast::TraitDef::cast(container.clone()){}
|
if let Some(it) = ast::TraitDef::cast(container.clone()){}
|
||||||
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
|
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
|
||||||
else {
|
else {
|
||||||
|
@ -220,75 +211,73 @@ fn some_thing() -> u32 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}"#]],
|
||||||
"###);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_match_ast_inside_let_statement() {
|
fn macro_expand_match_ast_inside_let_statement() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
macro_rules! match_ast {
|
||||||
macro_rules! match_ast {
|
|
||||||
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
||||||
(match ($node:expr) {}) => {{}};
|
(match ($node:expr) {}) => {{}};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let p = f(|it| {
|
let p = f(|it| {
|
||||||
let res = mat<|>ch_ast! { match c {}};
|
let res = mat<|>ch_ast! { match c {}};
|
||||||
Some(res)
|
Some(res)
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
match_ast
|
||||||
|
{}
|
||||||
|
"#]],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(res.name, "match_ast");
|
|
||||||
assert_snapshot!(res.expansion, @r###"{}"###);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_inner_macro_fail_to_expand() {
|
fn macro_expand_inner_macro_fail_to_expand() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
macro_rules! bar {
|
||||||
macro_rules! bar {
|
|
||||||
(BAD) => {};
|
(BAD) => {};
|
||||||
}
|
}
|
||||||
macro_rules! foo {
|
macro_rules! foo {
|
||||||
() => {bar!()};
|
() => {bar!()};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let res = fo<|>o!();
|
let res = fo<|>o!();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
foo
|
||||||
|
"#]],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(res.name, "foo");
|
|
||||||
assert_snapshot!(res.expansion, @r###""###);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_expand_with_dollar_crate() {
|
fn macro_expand_with_dollar_crate() {
|
||||||
let res = check_expand_macro(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
#[macro_export]
|
||||||
#[macro_export]
|
macro_rules! bar {
|
||||||
macro_rules! bar {
|
|
||||||
() => {0};
|
() => {0};
|
||||||
}
|
}
|
||||||
macro_rules! foo {
|
macro_rules! foo {
|
||||||
() => {$crate::bar!()};
|
() => {$crate::bar!()};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let res = fo<|>o!();
|
let res = fo<|>o!();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
foo
|
||||||
|
0 "#]],
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(res.name, "foo");
|
|
||||||
assert_snapshot!(res.expansion, @r###"0"###);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue