Skip to content

Commit e5acf65

Browse files
committed
move tests
1 parent 5f8b708 commit e5acf65

File tree

2 files changed

+60
-143
lines changed

2 files changed

+60
-143
lines changed

crates/hir_def/src/macro_expansion_tests/builtin.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,63 @@ unsafe {
270270
"##]],
271271
);
272272
}
273+
274+
#[test]
275+
fn test_include_bytes_expand() {
276+
check(
277+
r#"
278+
#[rustc_builtin_macro]
279+
macro_rules! include_bytes {
280+
($file:expr) => {{ /* compiler built-in */ }};
281+
($file:expr,) => {{ /* compiler built-in */ }};
282+
}
283+
284+
fn main() { include_bytes("foo"); }
285+
"#,
286+
expect![[r##"
287+
#[rustc_builtin_macro]
288+
macro_rules! include_bytes {
289+
($file:expr) => {{ /* compiler built-in */ }};
290+
($file:expr,) => {{ /* compiler built-in */ }};
291+
}
292+
293+
fn main() { include_bytes("foo"); }
294+
"##]],
295+
);
296+
}
297+
298+
#[test]
299+
fn test_concat_expand() {
300+
check(
301+
r##"
302+
#[rustc_builtin_macro]
303+
macro_rules! concat {}
304+
305+
fn main() { concat!("foo", "r", 0, r#"bar"#, "\n", false); }
306+
"##,
307+
expect![[r##"
308+
#[rustc_builtin_macro]
309+
macro_rules! concat {}
310+
311+
fn main() { "foor0bar\nfalse"; }
312+
"##]],
313+
);
314+
}
315+
316+
#[test]
317+
fn test_concat_idents_expand() {
318+
check(
319+
r##"
320+
#[rustc_builtin_macro]
321+
macro_rules! concat_idents {}
322+
323+
fn main() { concat_idents!(foo, bar); }
324+
"##,
325+
expect![[r##"
326+
#[rustc_builtin_macro]
327+
macro_rules! concat_idents {}
328+
329+
fn main() { foobar; }
330+
"##]],
331+
);
332+
}

crates/hir_expand/src/builtin_macro.rs

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -565,146 +565,3 @@ fn option_env_expand(
565565

566566
ExpandResult::ok(Some(ExpandedEager::new(expanded)))
567567
}
568-
569-
#[cfg(test)]
570-
mod tests {
571-
use std::sync::Arc;
572-
573-
use base_db::{fixture::WithFixture, SourceDatabase};
574-
use expect_test::{expect, Expect};
575-
use syntax::ast::HasName;
576-
577-
use crate::{
578-
name::AsName, test_db::TestDB, AstNode, EagerCallInfo, ExpandTo, MacroCallId,
579-
MacroCallKind, MacroCallLoc,
580-
};
581-
582-
use super::*;
583-
584-
fn expand_builtin_macro(ra_fixture: &str) -> String {
585-
let (db, file_id) = TestDB::with_single_file(ra_fixture);
586-
let parsed = db.parse(file_id);
587-
let mut macro_rules: Vec<_> =
588-
parsed.syntax_node().descendants().filter_map(ast::MacroRules::cast).collect();
589-
let mut macro_calls: Vec<_> =
590-
parsed.syntax_node().descendants().filter_map(ast::MacroCall::cast).collect();
591-
592-
let ast_id_map = db.ast_id_map(file_id.into());
593-
594-
assert_eq!(macro_rules.len(), 1, "test must contain exactly 1 `macro_rules!`");
595-
assert_eq!(macro_calls.len(), 1, "test must contain exactly 1 macro call");
596-
let macro_rules = ast::Macro::from(macro_rules.pop().unwrap());
597-
let macro_call = macro_calls.pop().unwrap();
598-
599-
let expander = find_by_name(&macro_rules.name().unwrap().as_name()).unwrap();
600-
let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules));
601-
602-
let krate = CrateId(0);
603-
let file_id = match expander {
604-
Either::Left(expander) => {
605-
// the first one should be a macro_rules
606-
let def = MacroDefId {
607-
krate: CrateId(0),
608-
kind: MacroDefKind::BuiltIn(expander, ast_id),
609-
local_inner: false,
610-
};
611-
612-
let loc = MacroCallLoc {
613-
def,
614-
krate,
615-
eager: None,
616-
kind: MacroCallKind::FnLike {
617-
ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
618-
expand_to: ExpandTo::Expr,
619-
},
620-
};
621-
622-
let id: MacroCallId = db.intern_macro(loc);
623-
id.as_file()
624-
}
625-
Either::Right(expander) => {
626-
// the first one should be a macro_rules
627-
let def = MacroDefId {
628-
krate,
629-
kind: MacroDefKind::BuiltInEager(expander, ast_id),
630-
local_inner: false,
631-
};
632-
633-
let args = macro_call.token_tree().unwrap();
634-
let parsed_args = mbe::syntax_node_to_token_tree(args.syntax()).0;
635-
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
636-
637-
let arg_id = db.intern_macro(MacroCallLoc {
638-
def,
639-
krate,
640-
eager: Some(EagerCallInfo {
641-
arg_or_expansion: Arc::new(parsed_args.clone()),
642-
included_file: None,
643-
}),
644-
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
645-
});
646-
647-
let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap();
648-
let expand_to = crate::ExpandTo::from_call_site(&macro_call);
649-
let loc = MacroCallLoc {
650-
def,
651-
krate,
652-
eager: Some(EagerCallInfo {
653-
arg_or_expansion: Arc::new(expanded.subtree),
654-
included_file: expanded.included_file,
655-
}),
656-
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
657-
};
658-
659-
let id: MacroCallId = db.intern_macro(loc);
660-
id.as_file()
661-
}
662-
};
663-
664-
db.parse_or_expand(file_id).unwrap().to_string()
665-
}
666-
667-
fn check_expansion(ra_fixture: &str, expect: Expect) {
668-
let expansion = expand_builtin_macro(ra_fixture);
669-
expect.assert_eq(&expansion);
670-
}
671-
672-
#[test]
673-
fn test_include_bytes_expand() {
674-
check_expansion(
675-
r#"
676-
#[rustc_builtin_macro]
677-
macro_rules! include_bytes {
678-
($file:expr) => {{ /* compiler built-in */ }};
679-
($file:expr,) => {{ /* compiler built-in */ }};
680-
}
681-
include_bytes("foo");
682-
"#,
683-
expect![[r#"b"""#]],
684-
);
685-
}
686-
687-
#[test]
688-
fn test_concat_expand() {
689-
check_expansion(
690-
r##"
691-
#[rustc_builtin_macro]
692-
macro_rules! concat {}
693-
concat!("foo", "r", 0, r#"bar"#, "\n", false);
694-
"##,
695-
expect![[r#""foor0bar\nfalse""#]],
696-
);
697-
}
698-
699-
#[test]
700-
fn test_concat_idents_expand() {
701-
check_expansion(
702-
r##"
703-
#[rustc_builtin_macro]
704-
macro_rules! concat_idents {}
705-
concat_idents!(foo, bar);
706-
"##,
707-
expect![[r#"foobar"#]],
708-
);
709-
}
710-
}

0 commit comments

Comments
 (0)