Skip to content

Commit

Permalink
Merge pull request #1558 from dtolnay/tupletests
Browse files Browse the repository at this point in the history
Add tuple comma tests
  • Loading branch information
dtolnay authored Dec 25, 2023
2 parents 3cf16c7 + ec8517b commit ed9b94e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 6 deletions.
59 changes: 57 additions & 2 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
mod macros;

use proc_macro2::{Delimiter, Group};
use quote::quote;
use syn::{Expr, ExprRange, Stmt};
use quote::{quote, ToTokens as _};
use syn::punctuated::Punctuated;
use syn::{parse_quote, token, Expr, ExprRange, ExprTuple, Stmt, Token};

#[test]
fn test_expr_parse() {
Expand Down Expand Up @@ -483,3 +484,57 @@ fn test_extended_interpolated_path() {
}
"###);
}

#[test]
fn test_tuple_comma() {
let mut expr = ExprTuple {
attrs: Vec::new(),
paren_token: token::Paren::default(),
elems: Punctuated::new(),
};
snapshot!(expr.to_token_stream() as Expr, @"Expr::Tuple");

expr.elems.push_value(parse_quote!(continue));
// Must not parse to Expr::Paren
snapshot!(expr.to_token_stream() as Expr, @r###"
Expr::Tuple {
elems: [
Expr::Continue,
Token![,],
],
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Expr, @r###"
Expr::Tuple {
elems: [
Expr::Continue,
Token![,],
],
}
"###);

expr.elems.push_value(parse_quote!(continue));
snapshot!(expr.to_token_stream() as Expr, @r###"
Expr::Tuple {
elems: [
Expr::Continue,
Token![,],
Expr::Continue,
],
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Expr, @r###"
Expr::Tuple {
elems: [
Expr::Continue,
Token![,],
Expr::Continue,
Token![,],
],
}
"###);
}
56 changes: 54 additions & 2 deletions tests/test_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
mod macros;

use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
use quote::quote;
use quote::{quote, ToTokens as _};
use syn::parse::Parser;
use syn::{Item, Pat, Stmt};
use syn::punctuated::Punctuated;
use syn::{parse_quote, token, Item, Pat, PatTuple, Stmt, Token};

#[test]
fn test_pat_ident() {
Expand Down Expand Up @@ -95,3 +96,54 @@ fn test_ranges() {
Pat::parse_single.parse_str("[_, (..=hi), _]").unwrap();
Pat::parse_single.parse_str("[_, lo..=hi, _]").unwrap();
}

#[test]
fn test_tuple_comma() {
let mut expr = PatTuple {
attrs: Vec::new(),
paren_token: token::Paren::default(),
elems: Punctuated::new(),
};
snapshot!(expr.to_token_stream() as Pat, @"Pat::Tuple");

expr.elems.push_value(parse_quote!(_));
// FIXME: must parse to Pat::Tuple, not Pat::Paren
snapshot!(expr.to_token_stream() as Pat, @r###"
Pat::Paren {
pat: Pat::Wild,
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Pat, @r###"
Pat::Tuple {
elems: [
Pat::Wild,
Token![,],
],
}
"###);

expr.elems.push_value(parse_quote!(_));
snapshot!(expr.to_token_stream() as Pat, @r###"
Pat::Tuple {
elems: [
Pat::Wild,
Token![,],
Pat::Wild,
],
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Pat, @r###"
Pat::Tuple {
elems: [
Pat::Wild,
Token![,],
Pat::Wild,
Token![,],
],
}
"###);
}
58 changes: 56 additions & 2 deletions tests/test_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
mod macros;

use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::quote;
use syn::Type;
use quote::{quote, ToTokens as _};
use syn::punctuated::Punctuated;
use syn::{parse_quote, token, Token, Type, TypeTuple};

#[test]
fn test_mut_self() {
Expand Down Expand Up @@ -341,3 +342,56 @@ fn test_trailing_plus() {
}
"###);
}

#[test]
fn test_tuple_comma() {
let mut expr = TypeTuple {
paren_token: token::Paren::default(),
elems: Punctuated::new(),
};
snapshot!(expr.to_token_stream() as Type, @"Type::Tuple");

expr.elems.push_value(parse_quote!(_));
// Must not parse to Type::Paren
snapshot!(expr.to_token_stream() as Type, @r###"
Type::Tuple {
elems: [
Type::Infer,
Token![,],
],
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Type, @r###"
Type::Tuple {
elems: [
Type::Infer,
Token![,],
],
}
"###);

expr.elems.push_value(parse_quote!(_));
snapshot!(expr.to_token_stream() as Type, @r###"
Type::Tuple {
elems: [
Type::Infer,
Token![,],
Type::Infer,
],
}
"###);

expr.elems.push_punct(<Token![,]>::default());
snapshot!(expr.to_token_stream() as Type, @r###"
Type::Tuple {
elems: [
Type::Infer,
Token![,],
Type::Infer,
Token![,],
],
}
"###);
}

0 comments on commit ed9b94e

Please sign in to comment.