blob: 822d22d67d8fb72fb626a28b195ccdd7576d5f24 [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 {
11 lifetimes: vec![
12 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -070013 attrs: Vec::new(),
David Tolnayb153dbc2016-10-04 23:39:10 -070014 lifetime: Lifetime::new("'a"),
15 bounds: Vec::new(),
16 },
17 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -070018 attrs: Vec::new(),
David Tolnayb153dbc2016-10-04 23:39:10 -070019 lifetime: Lifetime::new("'b"),
20 bounds: vec![
21 Lifetime::new("'a"),
22 ],
23 },
24 ],
25 ty_params: vec![
26 TyParam {
David Tolnaye7678922016-10-13 20:44:03 -070027 attrs: vec![
28 Attribute {
29 style: AttrStyle::Outer,
30 value: MetaItem::Word("may_dangle".into()),
31 is_sugared_doc: false,
32 },
33 ],
David Tolnayb153dbc2016-10-04 23:39:10 -070034 ident: Ident::new("T"),
35 bounds: vec![
36 TyParamBound::Region(Lifetime::new("'a")),
37 ],
38 default: Some(Ty::Tup(Vec::new())),
39 },
40 ],
41 where_clause: WhereClause {
42 predicates: vec![
43 WherePredicate::BoundPredicate(WhereBoundPredicate {
44 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070045 bounded_ty: Ty::Path(None, "T".into()),
David Tolnayb153dbc2016-10-04 23:39:10 -070046 bounds: vec![
47 TyParamBound::Trait(
48 PolyTraitRef {
49 bound_lifetimes: Vec::new(),
David Tolnaye7678922016-10-13 20:44:03 -070050 trait_ref: "Debug".into(),
David Tolnayb153dbc2016-10-04 23:39:10 -070051 },
52 TraitBoundModifier::None,
53 ),
54 ],
55 }),
56 ],
57 },
58 };
59
60 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
David Tolnaye7678922016-10-13 20:44:03 -070061 let tokens = quote! {
62 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070063 };
David Tolnay93413a52016-10-24 13:02:36 -070064 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
65 "MyTrait for Test < 'a , 'b , T > ",
66 "where T : Debug { }");
David Tolnayc879a502017-01-25 15:51:32 -080067 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070068
David Tolnayc879a502017-01-25 15:51:32 -080069 let turbofish = ty_generics.as_turbofish();
70 let tokens = quote! {
71 Test #turbofish
72 };
73 let expected = "Test :: < 'a , 'b , T >";
David Tolnaye7678922016-10-13 20:44:03 -070074 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070075}
David Tolnay23d83f92017-01-25 15:41:47 -080076
77#[test]
78fn test_ty_param_bound() {
79 let tokens = quote!('a);
80 let expected = TyParamBound::Region(Lifetime::new("'a"));
81 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
82
83 let tokens = quote!(Debug);
84 let expected = TyParamBound::Trait(
85 PolyTraitRef {
86 bound_lifetimes: Vec::new(),
87 trait_ref: "Debug".into(),
88 },
89 TraitBoundModifier::None,
90 );
91 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
92
93 let tokens = quote!(?Sized);
94 let expected = TyParamBound::Trait(
95 PolyTraitRef {
96 bound_lifetimes: Vec::new(),
97 trait_ref: "Sized".into(),
98 },
99 TraitBoundModifier::Maybe,
100 );
101 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
102}