aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-03-18 14:14:52 +0000
committerJonas Schievink <[email protected]>2021-03-18 14:14:52 +0000
commit4cf36545e65c91a92f7a29681fb7f4346475e81b (patch)
tree5f018aed7c3866dfe67bbef812dad9a5a434447b /crates
parentd3da042a6297b60799f6f97dd4d58d559a9b5838 (diff)
Create AstId for builtin_derive macro in tests
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_expand/src/builtin_derive.rs41
1 files changed, 28 insertions, 13 deletions
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs
index 5e908b223..a8d267c30 100644
--- a/crates/hir_expand/src/builtin_derive.rs
+++ b/crates/hir_expand/src/builtin_derive.rs
@@ -268,14 +268,13 @@ fn partial_ord_expand(
268mod tests { 268mod tests {
269 use base_db::{fixture::WithFixture, CrateId, SourceDatabase}; 269 use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
270 use expect_test::{expect, Expect}; 270 use expect_test::{expect, Expect};
271 use name::{known, Name}; 271 use name::AsName;
272 272
273 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; 273 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
274 274
275 use super::*; 275 use super::*;
276 276
277 fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String { 277 fn expand_builtin_derive(ra_fixture: &str) -> String {
278 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
279 let fixture = format!( 278 let fixture = format!(
280 r#"//- /main.rs crate:main deps:core 279 r#"//- /main.rs crate:main deps:core
281$0 280$0
@@ -288,18 +287,34 @@ $0
288 287
289 let (db, file_pos) = TestDB::with_position(&fixture); 288 let (db, file_pos) = TestDB::with_position(&fixture);
290 let file_id = file_pos.file_id; 289 let file_id = file_pos.file_id;
290 let ast_id_map = db.ast_id_map(file_id.into());
291 let parsed = db.parse(file_id); 291 let parsed = db.parse(file_id);
292 let items: Vec<_> = 292 let macros: Vec<_> =
293 parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect(); 293 parsed.syntax_node().descendants().filter_map(ast::Macro::cast).collect();
294 let items: Vec<_> = parsed
295 .syntax_node()
296 .descendants()
297 .filter(|node| !ast::Macro::can_cast(node.kind()))
298 .filter_map(ast::Item::cast)
299 .collect();
300
301 assert_eq!(macros.len(), 1, "test must contain exactly 1 macro definition");
302 assert_eq!(items.len(), 1, "test must contain exactly 1 item");
303
304 let macro_ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macros[0]));
305 let name = match &macros[0] {
306 ast::Macro::MacroRules(rules) => rules.name().unwrap().as_name(),
307 ast::Macro::MacroDef(def) => def.name().unwrap().as_name(),
308 };
294 309
295 let ast_id_map = db.ast_id_map(file_id.into()); 310 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
296 311
297 let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); 312 let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
298 313
299 let loc = MacroCallLoc { 314 let loc = MacroCallLoc {
300 def: MacroDefId { 315 def: MacroDefId {
301 krate: CrateId(0), 316 krate: CrateId(0),
302 ast_id: None, 317 ast_id: Some(macro_ast_id),
303 kind: MacroDefKind::BuiltInDerive(expander), 318 kind: MacroDefKind::BuiltInDerive(expander),
304 local_inner: false, 319 local_inner: false,
305 }, 320 },
@@ -315,8 +330,8 @@ $0
315 parsed.text().to_string() 330 parsed.text().to_string()
316 } 331 }
317 332
318 fn check_derive(ra_fixture: &str, name: Name, expected: Expect) { 333 fn check_derive(ra_fixture: &str, expected: Expect) {
319 let expanded = expand_builtin_derive(ra_fixture, name); 334 let expanded = expand_builtin_derive(ra_fixture);
320 expected.assert_eq(&expanded); 335 expected.assert_eq(&expanded);
321 } 336 }
322 337
@@ -324,10 +339,10 @@ $0
324 fn test_copy_expand_simple() { 339 fn test_copy_expand_simple() {
325 check_derive( 340 check_derive(
326 r#" 341 r#"
342 macro Copy {}
327 #[derive(Copy)] 343 #[derive(Copy)]
328 struct Foo; 344 struct Foo;
329 "#, 345 "#,
330 known::Copy,
331 expect![["impl< >core::marker::CopyforFoo< >{}"]], 346 expect![["impl< >core::marker::CopyforFoo< >{}"]],
332 ); 347 );
333 } 348 }
@@ -336,10 +351,10 @@ $0
336 fn test_copy_expand_with_type_params() { 351 fn test_copy_expand_with_type_params() {
337 check_derive( 352 check_derive(
338 r#" 353 r#"
354 macro Copy {}
339 #[derive(Copy)] 355 #[derive(Copy)]
340 struct Foo<A, B>; 356 struct Foo<A, B>;
341 "#, 357 "#,
342 known::Copy,
343 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]], 358 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
344 ); 359 );
345 } 360 }
@@ -348,10 +363,10 @@ $0
348 fn test_copy_expand_with_lifetimes() { 363 fn test_copy_expand_with_lifetimes() {
349 check_derive( 364 check_derive(
350 r#" 365 r#"
366 macro Copy {}
351 #[derive(Copy)] 367 #[derive(Copy)]
352 struct Foo<A, B, 'a, 'b>; 368 struct Foo<A, B, 'a, 'b>;
353 "#, 369 "#,
354 known::Copy,
355 // We currently just ignore lifetimes 370 // We currently just ignore lifetimes
356 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]], 371 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
357 ); 372 );
@@ -361,10 +376,10 @@ $0
361 fn test_clone_expand() { 376 fn test_clone_expand() {
362 check_derive( 377 check_derive(
363 r#" 378 r#"
379 macro Clone {}
364 #[derive(Clone)] 380 #[derive(Clone)]
365 struct Foo<A, B>; 381 struct Foo<A, B>;
366 "#, 382 "#,
367 known::Clone,
368 expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]], 383 expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
369 ); 384 );
370 } 385 }