blob: f743b78e003f64f50fc66136364b63c9f8f20087 [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
Alex Crichton605643b2017-07-05 18:35:14 -07009extern crate proc_macro2;
10use proc_macro2::Term;
11
David Tolnayb153dbc2016-10-04 23:39:10 -070012#[test]
13fn test_split_for_impl() {
David Tolnaye7678922016-10-13 20:44:03 -070014 // <'a, 'b: 'a, #[may_dangle] T: 'a = ()> where T: Debug
Alex Crichtonccbb45d2017-05-23 10:58:24 -070015 let mut generics = Generics {
16 gt_token: Some(Default::default()),
17 lt_token: Some(Default::default()),
18 lifetimes: vec![
19 LifetimeDef {
20 attrs: Default::default(),
Alex Crichton605643b2017-07-05 18:35:14 -070021 lifetime: Lifetime::new(Term::intern("'a"), Span::default()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070022 bounds: Default::default(),
23 colon_token: None,
24 },
25 LifetimeDef {
26 attrs: Default::default(),
Alex Crichton605643b2017-07-05 18:35:14 -070027 lifetime: Lifetime::new(Term::intern("'b"), Span::default()),
28 bounds: vec![Lifetime::new(Term::intern("'a"), Span::default())].into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 colon_token: Some(tokens::Colon::default()),
30 },
31 ].into(),
32 ty_params: vec![
33 TyParam {
34 attrs: vec![Attribute {
35 bracket_token: Default::default(),
36 pound_token: Default::default(),
37 style: AttrStyle::Outer,
38 path: "may_dangle".into(),
39 tts: vec![],
40 is_sugared_doc: false,
41 }],
42 ident: "T".into(),
Alex Crichton605643b2017-07-05 18:35:14 -070043 bounds: vec![TyParamBound::Region(Lifetime::new(Term::intern("'a"), Span::default()))].into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070044 default: Some(TyTup {
45 tys: Default::default(),
46 lone_comma: None,
47 paren_token: Default::default(),
48 }.into()),
49 colon_token: Some(Default::default()),
50 eq_token: Default::default(),
51 },
52 ].into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070053 where_clause: WhereClause {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 where_token: Some(Default::default()),
55 predicates: vec![
56 WherePredicate::BoundPredicate(WhereBoundPredicate {
57 bound_lifetimes: None,
58 colon_token: Default::default(),
59 bounded_ty: TyPath {
60 qself: None,
61 path: "T".into(),
62 }.into(),
63 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070064 TyParamBound::Trait(
65 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 bound_lifetimes: None,
David Tolnaye7678922016-10-13 20:44:03 -070067 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070068 },
69 TraitBoundModifier::None,
70 ),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 ].into(),
72 })
73 ].into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070074 },
75 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 generics.lifetimes.push_trailing(Default::default());
David Tolnayb153dbc2016-10-04 23:39:10 -070077
78 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070079 let tokens = quote! {
80 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070081 };
David Tolnay93413a52016-10-24 13:02:36 -070082 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
83 "MyTrait for Test < 'a , 'b , T > ",
84 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080085 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070086
David Tolnayc879a502017-01-25 15:51:32 -080087 let turbofish = ty_generics.as_turbofish();
88 let tokens = quote! {
89 Test #turbofish
90 };
91 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070092 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070093}
David Tolnay23d83f92017-01-25 15:41:47 -080094
95#[test]
96fn test_ty_param_bound() {
97 let tokens = quote!('a);
Alex Crichton605643b2017-07-05 18:35:14 -070098 let expected = TyParamBound::Region(Lifetime::new(Term::intern("'a"), Span::default()));
Alex Crichton954046c2017-05-30 21:49:42 -070099 assert_eq!(expected, tokens.to_string().parse().unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800100
101 let tokens = quote!(Debug);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700102 let expected = TyParamBound::Trait(
103 PolyTraitRef {
104 bound_lifetimes: None,
105 trait_ref: "Debug".into(),
106 },
107 TraitBoundModifier::None);
Alex Crichton954046c2017-05-30 21:49:42 -0700108 assert_eq!(expected, tokens.to_string().parse().unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800109
110 let tokens = quote!(?Sized);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111 let expected = TyParamBound::Trait(
112 PolyTraitRef {
113 bound_lifetimes: None,
114 trait_ref: "Sized".into(),
115 },
116 TraitBoundModifier::Maybe(Default::default()));
Alex Crichton954046c2017-05-30 21:49:42 -0700117 assert_eq!(expected, tokens.to_string().parse().unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800118}