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