diff options
Diffstat (limited to 'crates/hir_expand/src/builtin_macro.rs')
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 78 |
1 files changed, 35 insertions, 43 deletions
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 | } |