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, |
| 24 | value: MetaItem::Word("may_dangle".into()), |
| 25 | is_sugared_doc: false, |
| 26 | }], |
| 27 | ident: Ident::new("T"), |
| 28 | bounds: vec![TyParamBound::Region(Lifetime::new("'a"))], |
| 29 | default: Some(Ty::Tup(Vec::new())), |
| 30 | }], |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 31 | where_clause: WhereClause { |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame^] | 32 | predicates: vec![WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 33 | bound_lifetimes: Vec::new(), |
| 34 | bounded_ty: |
| 35 | Ty::Path(None, "T".into()), |
| 36 | bounds: vec![ |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 37 | TyParamBound::Trait( |
| 38 | PolyTraitRef { |
| 39 | bound_lifetimes: Vec::new(), |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 40 | trait_ref: "Debug".into(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 41 | }, |
| 42 | TraitBoundModifier::None, |
| 43 | ), |
| 44 | ], |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame^] | 45 | })], |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 46 | }, |
| 47 | }; |
| 48 | |
| 49 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 50 | let tokens = quote! { |
| 51 | impl #impl_generics MyTrait for Test #ty_generics #where_clause {} |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 52 | }; |
David Tolnay | 93413a5 | 2016-10-24 13:02:36 -0700 | [diff] [blame] | 53 | let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ", |
| 54 | "MyTrait for Test < 'a , 'b , T > ", |
| 55 | "where T : Debug { }"); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 56 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 57 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 58 | let turbofish = ty_generics.as_turbofish(); |
| 59 | let tokens = quote! { |
| 60 | Test #turbofish |
| 61 | }; |
| 62 | let expected = "Test :: < 'a , 'b , T >"; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 63 | assert_eq!(expected, tokens.to_string()); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 64 | } |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 65 | |
| 66 | #[test] |
| 67 | fn test_ty_param_bound() { |
| 68 | let tokens = quote!('a); |
| 69 | let expected = TyParamBound::Region(Lifetime::new("'a")); |
| 70 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 71 | |
| 72 | let tokens = quote!(Debug); |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame^] | 73 | let expected = TyParamBound::Trait(PolyTraitRef { |
| 74 | bound_lifetimes: Vec::new(), |
| 75 | trait_ref: "Debug".into(), |
| 76 | }, |
| 77 | TraitBoundModifier::None); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 78 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 79 | |
| 80 | let tokens = quote!(?Sized); |
David Tolnay | 2a78fc7 | 2017-03-12 18:28:54 -0700 | [diff] [blame^] | 81 | let expected = TyParamBound::Trait(PolyTraitRef { |
| 82 | bound_lifetimes: Vec::new(), |
| 83 | trait_ref: "Sized".into(), |
| 84 | }, |
| 85 | TraitBoundModifier::Maybe); |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 86 | assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap()); |
| 87 | } |