blob: e18d2ef6af789f0908e6036ea1c78faf530a9221 [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
Alex Crichtonccbb45d2017-05-23 10:58:24 -070012 let mut generics = Generics {
13 gt_token: Some(Default::default()),
14 lt_token: Some(Default::default()),
15 lifetimes: vec![
16 LifetimeDef {
17 attrs: Default::default(),
18 lifetime: Lifetime::new("'a"),
19 bounds: Default::default(),
20 colon_token: None,
21 },
22 LifetimeDef {
23 attrs: Default::default(),
24 lifetime: Lifetime::new("'b"),
25 bounds: vec![Lifetime::new("'a")].into(),
26 colon_token: Some(tokens::Colon::default()),
27 },
28 ].into(),
29 ty_params: vec![
30 TyParam {
31 attrs: vec![Attribute {
32 bracket_token: Default::default(),
33 pound_token: Default::default(),
34 style: AttrStyle::Outer,
35 path: "may_dangle".into(),
36 tts: vec![],
37 is_sugared_doc: false,
38 }],
39 ident: "T".into(),
40 bounds: vec![TyParamBound::Region(Lifetime::new("'a"))].into(),
41 default: Some(TyTup {
42 tys: Default::default(),
43 lone_comma: None,
44 paren_token: Default::default(),
45 }.into()),
46 colon_token: Some(Default::default()),
47 eq_token: Default::default(),
48 },
49 ].into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070050 where_clause: WhereClause {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070051 where_token: Some(Default::default()),
52 predicates: vec![
53 WherePredicate::BoundPredicate(WhereBoundPredicate {
54 bound_lifetimes: None,
55 colon_token: Default::default(),
56 bounded_ty: TyPath {
57 qself: None,
58 path: "T".into(),
59 }.into(),
60 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070061 TyParamBound::Trait(
62 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 bound_lifetimes: None,
David Tolnaye7678922016-10-13 20:44:03 -070064 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070065 },
66 TraitBoundModifier::None,
67 ),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070068 ].into(),
69 })
70 ].into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070071 },
72 };
Alex Crichtonccbb45d2017-05-23 10:58:24 -070073 generics.lifetimes.push_trailing(Default::default());
David Tolnayb153dbc2016-10-04 23:39:10 -070074
75 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070076 let tokens = quote! {
77 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070078 };
David Tolnay93413a52016-10-24 13:02:36 -070079 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
80 "MyTrait for Test < 'a , 'b , T > ",
81 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080082 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070083
David Tolnayc879a502017-01-25 15:51:32 -080084 let turbofish = ty_generics.as_turbofish();
85 let tokens = quote! {
86 Test #turbofish
87 };
88 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070089 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070090}
David Tolnay23d83f92017-01-25 15:41:47 -080091
92#[test]
93fn test_ty_param_bound() {
94 let tokens = quote!('a);
95 let expected = TyParamBound::Region(Lifetime::new("'a"));
Alex Crichtonccbb45d2017-05-23 10:58:24 -070096 assert_eq!(expected, parse_ty_param_bound(&tokens.to_string()).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -080097
98 let tokens = quote!(Debug);
Alex Crichtonccbb45d2017-05-23 10:58:24 -070099 let expected = TyParamBound::Trait(
100 PolyTraitRef {
101 bound_lifetimes: None,
102 trait_ref: "Debug".into(),
103 },
104 TraitBoundModifier::None);
105 assert_eq!(expected, parse_ty_param_bound(&tokens.to_string()).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800106
107 let tokens = quote!(?Sized);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108 let expected = TyParamBound::Trait(
109 PolyTraitRef {
110 bound_lifetimes: None,
111 trait_ref: "Sized".into(),
112 },
113 TraitBoundModifier::Maybe(Default::default()));
114 assert_eq!(expected, parse_ty_param_bound(&tokens.to_string()).unwrap());
David Tolnay23d83f92017-01-25 15:41:47 -0800115}