David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 1 | extern crate syn; |
| 2 | use syn::*; |
| 3 | |
| 4 | fn simple_path(name: &'static str) -> Path { |
| 5 | Path { |
| 6 | global: false, |
| 7 | segments: vec![ |
| 8 | PathSegment { |
| 9 | ident: Ident::new(name), |
| 10 | parameters: PathParameters::none(), |
| 11 | }, |
| 12 | ], |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | #[test] |
| 17 | fn test_split_for_impl() { |
| 18 | // <'a, 'b: 'a, T: 'a = ()> where T: Debug |
| 19 | let generics = Generics { |
| 20 | lifetimes: vec![ |
| 21 | LifetimeDef { |
| 22 | lifetime: Lifetime::new("'a"), |
| 23 | bounds: Vec::new(), |
| 24 | }, |
| 25 | LifetimeDef { |
| 26 | lifetime: Lifetime::new("'b"), |
| 27 | bounds: vec![ |
| 28 | Lifetime::new("'a"), |
| 29 | ], |
| 30 | }, |
| 31 | ], |
| 32 | ty_params: vec![ |
| 33 | TyParam { |
| 34 | ident: Ident::new("T"), |
| 35 | bounds: vec![ |
| 36 | TyParamBound::Region(Lifetime::new("'a")), |
| 37 | ], |
| 38 | default: Some(Ty::Tup(Vec::new())), |
| 39 | }, |
| 40 | ], |
| 41 | where_clause: WhereClause { |
| 42 | predicates: vec![ |
| 43 | WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 44 | bound_lifetimes: Vec::new(), |
| 45 | bounded_ty: Ty::Path(None, simple_path("T")), |
| 46 | bounds: vec![ |
| 47 | TyParamBound::Trait( |
| 48 | PolyTraitRef { |
| 49 | bound_lifetimes: Vec::new(), |
| 50 | trait_ref: simple_path("Debug"), |
| 51 | }, |
| 52 | TraitBoundModifier::None, |
| 53 | ), |
| 54 | ], |
| 55 | }), |
| 56 | ], |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 61 | |
| 62 | // <'a, 'b: 'a, T: 'a> |
| 63 | let expected_impl_generics = Generics { |
| 64 | lifetimes: vec![ |
| 65 | LifetimeDef { |
| 66 | lifetime: Lifetime::new("'a"), |
| 67 | bounds: Vec::new(), |
| 68 | }, |
| 69 | LifetimeDef { |
| 70 | lifetime: Lifetime::new("'b"), |
| 71 | bounds: vec![ |
| 72 | Lifetime::new("'a"), |
| 73 | ], |
| 74 | }, |
| 75 | ], |
| 76 | ty_params: vec![ |
| 77 | TyParam { |
| 78 | ident: Ident::new("T"), |
| 79 | bounds: vec![ |
| 80 | TyParamBound::Region(Lifetime::new("'a")), |
| 81 | ], |
| 82 | default: None, |
| 83 | }, |
| 84 | ], |
| 85 | where_clause: WhereClause::none(), |
| 86 | }; |
| 87 | |
| 88 | // <'a, 'b, T> |
| 89 | let expected_ty_generics = Generics { |
| 90 | lifetimes: vec![ |
| 91 | LifetimeDef { |
| 92 | lifetime: Lifetime::new("'a"), |
| 93 | bounds: Vec::new(), |
| 94 | }, |
| 95 | LifetimeDef { |
| 96 | lifetime: Lifetime::new("'b"), |
| 97 | bounds: Vec::new(), |
| 98 | }, |
| 99 | ], |
| 100 | ty_params: vec![ |
| 101 | TyParam { |
| 102 | ident: Ident::new("T"), |
| 103 | bounds: Vec::new(), |
| 104 | default: None, |
| 105 | }, |
| 106 | ], |
| 107 | where_clause: WhereClause::none(), |
| 108 | }; |
| 109 | |
| 110 | assert_eq!(impl_generics, expected_impl_generics); |
| 111 | assert_eq!(ty_generics, expected_ty_generics); |
| 112 | assert_eq!(where_clause, generics.where_clause); |
| 113 | } |