1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
use proc_macro2::{Ident, Span, TokenStream}; use quote::{quote, ToTokens, TokenStreamExt}; use syn::parse::{self, Parse, ParseStream}; use crate::scope::Scope; use crate::section_body::SectionBody; use crate::section_item::SectionItem; use crate::section_keyword::SectionKeyword; use crate::utils; use utils::extract_literal_string; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Section { section_kind: SectionKeyword, name: String, body: SectionBody, } impl Section { pub fn new( section_kind: SectionKeyword, name: impl ToString, body: SectionBody, ) -> Self { Self { section_kind, name: name.to_string(), body, } } fn quote_name(&self) -> Ident { let name = utils::escape_name(&self.name); let kind = self.section_kind.to_name(); let name = if kind.is_empty() { name } else { format!("{}_{}", kind, name) }; Ident::new(&name, Span::call_site()) } pub fn quote_inner(&self, scope: Scope) -> TokenStream { let mut token_stream = TokenStream::default(); self.to_tokens_inner(scope, &mut token_stream); token_stream } pub fn peek(input: ParseStream) -> bool { SectionKeyword::peek(input) } fn to_tokens_inner(&self, scope: Scope, tokens: &mut TokenStream) { if self.body.is_top_level() { let my_stmts: Vec<_> = self.body.items().iter().filter_map(|i| i.stmt()).collect(); let name = self.quote_name(); let inner = scope.quote_with(&my_stmts); tokens.append_all(quote! { #[test] fn #name() { #inner } }); return; } let mut stream = vec![]; for (idx, item) in self.body.items().iter().enumerate() { if let SectionItem::Sep(section) = item { let sb = self.body.get_stmts_before(idx); let sa = self.body.get_stmts_after(idx); let mut scope = scope.clone(); scope.push_mut(&sb, &sa); let inner = section.quote_inner(scope); stream.push(inner); } } let name = self.quote_name(); tokens.append_all(quote! { mod #name { use super::*; #(#stream)* } }); } } impl ToTokens for Section { fn to_tokens(&self, tokens: &mut TokenStream) { let scope = Scope::empty(); self.to_tokens_inner(scope, tokens); } } impl Parse for Section { fn parse(input: ParseStream) -> parse::Result<Self> { let section_keyword: SectionKeyword = input.parse()?; let name: syn::Lit = input.parse()?; let name = extract_literal_string(name).ok_or_else(|| { parse::Error::new(Span::call_site(), "Invalid section literal") })?; let content; syn::braced!(content in input); let inner_body = content.parse::<SectionBody>()?; Ok(Section::new(section_keyword, name, inner_body)) } } #[cfg(test)] mod tests { use super::*; use test_case::test_case; #[test_case( r#" section "tests" { let x = 1; when "hello" { assert!(true); then "whatever" { assert!(true); } } assert_eq!(x, 1); } "#, quote!( mod section_tests { use super::*; mod when_hello { use super::*; #[test] fn then_whatever() { { let x = 1; { assert!(true); { assert!(true); } } assert_eq!(x, 1); } } } } ) )] #[test_case( r#" section "tests" { assert!(1 == 1); case "one" { assert!(2 == 2); } assert!(3 == 3); case "two" { assert!(4 == 4); } assert!(5 == 5); } "#, quote!( mod section_tests { use super::*; #[test] fn case_one() { { assert!(1 == 1); { assert!(2 == 2); } assert!(3 == 3); assert!(5 == 5); } } #[test] fn case_two() { { assert!(1 == 1); assert!(3 == 3); { assert!(4 == 4); } assert!(5 == 5); } } } ) )] fn parse_and_quote(s: &str, exp: TokenStream) { let section = syn::parse_str::<Section>(s).unwrap(); let section = section.to_token_stream(); assert_eq!(exp.to_string(), section.to_string()); } }