From e1e7337239066677dd6bc82b826e861cf6710836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 18:14:05 +0100 Subject: expand test-files in modules --- sylt_macro/src/lib.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'sylt_macro/src') diff --git a/sylt_macro/src/lib.rs b/sylt_macro/src/lib.rs index 0f1a584..c306592 100644 --- a/sylt_macro/src/lib.rs +++ b/sylt_macro/src/lib.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::Path; use proc_macro::TokenStream; use quote::{format_ident, quote}; @@ -161,8 +161,8 @@ pub fn link(tokens: TokenStream) -> TokenStream { TokenStream::from(tokens) } -fn find_test_paths(directory: &Path) -> Vec { - let mut tests = Vec::new(); +fn find_test_paths(directory: &Path) -> proc_macro2::TokenStream { + let mut tests = quote! {}; for entry in std::fs::read_dir(directory).unwrap() { let path = entry.unwrap().path(); @@ -173,31 +173,31 @@ fn find_test_paths(directory: &Path) -> Vec { } if path.is_dir() { - tests.append(&mut find_test_paths(&path)); + tests.extend(find_test_paths(&path)); } else { assert!(!path.to_str().unwrap().contains(","), "You should be ashamed."); - tests.push(path); + + let path = path.to_str().unwrap(); + let test_name = format_ident!("file_{}", file_name.replace(".sy", "")); + let tokens = quote! { + test_file!(#test_name, #path); + }; + tests.extend(tokens); } } - tests + let directory = directory.file_name().unwrap().to_str().unwrap().replace("/", ""); + let directory = format_ident!("{}", directory); + quote! { + mod #directory { + #tests + } + } } #[proc_macro] pub fn find_tests(tokens: TokenStream) -> TokenStream { assert!(tokens.is_empty()); - let tests: Vec<_> = find_test_paths(Path::new("progs/")).iter().map(|path| { - let path = path.to_str().unwrap(); - let test_name = format_ident!("{}", path.replace("/", "_").replace(".sy", "")); - quote! { - test_file!(#test_name, #path); - } - }).collect(); - - let tokens = quote! { - #(#tests)* - }; - - TokenStream::from(tokens) + TokenStream::from(find_test_paths(Path::new("progs/"))) } -- cgit v1.2.1