blob: 92ae9e9ceb4fc51eb3adc34fa1b06fe5c531bdc1 [file] [log] [blame]
Alex Crichton2e0229c2017-05-23 09:34:50 -07001#![cfg(feature = "extra-traits")]
2
David Tolnayb153dbc2016-10-04 23:39:10 -07003extern crate syn;
4use syn::*;
5
David Tolnaye7678922016-10-13 20:44:03 -07006#[macro_use]
7extern crate quote;
David Tolnayb153dbc2016-10-04 23:39:10 -07008
9#[test]
10fn test_split_for_impl() {
David Tolnaye7678922016-10-13 20:44:03 -070011 // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug
David Tolnayb153dbc2016-10-04 23:39:10 -070012 let generics = Generics {
David Tolnay2a78fc72017-03-12 18:28:54 -070013 lifetimes: vec![LifetimeDef {
14 attrs: Vec::new(),
15 lifetime: Lifetime::new("'a"),
16 bounds: Vec::new(),
17 },
18 LifetimeDef {
19 attrs: Vec::new(),
20 lifetime: Lifetime::new("'b"),
21 bounds: vec![Lifetime::new("'a")],
22 }],
23 ty_params: vec![TyParam {
24 attrs: vec![Attribute {
25 style: AttrStyle::Outer,
Arnavione43b1b02017-04-19 02:47:45 -070026 path: "may_dangle".into(),
Arnavionbf395bf2017-04-15 15:35:22 -070027 tts: vec![],
David Tolnay2a78fc72017-03-12 18:28:54 -070028 is_sugared_doc: false,
29 }],
30 ident: Ident::new("T"),
31 bounds: vec![TyParamBound::Region(Lifetime::new("'a"))],
Alex Crichton62a0a592017-05-22 13:58:53 -070032 default: Some(TyTup { tys: Vec::new() }.into()),
David Tolnay2a78fc72017-03-12 18:28:54 -070033 }],
David Tolnayb153dbc2016-10-04 23:39:10 -070034 where_clause: WhereClause {
David Tolnay2a78fc72017-03-12 18:28:54 -070035 predicates: vec![WherePredicate::BoundPredicate(WhereBoundPredicate {
36 bound_lifetimes: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -070037 bounded_ty: TyPath {
38 qself: None,
39 path: "T".into(),
40 }.into(),
David Tolnay2a78fc72017-03-12 18:28:54 -070041 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070042 TyParamBound::Trait(
43 PolyTraitRef {
44 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070045 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070046 },
47 TraitBoundModifier::None,
48 ),
49 ],
David Tolnay2a78fc72017-03-12 18:28:54 -070050 })],
David Tolnayb153dbc2016-10-04 23:39:10 -070051 },
52 };
53
54 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070055 let tokens = quote! {
56 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070057 };
David Tolnay93413a52016-10-24 13:02:36 -070058 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
59 "MyTrait for Test < 'a , 'b , T > ",
60 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080061 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070062
David Tolnayc879a502017-01-25 15:51:32 -080063 let turbofish = ty_generics.as_turbofish();
64 let tokens = quote! {
65 Test #turbofish
66 };
67 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070068 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070069}
David Tolnay23d83f92017-01-25 15:41:47 -080070
71#[test]
72fn test_ty_param_bound() {
73 let tokens = quote!('a);
74 let expected = TyParamBound::Region(Lifetime::new("'a"));
75 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
76
77 let tokens = quote!(Debug);
David Tolnay2a78fc72017-03-12 18:28:54 -070078 let expected = TyParamBound::Trait(PolyTraitRef {
79 bound_lifetimes: Vec::new(),
80 trait_ref: "Debug".into(),
81 },
82 TraitBoundModifier::None);
David Tolnay23d83f92017-01-25 15:41:47 -080083 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
84
85 let tokens = quote!(?Sized);
David Tolnay2a78fc72017-03-12 18:28:54 -070086 let expected = TyParamBound::Trait(PolyTraitRef {
87 bound_lifetimes: Vec::new(),
88 trait_ref: "Sized".into(),
89 },
90 TraitBoundModifier::Maybe);
David Tolnay23d83f92017-01-25 15:41:47 -080091 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
92}