aboutsummaryrefslogtreecommitdiffstats
path: root/sylt_macro
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-03-05 18:14:05 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-03-05 18:14:05 +0100
commite1e7337239066677dd6bc82b826e861cf6710836 (patch)
tree191a2ee77dece3272dccd494b4a6992bb5873247 /sylt_macro
parentba2a7b3290102e3573cc7b2727026f4efe8a1d9f (diff)
downloadsylt-e1e7337239066677dd6bc82b826e861cf6710836.tar.gz
expand test-files in modules
Diffstat (limited to 'sylt_macro')
-rw-r--r--sylt_macro/Cargo.toml5
-rw-r--r--sylt_macro/src/lib.rs38
2 files changed, 22 insertions, 21 deletions
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/")))
}