blob: 08886bb7adc10aa139cb016f52900792ec924c61 [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,
Arnavione43b1b02017-04-19 02:47:45 -070024 path: "may_dangle".into(),
Arnavionbf395bf2017-04-15 15:35:22 -070025 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"))],
Alex Crichton62a0a592017-05-22 13:58:53 -070030 default: Some(TyTup { tys: Vec::new() }.into()),
David Tolnay2a78fc72017-03-12 18:28:54 -070031 }],
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(),
Alex Crichton62a0a592017-05-22 13:58:53 -070035 bounded_ty: TyPath {
36 qself: None,
37 path: "T".into(),
38 }.into(),
David Tolnay2a78fc72017-03-12 18:28:54 -070039 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070040 TyParamBound::Trait(
41 PolyTraitRef {
42 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070043 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070044 },
45 TraitBoundModifier::None,
46 ),
47 ],
David Tolnay2a78fc72017-03-12 18:28:54 -070048 })],
David Tolnayb153dbc2016-10-04 23:39:10 -070049 },
50 };
51
52 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070053 let tokens = quote! {
54 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070055 };
David Tolnay93413a52016-10-24 13:02:36 -070056 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
57 "MyTrait for Test < 'a , 'b , T > ",
58 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080059 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070060
David Tolnayc879a502017-01-25 15:51:32 -080061 let turbofish = ty_generics.as_turbofish();
62 let tokens = quote! {
63 Test #turbofish
64 };
65 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070066 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070067}
David Tolnay23d83f92017-01-25 15:41:47 -080068
69#[test]
70fn 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 Tolnay2a78fc72017-03-12 18:28:54 -070076 let expected = TyParamBound::Trait(PolyTraitRef {
77 bound_lifetimes: Vec::new(),
78 trait_ref: "Debug".into(),
79 },
80 TraitBoundModifier::None);
David Tolnay23d83f92017-01-25 15:41:47 -080081 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
82
83 let tokens = quote!(?Sized);
David Tolnay2a78fc72017-03-12 18:28:54 -070084 let expected = TyParamBound::Trait(PolyTraitRef {
85 bound_lifetimes: Vec::new(),
86 trait_ref: "Sized".into(),
87 },
88 TraitBoundModifier::Maybe);
David Tolnay23d83f92017-01-25 15:41:47 -080089 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
90}