diff options
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | sylt_macro/Cargo.toml | 5 | ||||
| -rw-r--r-- | sylt_macro/src/lib.rs | 38 |
4 files changed, 26 insertions, 26 deletions
@@ -589,6 +589,7 @@ dependencies = [ name = "sylt_macro" version = "0.1.0" dependencies = [ + "proc-macro2", "quote", "syn", ] @@ -802,11 +802,9 @@ pub struct Prog { #[cfg(test)] mod tests { - use std::path::Path; - use crate::error::ErrorKind; - use super::{run_file, run_string}; + use super::run_string; #[macro_export] macro_rules! assert_errs { @@ -912,8 +910,8 @@ mod tests { ($fn:ident, $path:literal) => { #[test] fn $fn() { - let file = Path::new($path); - run_file(&file, true, Vec::new()).unwrap(); + let file = std::path::Path::new($path); + crate::run_file(&file, true, Vec::new()).unwrap(); } }; } diff --git a/sylt_macro/Cargo.toml b/sylt_macro/Cargo.toml index 9ac045e..6d3c75f 100644 --- a/sylt_macro/Cargo.toml +++ b/sylt_macro/Cargo.toml @@ -10,5 +10,6 @@ proc-macro = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -syn = { version = "1.0", features=["full"] } -quote = "1.0" +syn = { version = "1", features=["full"] } +proc-macro2 = "1" +quote = "1" 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<PathBuf> { - 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<PathBuf> { } 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/"))) } |
