diff --git a/rstest_macros/src/error.rs b/rstest_macros/src/error.rs index a38eac3..bc521d3 100644 --- a/rstest_macros/src/error.rs +++ b/rstest_macros/src/error.rs @@ -32,7 +32,7 @@ pub(crate) fn fixture(test: &ItemFn, info: &FixtureInfo) -> TokenStream { } fn async_once<'a>(test: &'a ItemFn, info: &FixtureInfo) -> Errors<'a> { - match (test.sig.asyncness, info.attributes.get_once()) { + match (test.sig.asyncness, info.arguments.get_once()) { (Some(_asyncness), Some(once)) => Box::new(std::iter::once(syn::Error::new( once.span(), "Cannot apply #[once] to async fixture.", @@ -69,7 +69,7 @@ fn has_some_generics(test: &ItemFn) -> bool { } fn generics_once<'a>(test: &'a ItemFn, info: &FixtureInfo) -> Errors<'a> { - match (has_some_generics(test), info.attributes.get_once()) { + match (has_some_generics(test), info.arguments.get_once()) { (true, Some(once)) => Box::new(std::iter::once(syn::Error::new( once.span(), "Cannot apply #[once] on generic fixture.", diff --git a/rstest_macros/src/parse/fixture.rs b/rstest_macros/src/parse/fixture.rs index 07e3a8a..5b8dd69 100644 --- a/rstest_macros/src/parse/fixture.rs +++ b/rstest_macros/src/parse/fixture.rs @@ -11,12 +11,7 @@ use super::{ extract_defaults, extract_fixtures, extract_partials_return_type, future::extract_futures, parse_vector_trailing_till_double_comma, Attributes, ExtendWithFunctionAttrs, Fixture, }; -use crate::{ - error::ErrorsVec, - parse::extract_once, - refident::{MaybeIdent, RefIdent}, - utils::attr_is, -}; +use crate::{error::ErrorsVec, parse::extract_once, refident::RefIdent, utils::attr_is}; use crate::{parse::Attribute, utils::attr_in}; use proc_macro2::TokenStream; use quote::{format_ident, ToTokens}; @@ -83,9 +78,7 @@ impl ExtendWithFunctionAttrs for FixtureInfo { for (id, return_type) in partials_return_type { self.attributes.set_partial_return_type(id, return_type); } - if let Some(ident) = once { - self.attributes.set_once(ident) - }; + self.arguments.set_once(once); let (futures, global_awt) = futures; self.arguments.set_global_await(global_awt); self.arguments.set_futures(futures.into_iter()); @@ -300,20 +293,6 @@ impl FixtureModifiers { )) } - pub(crate) fn set_once(&mut self, once: syn::Ident) { - self.inner.attributes.push(Attribute::Attr(once)) - } - - pub(crate) fn get_once(&self) -> Option<&Ident> { - self.iter() - .find(|&a| a == &Attribute::Attr(format_ident!("once"))) - .and_then(|a| a.maybe_ident()) - } - - pub(crate) fn is_once(&self) -> bool { - self.get_once().is_some() - } - fn extract_type(&self, attr_name: &str) -> Option { self.iter() .filter_map(|m| match m { @@ -579,7 +558,7 @@ mod extend { info.extend_with_function_attrs(&mut item_fn).unwrap(); - assert!(info.attributes.is_once()); + assert!(info.arguments.is_once()); } #[test] @@ -593,7 +572,7 @@ mod extend { info.extend_with_function_attrs(&mut item_fn).unwrap(); - assert!(!info.attributes.is_once()); + assert!(!info.arguments.is_once()); } #[rstest] diff --git a/rstest_macros/src/parse/mod.rs b/rstest_macros/src/parse/mod.rs index 60ef4c6..2d0c687 100644 --- a/rstest_macros/src/parse/mod.rs +++ b/rstest_macros/src/parse/mod.rs @@ -674,6 +674,7 @@ pub(crate) mod arguments { pub(crate) struct ArgumentsInfo { args: HashMap, is_global_await: bool, + once: Option } impl ArgumentsInfo { @@ -714,6 +715,18 @@ pub(crate) mod arguments { pub(crate) fn is_global_await(&self) -> bool { self.is_global_await } + + pub(crate) fn set_once(&mut self, once: Option) { + self.once = once + } + + pub(crate) fn get_once(&self) -> Option<&Ident> { + self.once.as_ref() + } + + pub(crate) fn is_once(&self) -> bool { + self.get_once().is_some() + } } #[cfg(test)] diff --git a/rstest_macros/src/render/fixture.rs b/rstest_macros/src/render/fixture.rs index 2ef5227..c528dcb 100644 --- a/rstest_macros/src/render/fixture.rs +++ b/rstest_macros/src/render/fixture.rs @@ -69,7 +69,7 @@ pub(crate) fn render(mut fixture: ItemFn, info: FixtureInfo) -> TokenStream { let call_get = render_exec_call(parse_quote! { Self::get }, args, asyncness.is_some()); let mut call_impl = render_exec_call(parse_quote! { #name }, args, asyncness.is_some()); - if info.attributes.is_once() { + if info.arguments.is_once() { call_impl = wrap_call_impl_with_call_once_impl(call_impl, &output); output = wrap_return_type_as_static_ref(output); default_output = wrap_return_type_as_static_ref(default_output); @@ -110,7 +110,7 @@ fn render_partial_impl( .extract_partial_type(n) .unwrap_or_else(|| fixture.sig.output.clone()); - if info.attributes.is_once() { + if info.arguments.is_once() { output = wrap_return_type_as_static_ref(output); } diff --git a/rstest_macros/src/test.rs b/rstest_macros/src/test.rs index cd12d95..d540251 100644 --- a/rstest_macros/src/test.rs +++ b/rstest_macros/src/test.rs @@ -307,14 +307,7 @@ impl DisplayCode for T { impl crate::parse::fixture::FixtureInfo { pub(crate) fn with_once(mut self) -> Self { - self.attributes = self.attributes.with_once(); - self - } -} - -impl crate::parse::fixture::FixtureModifiers { - pub(crate) fn with_once(mut self) -> Self { - self.append(Attribute::attr("once")); + self.arguments.set_once(Some(ident("once"))); self } }