Skip to content

Commit

Permalink
refactor: clean up the macro implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JasterV committed Feb 26, 2024
1 parent 890a323 commit 3585ac5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions test-context-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ proc-macro = true
[dependencies]
quote = "1.0.3"
syn = { version = "^2", features = ["full"] }
proc-macro2 = "1.0.78"
79 changes: 44 additions & 35 deletions test-context-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;

/// Macro to use on tests to add the setup/teardown functionality of your context.
///
Expand Down Expand Up @@ -36,42 +37,9 @@ pub fn test_context(attr: TokenStream, item: TokenStream) -> TokenStream {
let wrapped_name = format_ident!("__test_context_wrapped_{}", name);

let outer_body = if is_async {
quote! {
{
use test_context::futures::FutureExt;
let mut ctx = <#context_type as test_context::AsyncTestContext>::setup().await;
let wrapped_ctx = &mut ctx;
let result = async move {
std::panic::AssertUnwindSafe(
#wrapped_name(wrapped_ctx)
).catch_unwind().await
}.await;
<#context_type as test_context::AsyncTestContext>::teardown(ctx).await;
match result {
Ok(returned_value) => returned_value,
Err(err) => {
std::panic::resume_unwind(err);
}
}
}
}
async_implementation(context_type, &wrapped_name)
} else {
quote! {
{
let mut ctx = <#context_type as test_context::TestContext>::setup();
let mut wrapper = std::panic::AssertUnwindSafe(&mut ctx);
let result = std::panic::catch_unwind(move || {
#wrapped_name(*wrapper)
});
<#context_type as test_context::TestContext>::teardown(ctx);
match result {
Ok(returned_value) => returned_value,
Err(err) => {
std::panic::resume_unwind(err);
}
}
}
}
sync_implementation(context_type, &wrapped_name)
};

let async_tag = if is_async {
Expand All @@ -88,3 +56,44 @@ pub fn test_context(attr: TokenStream, item: TokenStream) -> TokenStream {
};
result.into()
}

fn async_implementation(context_type: Ident, wrapped_name: &Ident) -> proc_macro2::TokenStream {
quote! {
{
use test_context::futures::FutureExt;
let mut ctx = <#context_type as test_context::AsyncTestContext>::setup().await;
let wrapped_ctx = &mut ctx;
let result = async move {
std::panic::AssertUnwindSafe(
#wrapped_name(wrapped_ctx)
).catch_unwind().await
}.await;
<#context_type as test_context::AsyncTestContext>::teardown(ctx).await;
match result {
Ok(returned_value) => returned_value,
Err(err) => {
std::panic::resume_unwind(err);
}
}
}
}
}

fn sync_implementation(context_type: Ident, wrapped_name: &Ident) -> proc_macro2::TokenStream {
quote! {
{
let mut ctx = <#context_type as test_context::TestContext>::setup();
let mut wrapper = std::panic::AssertUnwindSafe(&mut ctx);
let result = std::panic::catch_unwind(move || {
#wrapped_name(*wrapper)
});
<#context_type as test_context::TestContext>::teardown(ctx);
match result {
Ok(returned_value) => returned_value,
Err(err) => {
std::panic::resume_unwind(err);
}
}
}
}
}

0 comments on commit 3585ac5

Please sign in to comment.