1 #![cfg_attr(feature = "cargo-clippy", allow(blacklisted_name))]
5 extern crate proc_macro2
;
9 use proc_macro2
::{Ident, Span, TokenStream}
;
10 use quote
::TokenStreamExt
;
19 impl quote
::ToTokens
for X
{
20 fn to_tokens(&self, tokens
: &mut TokenStream
) {
21 tokens
.append(Ident
::new("X", Span
::call_site()));
26 fn test_quote_impl() {
28 impl<'a
, T
: ToTokens
> ToTokens
for &'a T
{
29 fn to_tokens(&self, tokens
: &mut TokenStream
) {
30 (**self).to_tokens(tokens
)
35 let expected
= concat
!(
36 "impl < 'a , T : ToTokens > ToTokens for & 'a T { ",
37 "fn to_tokens ( & self , tokens : & mut TokenStream ) { ",
38 "( * * self ) . to_tokens ( tokens ) ",
43 assert_eq
!(expected
, tokens
.to_string());
47 fn test_substitution() {
49 let tokens
= quote
!(#x <#x> (#x) [#x] {#x});
51 let expected
= "X < X > ( X ) [ X ] { X }";
53 assert_eq
!(expected
, tokens
.to_string());
58 let primes
= &[X
, X
, X
, X
];
60 assert_eq
!("X X X X", quote
!(#(#primes)*).to_string());
62 assert_eq
!("X , X , X , X ,", quote
!(#(#primes,)*).to_string());
64 assert_eq
!("X , X , X , X", quote
!(#(#primes),*).to_string());
69 let generics
= quote
!( <'a
, T
> );
71 let where_clause
= quote
!( where T
: Serialize
);
73 let field_ty
= quote
!(String
);
75 let item_ty
= quote
!(Cow
<'a
, str>);
77 let path
= quote
!(SomeTrait
::serialize_with
);
79 let value
= quote
!(self.x
);
82 struct SerializeWith
#generics #where_clause {
84 phantom
: ::std
::marker
::PhantomData
<#item_ty>,
87 impl #generics ::serde::Serialize for SerializeWith #generics #where_clause {
88 fn serialize
<S
>(&self, s
: &mut S
) -> Result
<(), S
::Error
>
89 where S
: ::serde
::Serializer
97 phantom
: ::std
::marker
::PhantomData
::<#item_ty>,
101 let expected
= concat
!(
102 "struct SerializeWith < 'a , T > where T : Serialize { ",
103 "value : & 'a String , ",
104 "phantom : :: std :: marker :: PhantomData < Cow < 'a , str > > , ",
106 "impl < 'a , T > :: serde :: Serialize for SerializeWith < 'a , T > where T : Serialize { ",
107 "fn serialize < S > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > ",
108 "where S : :: serde :: Serializer ",
110 "SomeTrait :: serialize_with ( self . value , s ) ",
114 "value : self . x , ",
115 "phantom : :: std :: marker :: PhantomData :: < Cow < 'a , str > > , ",
119 assert_eq
!(expected
, tokens
.to_string());
128 let iisize
= -1isize
;
135 let tokens
= quote
! {
136 #ii8 #ii16 #ii32 #ii64 #iisize
137 #uu8 #uu16 #uu32 #uu64 #uusize
139 let expected
= "-1i8 -1i16 -1i32 -1i64 -1isize 1u8 1u16 1u32 1u64 1usize";
140 assert_eq
!(expected
, tokens
.to_string());
149 let tokens
= quote
! {
153 let expected
= concat
!("2.345f32 2.345f64");
154 assert_eq
!(expected
, tokens
.to_string());
164 let heart = '\u{2764}';
166 let tokens = quote! {
167 #zero #pound #quote #apost #newline #heart
169 let expected = "'
\\u{0}' '
#' '\\\"' '\\'' '\\n' '\\u{2764}'";
170 assert_eq!(expected, tokens.to_string());
175 let s = "\0 a 'b \" c";
176 let tokens = quote!(#s);
177 let expected = "\"\\u{0} a \\'b \\\" c\"";
178 assert_eq!(expected, tokens.to_string());
183 let s = "\0 a 'b \" c".to_string();
184 let tokens = quote!(#s);
185 let expected = "\"\\u{0} a \\'b \\\" c\"";
186 assert_eq!(expected, tokens.to_string());
191 let foo = Ident::new("Foo", Span::call_site());
192 let bar = Ident::new(&format!("Bar{}", 7), Span::call_site());
193 let tokens = quote!(struct #foo; enum #bar {});
194 let expected = "struct Foo ; enum Bar7 { }";
195 assert_eq!(expected, tokens.to_string());
199 fn test_duplicate() {
202 let tokens = quote!(#ch #ch);
204 let expected = "'x' 'x'";
205 assert_eq!(expected, tokens.to_string());
209 fn test_fancy_repetition() {
210 let foo = vec!["a", "b"];
211 let bar = vec![true, false];
213 let tokens = quote! {
217 let expected = r#""a" : true , "b" : false"#;
218 assert_eq!(expected, tokens.to_string());
222 fn test_nested_fancy_repetition() {
223 let nested = vec![vec!['a', 'b', 'c'], vec!['x', 'y', 'z']];
225 let tokens = quote! {
231 let expected = "'a' 'b' 'c' , 'x' 'y' 'z'";
232 assert_eq!(expected, tokens.to_string());
236 fn test_empty_repetition() {
237 let tokens = quote!(#(a b)* #(c d),*);
238 assert_eq!("", tokens.to_string());
242 fn test_variable_name_conflict() {
243 // The implementation of `#(...),*` uses the variable `_i` but it should be
244 // fine, if a little confusing when debugging.
245 let _i = vec!['a', 'b'];
246 let tokens = quote! { #(#_i),* };
247 let expected = "'a' , 'b'";
248 assert_eq!(expected, tokens.to_string());
252 fn test_empty_quote() {
253 let tokens = quote!();
254 assert_eq!("", tokens.to_string());
259 let b = "str".to_owned().into_boxed_str();
260 let tokens = quote! { #b };
261 assert_eq!("\"str\"", tokens.to_string());
266 let owned: Cow<Ident> = Cow::Owned(Ident::new("owned", Span::call_site()));
268 let ident = Ident::new("borrowed", Span::call_site());
269 let borrowed = Cow::Borrowed(&ident);
271 let tokens = quote! { #owned #borrowed };
272 assert_eq!("owned borrowed", tokens.to_string());
277 fn field_i(i: usize) -> Ident {
278 Ident::new(&format!("__field{}", i), Span::call_site())
281 let fields = (0usize..3)
282 .map(field_i as fn(_) -> _)
283 .map(|var| quote! { #var });
285 let tokens = quote! { #(#fields)* };
286 assert_eq!("__field0 __field1 __field2", tokens.to_string());
290 fn test_append_tokens() {
291 let mut a = quote!(a);
294 assert_eq!("a b", a.to_string());