blob: 18ec457d036383f4be351289e3752ef658a4dd2e [file] [log] [blame]
David Tolnayb153dbc2016-10-04 23:39:10 -07001extern crate syn;
2use syn::*;
3
David Tolnaye7678922016-10-13 20:44:03 -07004#[macro_use]
5extern crate quote;
David Tolnayb153dbc2016-10-04 23:39:10 -07006
7#[test]
8fn test_split_for_impl() {
David Tolnaye7678922016-10-13 20:44:03 -07009 // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug
David Tolnayb153dbc2016-10-04 23:39:10 -070010 let generics = Generics {
David Tolnay2a78fc72017-03-12 18:28:54 -070011 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,
Arnavionbf395bf2017-04-15 15:35:22 -070024 name: "may_dangle".into(),
25 tts: vec![],
David Tolnay2a78fc72017-03-12 18:28:54 -070026 is_sugared_doc: false,
27 }],
28 ident: Ident::new("T"),
29 bounds: vec![TyParamBound::Region(Lifetime::new("'a"))],
30 default: Some(Ty::Tup(Vec::new())),
31 }],
David Tolnayb153dbc2016-10-04 23:39:10 -070032 where_clause: WhereClause {
David Tolnay2a78fc72017-03-12 18:28:54 -070033 predicates: vec![WherePredicate::BoundPredicate(WhereBoundPredicate {
34 bound_lifetimes: Vec::new(),
35 bounded_ty:
36 Ty::Path(None, "T".into()),
37 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070038 TyParamBound::Trait(
39 PolyTraitRef {
40 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070041 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070042 },
43 TraitBoundModifier::None,
44 ),
45 ],
David Tolnay2a78fc72017-03-12 18:28:54 -070046 })],
David Tolnayb153dbc2016-10-04 23:39:10 -070047 },
48 };
49
50 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070051 let tokens = quote! {
52 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070053 };
David Tolnay93413a52016-10-24 13:02:36 -070054 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
55 "MyTrait for Test < 'a , 'b , T > ",
56 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080057 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070058
David Tolnayc879a502017-01-25 15:51:32 -080059 let turbofish = ty_generics.as_turbofish();
60 let tokens = quote! {
61 Test #turbofish
62 };
63 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070064 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070065}
David Tolnay23d83f92017-01-25 15:41:47 -080066
67#[test]
68fn test_ty_param_bound() {
69 let tokens = quote!('a);
70 let expected = TyParamBound::Region(Lifetime::new("'a"));
71 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
72
73 let tokens = quote!(Debug);
David Tolnay2a78fc72017-03-12 18:28:54 -070074 let expected = TyParamBound::Trait(PolyTraitRef {
75 bound_lifetimes: Vec::new(),
76 trait_ref: "Debug".into(),
77 },
78 TraitBoundModifier::None);
David Tolnay23d83f92017-01-25 15:41:47 -080079 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
80
81 let tokens = quote!(?Sized);
David Tolnay2a78fc72017-03-12 18:28:54 -070082 let expected = TyParamBound::Trait(PolyTraitRef {
83 bound_lifetimes: Vec::new(),
84 trait_ref: "Sized".into(),
85 },
86 TraitBoundModifier::Maybe);
David Tolnay23d83f92017-01-25 15:41:47 -080087 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
88}