David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 1 | extern crate quote; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 2 | extern crate syn; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 3 | |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 4 | mod features; |
Alex Crichton | 605643b | 2017-07-05 18:35:14 -0700 | [diff] [blame] | 5 | |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 6 | #[macro_use] |
| 7 | mod macros; |
| 8 | |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 9 | use quote::quote; |
| 10 | use syn::{DeriveInput, ItemFn, TypeParamBound, WhereClause, WherePredicate}; |
Alex Crichton | eed4bc7 | 2018-05-17 10:59:15 -0700 | [diff] [blame] | 11 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 12 | #[test] |
| 13 | fn test_split_for_impl() { |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 14 | let code = quote! { |
| 15 | struct S<'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 16 | }; |
| 17 | |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 18 | let actual = snapshot!(code as DeriveInput); |
| 19 | |
| 20 | let generics = actual.generics; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 21 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 22 | |
| 23 | let generated = quote! { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 24 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 25 | }; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 26 | let expected = quote! { |
| 27 | impl<'a, 'b: 'a, #[may_dangle] T: 'a> MyTrait |
| 28 | for Test<'a, 'b, T> |
| 29 | where |
| 30 | T: Debug |
| 31 | {} |
| 32 | }; |
| 33 | assert_eq!(generated.to_string(), expected.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 34 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 35 | let turbofish = ty_generics.as_turbofish(); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 36 | let generated = quote! { |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 37 | Test #turbofish |
| 38 | }; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 39 | let expected = quote! { |
| 40 | Test::<'a, 'b, T> |
| 41 | }; |
| 42 | assert_eq!(generated.to_string(), expected.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 43 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 44 | |
| 45 | #[test] |
| 46 | fn test_ty_param_bound() { |
| 47 | let tokens = quote!('a); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 48 | snapshot!(tokens as TypeParamBound); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 49 | |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 50 | let tokens = quote!('_); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 51 | snapshot!(tokens as TypeParamBound); |
Bastien Orivel | 340553a | 2018-02-15 23:57:38 +0100 | [diff] [blame] | 52 | |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 53 | let tokens = quote!(Debug); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 54 | snapshot!(tokens as TypeParamBound); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 55 | |
| 56 | let tokens = quote!(?Sized); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 57 | snapshot!(tokens as TypeParamBound); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 58 | } |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 59 | |
| 60 | #[test] |
| 61 | fn test_fn_precedence_in_where_clause() { |
David Tolnay | c8b0e0f | 2019-03-07 22:46:32 -0800 | [diff] [blame] | 62 | // This should parse as two separate bounds, `FnOnce() -> i32` and `Send` - not |
| 63 | // `FnOnce() -> (i32 + Send)`. |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 64 | let code = quote! { |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 65 | fn f<G>() |
| 66 | where |
| 67 | G: FnOnce() -> i32 + Send, |
| 68 | { |
| 69 | } |
| 70 | }; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 71 | |
| 72 | let actual = snapshot!(code as ItemFn); |
| 73 | |
| 74 | let where_clause = actual.decl.generics.where_clause.as_ref().unwrap(); |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 75 | assert_eq!(where_clause.predicates.len(), 1); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 76 | |
| 77 | let predicate = match &where_clause.predicates[0] { |
| 78 | WherePredicate::Type(pred) => pred, |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 79 | _ => panic!("wrong predicate kind"), |
| 80 | }; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 81 | |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 82 | assert_eq!(predicate.bounds.len(), 2, "{:#?}", predicate.bounds); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 83 | |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 84 | let first_bound = &predicate.bounds[0]; |
| 85 | assert_eq!(quote!(#first_bound).to_string(), "FnOnce ( ) -> i32"); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 86 | |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 87 | let second_bound = &predicate.bounds[1]; |
| 88 | assert_eq!(quote!(#second_bound).to_string(), "Send"); |
| 89 | } |
David Tolnay | 38012de | 2018-09-02 13:32:47 -0700 | [diff] [blame] | 90 | |
| 91 | #[test] |
| 92 | fn test_where_clause_at_end_of_input() { |
| 93 | let tokens = quote! { |
| 94 | where |
| 95 | }; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 96 | |
| 97 | let where_clause = snapshot!(tokens as WhereClause); |
David Tolnay | 38012de | 2018-09-02 13:32:47 -0700 | [diff] [blame] | 98 | assert_eq!(where_clause.predicates.len(), 0); |
| 99 | } |