| 1 | #[macro_use ] |
| 2 | mod macros; |
| 3 | |
| 4 | use syn::punctuated::Punctuated; |
| 5 | use syn::{parse_quote, Attribute, Field, Lit, Pat, Stmt, Token}; |
| 6 | |
| 7 | #[test] |
| 8 | fn test_attribute() { |
| 9 | let attr: Attribute = parse_quote!(#[test]); |
| 10 | snapshot!(attr, @r###" |
| 11 | Attribute { |
| 12 | style: AttrStyle::Outer, |
| 13 | meta: Meta::Path { |
| 14 | segments: [ |
| 15 | PathSegment { |
| 16 | ident: "test", |
| 17 | }, |
| 18 | ], |
| 19 | }, |
| 20 | } |
| 21 | "### ); |
| 22 | |
| 23 | let attr: Attribute = parse_quote!(#![no_std]); |
| 24 | snapshot!(attr, @r###" |
| 25 | Attribute { |
| 26 | style: AttrStyle::Inner, |
| 27 | meta: Meta::Path { |
| 28 | segments: [ |
| 29 | PathSegment { |
| 30 | ident: "no_std", |
| 31 | }, |
| 32 | ], |
| 33 | }, |
| 34 | } |
| 35 | "### ); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn test_field() { |
| 40 | let field: Field = parse_quote!(pub enabled: bool); |
| 41 | snapshot!(field, @r###" |
| 42 | Field { |
| 43 | vis: Visibility::Public, |
| 44 | ident: Some("enabled"), |
| 45 | colon_token: Some, |
| 46 | ty: Type::Path { |
| 47 | path: Path { |
| 48 | segments: [ |
| 49 | PathSegment { |
| 50 | ident: "bool", |
| 51 | }, |
| 52 | ], |
| 53 | }, |
| 54 | }, |
| 55 | } |
| 56 | "### ); |
| 57 | |
| 58 | let field: Field = parse_quote!(primitive::bool); |
| 59 | snapshot!(field, @r###" |
| 60 | Field { |
| 61 | vis: Visibility::Inherited, |
| 62 | ty: Type::Path { |
| 63 | path: Path { |
| 64 | segments: [ |
| 65 | PathSegment { |
| 66 | ident: "primitive", |
| 67 | }, |
| 68 | PathSegment { |
| 69 | ident: "bool", |
| 70 | }, |
| 71 | ], |
| 72 | }, |
| 73 | }, |
| 74 | } |
| 75 | "### ); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_pat() { |
| 80 | let pat: Pat = parse_quote!(Some(false) | None); |
| 81 | snapshot!(&pat, @r###" |
| 82 | Pat::Or { |
| 83 | cases: [ |
| 84 | Pat::TupleStruct { |
| 85 | path: Path { |
| 86 | segments: [ |
| 87 | PathSegment { |
| 88 | ident: "Some", |
| 89 | }, |
| 90 | ], |
| 91 | }, |
| 92 | elems: [ |
| 93 | Pat::Lit(ExprLit { |
| 94 | lit: Lit::Bool { |
| 95 | value: false, |
| 96 | }, |
| 97 | }), |
| 98 | ], |
| 99 | }, |
| 100 | Pat::Ident { |
| 101 | ident: "None", |
| 102 | }, |
| 103 | ], |
| 104 | } |
| 105 | "### ); |
| 106 | |
| 107 | let boxed_pat: Box<Pat> = parse_quote!(Some(false) | None); |
| 108 | assert_eq!(*boxed_pat, pat); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_punctuated() { |
| 113 | let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true); |
| 114 | snapshot!(punctuated.pairs(), @r###" |
| 115 | [ |
| 116 | Lit::Bool { |
| 117 | value: true, |
| 118 | }, |
| 119 | Or, |
| 120 | Lit::Bool { |
| 121 | value: true, |
| 122 | }, |
| 123 | ] |
| 124 | "### ); |
| 125 | |
| 126 | let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true |); |
| 127 | snapshot!(punctuated.pairs(), @r###" |
| 128 | [ |
| 129 | Lit::Bool { |
| 130 | value: true, |
| 131 | }, |
| 132 | Or, |
| 133 | Lit::Bool { |
| 134 | value: true, |
| 135 | }, |
| 136 | Or, |
| 137 | ] |
| 138 | "### ); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn test_vec_stmt() { |
| 143 | let stmts: Vec<Stmt> = parse_quote! { |
| 144 | let _; |
| 145 | true |
| 146 | }; |
| 147 | snapshot!(stmts, @r###" |
| 148 | [ |
| 149 | Stmt::Local { |
| 150 | pat: Pat::Wild, |
| 151 | }, |
| 152 | Stmt::Expr( |
| 153 | Expr::Lit { |
| 154 | lit: Lit::Bool { |
| 155 | value: true, |
| 156 | }, |
| 157 | }, |
| 158 | None, |
| 159 | ), |
| 160 | ] |
| 161 | "### ); |
| 162 | } |
| 163 | |