mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
create a basic test proc macro that takes a db path and an mvcc flag
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -846,6 +846,7 @@ dependencies = [
|
||||
"tracing-subscriber",
|
||||
"turso",
|
||||
"turso_core",
|
||||
"turso_macros",
|
||||
"turso_parser",
|
||||
"twox-hash",
|
||||
"zerocopy 0.8.26",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
mod ext;
|
||||
extern crate proc_macro;
|
||||
mod atomic_enum;
|
||||
mod ext;
|
||||
mod test;
|
||||
|
||||
use proc_macro::{token_stream::IntoIter, Group, TokenStream, TokenTree};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -492,3 +494,8 @@ pub fn match_ignore_ascii_case(input: TokenStream) -> TokenStream {
|
||||
pub fn derive_atomic_enum(input: TokenStream) -> TokenStream {
|
||||
atomic_enum::derive_atomic_enum_inner(input)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
test::test_macro_attribute(args, input)
|
||||
}
|
||||
|
||||
91
macros/src/test.rs
Normal file
91
macros/src/test.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
parse_macro_input,
|
||||
punctuated::Punctuated,
|
||||
ItemFn, Meta, Token,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Args {
|
||||
path: Option<String>,
|
||||
mvcc: bool,
|
||||
}
|
||||
|
||||
impl Parse for Args {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let args = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
|
||||
let mut seen_args = HashSet::new();
|
||||
|
||||
let mut path = None;
|
||||
let mut mvcc = false;
|
||||
|
||||
let errors = args
|
||||
.into_iter()
|
||||
.filter_map(|meta| {
|
||||
match meta {
|
||||
Meta::NameValue(nv) => {
|
||||
let ident = nv.path.get_ident();
|
||||
if nv.path.is_ident("path") {
|
||||
if let syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(lit_str),
|
||||
..
|
||||
}) = &nv.value
|
||||
{
|
||||
path = Some(lit_str.value());
|
||||
seen_args.insert(ident.unwrap().clone());
|
||||
} else {
|
||||
return Some(syn::Error::new_spanned(
|
||||
nv.value,
|
||||
"argument is not a string literal",
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Some(syn::Error::new_spanned(nv.path, "unexpected argument"));
|
||||
}
|
||||
}
|
||||
Meta::Path(p) => {
|
||||
let ident = p.get_ident();
|
||||
if p.is_ident("mvcc") {
|
||||
mvcc = true;
|
||||
seen_args.insert(ident.unwrap().clone());
|
||||
} else {
|
||||
return Some(syn::Error::new_spanned(p, "unexpected flag"));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Some(syn::Error::new_spanned(meta, "unexpected argument format"));
|
||||
}
|
||||
};
|
||||
None
|
||||
})
|
||||
.reduce(|mut accum, err| {
|
||||
accum.combine(err);
|
||||
accum
|
||||
});
|
||||
|
||||
dbg!(&errors);
|
||||
if let Some(errors) = errors {
|
||||
return Err(errors);
|
||||
}
|
||||
|
||||
Ok(Args { path, mvcc })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_macro_attribute(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
eprintln!("args: {args} \ninput : {input}");
|
||||
let input = parse_macro_input!(input as ItemFn);
|
||||
|
||||
let mut args = parse_macro_input!(args as Args);
|
||||
|
||||
dbg!(&args);
|
||||
|
||||
quote::quote! {
|
||||
#[test]
|
||||
#input
|
||||
}
|
||||
.into()
|
||||
}
|
||||
@@ -40,6 +40,7 @@ tracing = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
test-log = { version = "0.2.17", features = ["trace"] }
|
||||
turso_macros.workspace = true
|
||||
|
||||
[features]
|
||||
default = ["test_helper"]
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex};
|
||||
use turso_core::{Connection, LimboError, Result, StepResult};
|
||||
|
||||
#[allow(clippy::arc_with_non_send_sync)]
|
||||
#[test]
|
||||
#[turso_macros::test(mvcc, path = 1)]
|
||||
fn test_wal_checkpoint_result() -> Result<()> {
|
||||
maybe_setup_tracing();
|
||||
let tmp_db = TempDatabase::new("test_wal.db");
|
||||
|
||||
Reference in New Issue
Block a user