| 1 |
use proc_macro2::TokenStream; |
| 2 |
use syn::{ |
| 3 |
parse::{Parse, ParseStream}, |
| 4 |
parse_quote, |
| 5 |
punctuated::Punctuated, |
| 6 |
token::{self, Async, Paren}, |
| 7 |
visit_mut::VisitMut, |
| 8 |
FnArg, Ident, ItemFn, Pat, Token, |
| 9 |
}; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
error::ErrorsVec, |
| 13 |
parse::just_once::{AttrBuilder, JustOnceFnAttributeExtractor, Validator}, |
| 14 |
refident::{IntoPat, MaybeIdent, MaybePat}, |
| 15 |
utils::{attr_is, attr_starts_with}, |
| 16 |
}; |
| 17 |
use fixture::{ArgumentValue, FixtureModifiers, FixturesFunctionExtractor}; |
| 18 |
use quote::ToTokens; |
| 19 |
use testcase::TestCase; |
| 20 |
|
| 21 |
use self::{ |
| 22 |
expressions::Expressions, |
| 23 |
just_once::JustOnceFnArgAttributeExtractor, |
| 24 |
vlist::{MatrixValues, ValueList}, |
| 25 |
}; |
| 26 |
|
| 27 |
// To use the macros this should be the first one module |
| 28 |
#[macro_use] |
| 29 |
pub(crate) mod macros; |
| 30 |
|
| 31 |
pub(crate) mod arguments; |
| 32 |
pub(crate) mod by_ref; |
| 33 |
pub(crate) mod context; |
| 34 |
pub(crate) mod expressions; |
| 35 |
pub(crate) mod fixture; |
| 36 |
pub(crate) mod future; |
| 37 |
pub(crate) mod ignore; |
| 38 |
pub(crate) mod just_once; |
| 39 |
pub(crate) mod rstest; |
| 40 |
pub(crate) mod testcase; |
| 41 |
pub(crate) mod vlist; |
| 42 |
|
| 43 |
pub(crate) trait ExtendWithFunctionAttrs { |
| 44 |
fn extend_with_function_attrs( |
| 45 |
&mut self, |
| 46 |
item_fn: &mut ItemFn, |
| 47 |
) -> std::result::Result<(), ErrorsVec>; |
| 48 |
} |
| 49 |
|
| 50 |
#[derive(Default, Debug, PartialEq, Clone)] |
| 51 |
pub(crate) struct Attributes { |
| 52 |
pub(crate) attributes: Vec<Attribute>, |
| 53 |
} |
| 54 |
|
| 55 |
impl Parse for Attributes { |
| 56 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 57 |
let vars = Punctuated::<Attribute, Token![::]>::parse_terminated(input)?; |
| 58 |
Ok(Attributes { |
| 59 |
attributes: vars.into_iter().collect(), |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
#[derive(Debug, PartialEq, Clone)] |
| 65 |
pub(crate) enum Attribute { |
| 66 |
Attr(Ident), |
| 67 |
Tagged(Ident, Vec<Pat>), |
| 68 |
Type(Ident, Box<syn::Type>), |
| 69 |
} |
| 70 |
|
| 71 |
impl Parse for Attribute { |
| 72 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 73 |
if input.peek2(Token![<]) { |
| 74 |
let tag = input.parse()?; |
| 75 |
let _open = input.parse::<Token![<]>()?; |
| 76 |
let inner = input.parse()?; |
| 77 |
let _close = input.parse::<Token![>]>()?; |
| 78 |
Ok(Attribute::Type(tag, inner)) |
| 79 |
} else if input.peek2(Token![::]) { |
| 80 |
let inner = input.parse()?; |
| 81 |
Ok(Attribute::Attr(inner)) |
| 82 |
} else if input.peek2(token::Paren) { |
| 83 |
let tag = input.parse()?; |
| 84 |
let content; |
| 85 |
let _ = syn::parenthesized!(content in input); |
| 86 |
let args = Punctuated::<Ident, Token![,]>::parse_terminated(&content)? |
| 87 |
.into_iter() |
| 88 |
.map(IntoPat::into_pat) |
| 89 |
.collect(); |
| 90 |
|
| 91 |
Ok(Attribute::Tagged(tag, args)) |
| 92 |
} else { |
| 93 |
Ok(Attribute::Attr(input.parse()?)) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
fn parse_vector_trailing_till_double_comma<T, P>(input: ParseStream) -> syn::Result<Vec<T>> |
| 99 |
where |
| 100 |
T: Parse, |
| 101 |
P: syn::token::Token + Parse, |
| 102 |
{ |
| 103 |
Ok( |
| 104 |
Punctuated::<Option<T>, P>::parse_separated_nonempty_with(input, |input_tokens| { |
| 105 |
if input_tokens.is_empty() || input_tokens.peek(Token![::]) { |
| 106 |
Ok(None) |
| 107 |
} else { |
| 108 |
T::parse(input_tokens).map(Some) |
| 109 |
} |
| 110 |
})? |
| 111 |
.into_iter() |
| 112 |
.flatten() |
| 113 |
.collect(), |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
#[allow(dead_code)] |
| 118 |
pub(crate) fn drain_stream(input: ParseStream) { |
| 119 |
// JUST TO SKIP ALL |
| 120 |
let _ = input.step(|cursor| { |
| 121 |
let mut rest = *cursor; |
| 122 |
while let Some((_, next)) = rest.token_tree() { |
| 123 |
rest = next |
| 124 |
} |
| 125 |
Ok(((), rest)) |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
#[derive(PartialEq, Debug, Clone, Default)] |
| 130 |
pub(crate) struct Positional(pub(crate) Vec<syn::Expr>); |
| 131 |
|
| 132 |
impl Parse for Positional { |
| 133 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 134 |
Ok(Self( |
| 135 |
Punctuated::<syn::Expr, Token![,]>::parse_terminated(input)? |
| 136 |
.into_iter() |
| 137 |
.collect(), |
| 138 |
)) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
#[derive(PartialEq, Debug, Clone)] |
| 143 |
pub(crate) struct Fixture { |
| 144 |
pub(crate) arg: Pat, |
| 145 |
pub(crate) resolve: syn::Path, |
| 146 |
pub(crate) positional: Positional, |
| 147 |
} |
| 148 |
|
| 149 |
impl Fixture { |
| 150 |
pub(crate) fn new(arg: Pat, resolve: syn::Path, positional: Positional) -> Self { |
| 151 |
Self { |
| 152 |
arg, |
| 153 |
resolve, |
| 154 |
positional, |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl Parse for Fixture { |
| 160 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 161 |
let resolve: syn::Path = input.parse()?; |
| 162 |
if input.peek(Paren) || input.peek(Token![as]) { |
| 163 |
let positional = if input.peek(Paren) { |
| 164 |
let content; |
| 165 |
let _ = syn::parenthesized!(content in input); |
| 166 |
content.parse()? |
| 167 |
} else { |
| 168 |
Default::default() |
| 169 |
}; |
| 170 |
|
| 171 |
if input.peek(Token![as]) { |
| 172 |
let _: Token![as] = input.parse()?; |
| 173 |
let ident: Ident = input.parse()?; |
| 174 |
Ok(Self::new(ident.into_pat(), resolve, positional)) |
| 175 |
} else { |
| 176 |
let name = resolve.get_ident().ok_or_else(|| { |
| 177 |
syn::Error::new_spanned( |
| 178 |
resolve.to_token_stream(), |
| 179 |
"Should be an ident".to_string(), |
| 180 |
) |
| 181 |
})?; |
| 182 |
Ok(Self::new( |
| 183 |
name.clone().into_pat(), |
| 184 |
name.clone().into(), |
| 185 |
positional, |
| 186 |
)) |
| 187 |
} |
| 188 |
} else { |
| 189 |
Err(syn::Error::new( |
| 190 |
input.span(), |
| 191 |
"fixture need arguments or 'as new_name' format", |
| 192 |
)) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
impl ToTokens for Fixture { |
| 198 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 199 |
self.arg.to_tokens(tokens) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
pub(crate) fn extract_fixtures(item_fn: &mut ItemFn) -> Result<Vec<Fixture>, ErrorsVec> { |
| 204 |
let mut fixtures_extractor = FixturesFunctionExtractor::default(); |
| 205 |
fixtures_extractor.visit_item_fn_mut(item_fn); |
| 206 |
|
| 207 |
if fixtures_extractor.1.is_empty() { |
| 208 |
Ok(fixtures_extractor.0) |
| 209 |
} else { |
| 210 |
Err(fixtures_extractor.1.into()) |
| 211 |
} |
| 212 |
} |
| 213 |
pub(crate) fn extract_defaults(item_fn: &mut ItemFn) -> Result<Vec<ArgumentValue>, ErrorsVec> { |
| 214 |
struct DefaultBuilder; |
| 215 |
impl AttrBuilder<Pat> for DefaultBuilder { |
| 216 |
type Out = ArgumentValue; |
| 217 |
|
| 218 |
fn build(attr: syn::Attribute, name: &Pat) -> syn::Result<Self::Out> { |
| 219 |
attr.parse_args::<syn::Expr>() |
| 220 |
.map(|e| ArgumentValue::new(name.clone(), e)) |
| 221 |
} |
| 222 |
} |
| 223 |
impl Validator<syn::FnArg> for DefaultBuilder {} |
| 224 |
|
| 225 |
let mut extractor = JustOnceFnArgAttributeExtractor::<DefaultBuilder>::new("default"); |
| 226 |
extractor.visit_item_fn_mut(item_fn); |
| 227 |
|
| 228 |
extractor.take() |
| 229 |
} |
| 230 |
|
| 231 |
pub(crate) fn extract_default_return_type( |
| 232 |
item_fn: &mut ItemFn, |
| 233 |
) -> Result<Option<syn::Type>, ErrorsVec> { |
| 234 |
struct DefaultTypeBuilder; |
| 235 |
impl AttrBuilder<ItemFn> for DefaultTypeBuilder { |
| 236 |
type Out = syn::Type; |
| 237 |
|
| 238 |
fn build(attr: syn::Attribute, _extra: &ItemFn) -> syn::Result<Self::Out> { |
| 239 |
attr.parse_args::<syn::Type>() |
| 240 |
} |
| 241 |
} |
| 242 |
impl Validator<syn::ItemFn> for DefaultTypeBuilder {} |
| 243 |
|
| 244 |
let mut extractor = |
| 245 |
JustOnceFnAttributeExtractor::<DefaultTypeBuilder>::new(FixtureModifiers::DEFAULT_RET_ATTR); |
| 246 |
|
| 247 |
extractor.visit_item_fn_mut(item_fn); |
| 248 |
extractor.take() |
| 249 |
} |
| 250 |
|
| 251 |
pub(crate) fn extract_partials_return_type( |
| 252 |
item_fn: &mut ItemFn, |
| 253 |
) -> Result<Vec<(usize, syn::Type)>, ErrorsVec> { |
| 254 |
let mut partials_type_extractor = PartialsTypeFunctionExtractor::default(); |
| 255 |
partials_type_extractor.visit_item_fn_mut(item_fn); |
| 256 |
partials_type_extractor.take() |
| 257 |
} |
| 258 |
|
| 259 |
pub(crate) fn extract_once(item_fn: &mut ItemFn) -> Result<Option<syn::Attribute>, ErrorsVec> { |
| 260 |
let mut extractor = JustOnceFnAttributeExtractor::from("once"); |
| 261 |
|
| 262 |
extractor.visit_item_fn_mut(item_fn); |
| 263 |
extractor.take() |
| 264 |
} |
| 265 |
|
| 266 |
pub(crate) fn extract_argument_attrs<'a, B: 'a + std::fmt::Debug>( |
| 267 |
node: &mut FnArg, |
| 268 |
is_valid_attr: fn(&syn::Attribute) -> bool, |
| 269 |
build: impl Fn(syn::Attribute) -> syn::Result<B> + 'a, |
| 270 |
) -> Box<dyn Iterator<Item = syn::Result<B>> + 'a> { |
| 271 |
let name = node.maybe_ident().cloned(); |
| 272 |
if name.is_none() { |
| 273 |
return Box::new(std::iter::empty()); |
| 274 |
} |
| 275 |
|
| 276 |
if let FnArg::Typed(ref mut arg) = node { |
| 277 |
// Extract interesting attributes |
| 278 |
let attrs = std::mem::take(&mut arg.attrs); |
| 279 |
let (extracted, remain): (Vec<_>, Vec<_>) = attrs.into_iter().partition(is_valid_attr); |
| 280 |
|
| 281 |
arg.attrs = remain; |
| 282 |
|
| 283 |
// Parse attrs |
| 284 |
Box::new(extracted.into_iter().map(build)) |
| 285 |
} else { |
| 286 |
Box::new(std::iter::empty()) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/// Simple struct used to visit function attributes and extract default return |
| 291 |
/// type |
| 292 |
struct PartialsTypeFunctionExtractor(Result<Vec<(usize, syn::Type)>, ErrorsVec>); |
| 293 |
|
| 294 |
impl PartialsTypeFunctionExtractor { |
| 295 |
fn take(self) -> Result<Vec<(usize, syn::Type)>, ErrorsVec> { |
| 296 |
self.0 |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
impl Default for PartialsTypeFunctionExtractor { |
| 301 |
fn default() -> Self { |
| 302 |
Self(Ok(Vec::default())) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
impl VisitMut for PartialsTypeFunctionExtractor { |
| 307 |
fn visit_item_fn_mut(&mut self, node: &mut ItemFn) { |
| 308 |
let attrs = std::mem::take(&mut node.attrs); |
| 309 |
let (partials, remain): (Vec<_>, Vec<_>) = |
| 310 |
attrs |
| 311 |
.into_iter() |
| 312 |
.partition(|attr| match attr.path().get_ident() { |
| 313 |
Some(name) => name |
| 314 |
.to_string() |
| 315 |
.starts_with(FixtureModifiers::PARTIAL_RET_ATTR), |
| 316 |
None => false, |
| 317 |
}); |
| 318 |
|
| 319 |
node.attrs = remain; |
| 320 |
let mut errors = ErrorsVec::default(); |
| 321 |
let mut data: Vec<(usize, syn::Type)> = Vec::default(); |
| 322 |
for attr in partials { |
| 323 |
match attr.parse_args::<syn::Type>() { |
| 324 |
Ok(t) => { |
| 325 |
match attr.path().get_ident().unwrap().to_string() |
| 326 |
[FixtureModifiers::PARTIAL_RET_ATTR.len()..] |
| 327 |
.parse() |
| 328 |
{ |
| 329 |
Ok(id) => data.push((id, t)), |
| 330 |
Err(_) => errors.push(syn::Error::new_spanned( |
| 331 |
attr, |
| 332 |
"Invalid partial syntax: should be partial_<n_arguments>", |
| 333 |
)), |
| 334 |
} |
| 335 |
} |
| 336 |
Err(e) => errors.push(e), |
| 337 |
} |
| 338 |
} |
| 339 |
self.0 = if !errors.is_empty() { |
| 340 |
Err(errors) |
| 341 |
} else { |
| 342 |
Ok(data) |
| 343 |
}; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
pub(crate) fn extract_case_args(item_fn: &mut ItemFn) -> Result<Vec<Pat>, ErrorsVec> { |
| 348 |
let mut extractor = JustOnceFnArgAttributeExtractor::from("case"); |
| 349 |
extractor.visit_item_fn_mut(item_fn); |
| 350 |
|
| 351 |
extractor.take() |
| 352 |
} |
| 353 |
|
| 354 |
/// Simple struct used to visit function attributes and extract cases and |
| 355 |
/// eventualy parsing errors |
| 356 |
#[derive(Default)] |
| 357 |
struct CasesFunctionExtractor(Vec<TestCase>, Vec<syn::Error>); |
| 358 |
|
| 359 |
impl VisitMut for CasesFunctionExtractor { |
| 360 |
fn visit_item_fn_mut(&mut self, node: &mut ItemFn) { |
| 361 |
let attrs = std::mem::take(&mut node.attrs); |
| 362 |
let mut attrs_buffer = Default::default(); |
| 363 |
let case: syn::PathSegment = parse_quote! { case }; |
| 364 |
for attr in attrs.into_iter() { |
| 365 |
if attr_starts_with(&attr, &case) { |
| 366 |
match attr.parse_args::<Expressions>() { |
| 367 |
Ok(expressions) => { |
| 368 |
let args = expressions.take(); |
| 369 |
let description = |
| 370 |
attr.path().segments.iter().nth(1).map(|p| p.ident.clone()); |
| 371 |
self.0.push(TestCase { |
| 372 |
args, |
| 373 |
attrs: std::mem::take(&mut attrs_buffer), |
| 374 |
description, |
| 375 |
}); |
| 376 |
} |
| 377 |
Err(err) => self.1.push(err), |
| 378 |
}; |
| 379 |
} else { |
| 380 |
attrs_buffer.push(attr) |
| 381 |
} |
| 382 |
} |
| 383 |
node.attrs = std::mem::take(&mut attrs_buffer); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
pub(crate) fn extract_cases(item_fn: &mut ItemFn) -> Result<Vec<TestCase>, ErrorsVec> { |
| 388 |
let mut cases_extractor = CasesFunctionExtractor::default(); |
| 389 |
cases_extractor.visit_item_fn_mut(item_fn); |
| 390 |
|
| 391 |
if cases_extractor.1.is_empty() { |
| 392 |
Ok(cases_extractor.0) |
| 393 |
} else { |
| 394 |
Err(cases_extractor.1.into()) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
pub(crate) fn extract_value_list(item_fn: &mut ItemFn) -> Result<Vec<ValueList>, ErrorsVec> { |
| 399 |
struct ValueListBuilder; |
| 400 |
impl AttrBuilder<Pat> for ValueListBuilder { |
| 401 |
type Out = ValueList; |
| 402 |
|
| 403 |
fn build(attr: syn::Attribute, extra: &Pat) -> syn::Result<Self::Out> { |
| 404 |
attr.parse_args::<MatrixValues>().map(|v| ValueList { |
| 405 |
arg: extra.clone(), |
| 406 |
values: v.into_values(), |
| 407 |
}) |
| 408 |
} |
| 409 |
} |
| 410 |
impl Validator<FnArg> for ValueListBuilder {} |
| 411 |
|
| 412 |
let mut extractor = JustOnceFnArgAttributeExtractor::<ValueListBuilder>::new("values"); |
| 413 |
|
| 414 |
extractor.visit_item_fn_mut(item_fn); |
| 415 |
extractor.take() |
| 416 |
} |
| 417 |
|
| 418 |
/// Simple struct used to visit function args attributes to extract the |
| 419 |
/// excluded ones and eventualy parsing errors |
| 420 |
struct ExcludedTraceAttributesFunctionExtractor(Result<Vec<Pat>, ErrorsVec>); |
| 421 |
impl From<Result<Vec<Pat>, ErrorsVec>> for ExcludedTraceAttributesFunctionExtractor { |
| 422 |
fn from(inner: Result<Vec<Pat>, ErrorsVec>) -> Self { |
| 423 |
Self(inner) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
impl ExcludedTraceAttributesFunctionExtractor { |
| 428 |
pub(crate) fn take(self) -> Result<Vec<Pat>, ErrorsVec> { |
| 429 |
self.0 |
| 430 |
} |
| 431 |
|
| 432 |
fn update_error(&mut self, mut errors: ErrorsVec) { |
| 433 |
match &mut self.0 { |
| 434 |
Ok(_) => self.0 = Err(errors), |
| 435 |
Err(err) => err.append(&mut errors), |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
fn update_excluded(&mut self, value: Pat) { |
| 440 |
if let Some(inner) = self.0.iter_mut().next() { |
| 441 |
inner.push(value); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl Default for ExcludedTraceAttributesFunctionExtractor { |
| 447 |
fn default() -> Self { |
| 448 |
Self(Ok(Default::default())) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
impl VisitMut for ExcludedTraceAttributesFunctionExtractor { |
| 453 |
fn visit_fn_arg_mut(&mut self, node: &mut FnArg) { |
| 454 |
let pat = match node.maybe_pat().cloned() { |
| 455 |
Some(pat) => pat, |
| 456 |
None => return, |
| 457 |
}; |
| 458 |
for r in extract_argument_attrs(node, |a| attr_is(a, "notrace"), |_a| Ok(())) { |
| 459 |
match r { |
| 460 |
Ok(_) => self.update_excluded(pat.clone()), |
| 461 |
Err(err) => self.update_error(err.into()), |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
syn::visit_mut::visit_fn_arg_mut(self, node); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
pub(crate) fn extract_excluded_trace(item_fn: &mut ItemFn) -> Result<Vec<Pat>, ErrorsVec> { |
| 470 |
let mut excluded_trace_extractor = ExcludedTraceAttributesFunctionExtractor::default(); |
| 471 |
excluded_trace_extractor.visit_item_fn_mut(item_fn); |
| 472 |
excluded_trace_extractor.take() |
| 473 |
} |
| 474 |
|
| 475 |
/// Simple struct used to visit function args attributes to check timeout syntax |
| 476 |
struct CheckTimeoutAttributesFunction(Result<(), ErrorsVec>); |
| 477 |
impl From<ErrorsVec> for CheckTimeoutAttributesFunction { |
| 478 |
fn from(errors: ErrorsVec) -> Self { |
| 479 |
Self(Err(errors)) |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
impl CheckTimeoutAttributesFunction { |
| 484 |
pub(crate) fn take(self) -> Result<(), ErrorsVec> { |
| 485 |
self.0 |
| 486 |
} |
| 487 |
|
| 488 |
fn check_if_can_implement_timeous( |
| 489 |
&self, |
| 490 |
timeouts: &[&syn::Attribute], |
| 491 |
asyncness: Option<&Async>, |
| 492 |
) -> Option<syn::Error> { |
| 493 |
if cfg!(feature = "async-timeout") || timeouts.is_empty() { |
| 494 |
None |
| 495 |
} else { |
| 496 |
asyncness.map(|a| { |
| 497 |
syn::Error::new( |
| 498 |
a.span, |
| 499 |
"Enable async-timeout feature to use timeout in async tests", |
| 500 |
) |
| 501 |
}) |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
impl Default for CheckTimeoutAttributesFunction { |
| 507 |
fn default() -> Self { |
| 508 |
Self(Ok(())) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
impl VisitMut for CheckTimeoutAttributesFunction { |
| 513 |
fn visit_item_fn_mut(&mut self, node: &mut ItemFn) { |
| 514 |
let timeouts = node |
| 515 |
.attrs |
| 516 |
.iter() |
| 517 |
.filter(|&a| attr_is(a, "timeout")) |
| 518 |
.collect::<Vec<_>>(); |
| 519 |
let mut errors = timeouts |
| 520 |
.iter() |
| 521 |
.map(|&attr| attr.parse_args::<syn::Expr>()) |
| 522 |
.filter_map(Result::err) |
| 523 |
.collect::<Vec<_>>(); |
| 524 |
|
| 525 |
if let Some(e) = |
| 526 |
self.check_if_can_implement_timeous(timeouts.as_slice(), node.sig.asyncness.as_ref()) |
| 527 |
{ |
| 528 |
errors.push(e); |
| 529 |
} |
| 530 |
if !errors.is_empty() { |
| 531 |
*self = Self(Err(errors.into())); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
pub(crate) fn check_timeout_attrs(item_fn: &mut ItemFn) -> Result<(), ErrorsVec> { |
| 537 |
let mut checker = CheckTimeoutAttributesFunction::default(); |
| 538 |
checker.visit_item_fn_mut(item_fn); |
| 539 |
checker.take() |
| 540 |
} |
| 541 |
|
| 542 |
#[cfg(test)] |
| 543 |
mod should { |
| 544 |
use super::*; |
| 545 |
use crate::test::*; |
| 546 |
|
| 547 |
mod parse_attributes { |
| 548 |
use super::assert_eq; |
| 549 |
use super::*; |
| 550 |
|
| 551 |
fn parse_attributes<S: AsRef<str>>(attributes: S) -> Attributes { |
| 552 |
parse_meta(attributes) |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn one_simple_ident() { |
| 557 |
let attributes = parse_attributes("my_ident"); |
| 558 |
|
| 559 |
let expected = Attributes { |
| 560 |
attributes: vec![Attribute::attr("my_ident")], |
| 561 |
}; |
| 562 |
|
| 563 |
assert_eq!(expected, attributes); |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn one_simple_group() { |
| 568 |
let attributes = parse_attributes("group_tag(first, second)"); |
| 569 |
|
| 570 |
let expected = Attributes { |
| 571 |
attributes: vec![Attribute::tagged("group_tag", vec!["first", "second"])], |
| 572 |
}; |
| 573 |
|
| 574 |
assert_eq!(expected, attributes); |
| 575 |
} |
| 576 |
|
| 577 |
#[test] |
| 578 |
fn one_simple_type() { |
| 579 |
let attributes = parse_attributes("type_tag<(u32, T, (String, i32))>"); |
| 580 |
|
| 581 |
let expected = Attributes { |
| 582 |
attributes: vec![Attribute::typed("type_tag", "(u32, T, (String, i32))")], |
| 583 |
}; |
| 584 |
|
| 585 |
assert_eq!(expected, attributes); |
| 586 |
} |
| 587 |
|
| 588 |
#[test] |
| 589 |
fn integrated() { |
| 590 |
let attributes = parse_attributes( |
| 591 |
r#" |
| 592 |
simple :: tagged(first, second) :: type_tag<(u32, T, (std::string::String, i32))> :: more_tagged(a,b)"#, |
| 593 |
); |
| 594 |
|
| 595 |
let expected = Attributes { |
| 596 |
attributes: vec![ |
| 597 |
Attribute::attr("simple"), |
| 598 |
Attribute::tagged("tagged", vec!["first", "second"]), |
| 599 |
Attribute::typed("type_tag", "(u32, T, (std::string::String, i32))"), |
| 600 |
Attribute::tagged("more_tagged", vec!["a", "b"]), |
| 601 |
], |
| 602 |
}; |
| 603 |
|
| 604 |
assert_eq!(expected, attributes); |
| 605 |
} |
| 606 |
} |
| 607 |
} |