| 1 |
/// `fixture`'s related data and parsing |
| 2 |
use syn::{ |
| 3 |
parse::{Parse, ParseStream}, |
| 4 |
parse_quote, |
| 5 |
visit_mut::VisitMut, |
| 6 |
Expr, FnArg, Ident, ItemFn, Pat, Token, |
| 7 |
}; |
| 8 |
|
| 9 |
use super::{ |
| 10 |
arguments::ArgumentsInfo, |
| 11 |
extract_default_return_type, extract_defaults, extract_fixtures, extract_partials_return_type, |
| 12 |
future::{extract_futures, extract_global_awt}, |
| 13 |
parse_vector_trailing_till_double_comma, Attributes, ExtendWithFunctionAttrs, Fixture, |
| 14 |
}; |
| 15 |
use crate::{ |
| 16 |
error::ErrorsVec, |
| 17 |
parse::extract_once, |
| 18 |
refident::{IntoPat, MaybeIdent, MaybePat, MaybePatTypeMut, RefPat}, |
| 19 |
utils::attr_is, |
| 20 |
}; |
| 21 |
use crate::{parse::Attribute, utils::attr_in}; |
| 22 |
use proc_macro2::TokenStream; |
| 23 |
use quote::{format_ident, ToTokens}; |
| 24 |
|
| 25 |
#[derive(PartialEq, Debug, Default)] |
| 26 |
pub(crate) struct FixtureInfo { |
| 27 |
pub(crate) data: FixtureData, |
| 28 |
pub(crate) attributes: FixtureModifiers, |
| 29 |
pub(crate) arguments: ArgumentsInfo, |
| 30 |
} |
| 31 |
|
| 32 |
impl Parse for FixtureModifiers { |
| 33 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 34 |
Ok(input.parse::<Attributes>()?.into()) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl Parse for FixtureInfo { |
| 39 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 40 |
Ok(if input.is_empty() { |
| 41 |
Default::default() |
| 42 |
} else { |
| 43 |
Self { |
| 44 |
data: input.parse()?, |
| 45 |
attributes: input |
| 46 |
.parse::<Token![::]>() |
| 47 |
.or_else(|_| Ok(Default::default())) |
| 48 |
.and_then(|_| input.parse())?, |
| 49 |
arguments: Default::default(), |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl ExtendWithFunctionAttrs for FixtureInfo { |
| 56 |
fn extend_with_function_attrs( |
| 57 |
&mut self, |
| 58 |
item_fn: &mut ItemFn, |
| 59 |
) -> std::result::Result<(), ErrorsVec> { |
| 60 |
let composed_tuple!( |
| 61 |
fixtures, |
| 62 |
defaults, |
| 63 |
default_return_type, |
| 64 |
partials_return_type, |
| 65 |
once, |
| 66 |
futures, |
| 67 |
global_awt |
| 68 |
) = merge_errors!( |
| 69 |
extract_fixtures(item_fn), |
| 70 |
extract_defaults(item_fn), |
| 71 |
extract_default_return_type(item_fn), |
| 72 |
extract_partials_return_type(item_fn), |
| 73 |
extract_once(item_fn), |
| 74 |
extract_futures(item_fn), |
| 75 |
extract_global_awt(item_fn) |
| 76 |
)?; |
| 77 |
self.data.items.extend( |
| 78 |
fixtures |
| 79 |
.into_iter() |
| 80 |
.map(|f| f.into()) |
| 81 |
.chain(defaults.into_iter().map(|d| d.into())), |
| 82 |
); |
| 83 |
if let Some(return_type) = default_return_type { |
| 84 |
self.attributes.set_default_return_type(return_type); |
| 85 |
} |
| 86 |
for (id, return_type) in partials_return_type { |
| 87 |
self.attributes.set_partial_return_type(id, return_type); |
| 88 |
} |
| 89 |
self.arguments.set_once(once); |
| 90 |
self.arguments.set_global_await(global_awt); |
| 91 |
self.arguments.set_futures(futures.into_iter()); |
| 92 |
self.arguments |
| 93 |
.register_inner_destructored_idents_names(item_fn); |
| 94 |
|
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn parse_attribute_args_just_once<'a, T: Parse>( |
| 100 |
attributes: impl Iterator<Item = &'a syn::Attribute>, |
| 101 |
name: &str, |
| 102 |
) -> (Option<T>, Vec<syn::Error>) { |
| 103 |
let mut errors = Vec::new(); |
| 104 |
let val = attributes |
| 105 |
.filter(|&a| attr_is(a, name)) |
| 106 |
.map(|a| (a, a.parse_args::<T>())) |
| 107 |
.fold(None, |first, (a, res)| match (first, res) { |
| 108 |
(None, Ok(parsed)) => Some(parsed), |
| 109 |
(first, Err(err)) => { |
| 110 |
errors.push(err); |
| 111 |
first |
| 112 |
} |
| 113 |
(first, _) => { |
| 114 |
errors.push(syn::Error::new_spanned( |
| 115 |
a, |
| 116 |
crate::error::messages::use_more_than_once(name), |
| 117 |
)); |
| 118 |
first |
| 119 |
} |
| 120 |
}); |
| 121 |
(val, errors) |
| 122 |
} |
| 123 |
|
| 124 |
/// Simple struct used to visit function attributes and extract Fixtures and |
| 125 |
/// eventually parsing errors |
| 126 |
#[derive(Default)] |
| 127 |
pub(crate) struct FixturesFunctionExtractor(pub(crate) Vec<Fixture>, pub(crate) Vec<syn::Error>); |
| 128 |
|
| 129 |
impl VisitMut for FixturesFunctionExtractor { |
| 130 |
fn visit_fn_arg_mut(&mut self, node: &mut FnArg) { |
| 131 |
let arg = match node.maybe_pat_type_mut() { |
| 132 |
Some(pt) => pt, |
| 133 |
None => return, |
| 134 |
}; |
| 135 |
let (extracted, remain): (Vec<_>, Vec<_>) = std::mem::take(&mut arg.attrs) |
| 136 |
.into_iter() |
| 137 |
.partition(|attr| attr_in(attr, &["with", "from"])); |
| 138 |
arg.attrs = remain; |
| 139 |
|
| 140 |
let (pos, errors) = parse_attribute_args_just_once(extracted.iter(), "with"); |
| 141 |
self.1.extend(errors); |
| 142 |
let (resolve, errors): (Option<syn::Path>, _) = |
| 143 |
parse_attribute_args_just_once(extracted.iter(), "from"); |
| 144 |
self.1.extend(errors); |
| 145 |
|
| 146 |
match (resolve, arg.pat.maybe_ident()) { |
| 147 |
(Some(res), _) => self.0.push(Fixture::new( |
| 148 |
arg.pat.as_ref().clone(), |
| 149 |
res, |
| 150 |
pos.unwrap_or_default(), |
| 151 |
)), |
| 152 |
(None, Some(ident)) if pos.is_some() => self.0.push(Fixture::new( |
| 153 |
arg.pat.as_ref().clone(), |
| 154 |
ident.clone().into(), |
| 155 |
pos.unwrap_or_default(), |
| 156 |
)), |
| 157 |
(None, None) if pos.is_some() => { |
| 158 |
self.1.push(syn::Error::new_spanned( |
| 159 |
node, |
| 160 |
crate::error::messages::DESTRUCT_WITHOUT_FROM, |
| 161 |
)); |
| 162 |
} |
| 163 |
_ => {} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
#[derive(PartialEq, Debug, Default)] |
| 169 |
pub(crate) struct FixtureData { |
| 170 |
pub items: Vec<FixtureItem>, |
| 171 |
} |
| 172 |
|
| 173 |
impl FixtureData { |
| 174 |
pub(crate) fn fixtures(&self) -> impl Iterator<Item = &Fixture> { |
| 175 |
self.items.iter().filter_map(|f| match f { |
| 176 |
FixtureItem::Fixture(ref fixture) => Some(fixture.as_ref()), |
| 177 |
_ => None, |
| 178 |
}) |
| 179 |
} |
| 180 |
|
| 181 |
pub(crate) fn values(&self) -> impl Iterator<Item = &ArgumentValue> { |
| 182 |
self.items.iter().filter_map(|f| match f { |
| 183 |
FixtureItem::ArgumentValue(ref value) => Some(value.as_ref()), |
| 184 |
_ => None, |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl Parse for FixtureData { |
| 190 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 191 |
if input.peek(Token![::]) { |
| 192 |
Ok(Default::default()) |
| 193 |
} else { |
| 194 |
Ok(Self { |
| 195 |
items: parse_vector_trailing_till_double_comma::<_, Token![,]>(input)?, |
| 196 |
}) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
#[derive(PartialEq, Debug)] |
| 202 |
pub(crate) struct ArgumentValue { |
| 203 |
pub arg: Pat, |
| 204 |
pub expr: Expr, |
| 205 |
} |
| 206 |
|
| 207 |
impl ArgumentValue { |
| 208 |
pub(crate) fn new(arg: Pat, expr: Expr) -> Self { |
| 209 |
Self { arg, expr } |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
#[derive(PartialEq, Debug)] |
| 214 |
pub(crate) enum FixtureItem { |
| 215 |
Fixture(Box<Fixture>), |
| 216 |
ArgumentValue(Box<ArgumentValue>), |
| 217 |
} |
| 218 |
|
| 219 |
impl From<Fixture> for FixtureItem { |
| 220 |
fn from(f: Fixture) -> Self { |
| 221 |
FixtureItem::Fixture(f.into()) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
impl Parse for FixtureItem { |
| 226 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 227 |
if input.peek2(Token![=]) { |
| 228 |
input.parse::<ArgumentValue>().map(|v| v.into()) |
| 229 |
} else { |
| 230 |
input.parse::<Fixture>().map(|v| v.into()) |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
impl RefPat for FixtureItem { |
| 236 |
fn pat(&self) -> &Pat { |
| 237 |
match self { |
| 238 |
FixtureItem::Fixture(ref fix) => &fix.arg, |
| 239 |
FixtureItem::ArgumentValue(ref av) => &av.arg, |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
impl MaybePat for FixtureItem { |
| 245 |
fn maybe_pat(&self) -> Option<&syn::Pat> { |
| 246 |
Some(self.pat()) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
impl ToTokens for FixtureItem { |
| 251 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 252 |
self.pat().to_tokens(tokens) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl From<ArgumentValue> for FixtureItem { |
| 257 |
fn from(av: ArgumentValue) -> Self { |
| 258 |
FixtureItem::ArgumentValue(Box::new(av)) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
impl Parse for ArgumentValue { |
| 263 |
fn parse(input: ParseStream) -> syn::Result<Self> { |
| 264 |
let name: Ident = input.parse()?; |
| 265 |
let _eq: Token![=] = input.parse()?; |
| 266 |
let expr = input.parse()?; |
| 267 |
Ok(ArgumentValue::new(name.into_pat(), expr)) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
wrap_attributes!(FixtureModifiers); |
| 272 |
|
| 273 |
impl FixtureModifiers { |
| 274 |
pub(crate) const DEFAULT_RET_ATTR: &'static str = "default"; |
| 275 |
pub(crate) const PARTIAL_RET_ATTR: &'static str = "partial_"; |
| 276 |
|
| 277 |
pub(crate) fn extract_default_type(&self) -> Option<syn::ReturnType> { |
| 278 |
self.extract_type(Self::DEFAULT_RET_ATTR) |
| 279 |
} |
| 280 |
|
| 281 |
pub(crate) fn extract_partial_type(&self, pos: usize) -> Option<syn::ReturnType> { |
| 282 |
self.extract_type(&format!("{}{}", Self::PARTIAL_RET_ATTR, pos)) |
| 283 |
} |
| 284 |
|
| 285 |
pub(crate) fn set_default_return_type(&mut self, return_type: syn::Type) { |
| 286 |
self.inner.attributes.push(Attribute::Type( |
| 287 |
format_ident!("{}", Self::DEFAULT_RET_ATTR), |
| 288 |
Box::new(return_type), |
| 289 |
)) |
| 290 |
} |
| 291 |
|
| 292 |
pub(crate) fn set_partial_return_type(&mut self, id: usize, return_type: syn::Type) { |
| 293 |
self.inner.attributes.push(Attribute::Type( |
| 294 |
format_ident!("{}{}", Self::PARTIAL_RET_ATTR, id), |
| 295 |
Box::new(return_type), |
| 296 |
)) |
| 297 |
} |
| 298 |
|
| 299 |
fn extract_type(&self, attr_name: &str) -> Option<syn::ReturnType> { |
| 300 |
self.iter() |
| 301 |
.filter_map(|m| match m { |
| 302 |
Attribute::Type(name, t) if name == attr_name => Some(parse_quote! { -> #t}), |
| 303 |
_ => None, |
| 304 |
}) |
| 305 |
.next() |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
#[cfg(test)] |
| 310 |
mod should { |
| 311 |
use super::*; |
| 312 |
use crate::test::{assert_eq, *}; |
| 313 |
|
| 314 |
mod parse { |
| 315 |
use super::{assert_eq, *}; |
| 316 |
|
| 317 |
fn parse_fixture<S: AsRef<str>>(fixture_data: S) -> FixtureInfo { |
| 318 |
parse_meta(fixture_data) |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn happy_path() { |
| 323 |
let data = parse_fixture( |
| 324 |
r#"my_fixture(42, "other"), other(vec![42]), value=42, other_value=vec![1.0] |
| 325 |
:: trace :: no_trace(some)"#, |
| 326 |
); |
| 327 |
|
| 328 |
let expected = FixtureInfo { |
| 329 |
data: vec![ |
| 330 |
fixture("my_fixture", &["42", r#""other""#]).into(), |
| 331 |
fixture("other", &["vec![42]"]).into(), |
| 332 |
arg_value("value", "42").into(), |
| 333 |
arg_value("other_value", "vec![1.0]").into(), |
| 334 |
] |
| 335 |
.into(), |
| 336 |
attributes: Attributes { |
| 337 |
attributes: vec![ |
| 338 |
Attribute::attr("trace"), |
| 339 |
Attribute::tagged("no_trace", vec!["some"]), |
| 340 |
], |
| 341 |
} |
| 342 |
.into(), |
| 343 |
arguments: Default::default(), |
| 344 |
}; |
| 345 |
|
| 346 |
assert_eq!(expected, data); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn some_literals() { |
| 351 |
let args_expressions = literal_expressions_str(); |
| 352 |
let fixture = parse_fixture(&format!("my_fixture({})", args_expressions.join(", "))); |
| 353 |
let args = fixture.data.fixtures().next().unwrap().positional.clone(); |
| 354 |
|
| 355 |
assert_eq!(to_args!(args_expressions), args.0); |
| 356 |
} |
| 357 |
|
| 358 |
#[test] |
| 359 |
fn empty_fixtures() { |
| 360 |
let data = parse_fixture(r#"::trace::no_trace(some)"#); |
| 361 |
|
| 362 |
let expected = FixtureInfo { |
| 363 |
attributes: Attributes { |
| 364 |
attributes: vec![ |
| 365 |
Attribute::attr("trace"), |
| 366 |
Attribute::tagged("no_trace", vec!["some"]), |
| 367 |
], |
| 368 |
} |
| 369 |
.into(), |
| 370 |
..Default::default() |
| 371 |
}; |
| 372 |
|
| 373 |
assert_eq!(expected, data); |
| 374 |
} |
| 375 |
|
| 376 |
#[test] |
| 377 |
fn empty_attributes() { |
| 378 |
let data = parse_fixture(r#"my_fixture(42, "other")"#); |
| 379 |
|
| 380 |
let expected = FixtureInfo { |
| 381 |
data: vec![fixture("my_fixture", &["42", r#""other""#]).into()].into(), |
| 382 |
..Default::default() |
| 383 |
}; |
| 384 |
|
| 385 |
assert_eq!(expected, data); |
| 386 |
} |
| 387 |
|
| 388 |
#[rstest] |
| 389 |
#[case("first(42),", 1)] |
| 390 |
#[case("first(42), second=42,", 2)] |
| 391 |
#[case(r#"fixture(42, "other"), :: trace"#, 1)] |
| 392 |
#[case(r#"second=42, fixture(42, "other"), :: trace"#, 2)] |
| 393 |
fn should_accept_trailing_comma(#[case] input: &str, #[case] expected: usize) { |
| 394 |
let info: FixtureInfo = input.ast(); |
| 395 |
|
| 396 |
assert_eq!( |
| 397 |
expected, |
| 398 |
info.data.fixtures().count() + info.data.values().count() |
| 399 |
); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
#[cfg(test)] |
| 405 |
mod extend { |
| 406 |
use super::*; |
| 407 |
use crate::test::{assert_eq, *}; |
| 408 |
use syn::ItemFn; |
| 409 |
|
| 410 |
mod should { |
| 411 |
use super::{assert_eq, *}; |
| 412 |
|
| 413 |
#[test] |
| 414 |
fn use_with_attributes() { |
| 415 |
let to_parse = r#" |
| 416 |
fn my_fix(#[with(2)] f1: &str, #[with(vec![1,2], "s")] f2: u32) {} |
| 417 |
"#; |
| 418 |
|
| 419 |
let mut item_fn: ItemFn = to_parse.ast(); |
| 420 |
let mut info = FixtureInfo::default(); |
| 421 |
|
| 422 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 423 |
|
| 424 |
let expected = FixtureInfo { |
| 425 |
data: vec![ |
| 426 |
fixture("f1", &["2"]).into(), |
| 427 |
fixture("f2", &["vec![1,2]", r#""s""#]).into(), |
| 428 |
] |
| 429 |
.into(), |
| 430 |
..Default::default() |
| 431 |
}; |
| 432 |
|
| 433 |
assert!(!format!("{:?}", item_fn).contains("with")); |
| 434 |
assert_eq!(expected, info); |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn rename_with_attributes() { |
| 439 |
let mut item_fn = r#" |
| 440 |
fn test_fn( |
| 441 |
#[from(long_fixture_name)] |
| 442 |
#[with(42, "other")] short: u32, |
| 443 |
#[from(sub_module::fix)] |
| 444 |
f: u32, |
| 445 |
#[from(simple)] |
| 446 |
s: &str, |
| 447 |
no_change: i32) { |
| 448 |
} |
| 449 |
"# |
| 450 |
.ast(); |
| 451 |
|
| 452 |
let expected = FixtureInfo { |
| 453 |
data: vec![ |
| 454 |
fixture("short", &["42", r#""other""#]) |
| 455 |
.with_resolve("long_fixture_name") |
| 456 |
.into(), |
| 457 |
fixture("f", &[]).with_resolve("sub_module::fix").into(), |
| 458 |
fixture("s", &[]).with_resolve("simple").into(), |
| 459 |
] |
| 460 |
.into(), |
| 461 |
..Default::default() |
| 462 |
}; |
| 463 |
|
| 464 |
let mut data = FixtureInfo::default(); |
| 465 |
data.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 466 |
|
| 467 |
assert_eq!(expected, data); |
| 468 |
} |
| 469 |
|
| 470 |
#[test] |
| 471 |
fn use_default_values_attributes() { |
| 472 |
let to_parse = r#" |
| 473 |
fn my_fix(#[default(2)] f1: &str, #[default((vec![1,2], "s"))] f2: (Vec<u32>, &str)) {} |
| 474 |
"#; |
| 475 |
|
| 476 |
let mut item_fn: ItemFn = to_parse.ast(); |
| 477 |
let mut info = FixtureInfo::default(); |
| 478 |
|
| 479 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 480 |
|
| 481 |
let expected = FixtureInfo { |
| 482 |
data: vec![ |
| 483 |
arg_value("f1", "2").into(), |
| 484 |
arg_value("f2", r#"(vec![1,2], "s")"#).into(), |
| 485 |
] |
| 486 |
.into(), |
| 487 |
..Default::default() |
| 488 |
}; |
| 489 |
|
| 490 |
assert!(!format!("{:?}", item_fn).contains("default")); |
| 491 |
assert_eq!(expected, info); |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn find_default_return_type() { |
| 496 |
let mut item_fn: ItemFn = r#" |
| 497 |
#[simple] |
| 498 |
#[first(comp)] |
| 499 |
#[second::default] |
| 500 |
#[default(impl Iterator<Item=(u32, i32)>)] |
| 501 |
#[last::more] |
| 502 |
fn my_fix<I, J>(f1: I, f2: J) -> impl Iterator<Item=(I, J)> {} |
| 503 |
"# |
| 504 |
.ast(); |
| 505 |
|
| 506 |
let mut info = FixtureInfo::default(); |
| 507 |
|
| 508 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 509 |
|
| 510 |
assert_eq!( |
| 511 |
info.attributes.extract_default_type(), |
| 512 |
Some(parse_quote! { -> impl Iterator<Item=(u32, i32)> }) |
| 513 |
); |
| 514 |
assert_eq!( |
| 515 |
attrs("#[simple]#[first(comp)]#[second::default]#[last::more]"), |
| 516 |
item_fn.attrs |
| 517 |
); |
| 518 |
} |
| 519 |
|
| 520 |
#[test] |
| 521 |
fn find_partials_return_type() { |
| 522 |
let mut item_fn: ItemFn = r#" |
| 523 |
#[simple] |
| 524 |
#[first(comp)] |
| 525 |
#[second::default] |
| 526 |
#[partial_1(impl Iterator<Item=(u32, J, K)>)] |
| 527 |
#[partial_2(impl Iterator<Item=(u32, i32, K)>)] |
| 528 |
#[last::more] |
| 529 |
fn my_fix<I, J, K>(f1: I, f2: J, f3: K) -> impl Iterator<Item=(I, J, K)> {} |
| 530 |
"# |
| 531 |
.ast(); |
| 532 |
|
| 533 |
let mut info = FixtureInfo::default(); |
| 534 |
|
| 535 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 536 |
|
| 537 |
assert_eq!( |
| 538 |
info.attributes.extract_partial_type(1), |
| 539 |
Some(parse_quote! { -> impl Iterator<Item=(u32, J, K)> }) |
| 540 |
); |
| 541 |
assert_eq!( |
| 542 |
info.attributes.extract_partial_type(2), |
| 543 |
Some(parse_quote! { -> impl Iterator<Item=(u32, i32, K)> }) |
| 544 |
); |
| 545 |
assert_eq!( |
| 546 |
attrs("#[simple]#[first(comp)]#[second::default]#[last::more]"), |
| 547 |
item_fn.attrs |
| 548 |
); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn find_once_attribute() { |
| 553 |
let mut item_fn: ItemFn = r#" |
| 554 |
#[simple] |
| 555 |
#[first(comp)] |
| 556 |
#[second::default] |
| 557 |
#[once] |
| 558 |
#[last::more] |
| 559 |
fn my_fix<I, J, K>(f1: I, f2: J, f3: K) -> impl Iterator<Item=(I, J, K)> {} |
| 560 |
"# |
| 561 |
.ast(); |
| 562 |
|
| 563 |
let mut info = FixtureInfo::default(); |
| 564 |
|
| 565 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 566 |
|
| 567 |
assert!(info.arguments.is_once()); |
| 568 |
} |
| 569 |
|
| 570 |
#[test] |
| 571 |
fn no_once_attribute() { |
| 572 |
let mut item_fn: ItemFn = r#" |
| 573 |
fn my_fix<I, J, K>(f1: I, f2: J, f3: K) -> impl Iterator<Item=(I, J, K)> {} |
| 574 |
"# |
| 575 |
.ast(); |
| 576 |
|
| 577 |
let mut info = FixtureInfo::default(); |
| 578 |
|
| 579 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 580 |
|
| 581 |
assert!(!info.arguments.is_once()); |
| 582 |
} |
| 583 |
|
| 584 |
#[rstest] |
| 585 |
fn extract_future() { |
| 586 |
let mut item_fn = "fn f(#[future] a: u32, b: u32) {}".ast(); |
| 587 |
let expected = "fn f(a: u32, b: u32) {}".ast(); |
| 588 |
|
| 589 |
let mut info = FixtureInfo::default(); |
| 590 |
|
| 591 |
info.extend_with_function_attrs(&mut item_fn).unwrap(); |
| 592 |
|
| 593 |
assert_eq!(item_fn, expected); |
| 594 |
assert!(info.arguments.is_future(&pat("a"))); |
| 595 |
assert!(!info.arguments.is_future(&pat("b"))); |
| 596 |
} |
| 597 |
|
| 598 |
mod raise_error { |
| 599 |
use super::{assert_eq, *}; |
| 600 |
use rstest_test::assert_in; |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn for_invalid_expressions() { |
| 604 |
let mut item_fn: ItemFn = r#" |
| 605 |
fn my_fix(#[with(valid)] f1: &str, #[with(with(,.,))] f2: u32, #[with(with(use))] f3: u32) {} |
| 606 |
"# |
| 607 |
.ast(); |
| 608 |
|
| 609 |
let errors = FixtureInfo::default() |
| 610 |
.extend_with_function_attrs(&mut item_fn) |
| 611 |
.unwrap_err(); |
| 612 |
|
| 613 |
assert_eq!(2, errors.len()); |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn for_invalid_default_type() { |
| 618 |
let mut item_fn: ItemFn = r#" |
| 619 |
#[default(no<valid::>type)] |
| 620 |
fn my_fix<I>() -> I {} |
| 621 |
"# |
| 622 |
.ast(); |
| 623 |
|
| 624 |
let errors = FixtureInfo::default() |
| 625 |
.extend_with_function_attrs(&mut item_fn) |
| 626 |
.unwrap_err(); |
| 627 |
|
| 628 |
assert_eq!(1, errors.len()); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn with_used_more_than_once() { |
| 633 |
let mut item_fn: ItemFn = r#" |
| 634 |
fn my_fix(#[with(1)] #[with(2)] fixture1: &str, #[with(1)] #[with(2)] #[with(3)] fixture2: &str) {} |
| 635 |
"# |
| 636 |
.ast(); |
| 637 |
|
| 638 |
let errors = FixtureInfo::default() |
| 639 |
.extend_with_function_attrs(&mut item_fn) |
| 640 |
.err() |
| 641 |
.unwrap_or_default(); |
| 642 |
|
| 643 |
assert_eq!(3, errors.len()); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn fixture_destruct_without_from() { |
| 648 |
let mut item_fn: ItemFn = r#" |
| 649 |
fn my_fix(#[with(1)] T{a}: T) {} |
| 650 |
"# |
| 651 |
.ast(); |
| 652 |
|
| 653 |
let errors = FixtureInfo::default() |
| 654 |
.extend_with_function_attrs(&mut item_fn) |
| 655 |
.err() |
| 656 |
.unwrap_or_default(); |
| 657 |
|
| 658 |
assert_in!(errors[0].to_string(), "destruct"); |
| 659 |
} |
| 660 |
|
| 661 |
#[test] |
| 662 |
fn from_used_more_than_once() { |
| 663 |
let mut item_fn: ItemFn = r#" |
| 664 |
fn my_fix(#[from(a)] #[from(b)] fixture1: &str, #[from(c)] #[from(d)] #[from(e)] fixture2: &str) {} |
| 665 |
"# |
| 666 |
.ast(); |
| 667 |
|
| 668 |
let errors = FixtureInfo::default() |
| 669 |
.extend_with_function_attrs(&mut item_fn) |
| 670 |
.err() |
| 671 |
.unwrap_or_default(); |
| 672 |
|
| 673 |
assert_eq!(3, errors.len()); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
fn future_is_used_more_than_once() { |
| 678 |
let mut item_fn: ItemFn = r#" |
| 679 |
fn my_fix(#[future] #[future] fixture1: u32) {} |
| 680 |
"# |
| 681 |
.ast(); |
| 682 |
|
| 683 |
let errors = FixtureInfo::default() |
| 684 |
.extend_with_function_attrs(&mut item_fn) |
| 685 |
.err() |
| 686 |
.unwrap_or_default(); |
| 687 |
|
| 688 |
assert_eq!(1, errors.len()); |
| 689 |
assert_in!(errors[0].to_string(), "more than once"); |
| 690 |
} |
| 691 |
|
| 692 |
#[test] |
| 693 |
fn default_used_more_than_once() { |
| 694 |
let mut item_fn: ItemFn = r#" |
| 695 |
fn my_fix(#[default(2)] #[default(3)] f1: u32) {} |
| 696 |
"# |
| 697 |
.ast(); |
| 698 |
|
| 699 |
let errors = FixtureInfo::default() |
| 700 |
.extend_with_function_attrs(&mut item_fn) |
| 701 |
.err() |
| 702 |
.unwrap_or_default(); |
| 703 |
|
| 704 |
assert_eq!(1, errors.len()); |
| 705 |
assert_in!(errors[0].to_string(), "more than once"); |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn if_once_is_defined_more_than_once() { |
| 710 |
let mut item_fn: ItemFn = r#" |
| 711 |
#[once] |
| 712 |
#[once] |
| 713 |
fn my_fix<I>() -> I {} |
| 714 |
"# |
| 715 |
.ast(); |
| 716 |
|
| 717 |
let mut info = FixtureInfo::default(); |
| 718 |
|
| 719 |
let error = info.extend_with_function_attrs(&mut item_fn).unwrap_err(); |
| 720 |
|
| 721 |
assert_in!( |
| 722 |
format!("{:?}", error).to_lowercase(), |
| 723 |
"cannot use #[once] more than once" |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
#[test] |
| 728 |
fn if_default_is_defined_more_than_once() { |
| 729 |
let mut item_fn: ItemFn = r#" |
| 730 |
#[default(u32)] |
| 731 |
#[default(u32)] |
| 732 |
fn my_fix<I>() -> I {} |
| 733 |
"# |
| 734 |
.ast(); |
| 735 |
|
| 736 |
let mut info = FixtureInfo::default(); |
| 737 |
|
| 738 |
let error = info.extend_with_function_attrs(&mut item_fn).unwrap_err(); |
| 739 |
|
| 740 |
assert_in!( |
| 741 |
format!("{:?}", error).to_lowercase(), |
| 742 |
"cannot use #[default] more than once" |
| 743 |
); |
| 744 |
} |
| 745 |
|
| 746 |
#[test] |
| 747 |
fn for_invalid_partial_type() { |
| 748 |
let mut item_fn: ItemFn = r#" |
| 749 |
#[partial_1(no<valid::>type)] |
| 750 |
fn my_fix<I>(x: I, y: u32) -> I {} |
| 751 |
"# |
| 752 |
.ast(); |
| 753 |
|
| 754 |
let errors = FixtureInfo::default() |
| 755 |
.extend_with_function_attrs(&mut item_fn) |
| 756 |
.unwrap_err(); |
| 757 |
|
| 758 |
assert_eq!(1, errors.len()); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn if_partial_is_not_correct() { |
| 763 |
let mut item_fn: ItemFn = r#" |
| 764 |
#[partial_not_a_number(u32)] |
| 765 |
fn my_fix<I, J>(f1: I, f2: &str) -> I {} |
| 766 |
"# |
| 767 |
.ast(); |
| 768 |
|
| 769 |
let mut info = FixtureInfo::default(); |
| 770 |
|
| 771 |
let error = info.extend_with_function_attrs(&mut item_fn).unwrap_err(); |
| 772 |
|
| 773 |
assert_in!( |
| 774 |
format!("{:?}", error).to_lowercase(), |
| 775 |
"invalid partial syntax" |
| 776 |
); |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |