interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / syn-2.0.119 / tests / test_visibility.rs

191 lines · 5149 bytes Raw Blame History
1#![allow(
2 clippy::elidable_lifetime_names,
3 clippy::needless_lifetimes,
4 clippy::uninlined_format_args
5)]
6
7#[macro_use]
8mod snapshot;
9
10mod debug;
11
12use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
13use quote::quote;
14use syn::parse::{Parse, ParseStream};
15use syn::{DeriveInput, Result, Visibility};
16
17#[derive(Debug)]
18struct VisRest {
19 vis: Visibility,
20 rest: TokenStream,
21}
22
23impl 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
32macro_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]
57fn test_pub() {
58 assert_vis_parse!("pub", Ok(Visibility::Public(_)));
59}
60
61#[test]
62fn test_inherited() {
63 assert_vis_parse!("", Ok(Visibility::Inherited));
64}
65
66#[test]
67fn test_in() {
68 assert_vis_parse!("pub(in foo::bar)", Ok(Visibility::Restricted(_)));
69}
70
71#[test]
72fn test_pub_crate() {
73 assert_vis_parse!("pub(crate)", Ok(Visibility::Restricted(_)));
74}
75
76#[test]
77fn test_pub_self() {
78 assert_vis_parse!("pub(self)", Ok(Visibility::Restricted(_)));
79}
80
81#[test]
82fn test_pub_super() {
83 assert_vis_parse!("pub(super)", Ok(Visibility::Restricted(_)));
84}
85
86#[test]
87fn test_missing_in() {
88 assert_vis_parse!("pub(foo::bar)", Ok(Visibility::Public(_)) + "(foo::bar)");
89}
90
91#[test]
92fn test_missing_in_path() {
93 assert_vis_parse!("pub(in)", Err);
94}
95
96#[test]
97fn 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]
105fn test_junk_after_in() {
106 assert_vis_parse!("pub(in some::path @@garbage)", Err);
107}
108
109#[test]
110fn 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 ident: Some("f"),
137 colon_token: Some,
138 ty: Type::Tuple,
139 },
140 ],
141 },
142 },
143 }
144 "#);
145}
146
147#[test]
148fn test_inherited_vis_unnamed_field() {
149 // mimics `struct S($vis $ty);` where $vis is empty
150 let tokens = TokenStream::from_iter([
151 TokenTree::Ident(Ident::new("struct", Span::call_site())),
152 TokenTree::Ident(Ident::new("S", Span::call_site())),
153 TokenTree::Group(Group::new(
154 Delimiter::Parenthesis,
155 TokenStream::from_iter([
156 TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())),
157 TokenTree::Group(Group::new(Delimiter::None, quote!(str))),
158 ]),
159 )),
160 TokenTree::Punct(Punct::new(';', Spacing::Alone)),
161 ]);
162
163 snapshot!(tokens as DeriveInput, @r#"
164 DeriveInput {
165 vis: Visibility::Inherited,
166 ident: "S",
167 generics: Generics,
168 data: Data::Struct {
169 fields: Fields::Unnamed {
170 unnamed: [
171 Field {
172 vis: Visibility::Inherited,
173 ty: Type::Group {
174 elem: Type::Path {
175 path: Path {
176 segments: [
177 PathSegment {
178 ident: "str",
179 },
180 ],
181 },
182 },
183 },
184 },
185 ],
186 },
187 semi_token: Some,
188 },
189 }
190 "#);
191}