Debian Codegraph @

third_party/rust/syn/tests/test_path.rs

raw
1 #![allow(clippy::needless_lifetimes, clippy::uninlined_format_args)]
2
3 #[macro_use]
4 mod macros;
5
6 use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
7 use quote::{quote, ToTokens};
8 use syn::{parse_quote, Expr, Type, TypePath};
9
10 #[test]
11 fn parse_interpolated_leading_component() {
12 // mimics the token stream corresponding to `$mod::rest`
13 let tokens = TokenStream::from_iter([
14 TokenTree::Group(Group::new(Delimiter::None, quote! { first })),
15 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
16 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
17 TokenTree::Ident(Ident::new("rest", Span::call_site())),
18 ]);
19
20 snapshot!(tokens.clone() as Expr, @r#"
21 Expr::Path {
22 path: Path {
23 segments: [
24 PathSegment {
25 ident: "first",
26 },
27 Token![::],
28 PathSegment {
29 ident: "rest",
30 },
31 ],
32 },
33 }
34 "#);
35
36 snapshot!(tokens as Type, @r#"
37 Type::Path {
38 path: Path {
39 segments: [
40 PathSegment {
41 ident: "first",
42 },
43 Token![::],
44 PathSegment {
45 ident: "rest",
46 },
47 ],
48 },
49 }
50 "#);
51 }
52
53 #[test]
54 fn print_incomplete_qpath() {
55 // qpath with `as` token
56 let mut ty: TypePath = parse_quote!(<Self as A>::Q);
57 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self as A > :: Q`)");
58 assert!(ty.path.segments.pop().is_some());
59 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self as A > ::`)");
60 assert!(ty.path.segments.pop().is_some());
61 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self >`)");
62 assert!(ty.path.segments.pop().is_none());
63
64 // qpath without `as` token
65 let mut ty: TypePath = parse_quote!(<Self>::A::B);
66 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self > :: A :: B`)");
67 assert!(ty.path.segments.pop().is_some());
68 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self > :: A ::`)");
69 assert!(ty.path.segments.pop().is_some());
70 snapshot!(ty.to_token_stream(), @"TokenStream(`< Self > ::`)");
71 assert!(ty.path.segments.pop().is_none());
72
73 // normal path
74 let mut ty: TypePath = parse_quote!(Self::A::B);
75 snapshot!(ty.to_token_stream(), @"TokenStream(`Self :: A :: B`)");
76 assert!(ty.path.segments.pop().is_some());
77 snapshot!(ty.to_token_stream(), @"TokenStream(`Self :: A ::`)");
78 assert!(ty.path.segments.pop().is_some());
79 snapshot!(ty.to_token_stream(), @"TokenStream(`Self ::`)");
80 assert!(ty.path.segments.pop().is_some());
81 snapshot!(ty.to_token_stream(), @"TokenStream(``)");
82 assert!(ty.path.segments.pop().is_none());
83 }
84
85 #[test]
86 fn parse_parenthesized_path_arguments_with_disambiguator() {
87 #[rustfmt::skip]
88 let tokens = quote!(dyn FnOnce::() -> !);
89 snapshot!(tokens as Type, @r#"
90 Type::TraitObject {
91 dyn_token: Some,
92 bounds: [
93 TypeParamBound::Trait(TraitBound {
94 path: Path {
95 segments: [
96 PathSegment {
97 ident: "FnOnce",
98 arguments: PathArguments::Parenthesized {
99 output: ReturnType::Type(
100 Type::Never,
101 ),
102 },
103 },
104 ],
105 },
106 }),
107 ],
108 }
109 "#);
110 }