Skip to content

Commit

Permalink
Refactoring. Move once from Attributes to
Browse files Browse the repository at this point in the history
ArgumentsInfo
  • Loading branch information
la10736 committed Mar 22, 2023
1 parent 2fb380e commit c16601d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
4 changes: 2 additions & 2 deletions rstest_macros/src/error.rs
Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down
29 changes: 4 additions & 25 deletions rstest_macros/src/parse/fixture.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<syn::ReturnType> {
self.iter()
.filter_map(|m| match m {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions rstest_macros/src/parse/mod.rs
Expand Up @@ -674,6 +674,7 @@ pub(crate) mod arguments {
pub(crate) struct ArgumentsInfo {
args: HashMap<Ident, ArgumentInfo>,
is_global_await: bool,
once: Option<Ident>
}

impl ArgumentsInfo {
Expand Down Expand Up @@ -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<Ident>) {
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)]
Expand Down
4 changes: 2 additions & 2 deletions rstest_macros/src/render/fixture.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 1 addition & 8 deletions rstest_macros/src/test.rs
Expand Up @@ -307,14 +307,7 @@ impl<T: ToTokens> 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
}
}
Expand Down

0 comments on commit c16601d

Please sign in to comment.