1 mod test_basic { 2 use paste::paste; 3 4 struct Struct; 5 6 paste! { 7 impl Struct { 8 fn [<a b c>]() {} 9 } 10 } 11 12 #[test] test()13 fn test() { 14 Struct::abc(); 15 } 16 } 17 18 mod test_in_impl { 19 use paste::paste; 20 21 struct Struct; 22 23 impl Struct { 24 paste! { 25 fn [<a b c>]() {} 26 } 27 } 28 29 #[test] test()30 fn test() { 31 Struct::abc(); 32 } 33 } 34 35 mod test_none_delimited_single_ident { 36 use paste::paste; 37 38 macro_rules! m { 39 ($id:ident) => { 40 paste! { 41 fn f() -> &'static str { 42 stringify!($id) 43 } 44 } 45 }; 46 } 47 48 m!(i32x4); 49 50 #[test] test()51 fn test() { 52 assert_eq!(f(), "i32x4"); 53 } 54 } 55 56 mod test_none_delimited_single_lifetime { 57 use paste::paste; 58 59 macro_rules! m { 60 ($life:lifetime) => { 61 paste! { 62 pub struct S; 63 impl<$life> S { 64 fn f() {} 65 } 66 } 67 }; 68 } 69 70 m!('a); 71 72 #[test] test()73 fn test() { 74 S::f(); 75 } 76 } 77 78 mod test_to_lower { 79 use paste::paste; 80 81 macro_rules! m { 82 ($id:ident) => { 83 paste! { 84 fn [<my_ $id:lower _here>](_arg: u8) -> &'static str { 85 stringify!([<$id:lower>]) 86 } 87 } 88 }; 89 } 90 91 m!(Test); 92 93 #[test] test_to_lower()94 fn test_to_lower() { 95 assert_eq!(my_test_here(0), "test"); 96 } 97 } 98 99 mod test_to_upper { 100 use paste::paste; 101 102 macro_rules! m { 103 ($id:ident) => { 104 paste! { 105 const [<MY_ $id:upper _HERE>]: &str = stringify!([<$id:upper>]); 106 } 107 }; 108 } 109 110 m!(Test); 111 112 #[test] test_to_upper()113 fn test_to_upper() { 114 assert_eq!(MY_TEST_HERE, "TEST"); 115 } 116 } 117 118 mod test_to_snake { 119 use paste::paste; 120 121 macro_rules! m { 122 ($id:ident) => { 123 paste! { 124 const DEFAULT_SNAKE: &str = stringify!([<$id:snake>]); 125 const LOWER_SNAKE: &str = stringify!([<$id:snake:lower>]); 126 const UPPER_SNAKE: &str = stringify!([<$id:snake:upper>]); 127 } 128 }; 129 } 130 131 m!(ThisIsButATest); 132 133 #[test] test_to_snake()134 fn test_to_snake() { 135 assert_eq!(DEFAULT_SNAKE, "this_is_but_a_test"); 136 assert_eq!(LOWER_SNAKE, "this_is_but_a_test"); 137 assert_eq!(UPPER_SNAKE, "THIS_IS_BUT_A_TEST"); 138 } 139 } 140 141 mod test_to_camel { 142 use paste::paste; 143 144 macro_rules! m { 145 ($id:ident) => { 146 paste! { 147 const DEFAULT_CAMEL: &str = stringify!([<$id:camel>]); 148 const LOWER_CAMEL: &str = stringify!([<$id:camel:lower>]); 149 const UPPER_CAMEL: &str = stringify!([<$id:camel:upper>]); 150 } 151 }; 152 } 153 154 m!(this_is_but_a_test); 155 156 #[test] test_to_camel()157 fn test_to_camel() { 158 assert_eq!(DEFAULT_CAMEL, "ThisIsButATest"); 159 assert_eq!(LOWER_CAMEL, "thisisbutatest"); 160 assert_eq!(UPPER_CAMEL, "THISISBUTATEST"); 161 } 162 } 163 164 mod test_doc_expr { 165 // https://github.com/dtolnay/paste/issues/29 166 167 use paste::paste; 168 169 macro_rules! doc_expr { 170 ($doc:expr) => { 171 paste! { 172 #[doc = $doc] 173 pub struct S; 174 } 175 }; 176 } 177 178 doc_expr!(stringify!()); 179 180 #[test] test_doc_expr()181 fn test_doc_expr() { 182 let _: S; 183 } 184 } 185 186 mod test_type_in_path { 187 // https://github.com/dtolnay/paste/issues/31 188 189 use paste::paste; 190 191 mod keys { 192 #[derive(Default)] 193 pub struct Mib<T = ()>(std::marker::PhantomData<T>); 194 } 195 196 macro_rules! types { 197 ($mib:ty) => { 198 paste! { 199 #[derive(Default)] 200 pub struct S(pub keys::$mib); 201 } 202 }; 203 } 204 205 macro_rules! write { 206 ($fn:ident, $field:ty) => { 207 paste! { 208 pub fn $fn() -> $field { 209 $field::default() 210 } 211 } 212 }; 213 } 214 215 types! {Mib<[usize; 2]>} 216 write! {get_a, keys::Mib} 217 write! {get_b, usize} 218 219 #[test] test_type_in_path()220 fn test_type_in_path() { 221 let _: S; 222 let _ = get_a; 223 let _ = get_b; 224 } 225 } 226 227 mod test_type_in_fn_arg { 228 // https://github.com/dtolnay/paste/issues/38 229 230 use paste::paste; 231 _jit_address(_node: ())232 fn _jit_address(_node: ()) {} 233 234 macro_rules! jit_reexport { 235 ($fn:ident, $arg:ident : $typ:ty) => { 236 paste! { 237 pub fn $fn($arg: $typ) { 238 [<_jit_ $fn>]($arg); 239 } 240 } 241 }; 242 } 243 244 jit_reexport!(address, node: ()); 245 246 #[test] test_type_in_fn_arg()247 fn test_type_in_fn_arg() { 248 let _ = address; 249 } 250 } 251 252 mod test_pat_in_expr_position { 253 // https://github.com/xiph/rav1e/pull/2324/files 254 255 use paste::paste; 256 257 macro_rules! rav1e_bad { 258 ($e:pat) => { 259 paste! { 260 #[test] 261 fn test() { 262 let _ = $e; 263 } 264 } 265 }; 266 } 267 268 rav1e_bad!(std::fmt::Error); 269 } 270