aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-03-10 20:05:02 +0000
committerJonas Schievink <[email protected]>2021-03-10 20:05:02 +0000
commitbc4ecb199b4ad3a580d3b29e659e707ce1a29f04 (patch)
tree487a52f6a620479abc33f79380e6933aaa14a726
parent6c32e2d8a045e4ba7d45ac9651572a226324cefa (diff)
Use expect-test for builtin macro/derive tests
-rw-r--r--Cargo.lock1
-rw-r--r--crates/hir_expand/Cargo.toml1
-rw-r--r--crates/hir_expand/src/builtin_derive.rs66
-rw-r--r--crates/hir_expand/src/builtin_macro.rs78
4 files changed, 66 insertions, 80 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8c28929ed..b1fef2e80 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -526,6 +526,7 @@ dependencies = [
526 "base_db", 526 "base_db",
527 "cfg", 527 "cfg",
528 "either", 528 "either",
529 "expect-test",
529 "la-arena", 530 "la-arena",
530 "log", 531 "log",
531 "mbe", 532 "mbe",
diff --git a/crates/hir_expand/Cargo.toml b/crates/hir_expand/Cargo.toml
index 7bb40ba9b..f649ab925 100644
--- a/crates/hir_expand/Cargo.toml
+++ b/crates/hir_expand/Cargo.toml
@@ -25,3 +25,4 @@ mbe = { path = "../mbe", version = "0.0.0" }
25 25
26[dev-dependencies] 26[dev-dependencies]
27test_utils = { path = "../test_utils" } 27test_utils = { path = "../test_utils" }
28expect-test = "1.1"
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs
index b7f1aae8f..dfdb9cf59 100644
--- a/crates/hir_expand/src/builtin_derive.rs
+++ b/crates/hir_expand/src/builtin_derive.rs
@@ -267,13 +267,14 @@ fn partial_ord_expand(
267#[cfg(test)] 267#[cfg(test)]
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 name::{known, Name}; 271 use name::{known, Name};
271 272
272 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; 273 use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
273 274
274 use super::*; 275 use super::*;
275 276
276 fn expand_builtin_derive(s: &str, name: Name) -> String { 277 fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String {
277 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); 278 let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
278 let fixture = format!( 279 let fixture = format!(
279 r#"//- /main.rs crate:main deps:core 280 r#"//- /main.rs crate:main deps:core
@@ -282,7 +283,7 @@ $0
282//- /lib.rs crate:core 283//- /lib.rs crate:core
283// empty 284// empty
284"#, 285"#,
285 s 286 ra_fixture
286 ); 287 );
287 288
288 let (db, file_pos) = TestDB::with_position(&fixture); 289 let (db, file_pos) = TestDB::with_position(&fixture);
@@ -314,66 +315,57 @@ $0
314 parsed.text().to_string() 315 parsed.text().to_string()
315 } 316 }
316 317
318 fn check_derive(ra_fixture: &str, name: Name, expected: Expect) {
319 let expanded = expand_builtin_derive(ra_fixture, name);
320 expected.assert_eq(&expanded);
321 }
322
317 #[test] 323 #[test]
318 fn test_copy_expand_simple() { 324 fn test_copy_expand_simple() {
319 let expanded = expand_builtin_derive( 325 check_derive(
320 r#" 326 r#"
321 #[derive(Copy)] 327 #[derive(Copy)]
322 struct Foo; 328 struct Foo;
323"#, 329 "#,
324 known::Copy, 330 known::Copy,
331 expect![["impl< >core::marker::CopyforFoo< >{}"]],
325 ); 332 );
326
327 assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}");
328 } 333 }
329 334
330 #[test] 335 #[test]
331 fn test_copy_expand_with_type_params() { 336 fn test_copy_expand_with_type_params() {
332 let expanded = expand_builtin_derive( 337 check_derive(
333 r#" 338 r#"
334 #[derive(Copy)] 339 #[derive(Copy)]
335 struct Foo<A, B>; 340 struct Foo<A, B>;
336"#, 341 "#,
337 known::Copy, 342 known::Copy,
338 ); 343 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
339
340 assert_eq!(
341 expanded,
342 "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
343 ); 344 );
344 } 345 }
345 346
346 #[test] 347 #[test]
347 fn test_copy_expand_with_lifetimes() { 348 fn test_copy_expand_with_lifetimes() {
348 let expanded = expand_builtin_derive( 349 check_derive(
349 r#" 350 r#"
350 #[derive(Copy)] 351 #[derive(Copy)]
351 struct Foo<A, B, 'a, 'b>; 352 struct Foo<A, B, 'a, 'b>;
352"#, 353 "#,
353 known::Copy, 354 known::Copy,
354 ); 355 // We currently just ignore lifetimes
355 356 expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
356 // We currently just ignore lifetimes
357
358 assert_eq!(
359 expanded,
360 "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
361 ); 357 );
362 } 358 }
363 359
364 #[test] 360 #[test]
365 fn test_clone_expand() { 361 fn test_clone_expand() {
366 let expanded = expand_builtin_derive( 362 check_derive(
367 r#" 363 r#"
368 #[derive(Clone)] 364 #[derive(Clone)]
369 struct Foo<A, B>; 365 struct Foo<A, B>;
370"#, 366 "#,
371 known::Clone, 367 known::Clone,
372 ); 368 expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
373
374 assert_eq!(
375 expanded,
376 "impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"
377 ); 369 );
378 } 370 }
379} 371}
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index b34f1a4f2..2a79c892b 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -491,6 +491,7 @@ mod tests {
491 MacroCallLoc, 491 MacroCallLoc,
492 }; 492 };
493 use base_db::{fixture::WithFixture, SourceDatabase}; 493 use base_db::{fixture::WithFixture, SourceDatabase};
494 use expect_test::{expect, Expect};
494 use std::sync::Arc; 495 use std::sync::Arc;
495 use syntax::ast::NameOwner; 496 use syntax::ast::NameOwner;
496 497
@@ -574,87 +575,86 @@ mod tests {
574 db.parse_or_expand(file_id).unwrap().to_string() 575 db.parse_or_expand(file_id).unwrap().to_string()
575 } 576 }
576 577
578 fn check_expansion(ra_fixture: &str, expect: Expect) {
579 let expansion = expand_builtin_macro(ra_fixture);
580 expect.assert_eq(&expansion);
581 }
582
577 #[test] 583 #[test]
578 fn test_column_expand() { 584 fn test_column_expand() {
579 let expanded = expand_builtin_macro( 585 check_expansion(
580 r#" 586 r#"
581 #[rustc_builtin_macro] 587 #[rustc_builtin_macro]
582 macro_rules! column {() => {}} 588 macro_rules! column {() => {}}
583 column!() 589 column!()
584 "#, 590 "#,
591 expect![["0"]],
585 ); 592 );
586
587 assert_eq!(expanded, "0");
588 } 593 }
589 594
590 #[test] 595 #[test]
591 fn test_line_expand() { 596 fn test_line_expand() {
592 let expanded = expand_builtin_macro( 597 check_expansion(
593 r#" 598 r#"
594 #[rustc_builtin_macro] 599 #[rustc_builtin_macro]
595 macro_rules! line {() => {}} 600 macro_rules! line {() => {}}
596 line!() 601 line!()
597 "#, 602 "#,
603 expect![["0"]],
598 ); 604 );
599
600 assert_eq!(expanded, "0");
601 } 605 }
602 606
603 #[test] 607 #[test]
604 fn test_stringify_expand() { 608 fn test_stringify_expand() {
605 let expanded = expand_builtin_macro( 609 check_expansion(
606 r#" 610 r#"
607 #[rustc_builtin_macro] 611 #[rustc_builtin_macro]
608 macro_rules! stringify {() => {}} 612 macro_rules! stringify {() => {}}
609 stringify!(a b c) 613 stringify!(a b c)
610 "#, 614 "#,
615 expect![["\"a b c\""]],
611 ); 616 );
612
613 assert_eq!(expanded, "\"a b c\"");
614 } 617 }
615 618
616 #[test] 619 #[test]
617 fn test_env_expand() { 620 fn test_env_expand() {
618 let expanded = expand_builtin_macro( 621 check_expansion(
619 r#" 622 r#"
620 #[rustc_builtin_macro] 623 #[rustc_builtin_macro]
621 macro_rules! env {() => {}} 624 macro_rules! env {() => {}}
622 env!("TEST_ENV_VAR") 625 env!("TEST_ENV_VAR")
623 "#, 626 "#,
627 expect![["\"__RA_UNIMPLEMENTED__\""]],
624 ); 628 );
625
626 assert_eq!(expanded, "\"__RA_UNIMPLEMENTED__\"");
627 } 629 }
628 630
629 #[test] 631 #[test]
630 fn test_option_env_expand() { 632 fn test_option_env_expand() {
631 let expanded = expand_builtin_macro( 633 check_expansion(
632 r#" 634 r#"
633 #[rustc_builtin_macro] 635 #[rustc_builtin_macro]
634 macro_rules! option_env {() => {}} 636 macro_rules! option_env {() => {}}
635 option_env!("TEST_ENV_VAR") 637 option_env!("TEST_ENV_VAR")
636 "#, 638 "#,
639 expect![["std::option::Option::None:: < &str>"]],
637 ); 640 );
638
639 assert_eq!(expanded, "std::option::Option::None:: < &str>");
640 } 641 }
641 642
642 #[test] 643 #[test]
643 fn test_file_expand() { 644 fn test_file_expand() {
644 let expanded = expand_builtin_macro( 645 check_expansion(
645 r#" 646 r#"
646 #[rustc_builtin_macro] 647 #[rustc_builtin_macro]
647 macro_rules! file {() => {}} 648 macro_rules! file {() => {}}
648 file!() 649 file!()
649 "#, 650 "#,
651 expect![[r#""""#]],
650 ); 652 );
651
652 assert_eq!(expanded, "\"\"");
653 } 653 }
654 654
655 #[test] 655 #[test]
656 fn test_assert_expand() { 656 fn test_assert_expand() {
657 let expanded = expand_builtin_macro( 657 check_expansion(
658 r#" 658 r#"
659 #[rustc_builtin_macro] 659 #[rustc_builtin_macro]
660 macro_rules! assert { 660 macro_rules! assert {
@@ -663,14 +663,13 @@ mod tests {
663 } 663 }
664 assert!(true, "{} {:?}", arg1(a, b, c), arg2); 664 assert!(true, "{} {:?}", arg1(a, b, c), arg2);
665 "#, 665 "#,
666 expect![["{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}"]],
666 ); 667 );
667
668 assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
669 } 668 }
670 669
671 #[test] 670 #[test]
672 fn test_compile_error_expand() { 671 fn test_compile_error_expand() {
673 let expanded = expand_builtin_macro( 672 check_expansion(
674 r#" 673 r#"
675 #[rustc_builtin_macro] 674 #[rustc_builtin_macro]
676 macro_rules! compile_error { 675 macro_rules! compile_error {
@@ -679,15 +678,14 @@ mod tests {
679 } 678 }
680 compile_error!("error!"); 679 compile_error!("error!");
681 "#, 680 "#,
681 // This expands to nothing (since it's in item position), but emits an error.
682 expect![[""]],
682 ); 683 );
683
684 // This expands to nothing (since it's in item position), but emits an error.
685 assert_eq!(expanded, "");
686 } 684 }
687 685
688 #[test] 686 #[test]
689 fn test_format_args_expand() { 687 fn test_format_args_expand() {
690 let expanded = expand_builtin_macro( 688 check_expansion(
691 r#" 689 r#"
692 #[rustc_builtin_macro] 690 #[rustc_builtin_macro]
693 macro_rules! format_args { 691 macro_rules! format_args {
@@ -696,17 +694,15 @@ mod tests {
696 } 694 }
697 format_args!("{} {:?}", arg1(a, b, c), arg2); 695 format_args!("{} {:?}", arg1(a, b, c), arg2);
698 "#, 696 "#,
699 ); 697 expect![[
700 698 r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
701 assert_eq!( 699 ]],
702 expanded,
703 r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
704 ); 700 );
705 } 701 }
706 702
707 #[test] 703 #[test]
708 fn test_format_args_expand_with_comma_exprs() { 704 fn test_format_args_expand_with_comma_exprs() {
709 let expanded = expand_builtin_macro( 705 check_expansion(
710 r#" 706 r#"
711 #[rustc_builtin_macro] 707 #[rustc_builtin_macro]
712 macro_rules! format_args { 708 macro_rules! format_args {
@@ -715,17 +711,15 @@ mod tests {
715 } 711 }
716 format_args!("{} {:?}", a::<A,B>(), b); 712 format_args!("{} {:?}", a::<A,B>(), b);
717 "#, 713 "#,
718 ); 714 expect![[
719 715 r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A,B>()),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(b),std::fmt::Display::fmt),])"#
720 assert_eq!( 716 ]],
721 expanded,
722 r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A,B>()),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(b),std::fmt::Display::fmt),])"#
723 ); 717 );
724 } 718 }
725 719
726 #[test] 720 #[test]
727 fn test_include_bytes_expand() { 721 fn test_include_bytes_expand() {
728 let expanded = expand_builtin_macro( 722 check_expansion(
729 r#" 723 r#"
730 #[rustc_builtin_macro] 724 #[rustc_builtin_macro]
731 macro_rules! include_bytes { 725 macro_rules! include_bytes {
@@ -734,21 +728,19 @@ mod tests {
734 } 728 }
735 include_bytes("foo"); 729 include_bytes("foo");
736 "#, 730 "#,
731 expect![[r#"b"""#]],
737 ); 732 );
738
739 assert_eq!(expanded, r#"b"""#);
740 } 733 }
741 734
742 #[test] 735 #[test]
743 fn test_concat_expand() { 736 fn test_concat_expand() {
744 let expanded = expand_builtin_macro( 737 check_expansion(
745 r##" 738 r##"
746 #[rustc_builtin_macro] 739 #[rustc_builtin_macro]
747 macro_rules! concat {} 740 macro_rules! concat {}
748 concat!("foo", "r", 0, r#"bar"#, false); 741 concat!("foo", "r", 0, r#"bar"#, false);
749 "##, 742 "##,
743 expect![[r#""foor0barfalse""#]],
750 ); 744 );
751
752 assert_eq!(expanded, r#""foor0barfalse""#);
753 } 745 }
754} 746}