diff options
Diffstat (limited to 'crates/ra_mbe/src/tests.rs')
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 44f381938..a7fcea0ac 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -427,22 +427,28 @@ MACRO_ITEMS@[0; 40) | |||
427 | ); | 427 | ); |
428 | } | 428 | } |
429 | 429 | ||
430 | #[test] | 430 | fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree { |
431 | fn test_expand_literals_to_token_tree() { | 431 | if let tt::TokenTree::Subtree(subtree) = tt { |
432 | fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree { | 432 | return &subtree; |
433 | if let tt::TokenTree::Subtree(subtree) = tt { | ||
434 | return &subtree; | ||
435 | } | ||
436 | unreachable!("It is not a subtree"); | ||
437 | } | 433 | } |
434 | unreachable!("It is not a subtree"); | ||
435 | } | ||
436 | fn to_literal(tt: &tt::TokenTree) -> &tt::Literal { | ||
437 | if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt { | ||
438 | return lit; | ||
439 | } | ||
440 | unreachable!("It is not a literal"); | ||
441 | } | ||
438 | 442 | ||
439 | fn to_literal(tt: &tt::TokenTree) -> &tt::Literal { | 443 | fn to_punct(tt: &tt::TokenTree) -> &tt::Punct { |
440 | if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt { | 444 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(lit)) = tt { |
441 | return lit; | 445 | return lit; |
442 | } | ||
443 | unreachable!("It is not a literal"); | ||
444 | } | 446 | } |
447 | unreachable!("It is not a Punct"); | ||
448 | } | ||
445 | 449 | ||
450 | #[test] | ||
451 | fn test_expand_literals_to_token_tree() { | ||
446 | let expansion = parse_macro( | 452 | let expansion = parse_macro( |
447 | r#" | 453 | r#" |
448 | macro_rules! literals { | 454 | macro_rules! literals { |
@@ -471,6 +477,22 @@ fn test_expand_literals_to_token_tree() { | |||
471 | } | 477 | } |
472 | 478 | ||
473 | #[test] | 479 | #[test] |
480 | fn test_attr_to_token_tree() { | ||
481 | let expansion = parse_to_token_tree_by_syntax( | ||
482 | r#" | ||
483 | #[derive(Copy)] | ||
484 | struct Foo; | ||
485 | "#, | ||
486 | ); | ||
487 | |||
488 | assert_eq!(to_punct(&expansion.token_trees[0]).char, '#'); | ||
489 | assert_eq!( | ||
490 | to_subtree(&expansion.token_trees[1]).delimiter_kind(), | ||
491 | Some(tt::DelimiterKind::Bracket) | ||
492 | ); | ||
493 | } | ||
494 | |||
495 | #[test] | ||
474 | fn test_two_idents() { | 496 | fn test_two_idents() { |
475 | parse_macro( | 497 | parse_macro( |
476 | r#" | 498 | r#" |
@@ -1427,8 +1449,8 @@ impl MacroFixture { | |||
1427 | let macro_invocation = | 1449 | let macro_invocation = |
1428 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 1450 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
1429 | 1451 | ||
1430 | let (invocation_tt, _) = | 1452 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()) |
1431 | ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); | 1453 | .ok_or_else(|| ExpandError::ConversionError)?; |
1432 | 1454 | ||
1433 | self.rules.expand(&invocation_tt).result() | 1455 | self.rules.expand(&invocation_tt).result() |
1434 | } | 1456 | } |
@@ -1517,6 +1539,16 @@ pub(crate) fn parse_macro(ra_fixture: &str) -> MacroFixture { | |||
1517 | MacroFixture { rules } | 1539 | MacroFixture { rules } |
1518 | } | 1540 | } |
1519 | 1541 | ||
1542 | pub(crate) fn parse_to_token_tree_by_syntax(ra_fixture: &str) -> tt::Subtree { | ||
1543 | let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap(); | ||
1544 | let tt = syntax_node_to_token_tree(source_file.syntax()).unwrap().0; | ||
1545 | |||
1546 | let parsed = parse_to_token_tree(ra_fixture).unwrap().0; | ||
1547 | assert_eq!(tt, parsed); | ||
1548 | |||
1549 | parsed | ||
1550 | } | ||
1551 | |||
1520 | fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String { | 1552 | fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String { |
1521 | let mut level = 0; | 1553 | let mut level = 0; |
1522 | let mut buf = String::new(); | 1554 | let mut buf = String::new(); |
@@ -1662,5 +1694,5 @@ fn test_expand_bad_literal() { | |||
1662 | macro_rules! foo { ($i:literal) => {}; } | 1694 | macro_rules! foo { ($i:literal) => {}; } |
1663 | "#, | 1695 | "#, |
1664 | ) | 1696 | ) |
1665 | .assert_expand_err(r#"foo!(&k");"#, &ExpandError::BindingError("".to_string())); | 1697 | .assert_expand_err(r#"foo!(&k");"#, &ExpandError::BindingError("".into())); |
1666 | } | 1698 | } |