Debian Codegraph @

.cargo/vendor/syn/tests/test_path.rs

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