blob: 3e2fd42fa353272268c3690757d09ed38716c4e1 [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();
61
David Tolnaye7678922016-10-13 20:44:03 -070062 let tokens = quote! {
63 impl #impl_generics MyTrait for Test #ty_generics #where_clause {}
David Tolnayb153dbc2016-10-04 23:39:10 -070064 };
65
David Tolnay93413a52016-10-24 13:02:36 -070066 let expected = concat!("impl < 'a , 'b : 'a , # [ may_dangle ] T : 'a > ",
67 "MyTrait for Test < 'a , 'b , T > ",
68 "where T : Debug { }");
David Tolnayb153dbc2016-10-04 23:39:10 -070069
David Tolnaye7678922016-10-13 20:44:03 -070070 assert_eq!(expected, tokens.to_string());
David Tolnayb153dbc2016-10-04 23:39:10 -070071}
David Tolnay23d83f92017-01-25 15:41:47 -080072
73#[test]
74fn test_ty_param_bound() {
75 let tokens = quote!('a);
76 let expected = TyParamBound::Region(Lifetime::new("'a"));
77 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
78
79 let tokens = quote!(Debug);
80 let expected = TyParamBound::Trait(
81 PolyTraitRef {
82 bound_lifetimes: Vec::new(),
83 trait_ref: "Debug".into(),
84 },
85 TraitBoundModifier::None,
86 );
87 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
88
89 let tokens = quote!(?Sized);
90 let expected = TyParamBound::Trait(
91 PolyTraitRef {
92 bound_lifetimes: Vec::new(),
93 trait_ref: "Sized".into(),
94 },
95 TraitBoundModifier::Maybe,
96 );
97 assert_eq!(expected, parse_ty_param_bound(tokens.as_str()).unwrap());
98}