123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- #[macro_use]
- mod macros;
- use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
- use quote::quote;
- use std::iter::FromIterator;
- use syn::{Expr, ExprRange};
- #[test]
- fn test_expr_parse() {
- let tokens = quote!(..100u32);
- snapshot!(tokens as Expr, @r###"
- Expr::Range {
- limits: HalfOpen,
- to: Some(Expr::Lit {
- lit: 100u32,
- }),
- }
- "###);
- let tokens = quote!(..100u32);
- snapshot!(tokens as ExprRange, @r###"
- ExprRange {
- limits: HalfOpen,
- to: Some(Expr::Lit {
- lit: 100u32,
- }),
- }
- "###);
- }
- #[test]
- fn test_await() {
- // Must not parse as Expr::Field.
- let tokens = quote!(fut.await);
- snapshot!(tokens as Expr, @r###"
- Expr::Await {
- base: Expr::Path {
- path: Path {
- segments: [
- PathSegment {
- ident: "fut",
- arguments: None,
- },
- ],
- },
- },
- }
- "###);
- }
- #[rustfmt::skip]
- #[test]
- fn test_tuple_multi_index() {
- for &input in &[
- "tuple.0.0",
- "tuple .0.0",
- "tuple. 0.0",
- "tuple.0 .0",
- "tuple.0. 0",
- "tuple . 0 . 0",
- ] {
- snapshot!(input as Expr, @r###"
- Expr::Field {
- base: Expr::Field {
- base: Expr::Path {
- path: Path {
- segments: [
- PathSegment {
- ident: "tuple",
- arguments: None,
- },
- ],
- },
- },
- member: Unnamed(Index {
- index: 0,
- }),
- },
- member: Unnamed(Index {
- index: 0,
- }),
- }
- "###);
- }
- for tokens in vec![
- quote!(tuple.0.0),
- quote!(tuple .0.0),
- quote!(tuple. 0.0),
- quote!(tuple.0 .0),
- quote!(tuple.0. 0),
- quote!(tuple . 0 . 0),
- ] {
- snapshot!(tokens as Expr, @r###"
- Expr::Field {
- base: Expr::Field {
- base: Expr::Path {
- path: Path {
- segments: [
- PathSegment {
- ident: "tuple",
- arguments: None,
- },
- ],
- },
- },
- member: Unnamed(Index {
- index: 0,
- }),
- },
- member: Unnamed(Index {
- index: 0,
- }),
- }
- "###);
- }
- }
- #[test]
- fn test_macro_variable_func() {
- // mimics the token stream corresponding to `$fn()`
- let tokens = TokenStream::from_iter(vec![
- TokenTree::Group(Group::new(Delimiter::None, quote! { f })),
- TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
- ]);
- snapshot!(tokens as Expr, @r###"
- Expr::Call {
- func: Expr::Group {
- expr: Expr::Path {
- path: Path {
- segments: [
- PathSegment {
- ident: "f",
- arguments: None,
- },
- ],
- },
- },
- },
- }
- "###);
- let tokens = TokenStream::from_iter(vec![
- TokenTree::Punct(Punct::new('#', Spacing::Alone)),
- TokenTree::Group(Group::new(Delimiter::Bracket, quote! { outside })),
- TokenTree::Group(Group::new(Delimiter::None, quote! { #[inside] f })),
- TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
- ]);
- snapshot!(tokens as Expr, @r###"
- Expr::Call {
- attrs: [
- Attribute {
- style: Outer,
- path: Path {
- segments: [
- PathSegment {
- ident: "outside",
- arguments: None,
- },
- ],
- },
- tokens: TokenStream(``),
- },
- ],
- func: Expr::Group {
- expr: Expr::Path {
- attrs: [
- Attribute {
- style: Outer,
- path: Path {
- segments: [
- PathSegment {
- ident: "inside",
- arguments: None,
- },
- ],
- },
- tokens: TokenStream(``),
- },
- ],
- path: Path {
- segments: [
- PathSegment {
- ident: "f",
- arguments: None,
- },
- ],
- },
- },
- },
- }
- "###);
- }
- #[test]
- fn test_macro_variable_macro() {
- // mimics the token stream corresponding to `$macro!()`
- let tokens = TokenStream::from_iter(vec![
- TokenTree::Group(Group::new(Delimiter::None, quote! { m })),
- TokenTree::Punct(Punct::new('!', Spacing::Alone)),
- TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
- ]);
- snapshot!(tokens as Expr, @r###"
- Expr::Macro {
- mac: Macro {
- path: Path {
- segments: [
- PathSegment {
- ident: "m",
- arguments: None,
- },
- ],
- },
- delimiter: Paren,
- tokens: TokenStream(``),
- },
- }
- "###);
- }
- #[test]
- fn test_macro_variable_struct() {
- // mimics the token stream corresponding to `$struct {}`
- let tokens = TokenStream::from_iter(vec![
- TokenTree::Group(Group::new(Delimiter::None, quote! { S })),
- TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
- ]);
- snapshot!(tokens as Expr, @r###"
- Expr::Struct {
- path: Path {
- segments: [
- PathSegment {
- ident: "S",
- arguments: None,
- },
- ],
- },
- }
- "###);
- }
- #[test]
- fn test_macro_variable_match_arm() {
- // mimics the token stream corresponding to `match v { _ => $expr }`
- let tokens = TokenStream::from_iter(vec![
- TokenTree::Ident(Ident::new("match", Span::call_site())),
- TokenTree::Ident(Ident::new("v", Span::call_site())),
- TokenTree::Group(Group::new(
- Delimiter::Brace,
- TokenStream::from_iter(vec![
- TokenTree::Punct(Punct::new('_', Spacing::Alone)),
- TokenTree::Punct(Punct::new('=', Spacing::Joint)),
- TokenTree::Punct(Punct::new('>', Spacing::Alone)),
- TokenTree::Group(Group::new(Delimiter::None, quote! { #[a] () })),
- ]),
- )),
- ]);
- snapshot!(tokens as Expr, @r###"
- Expr::Match {
- expr: Expr::Path {
- path: Path {
- segments: [
- PathSegment {
- ident: "v",
- arguments: None,
- },
- ],
- },
- },
- arms: [
- Arm {
- pat: Pat::Wild,
- body: Expr::Group {
- expr: Expr::Tuple {
- attrs: [
- Attribute {
- style: Outer,
- path: Path {
- segments: [
- PathSegment {
- ident: "a",
- arguments: None,
- },
- ],
- },
- tokens: TokenStream(``),
- },
- ],
- },
- },
- },
- ],
- }
- "###);
- }
- // https://github.com/dtolnay/syn/issues/1019
- #[test]
- fn test_closure_vs_rangefull() {
- #[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/4808
- let tokens = quote!(|| .. .method());
- snapshot!(tokens as Expr, @r###"
- Expr::MethodCall {
- receiver: Expr::Closure {
- output: Default,
- body: Expr::Range {
- limits: HalfOpen,
- },
- },
- method: "method",
- }
- "###);
- }
- #[test]
- fn test_postfix_operator_after_cast() {
- syn::parse_str::<Expr>("|| &x as T[0]").unwrap_err();
- syn::parse_str::<Expr>("|| () as ()()").unwrap_err();
- }
|