diff options
author | Edwin Cheng <[email protected]> | 2019-12-13 19:39:15 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-12-18 03:20:23 +0000 |
commit | e16f3a5ee2f7df3f42827f3f279b5ed6774bde8e (patch) | |
tree | 1466ff09d968e384bc286555cfe1b98c29560dde | |
parent | 325532d11960733bebdd38b19a6f33a891872803 (diff) |
Add test for token map
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 6bcfedcac..ae7f3dfd4 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -89,6 +89,32 @@ macro_rules! foobar { | |||
89 | } | 89 | } |
90 | 90 | ||
91 | #[test] | 91 | #[test] |
92 | fn test_token_map() { | ||
93 | use ra_parser::SyntaxKind::*; | ||
94 | use ra_syntax::T; | ||
95 | |||
96 | let macro_definition = r#" | ||
97 | macro_rules! foobar { | ||
98 | ($e:ident) => { fn $e() {} } | ||
99 | } | ||
100 | "#; | ||
101 | let rules = create_rules(macro_definition); | ||
102 | let (expansion, (token_map, content)) = expand_and_map(&rules, "foobar!(baz);"); | ||
103 | |||
104 | let get_text = |id, kind| -> String { | ||
105 | content[token_map.range_by_token(id).unwrap().range(kind).unwrap()].to_string() | ||
106 | }; | ||
107 | |||
108 | assert_eq!(expansion.token_trees.len(), 4); | ||
109 | // {($e:ident) => { fn $e() {} }} | ||
110 | // 012345 67 8 9 T12 3 | ||
111 | |||
112 | assert_eq!(get_text(tt::TokenId(9), IDENT), "fn"); | ||
113 | assert_eq!(get_text(tt::TokenId(12), T!['(']), "("); | ||
114 | assert_eq!(get_text(tt::TokenId(13), T!['{']), "{"); | ||
115 | } | ||
116 | |||
117 | #[test] | ||
92 | fn test_convert_tt() { | 118 | fn test_convert_tt() { |
93 | let macro_definition = r#" | 119 | let macro_definition = r#" |
94 | macro_rules! impl_froms { | 120 | macro_rules! impl_froms { |
@@ -1443,6 +1469,23 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { | |||
1443 | rules.expand(&invocation_tt).unwrap() | 1469 | rules.expand(&invocation_tt).unwrap() |
1444 | } | 1470 | } |
1445 | 1471 | ||
1472 | pub(crate) fn expand_and_map( | ||
1473 | rules: &MacroRules, | ||
1474 | invocation: &str, | ||
1475 | ) -> (tt::Subtree, (TokenMap, String)) { | ||
1476 | let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); | ||
1477 | let macro_invocation = | ||
1478 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | ||
1479 | |||
1480 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); | ||
1481 | let expanded = rules.expand(&invocation_tt).unwrap(); | ||
1482 | |||
1483 | let (node, expanded_token_tree) = | ||
1484 | token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap(); | ||
1485 | |||
1486 | (expanded, (expanded_token_tree, node.syntax_node().to_string())) | ||
1487 | } | ||
1488 | |||
1446 | pub(crate) enum MacroKind { | 1489 | pub(crate) enum MacroKind { |
1447 | Items, | 1490 | Items, |
1448 | Stmts, | 1491 | Stmts, |