diff options
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 89 |
1 files changed, 87 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 9aad08db9..e78bc734b 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -220,9 +220,10 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
220 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; | 220 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; |
221 | let file = token_tree_to_macro_items(&expansion); | 221 | let file = token_tree_to_macro_items(&expansion); |
222 | let file = file.unwrap().syntax().debug_dump().trim().to_string(); | 222 | let file = file.unwrap().syntax().debug_dump().trim().to_string(); |
223 | let file = file.replace("C_C__C", "$crate"); | 223 | let tree = tree.unwrap().syntax().debug_dump().trim().to_string(); |
224 | 224 | ||
225 | assert_eq!(tree.unwrap().syntax().debug_dump().trim(), file,); | 225 | let file = file.replace("C_C__C", "$crate"); |
226 | assert_eq!(tree, file,); | ||
226 | } | 227 | } |
227 | 228 | ||
228 | #[test] | 229 | #[test] |
@@ -349,6 +350,21 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
349 | } | 350 | } |
350 | 351 | ||
351 | #[test] | 352 | #[test] |
353 | fn test_match_group_pattern_with_multiple_statement_without_semi() { | ||
354 | let rules = create_rules( | ||
355 | r#" | ||
356 | macro_rules! foo { | ||
357 | ($ ($ i:ident),*) => ( fn baz { $ ( | ||
358 | $i() | ||
359 | );*} ); | ||
360 | } | ||
361 | "#, | ||
362 | ); | ||
363 | |||
364 | assert_expansion(&rules, "foo! { foo, bar }", "fn baz {foo () ; bar () ;}"); | ||
365 | } | ||
366 | |||
367 | #[test] | ||
352 | fn test_match_group_empty_fixed_token() { | 368 | fn test_match_group_empty_fixed_token() { |
353 | let rules = create_rules( | 369 | let rules = create_rules( |
354 | r#" | 370 | r#" |
@@ -692,6 +708,33 @@ MACRO_ITEMS@[0; 40) | |||
692 | } | 708 | } |
693 | 709 | ||
694 | #[test] | 710 | #[test] |
711 | fn test_ty_with_complex_type() { | ||
712 | let rules = create_rules( | ||
713 | r#" | ||
714 | macro_rules! foo { | ||
715 | ($ i:ty) => ( | ||
716 | fn bar() -> $ i { unimplemented!() } | ||
717 | ) | ||
718 | } | ||
719 | "#, | ||
720 | ); | ||
721 | |||
722 | // Reference lifetime struct with generic type | ||
723 | assert_expansion( | ||
724 | &rules, | ||
725 | "foo! { &'a Baz<u8> }", | ||
726 | "fn bar () -> & 'a Baz < u8 > {unimplemented ! ()}", | ||
727 | ); | ||
728 | |||
729 | // extern "Rust" func type | ||
730 | assert_expansion( | ||
731 | &rules, | ||
732 | r#"foo! { extern "Rust" fn() -> Ret }"#, | ||
733 | r#"fn bar () -> extern "Rust" fn () -> Ret {unimplemented ! ()}"#, | ||
734 | ); | ||
735 | } | ||
736 | |||
737 | #[test] | ||
695 | fn test_pat_() { | 738 | fn test_pat_() { |
696 | let rules = create_rules( | 739 | let rules = create_rules( |
697 | r#" | 740 | r#" |
@@ -854,6 +897,26 @@ MACRO_ITEMS@[0; 40) | |||
854 | 897 | ||
855 | // The following tests are based on real world situations | 898 | // The following tests are based on real world situations |
856 | #[test] | 899 | #[test] |
900 | fn test_vec() { | ||
901 | let rules = create_rules( | ||
902 | r#" | ||
903 | macro_rules! vec { | ||
904 | ($($item:expr),*) => { | ||
905 | { | ||
906 | let mut v = Vec::new(); | ||
907 | $( | ||
908 | v.push($item); | ||
909 | )* | ||
910 | v | ||
911 | } | ||
912 | }; | ||
913 | } | ||
914 | "#, | ||
915 | ); | ||
916 | assert_expansion(&rules, r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#); | ||
917 | } | ||
918 | |||
919 | #[test] | ||
857 | fn test_winapi_struct() { | 920 | fn test_winapi_struct() { |
858 | // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/macros.rs#L366 | 921 | // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/macros.rs#L366 |
859 | 922 | ||
@@ -886,4 +949,26 @@ macro_rules! STRUCT { | |||
886 | assert_expansion(&rules, r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#, | 949 | assert_expansion(&rules, r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#, |
887 | "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}"); | 950 | "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}"); |
888 | } | 951 | } |
952 | |||
953 | #[test] | ||
954 | fn test_int_base() { | ||
955 | let rules = create_rules( | ||
956 | r#" | ||
957 | macro_rules! int_base { | ||
958 | ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => { | ||
959 | #[stable(feature = "rust1", since = "1.0.0")] | ||
960 | impl fmt::$Trait for $T { | ||
961 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
962 | $Radix.fmt_int(*self as $U, f) | ||
963 | } | ||
964 | } | ||
965 | } | ||
966 | } | ||
967 | "#, | ||
968 | ); | ||
969 | |||
970 | assert_expansion(&rules, r#" int_base!{Binary for isize as usize -> Binary}"#, | ||
971 | "# [stable (feature = \"rust1\" , since = \"1.0.0\")] impl fmt :: Binary for isize {fn fmt (& self , f : & mut fmt :: Formatter < \'_ >) -> fmt :: Result {Binary . fmt_int (* self as usize , f)}}" | ||
972 | ); | ||
973 | } | ||
889 | } | 974 | } |