Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame^] | 1 | #![cfg(feature = "extra-traits")] |
| 2 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 3 | extern crate syn; |
| 4 | use syn::*; |
| 5 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 6 | #[macro_use] |
| 7 | extern crate quote; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 8 | |
| 9 | #[test] |
| 10 | fn test_split_for_impl() { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 11 | // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 12 | let generics = Generics { |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 13 | lifetimes: vec![LifetimeDef { |
| 14 | attrs: Vec::new(), |
| 15 | lifetime: Lifetime::new("'a"), |
| 16 | bounds: Vec::new(), |
| 17 | }, |
| 18 | LifetimeDef { |
| 19 | attrs: Vec::new(), |
| 20 | lifetime: Lifetime::new("'b"), |
| 21 | bounds: vec![Lifetime::new("'a")], |
| 22 | }], |
| 23 | ty_params: vec![TyParam { |
| 24 | attrs: vec![Attribute { |
| 25 | style: AttrStyle::Outer, |
Arnavion | e43b1b0 | 2017-04-19 02:47:45 -0700 | [diff] [blame] | 26 | path: "may_dangle".into(), |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 27 | tts: vec![], |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 28 | is_sugared_doc: false, |
| 29 | }], |
| 30 | ident: Ident::new("T"), |
| 31 | bounds: vec![TyParamBound::Region(Lifetime::new("'a"))], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | default: Some(TyTup { tys: Vec::new() }.into()), |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 33 | }], |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 34 | where_clause: WhereClause { |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 35 | predicates: vec![WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 36 | bound_lifetimes: Vec::new(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 37 | bounded_ty: TyPath { |
| 38 | qself: None, |
| 39 | path: "T".into(), |
| 40 | }.into(), |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 41 | bounds: vec![ |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 42 | TyParamBound::Trait( |
| 43 | PolyTraitRef { |
| 44 | bound_lifetimes: Vec::new(), |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 45 | trait_ref: "Debug".into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 46 | }, |
| 47 | TraitBoundModifier::None, |
| 48 | ), |
| 49 | ], |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 50 | })], |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 51 | }, |
| 52 | }; |
| 53 | |
| 54 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 55 | let tokens = quote! { |
| 56 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 57 | }; |
David Tolnay | 93413a5 | 2016-10-24 13:02:36 -0700 | [diff] [blame] | 58 | let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 59 | "MyTrait for Test < 'a , 'b , T > ", |
| 60 | "where T : Debug { }"); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 61 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 62 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 63 | let turbofish = ty_generics.as_turbofish(); |
| 64 | let tokens = quote! { |
| 65 | Test #turbofish |
| 66 | }; |
| 67 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 68 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 69 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 70 | |
| 71 | #[test] |
| 72 | fn test_ty_param_bound() { |
| 73 | let tokens = quote!('a); |
| 74 | let expected = TyParamBound::Region(Lifetime::new("'a")); |
| 75 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 76 | |
| 77 | let tokens = quote!(Debug); |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 78 | let expected = TyParamBound::Trait(PolyTraitRef { |
| 79 | bound_lifetimes: Vec::new(), |
| 80 | trait_ref: "Debug".into(), |
| 81 | }, |
| 82 | TraitBoundModifier::None); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 83 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 84 | |
| 85 | let tokens = quote!(?Sized); |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame] | 86 | let expected = TyParamBound::Trait(PolyTraitRef { |
| 87 | bound_lifetimes: Vec::new(), |
| 88 | trait_ref: "Sized".into(), |
| 89 | }, |
| 90 | TraitBoundModifier::Maybe); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 91 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 92 | } |