Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn-2.0.119/tests/test_pat.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn-2.0.119 / tests / test_pat.rs` 158 lines ยท 4575 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] 8 mod snapshot; 9 10 mod debug; 11 12 use proc_macro2::{Delimiter, Group, TokenStream, TokenTree}; 13 use quote::{quote, ToTokens as _}; 14 use syn::parse::Parser; 15 use syn::punctuated::Punctuated; 16 use syn::{parse_quote, token, Item, Pat, PatTuple, Stmt, Token}; 17 18 #[test] 19 fn test_pat_ident() { 20 match Pat::parse_single.parse2(quote!(self)).unwrap() { 21 Pat::Ident(_) => (), 22 value => panic!("expected PatIdent, got {:?}", value), 23 } 24 } 25 26 #[test] 27 fn test_pat_path() { 28 match Pat::parse_single.parse2(quote!(self::CONST)).unwrap() { 29 Pat::Path(_) => (), 30 value => panic!("expected PatPath, got {:?}", value), 31 } 32 } 33 34 #[test] 35 fn test_leading_vert() { 36 // https://github.com/rust-lang/rust/blob/1.43.0/src/test/ui/or-patterns/remove-leading-vert.rs 37 38 syn::parse_str::("fn f() {}").unwrap(); 39 syn::parse_str::("fn fun1(| A: E) {}").unwrap_err(); 40 syn::parse_str::("fn fun2(|| A: E) {}").unwrap_err(); 41 42 syn::parse_str::("let | () = ();").unwrap_err(); 43 syn::parse_str::("let (| A): E;").unwrap(); 44 syn::parse_str::("let (|| A): (E);").unwrap_err(); 45 syn::parse_str::("let (| A,): (E,);").unwrap(); 46 syn::parse_str::("let [| A]: [E; 1];").unwrap(); 47 syn::parse_str::("let [|| A]: [E; 1];").unwrap_err(); 48 syn::parse_str::("let TS(| A): TS;").unwrap(); 49 syn::parse_str::("let TS(|| A): TS;").unwrap_err(); 50 syn::parse_str::("let NS { f: | A }: NS;").unwrap(); 51 syn::parse_str::("let NS { f: || A }: NS;").unwrap_err(); 52 } 53 54 #[test] 55 fn test_group() { 56 let group = Group::new(Delimiter::None, quote!(Some(_))); 57 let tokens = TokenStream::from_iter([TokenTree::Group(group)]); 58 let pat = Pat::parse_single.parse2(tokens).unwrap(); 59 60 snapshot!(pat, @r#" 61 Pat::TupleStruct { 62 path: Path { 63 segments: [ 64 PathSegment { 65 ident: "Some", 66 }, 67 ], 68 }, 69 elems: [ 70 Pat::Wild, 71 ], 72 } 73 "#); 74 } 75 76 #[test] 77 fn test_ranges() { 78 Pat::parse_single.parse_str("..").unwrap(); 79 Pat::parse_single.parse_str("..hi").unwrap(); 80 Pat::parse_single.parse_str("lo..").unwrap(); 81 Pat::parse_single.parse_str("lo..hi").unwrap(); 82 83 Pat::parse_single.parse_str("..=").unwrap_err(); 84 Pat::parse_single.parse_str("..=hi").unwrap(); 85 Pat::parse_single.parse_str("lo..=").unwrap_err(); 86 Pat::parse_single.parse_str("lo..=hi").unwrap(); 87 88 Pat::parse_single.parse_str("...").unwrap_err(); 89 Pat::parse_single.parse_str("...hi").unwrap_err(); 90 Pat::parse_single.parse_str("lo...").unwrap_err(); 91 Pat::parse_single.parse_str("lo...hi").unwrap(); 92 93 Pat::parse_single.parse_str("[lo..]").unwrap_err(); 94 Pat::parse_single.parse_str("[..=hi]").unwrap_err(); 95 Pat::parse_single.parse_str("[(lo..)]").unwrap(); 96 Pat::parse_single.parse_str("[(..=hi)]").unwrap(); 97 Pat::parse_single.parse_str("[lo..=hi]").unwrap(); 98 99 Pat::parse_single.parse_str("[_, lo.., _]").unwrap_err(); 100 Pat::parse_single.parse_str("[_, ..=hi, _]").unwrap_err(); 101 Pat::parse_single.parse_str("[_, (lo..), _]").unwrap(); 102 Pat::parse_single.parse_str("[_, (..=hi), _]").unwrap(); 103 Pat::parse_single.parse_str("[_, lo..=hi, _]").unwrap(); 104 } 105 106 #[test] 107 fn test_tuple_comma() { 108 let mut expr = PatTuple { 109 attrs: Vec::new(), 110 paren_token: token::Paren::default(), 111 elems: Punctuated::new(), 112 }; 113 snapshot!(expr.to_token_stream() as Pat, @"Pat::Tuple"); 114 115 expr.elems.push_value(parse_quote!(_)); 116 // Must not parse to Pat::Paren 117 snapshot!(expr.to_token_stream() as Pat, @r#" 118 Pat::Tuple { 119 elems: [ 120 Pat::Wild, 121 Token![,], 122 ], 123 } 124 "#); 125 126 expr.elems.push_punct(::default()); 127 snapshot!(expr.to_token_stream() as Pat, @r#" 128 Pat::Tuple { 129 elems: [ 130 Pat::Wild, 131 Token![,], 132 ], 133 } 134 "#); 135 136 expr.elems.push_value(parse_quote!(_)); 137 snapshot!(expr.to_token_stream() as Pat, @r#" 138 Pat::Tuple { 139 elems: [ 140 Pat::Wild, 141 Token![,], 142 Pat::Wild, 143 ], 144 } 145 "#); 146 147 expr.elems.push_punct(::default()); 148 snapshot!(expr.to_token_stream() as Pat, @r#" 149 Pat::Tuple { 150 elems: [ 151 Pat::Wild, 152 Token![,], 153 Pat::Wild, 154 Token![,], 155 ], 156 } 157 "#); 158 }