aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / syn / tests / test_visibility.rs
| 1 | #![allow( |
| 2 | clippy::elidable_lifetime_names, |
| 3 | clippy::needless_lifetimes, |
| 4 | clippy::uninlined_format_args |
| 5 | )] |
| 6 | |
| 7 | #[macro_use] |
| 8 | mod snapshot; |
| 9 | |
| 10 | mod debug; |
| 11 | |
| 12 | use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree}; |
| 13 | use quote::quote; |
| 14 | use syn::parse::{Parse, ParseStream}; |
| 15 | use syn::{DeriveInput, Result, Visibility}; |
| 16 | |
| 17 | #[derive(Debug)] |
| 18 | struct VisRest { |
| 19 | vis: Visibility, |
| 20 | rest: TokenStream, |
| 21 | } |
| 22 | |
| 23 | impl Parse for VisRest { |
| 24 | fn parse(input: ParseStream) -> Result<Self> { |
| 25 | Ok(VisRest { |
| 26 | vis: input.parse()?, |
| 27 | rest: input.parse()?, |
| 28 | }) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | macro_rules! assert_vis_parse { |
| 33 | ($input:expr, Ok($p:pat)) => { |
| 34 | assert_vis_parse!($input, Ok($p) + ""); |
| 35 | }; |
| 36 | |
| 37 | ($input:expr, Ok($p:pat) + $rest:expr) => { |
| 38 | let expected = $rest.parse::<TokenStream>().unwrap(); |
| 39 | let parse: VisRest = syn::parse_str($input).unwrap(); |
| 40 | |
| 41 | match parse.vis { |
| 42 | $p => {} |
| 43 | _ => panic!("expected {}, got {:?}", stringify!($p), parse.vis), |
| 44 | } |
| 45 | |
| 46 | // NOTE: Round-trips through `to_string` to avoid potential whitespace |
| 47 | // diffs. |
| 48 | assert_eq!(parse.rest.to_string(), expected.to_string()); |
| 49 | }; |
| 50 | |
| 51 | ($input:expr, Err) => { |
| 52 | syn::parse2::<VisRest>($input.parse().unwrap()).unwrap_err(); |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn test_pub() { |
| 58 | assert_vis_parse!("pub", Ok(Visibility::Public(_))); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn test_inherited() { |
| 63 | assert_vis_parse!("", Ok(Visibility::Inherited)); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn test_in() { |
| 68 | assert_vis_parse!("pub(in foo::bar)", Ok(Visibility::Restricted(_))); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn test_pub_crate() { |
| 73 | assert_vis_parse!("pub(crate)", Ok(Visibility::Restricted(_))); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_pub_self() { |
| 78 | assert_vis_parse!("pub(self)", Ok(Visibility::Restricted(_))); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn test_pub_super() { |
| 83 | assert_vis_parse!("pub(super)", Ok(Visibility::Restricted(_))); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn test_missing_in() { |
| 88 | assert_vis_parse!("pub(foo::bar)", Ok(Visibility::Public(_)) + "(foo::bar)"); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn test_missing_in_path() { |
| 93 | assert_vis_parse!("pub(in)", Err); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_crate_path() { |
| 98 | assert_vis_parse!( |
| 99 | "pub(crate::A, crate::B)", |
| 100 | Ok(Visibility::Public(_)) + "(crate::A, crate::B)" |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_junk_after_in() { |
| 106 | assert_vis_parse!("pub(in some::path @@garbage)", Err); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn test_inherited_vis_named_field() { |
| 111 | // mimics `struct S { $vis $field: () }` where $vis is empty |
| 112 | let tokens = TokenStream::from_iter([ |
| 113 | TokenTree::Ident(Ident::new("struct", Span::call_site())), |
| 114 | TokenTree::Ident(Ident::new("S", Span::call_site())), |
| 115 | TokenTree::Group(Group::new( |
| 116 | Delimiter::Brace, |
| 117 | TokenStream::from_iter([ |
| 118 | TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())), |
| 119 | TokenTree::Group(Group::new(Delimiter::None, quote!(f))), |
| 120 | TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 121 | TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())), |
| 122 | ]), |
| 123 | )), |
| 124 | ]); |
| 125 | |
| 126 | snapshot!(tokens as DeriveInput, @r#" |
| 127 | DeriveInput { |
| 128 | vis: Visibility::Inherited, |
| 129 | ident: "S", |
| 130 | generics: Generics, |
| 131 | data: Data::Struct { |
| 132 | fields: Fields::Named { |
| 133 | named: [ |
| 134 | Field { |
| 135 | vis: Visibility::Inherited, |
| 136 | modifiers: FieldModifiers, |
| 137 | ident: Some("f"), |
| 138 | colon_token: Some, |
| 139 | ty: Type::Tuple, |
| 140 | }, |
| 141 | ], |
| 142 | }, |
| 143 | }, |
| 144 | } |
| 145 | "#); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn test_inherited_vis_unnamed_field() { |
| 150 | // mimics `struct S($vis $ty);` where $vis is empty |
| 151 | let tokens = TokenStream::from_iter([ |
| 152 | TokenTree::Ident(Ident::new("struct", Span::call_site())), |
| 153 | TokenTree::Ident(Ident::new("S", Span::call_site())), |
| 154 | TokenTree::Group(Group::new( |
| 155 | Delimiter::Parenthesis, |
| 156 | TokenStream::from_iter([ |
| 157 | TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())), |
| 158 | TokenTree::Group(Group::new(Delimiter::None, quote!(str))), |
| 159 | ]), |
| 160 | )), |
| 161 | TokenTree::Punct(Punct::new(';', Spacing::Alone)), |
| 162 | ]); |
| 163 | |
| 164 | snapshot!(tokens as DeriveInput, @r#" |
| 165 | DeriveInput { |
| 166 | vis: Visibility::Inherited, |
| 167 | ident: "S", |
| 168 | generics: Generics, |
| 169 | data: Data::Struct { |
| 170 | fields: Fields::Unnamed { |
| 171 | unnamed: [ |
| 172 | Field { |
| 173 | vis: Visibility::Inherited, |
| 174 | modifiers: FieldModifiers, |
| 175 | ty: Type::Group { |
| 176 | elem: Type::Path { |
| 177 | path: Path { |
| 178 | segments: [ |
| 179 | PathSegment { |
| 180 | ident: "str", |
| 181 | }, |
| 182 | ], |
| 183 | }, |
| 184 | }, |
| 185 | }, |
| 186 | }, |
| 187 | ], |
| 188 | }, |
| 189 | semi_token: Some, |
| 190 | }, |
| 191 | } |
| 192 | "#); |
| 193 | } |