From f8d523e0b362d52a4c525277cc95da08ed3766f3 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Thu, 6 Jun 2024 23:26:28 +0200 Subject: [PATCH] Allow attributes after #[timeout] --- ntest_timeout/src/lib.rs | 2 ++ ntest_timeout/tests/timeout.rs | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 ntest_timeout/tests/timeout.rs diff --git a/ntest_timeout/src/lib.rs b/ntest_timeout/src/lib.rs index 18cc48b..49a5ca2 100644 --- a/ntest_timeout/src/lib.rs +++ b/ntest_timeout/src/lib.rs @@ -56,8 +56,10 @@ pub fn timeout(attr: TokenStream, item: TokenStream) -> TokenStream { let sig = &input.sig; let output = &sig.output; let body = &input.block; + let attrs = &input.attrs; check_other_attributes(&input); let result = quote! { + #(#attrs)* #vis #sig { fn ntest_callback() #output #body diff --git a/ntest_timeout/tests/timeout.rs b/ntest_timeout/tests/timeout.rs new file mode 100644 index 0000000..113b543 --- /dev/null +++ b/ntest_timeout/tests/timeout.rs @@ -0,0 +1,46 @@ +use ntest_timeout::timeout; +use std::{thread, time}; + +#[test] +#[timeout(100)] +fn no_timeout_1() { + let fifty_millis = time::Duration::from_millis(50); + thread::sleep(fifty_millis); +} + +#[timeout(100)] +#[test] +fn no_timeout_2() { + let fifty_millis = time::Duration::from_millis(50); + thread::sleep(fifty_millis); +} + +#[test] +#[timeout(10)] +#[should_panic] +fn timeout_1() { + loop {} +} + +#[timeout(10)] +#[should_panic] +#[test] +fn timeout_2() { + loop {} +} + +#[test] +#[timeout(100)] +fn timeout_with_result_1() -> Result<(), String> { + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + Ok(()) +} + +#[timeout(100)] +#[test] +fn timeout_with_result_2() -> Result<(), String> { + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + Ok(()) +}