blob: 0cdce4e37dbf17880e342046271968b8a4d5dc3c [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,
24 value: MetaItem::Word("may_dangle".into()),
25 is_sugared_doc: false,
26 }],
27 ident: Ident::new("T"),
28 bounds: vec![TyParamBound::Region(Lifetime::new("'a"))],
29 default: Some(Ty::Tup(Vec::new())),
30 }],
David Tolnayb153dbc2016-10-04 23:39:10 -070031 where_clause: WhereClause {
David Tolnay2a78fc72017-03-12 18:28:54 -070032 predicates: vec![WherePredicate::BoundPredicate(WhereBoundPredicate {
33 bound_lifetimes: Vec::new(),
34 bounded_ty:
35 Ty::Path(None, "T".into()),
36 bounds: vec![
David Tolnayb153dbc2016-10-04 23:39:10 -070037 TyParamBound::Trait(
38 PolyTraitRef {
39 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070040 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070041 },
42 TraitBoundModifier::None,
43 ),
44 ],
David Tolnay2a78fc72017-03-12 18:28:54 -070045 })],
David Tolnayb153dbc2016-10-04 23:39:10 -070046 },
47 };
48
49 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070050 let tokens = quote! {
51 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070052 };
David Tolnay93413a52016-10-24 13:02:36 -070053 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
54 "MyTrait for Test < 'a , 'b , T > ",
55 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080056 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070057
David Tolnayc879a502017-01-25 15:51:32 -080058 let turbofish = ty_generics.as_turbofish();
59 let tokens = quote! {
60 Test #turbofish
61 };
62 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070063 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070064}
David Tolnay23d83f92017-01-25 15:41:47 -080065
66#[test]
67fn test_ty_param_bound() {
68 let tokens = quote!('a);
69 let expected = TyParamBound::Region(Lifetime::new("'a"));
70 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
71
72 let tokens = quote!(Debug);
David Tolnay2a78fc72017-03-12 18:28:54 -070073 let expected = TyParamBound::Trait(PolyTraitRef {
74 bound_lifetimes: Vec::new(),
75 trait_ref: "Debug".into(),
76 },
77 TraitBoundModifier::None);
David Tolnay23d83f92017-01-25 15:41:47 -080078 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
79
80 let tokens = quote!(?Sized);
David Tolnay2a78fc72017-03-12 18:28:54 -070081 let expected = TyParamBound::Trait(PolyTraitRef {
82 bound_lifetimes: Vec::new(),
83 trait_ref: "Sized".into(),
84 },
85 TraitBoundModifier::Maybe);
David Tolnay23d83f92017-01-25 15:41:47 -080086 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
87}