| 1 |
#![cfg(test)] |
| 2 |
|
| 3 |
use syn::{ |
| 4 |
parse::{Parse, ParseStream, Result}, |
| 5 |
parse2, parse_str, |
| 6 |
visit::Visit, |
| 7 |
ItemFn, ItemMod, LocalInit, |
| 8 |
}; |
| 9 |
|
| 10 |
use super::*; |
| 11 |
use crate::test::{assert_eq, fixture, *}; |
| 12 |
use crate::utils::*; |
| 13 |
|
| 14 |
trait SetAsync { |
| 15 |
fn set_async(&mut self, is_async: bool); |
| 16 |
} |
| 17 |
|
| 18 |
impl SetAsync for ItemFn { |
| 19 |
fn set_async(&mut self, is_async: bool) { |
| 20 |
self.sig.asyncness = if is_async { |
| 21 |
Some(parse_quote! { async }) |
| 22 |
} else { |
| 23 |
None |
| 24 |
}; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
fn trace_argument_code_string(arg_name: &str) -> String { |
| 29 |
let arg_name = ident(arg_name); |
| 30 |
let statement: Stmt = parse_quote! { |
| 31 |
println!("{} = {:?}", stringify!(#arg_name) ,#arg_name); |
| 32 |
}; |
| 33 |
statement.display_code() |
| 34 |
} |
| 35 |
|
| 36 |
mod single_test_should { |
| 37 |
use rstest_test::{assert_in, assert_not_in}; |
| 38 |
|
| 39 |
use crate::{ |
| 40 |
parse::arguments::{ArgumentsInfo, FutureArg}, |
| 41 |
test::{assert_eq, *}, |
| 42 |
}; |
| 43 |
|
| 44 |
use super::*; |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn add_return_type_if_any() { |
| 48 |
let input_fn: ItemFn = "fn function(fix: String) -> Result<i32, String> { Ok(42) }".ast(); |
| 49 |
|
| 50 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 51 |
|
| 52 |
assert_eq!(result.sig.output, input_fn.sig.output); |
| 53 |
} |
| 54 |
|
| 55 |
fn extract_inner_test_function(outer: &ItemFn) -> ItemFn { |
| 56 |
let first_stmt = outer.block.stmts.get(0).unwrap(); |
| 57 |
|
| 58 |
parse_quote! { |
| 59 |
#first_stmt |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn include_given_function() { |
| 65 |
let input_fn: ItemFn = r#" |
| 66 |
pub fn test<R: AsRef<str>, B>(mut s: String, v: &u32, a: &mut [i32], r: R) -> (u32, B, String, &str) |
| 67 |
where B: Borrow<u32> |
| 68 |
{ |
| 69 |
let some = 42; |
| 70 |
assert_eq!(42, some); |
| 71 |
} |
| 72 |
"#.ast(); |
| 73 |
|
| 74 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 75 |
|
| 76 |
let inner_fn = extract_inner_test_function(&result); |
| 77 |
let inner_fn_impl: Stmt = inner_fn.block.stmts.last().cloned().unwrap(); |
| 78 |
|
| 79 |
assert_eq!(inner_fn.sig, input_fn.sig); |
| 80 |
assert_eq!(inner_fn_impl.display_code(), input_fn.block.display_code()); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn not_remove_lifetimes() { |
| 85 |
let input_fn: ItemFn = r#" |
| 86 |
pub fn test<'a, 'b, 'c: 'a + 'b>(a: A<'a>, b: A<'b>, c: A<'c>) -> A<'c> |
| 87 |
{ |
| 88 |
} |
| 89 |
"# |
| 90 |
.ast(); |
| 91 |
|
| 92 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 93 |
|
| 94 |
assert_eq!(3, result.sig.generics.lifetimes().count()); |
| 95 |
} |
| 96 |
|
| 97 |
#[rstest] |
| 98 |
fn not_copy_any_attributes( |
| 99 |
#[values( |
| 100 |
"#[test]", |
| 101 |
"#[very::complicated::path]", |
| 102 |
"#[test]#[should_panic]", |
| 103 |
"#[should_panic]#[test]", |
| 104 |
"#[a]#[b]#[c]" |
| 105 |
)] |
| 106 |
attributes: &str, |
| 107 |
) { |
| 108 |
let attributes = attrs(attributes); |
| 109 |
let mut input_fn: ItemFn = r#"pub fn test(_s: String){}"#.ast(); |
| 110 |
input_fn.attrs = attributes; |
| 111 |
|
| 112 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 113 |
let first_stmt = result.block.stmts.get(0).unwrap(); |
| 114 |
|
| 115 |
let inner_fn: ItemFn = parse_quote! { |
| 116 |
#first_stmt |
| 117 |
}; |
| 118 |
|
| 119 |
assert!(inner_fn.attrs.is_empty()); |
| 120 |
} |
| 121 |
|
| 122 |
#[rstest] |
| 123 |
#[case::sync(false)] |
| 124 |
#[case::async_fn(true)] |
| 125 |
fn use_injected_test_attribute_to_mark_test_functions_if_any( |
| 126 |
#[case] is_async: bool, |
| 127 |
#[values( |
| 128 |
"#[test]", |
| 129 |
"#[other::test]", |
| 130 |
"#[very::complicated::path::test]", |
| 131 |
"#[prev]#[test]", |
| 132 |
"#[test]#[after]", |
| 133 |
"#[prev]#[other::test]" |
| 134 |
)] |
| 135 |
attributes: &str, |
| 136 |
) { |
| 137 |
let attributes = attrs(attributes); |
| 138 |
let mut input_fn: ItemFn = r#"fn test(_s: String) {} "#.ast(); |
| 139 |
let mut info = RsTestInfo::default(); |
| 140 |
input_fn.set_async(is_async); |
| 141 |
input_fn.attrs = attributes.clone(); |
| 142 |
info.arguments.set_test_attr(Some(TestAttr::InAttrs)); |
| 143 |
|
| 144 |
let result: ItemFn = single(input_fn.clone(), info).ast(); |
| 145 |
|
| 146 |
assert_eq!(result.attrs, attributes); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn use_global_await() { |
| 151 |
let input_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {} "#.ast(); |
| 152 |
let mut info: RsTestInfo = Default::default(); |
| 153 |
info.arguments.set_global_await(true); |
| 154 |
info.arguments.add_future(pat("a")); |
| 155 |
info.arguments.add_future(pat("b")); |
| 156 |
|
| 157 |
let item_fn: ItemFn = single(input_fn.clone(), info).ast(); |
| 158 |
|
| 159 |
assert_in!( |
| 160 |
item_fn.block.display_code(), |
| 161 |
await_argument_code_string("a") |
| 162 |
); |
| 163 |
assert_in!( |
| 164 |
item_fn.block.display_code(), |
| 165 |
await_argument_code_string("b") |
| 166 |
); |
| 167 |
assert_not_in!( |
| 168 |
item_fn.block.display_code(), |
| 169 |
await_argument_code_string("c") |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn use_selective_await() { |
| 175 |
let input_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {} "#.ast(); |
| 176 |
let mut info: RsTestInfo = Default::default(); |
| 177 |
info.arguments.set_future(pat("a"), FutureArg::Define); |
| 178 |
info.arguments.set_future(pat("b"), FutureArg::Await); |
| 179 |
|
| 180 |
let item_fn: ItemFn = single(input_fn.clone(), info).ast(); |
| 181 |
|
| 182 |
assert_not_in!( |
| 183 |
item_fn.block.display_code(), |
| 184 |
await_argument_code_string("a",) |
| 185 |
); |
| 186 |
assert_in!( |
| 187 |
item_fn.block.display_code(), |
| 188 |
await_argument_code_string("b") |
| 189 |
); |
| 190 |
assert_not_in!( |
| 191 |
item_fn.block.display_code(), |
| 192 |
await_argument_code_string("c") |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn use_ref_if_any() { |
| 198 |
let input_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {} "#.ast(); |
| 199 |
let mut info: RsTestInfo = Default::default(); |
| 200 |
info.arguments.set_by_ref(pat("a")); |
| 201 |
info.arguments.set_by_ref(pat("c")); |
| 202 |
|
| 203 |
let item_fn: ItemFn = single(input_fn.clone(), info).ast(); |
| 204 |
|
| 205 |
assert_in!( |
| 206 |
item_fn.block.stmts.last().display_code(), |
| 207 |
ref_argument_code_string("a") |
| 208 |
); |
| 209 |
assert_not_in!( |
| 210 |
item_fn.block.stmts.last().display_code(), |
| 211 |
ref_argument_code_string("b") |
| 212 |
); |
| 213 |
assert_in!( |
| 214 |
item_fn.block.stmts.last().display_code(), |
| 215 |
ref_argument_code_string("c") |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn trace_arguments_values() { |
| 221 |
let input_fn: ItemFn = r#"#[trace]fn test(s: String, a:i32, mut b: i32) {} "#.ast(); |
| 222 |
|
| 223 |
let item_fn: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 224 |
|
| 225 |
assert_in!( |
| 226 |
item_fn.block.display_code(), |
| 227 |
trace_argument_code_string("s") |
| 228 |
); |
| 229 |
assert_in!( |
| 230 |
item_fn.block.display_code(), |
| 231 |
trace_argument_code_string("a") |
| 232 |
); |
| 233 |
assert_in!( |
| 234 |
item_fn.block.display_code(), |
| 235 |
trace_argument_code_string("b") |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn trace_not_all_arguments_values() { |
| 241 |
let input_fn: ItemFn = |
| 242 |
r#"#[trace] fn test(a_trace: i32, b_no_trace:i32, c_no_trace:i32, d_trace:i32, mut e_no_trace:i32) {} "# |
| 243 |
.ast(); |
| 244 |
|
| 245 |
let mut attributes = RsTestAttributes::default(); |
| 246 |
attributes.add_notraces(vec![ |
| 247 |
pat("b_no_trace"), |
| 248 |
pat("c_no_trace"), |
| 249 |
pat("mut e_no_trace"), |
| 250 |
]); |
| 251 |
|
| 252 |
let item_fn: ItemFn = single( |
| 253 |
input_fn.clone(), |
| 254 |
RsTestInfo { |
| 255 |
attributes, |
| 256 |
..Default::default() |
| 257 |
}, |
| 258 |
) |
| 259 |
.ast(); |
| 260 |
|
| 261 |
assert_in!( |
| 262 |
item_fn.block.display_code(), |
| 263 |
trace_argument_code_string("a_trace") |
| 264 |
); |
| 265 |
assert_not_in!( |
| 266 |
item_fn.block.display_code(), |
| 267 |
trace_argument_code_string("b_no_trace") |
| 268 |
); |
| 269 |
assert_not_in!( |
| 270 |
item_fn.block.display_code(), |
| 271 |
trace_argument_code_string("c_no_trace") |
| 272 |
); |
| 273 |
assert_in!( |
| 274 |
item_fn.block.display_code(), |
| 275 |
trace_argument_code_string("d_trace") |
| 276 |
); |
| 277 |
assert_not_in!( |
| 278 |
item_fn.block.display_code(), |
| 279 |
trace_argument_code_string("e_no_trace") |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
#[rstest] |
| 284 |
#[case::sync("", parse_quote! { #[test] })] |
| 285 |
fn add_default_test_attribute( |
| 286 |
#[case] prefix: &str, |
| 287 |
#[case] test_attribute: Attribute, |
| 288 |
#[values( |
| 289 |
"", |
| 290 |
"#[no_one]", |
| 291 |
"#[should_panic]", |
| 292 |
"#[should_panic]#[other]", |
| 293 |
"#[a::b::c]#[should_panic]" |
| 294 |
)] |
| 295 |
attributes: &str, |
| 296 |
) { |
| 297 |
let attributes = attrs(attributes); |
| 298 |
let mut input_fn: ItemFn = format!(r#"{} fn test(_s: String) {{}} "#, prefix).ast(); |
| 299 |
input_fn.attrs = attributes.clone(); |
| 300 |
|
| 301 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 302 |
|
| 303 |
let (generated_attribute, old_attributes) = result.attrs.split_last().unwrap(); |
| 304 |
assert_eq!(old_attributes, attributes.as_slice()); |
| 305 |
assert_eq!(generated_attribute, &test_attribute); |
| 306 |
} |
| 307 |
|
| 308 |
#[rstest] |
| 309 |
#[case::sync(false, false)] |
| 310 |
#[case::async_fn(true, true)] |
| 311 |
fn use_await_for_no_async_test_function(#[case] is_async: bool, #[case] use_await: bool) { |
| 312 |
let mut input_fn: ItemFn = r#"fn test(_s: String) {} "#.ast(); |
| 313 |
input_fn.set_async(is_async); |
| 314 |
if is_async { |
| 315 |
input_fn.attrs.push(parse_quote!(#[async_std::test])); |
| 316 |
} |
| 317 |
|
| 318 |
let result: ItemFn = single(input_fn.clone(), Default::default()).ast(); |
| 319 |
|
| 320 |
let last_stmt = result.block.stmts.last().unwrap(); |
| 321 |
|
| 322 |
assert_eq!(use_await, last_stmt.is_await()); |
| 323 |
} |
| 324 |
#[test] |
| 325 |
fn add_future_boilerplate_if_requested() { |
| 326 |
let item_fn: ItemFn = r#" |
| 327 |
#[async_std::test] |
| 328 |
async fn test(async_ref_u32: &u32, async_u32: u32,simple: u32) |
| 329 |
{ } |
| 330 |
"# |
| 331 |
.ast(); |
| 332 |
|
| 333 |
let mut arguments = ArgumentsInfo::default(); |
| 334 |
arguments.add_future(pat("async_ref_u32")); |
| 335 |
arguments.add_future(pat("async_u32")); |
| 336 |
|
| 337 |
let info = RsTestInfo { |
| 338 |
arguments, |
| 339 |
..Default::default() |
| 340 |
}; |
| 341 |
|
| 342 |
let result: ItemFn = single(item_fn.clone(), info).ast(); |
| 343 |
let inner_fn = extract_inner_test_function(&result); |
| 344 |
|
| 345 |
let expected = parse_str::<syn::ItemFn>( |
| 346 |
r#"async fn test<'_async_ref_u32>( |
| 347 |
async_ref_u32: impl ::core::future::Future<Output = &'_async_ref_u32 u32>, |
| 348 |
async_u32: impl ::core::future::Future<Output = u32>, |
| 349 |
simple: u32 |
| 350 |
) |
| 351 |
{ } |
| 352 |
"#, |
| 353 |
) |
| 354 |
.unwrap(); |
| 355 |
|
| 356 |
assert_eq!(inner_fn.sig, expected.sig); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
#[derive(Debug)] |
| 361 |
struct TestsGroup { |
| 362 |
requested_test: ItemFn, |
| 363 |
module: ItemMod, |
| 364 |
} |
| 365 |
|
| 366 |
impl Parse for TestsGroup { |
| 367 |
fn parse(input: ParseStream) -> Result<Self> { |
| 368 |
Ok(Self { |
| 369 |
requested_test: input.parse()?, |
| 370 |
module: input.parse()?, |
| 371 |
}) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
trait QueryAttrs { |
| 376 |
#[allow(dead_code)] |
| 377 |
fn has_attr(&self, attr: &syn::Path) -> bool; |
| 378 |
fn has_attr_that_ends_with(&self, attr: &syn::PathSegment) -> bool; |
| 379 |
} |
| 380 |
|
| 381 |
impl QueryAttrs for ItemFn { |
| 382 |
fn has_attr(&self, attr: &syn::Path) -> bool { |
| 383 |
self.attrs.iter().find(|a| a.path() == attr).is_some() |
| 384 |
} |
| 385 |
|
| 386 |
fn has_attr_that_ends_with(&self, name: &syn::PathSegment) -> bool { |
| 387 |
self.attrs |
| 388 |
.iter() |
| 389 |
.find(|a| attr_ends_with(a, name)) |
| 390 |
.is_some() |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/// To extract all test functions |
| 395 |
struct TestFunctions(Vec<ItemFn>); |
| 396 |
|
| 397 |
fn is_test_fn(item_fn: &ItemFn) -> bool { |
| 398 |
item_fn.has_attr_that_ends_with(&parse_quote! { test }) |
| 399 |
} |
| 400 |
|
| 401 |
impl TestFunctions { |
| 402 |
fn is_test_fn(item_fn: &ItemFn) -> bool { |
| 403 |
is_test_fn(item_fn) |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
impl<'ast> Visit<'ast> for TestFunctions { |
| 408 |
//noinspection RsTypeCheck |
| 409 |
fn visit_item_fn(&mut self, item_fn: &'ast ItemFn) { |
| 410 |
if Self::is_test_fn(item_fn) { |
| 411 |
self.0.push(item_fn.clone()) |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
trait Named { |
| 417 |
fn name(&self) -> String; |
| 418 |
} |
| 419 |
|
| 420 |
impl Named for Ident { |
| 421 |
fn name(&self) -> String { |
| 422 |
self.to_string() |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
impl Named for ItemFn { |
| 427 |
fn name(&self) -> String { |
| 428 |
self.sig.ident.name() |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
impl Named for ItemMod { |
| 433 |
fn name(&self) -> String { |
| 434 |
self.ident.name() |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
trait Names { |
| 439 |
fn names(&self) -> Vec<String>; |
| 440 |
} |
| 441 |
|
| 442 |
impl<T: Named> Names for Vec<T> { |
| 443 |
fn names(&self) -> Vec<String> { |
| 444 |
self.iter().map(Named::name).collect() |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
trait ModuleInspector { |
| 449 |
fn get_all_tests(&self) -> Vec<ItemFn>; |
| 450 |
fn get_tests(&self) -> Vec<ItemFn>; |
| 451 |
fn get_modules(&self) -> Vec<ItemMod>; |
| 452 |
} |
| 453 |
|
| 454 |
impl ModuleInspector for ItemMod { |
| 455 |
fn get_tests(&self) -> Vec<ItemFn> { |
| 456 |
self.content |
| 457 |
.as_ref() |
| 458 |
.map(|(_, items)| { |
| 459 |
items |
| 460 |
.iter() |
| 461 |
.filter_map(|it| match it { |
| 462 |
syn::Item::Fn(item_fn) if is_test_fn(item_fn) => Some(item_fn.clone()), |
| 463 |
_ => None, |
| 464 |
}) |
| 465 |
.collect() |
| 466 |
}) |
| 467 |
.unwrap_or_default() |
| 468 |
} |
| 469 |
|
| 470 |
fn get_all_tests(&self) -> Vec<ItemFn> { |
| 471 |
let mut f = TestFunctions(vec![]); |
| 472 |
f.visit_item_mod(&self); |
| 473 |
f.0 |
| 474 |
} |
| 475 |
|
| 476 |
fn get_modules(&self) -> Vec<ItemMod> { |
| 477 |
self.content |
| 478 |
.as_ref() |
| 479 |
.map(|(_, items)| { |
| 480 |
items |
| 481 |
.iter() |
| 482 |
.filter_map(|it| match it { |
| 483 |
syn::Item::Mod(item_mod) => Some(item_mod.clone()), |
| 484 |
_ => None, |
| 485 |
}) |
| 486 |
.collect() |
| 487 |
}) |
| 488 |
.unwrap_or_default() |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
impl ModuleInspector for TestsGroup { |
| 493 |
fn get_all_tests(&self) -> Vec<ItemFn> { |
| 494 |
self.module.get_all_tests() |
| 495 |
} |
| 496 |
|
| 497 |
fn get_tests(&self) -> Vec<ItemFn> { |
| 498 |
self.module.get_tests() |
| 499 |
} |
| 500 |
|
| 501 |
fn get_modules(&self) -> Vec<ItemMod> { |
| 502 |
self.module.get_modules() |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
#[derive(Default, Debug)] |
| 507 |
struct Assignments(HashMap<String, syn::Expr>); |
| 508 |
|
| 509 |
impl<'ast> Visit<'ast> for Assignments { |
| 510 |
//noinspection RsTypeCheck |
| 511 |
fn visit_local(&mut self, assign: &syn::Local) { |
| 512 |
match &assign { |
| 513 |
syn::Local { |
| 514 |
pat: syn::Pat::Ident(pat), |
| 515 |
init: Some(LocalInit { expr, .. }), |
| 516 |
.. |
| 517 |
} => { |
| 518 |
self.0.insert(pat.ident.to_string(), expr.as_ref().clone()); |
| 519 |
} |
| 520 |
_ => {} |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
impl Assignments { |
| 526 |
pub fn collect_assignments(item_fn: &ItemFn) -> Self { |
| 527 |
let mut collect = Self::default(); |
| 528 |
collect.visit_item_fn(item_fn); |
| 529 |
collect |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
impl From<TokenStream> for TestsGroup { |
| 534 |
fn from(tokens: TokenStream) -> Self { |
| 535 |
syn::parse2::<TestsGroup>(tokens).unwrap() |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
mod cases_should { |
| 540 |
|
| 541 |
use rstest_test::{assert_in, assert_not_in}; |
| 542 |
|
| 543 |
use crate::parse::{ |
| 544 |
arguments::{ArgumentsInfo, FutureArg}, |
| 545 |
rstest::{RsTestData, RsTestItem}, |
| 546 |
}; |
| 547 |
|
| 548 |
use super::{assert_eq, *}; |
| 549 |
|
| 550 |
fn into_rstest_data(item_fn: &ItemFn) -> RsTestData { |
| 551 |
RsTestData { |
| 552 |
items: fn_args_pats(item_fn) |
| 553 |
.cloned() |
| 554 |
.map(RsTestItem::CaseArgName) |
| 555 |
.collect(), |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
struct TestCaseBuilder { |
| 560 |
item_fn: ItemFn, |
| 561 |
info: RsTestInfo, |
| 562 |
} |
| 563 |
|
| 564 |
impl TestCaseBuilder { |
| 565 |
fn new(item_fn: ItemFn) -> Self { |
| 566 |
let info: RsTestInfo = into_rstest_data(&item_fn).into(); |
| 567 |
Self { item_fn, info } |
| 568 |
} |
| 569 |
|
| 570 |
fn from<S: AsRef<str>>(s: S) -> Self { |
| 571 |
Self::new(s.as_ref().ast()) |
| 572 |
} |
| 573 |
|
| 574 |
fn set_async(mut self, is_async: bool) -> Self { |
| 575 |
self.item_fn.set_async(is_async); |
| 576 |
// when building an async test case, let's set an implicit test attribute |
| 577 |
// so we can use the proper async runtime. |
| 578 |
if is_async { |
| 579 |
self.item_fn.attrs.push(parse_quote!(#[async_std::test])); |
| 580 |
} |
| 581 |
self |
| 582 |
} |
| 583 |
|
| 584 |
fn push_case<T: Into<TestCase>>(mut self, case: T) -> Self { |
| 585 |
self.info.push_case(case.into()); |
| 586 |
self |
| 587 |
} |
| 588 |
|
| 589 |
fn extend<T: Into<TestCase>>(mut self, cases: impl Iterator<Item = T>) -> Self { |
| 590 |
self.info.extend(cases.map(Into::into)); |
| 591 |
self |
| 592 |
} |
| 593 |
|
| 594 |
fn take(self) -> (ItemFn, RsTestInfo) { |
| 595 |
(self.item_fn, self.info) |
| 596 |
} |
| 597 |
|
| 598 |
fn add_notrace(mut self, pats: Vec<Pat>) -> Self { |
| 599 |
self.info.attributes.add_notraces(pats); |
| 600 |
self |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
fn one_simple_case() -> (ItemFn, RsTestInfo) { |
| 605 |
TestCaseBuilder::from(r#"fn test(mut fix: String) { println!("user code") }"#) |
| 606 |
.push_case(r#"String::from("3")"#) |
| 607 |
.take() |
| 608 |
} |
| 609 |
|
| 610 |
fn some_simple_cases(cases: i32) -> (ItemFn, RsTestInfo) { |
| 611 |
TestCaseBuilder::from(r#"fn test(mut fix: String) { println!("user code") }"#) |
| 612 |
.extend((0..cases).map(|_| r#"String::from("3")"#)) |
| 613 |
.take() |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn create_a_module_named_as_test_function() { |
| 618 |
let (item_fn, info) = |
| 619 |
TestCaseBuilder::from("fn should_be_the_module_name(mut fix: String) {}").take(); |
| 620 |
|
| 621 |
let tokens = parametrize(item_fn, info); |
| 622 |
|
| 623 |
let output = TestsGroup::from(tokens); |
| 624 |
|
| 625 |
assert_eq!(output.module.ident, "should_be_the_module_name"); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn copy_user_function() { |
| 630 |
let (item_fn, info) = TestCaseBuilder::from( |
| 631 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#, |
| 632 |
) |
| 633 |
.take(); |
| 634 |
|
| 635 |
let tokens = parametrize(item_fn.clone(), info); |
| 636 |
|
| 637 |
let mut output = TestsGroup::from(tokens); |
| 638 |
let test_impl: Stmt = output.requested_test.block.stmts.last().cloned().unwrap(); |
| 639 |
|
| 640 |
output.requested_test.attrs = vec![]; |
| 641 |
assert_eq!(output.requested_test.sig, item_fn.sig); |
| 642 |
assert_eq!(test_impl.display_code(), item_fn.block.display_code()); |
| 643 |
} |
| 644 |
|
| 645 |
#[test] |
| 646 |
fn should_not_copy_should_panic_attribute() { |
| 647 |
let (item_fn, info) = TestCaseBuilder::from( |
| 648 |
r#"#[should_panic] fn with_should_panic(mut fix: String) { println!("user code") }"#, |
| 649 |
) |
| 650 |
.take(); |
| 651 |
|
| 652 |
let tokens = parametrize(item_fn.clone(), info); |
| 653 |
|
| 654 |
let output = TestsGroup::from(tokens); |
| 655 |
|
| 656 |
assert!(!format!("{:?}", output.requested_test.attrs).contains("should_panic")); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn should_mark_test_with_given_attributes() { |
| 661 |
let (item_fn, info) = |
| 662 |
TestCaseBuilder::from(r#"#[should_panic] #[other(value)] fn test(s: String){}"#) |
| 663 |
.push_case(r#"String::from("3")"#) |
| 664 |
.take(); |
| 665 |
|
| 666 |
let tokens = parametrize(item_fn.clone(), info); |
| 667 |
|
| 668 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 669 |
|
| 670 |
// Sanity check |
| 671 |
assert!(tests.len() > 0); |
| 672 |
|
| 673 |
for t in tests { |
| 674 |
assert_eq!(item_fn.attrs, &t.attrs[..t.attrs.len() - 1]); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
#[rstest] |
| 679 |
#[case::empty("")] |
| 680 |
#[case::some_attrs("#[a]#[b::c]#[should_panic]")] |
| 681 |
fn should_add_attributes_given_in_the_test_case( |
| 682 |
#[case] fnattrs: &str, |
| 683 |
#[values("", "#[should_panic]", "#[first]#[second(arg)]")] case_attrs: &str, |
| 684 |
) { |
| 685 |
let given_attrs = attrs(fnattrs); |
| 686 |
let case_attrs = attrs(case_attrs); |
| 687 |
let (mut item_fn, info) = TestCaseBuilder::from(r#"fn test(v: i32){}"#) |
| 688 |
.push_case(TestCase::from("42").with_attrs(case_attrs.clone())) |
| 689 |
.take(); |
| 690 |
|
| 691 |
item_fn.attrs = given_attrs.clone(); |
| 692 |
|
| 693 |
let tokens = parametrize(item_fn, info); |
| 694 |
|
| 695 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 696 |
let test_attrs = tests[0].attrs.split_last().unwrap().1; |
| 697 |
|
| 698 |
let l = given_attrs.len(); |
| 699 |
|
| 700 |
assert_eq!(case_attrs.as_slice(), &test_attrs[l..]); |
| 701 |
assert_eq!(given_attrs.as_slice(), &test_attrs[..l]); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn mark_user_function_as_test() { |
| 706 |
let (item_fn, info) = TestCaseBuilder::from( |
| 707 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#, |
| 708 |
) |
| 709 |
.take(); |
| 710 |
let tokens = parametrize(item_fn, info); |
| 711 |
|
| 712 |
let output = TestsGroup::from(tokens); |
| 713 |
|
| 714 |
assert_eq!( |
| 715 |
output.requested_test.attrs, |
| 716 |
vec![parse_quote! {#[cfg(test)]}] |
| 717 |
); |
| 718 |
} |
| 719 |
|
| 720 |
#[test] |
| 721 |
fn mark_module_as_test() { |
| 722 |
let (item_fn, info) = TestCaseBuilder::from( |
| 723 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#, |
| 724 |
) |
| 725 |
.take(); |
| 726 |
let tokens = parametrize(item_fn, info); |
| 727 |
|
| 728 |
let output = TestsGroup::from(tokens); |
| 729 |
|
| 730 |
assert_eq!(output.module.attrs, vec![parse_quote! {#[cfg(test)]}]); |
| 731 |
} |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn add_a_test_case() { |
| 735 |
let (item_fn, info) = one_simple_case(); |
| 736 |
|
| 737 |
let tokens = parametrize(item_fn, info); |
| 738 |
|
| 739 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 740 |
|
| 741 |
assert_eq!(1, tests.len()); |
| 742 |
assert!(&tests[0].sig.ident.to_string().starts_with("case_")) |
| 743 |
} |
| 744 |
|
| 745 |
#[test] |
| 746 |
fn add_return_type_if_any() { |
| 747 |
let (item_fn, info) = |
| 748 |
TestCaseBuilder::from("fn function(fix: String) -> Result<i32, String> { Ok(42) }") |
| 749 |
.push_case(r#"String::from("3")"#) |
| 750 |
.take(); |
| 751 |
|
| 752 |
let tokens = parametrize(item_fn.clone(), info); |
| 753 |
|
| 754 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 755 |
|
| 756 |
assert_eq!(tests[0].sig.output, item_fn.sig.output); |
| 757 |
} |
| 758 |
|
| 759 |
#[test] |
| 760 |
fn not_copy_user_function() { |
| 761 |
let t_name = "test_name"; |
| 762 |
let (item_fn, info) = TestCaseBuilder::from(format!( |
| 763 |
"fn {}(fix: String) -> Result<i32, String> {{ Ok(42) }}", |
| 764 |
t_name |
| 765 |
)) |
| 766 |
.push_case(r#"String::from("3")"#) |
| 767 |
.take(); |
| 768 |
|
| 769 |
let tokens = parametrize(item_fn, info); |
| 770 |
|
| 771 |
let test = &TestsGroup::from(tokens).get_all_tests()[0]; |
| 772 |
let inner_functions = extract_inner_functions(&test.block); |
| 773 |
|
| 774 |
assert_eq!(0, inner_functions.filter(|f| f.sig.ident == t_name).count()); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn starts_case_number_from_1() { |
| 779 |
let (item_fn, info) = one_simple_case(); |
| 780 |
|
| 781 |
let tokens = parametrize(item_fn.clone(), info); |
| 782 |
|
| 783 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 784 |
|
| 785 |
assert!( |
| 786 |
&tests[0].sig.ident.to_string().starts_with("case_1"), |
| 787 |
"Should starts with case_1 but is {}", |
| 788 |
tests[0].sig.ident.to_string() |
| 789 |
) |
| 790 |
} |
| 791 |
|
| 792 |
#[test] |
| 793 |
fn add_all_test_cases() { |
| 794 |
let (item_fn, info) = some_simple_cases(5); |
| 795 |
|
| 796 |
let tokens = parametrize(item_fn.clone(), info); |
| 797 |
|
| 798 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 799 |
|
| 800 |
let valid_names = tests |
| 801 |
.iter() |
| 802 |
.filter(|it| it.sig.ident.to_string().starts_with("case_")); |
| 803 |
assert_eq!(5, valid_names.count()) |
| 804 |
} |
| 805 |
|
| 806 |
#[test] |
| 807 |
fn left_pad_case_number_by_zeros() { |
| 808 |
let (item_fn, info) = some_simple_cases(1000); |
| 809 |
|
| 810 |
let tokens = parametrize(item_fn.clone(), info); |
| 811 |
|
| 812 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 813 |
|
| 814 |
let first_name = tests[0].sig.ident.to_string(); |
| 815 |
let last_name = tests[999].sig.ident.to_string(); |
| 816 |
|
| 817 |
assert!( |
| 818 |
first_name.ends_with("_0001"), |
| 819 |
"Should ends by _0001 but is {}", |
| 820 |
first_name |
| 821 |
); |
| 822 |
assert!( |
| 823 |
last_name.ends_with("_1000"), |
| 824 |
"Should ends by _1000 but is {}", |
| 825 |
last_name |
| 826 |
); |
| 827 |
|
| 828 |
let valid_names = tests |
| 829 |
.iter() |
| 830 |
.filter(|it| it.sig.ident.to_string().len() == first_name.len()); |
| 831 |
assert_eq!(1000, valid_names.count()) |
| 832 |
} |
| 833 |
|
| 834 |
#[test] |
| 835 |
fn use_description_if_any() { |
| 836 |
let (item_fn, mut info) = one_simple_case(); |
| 837 |
let description = "show_this_description"; |
| 838 |
|
| 839 |
if let &mut RsTestItem::TestCase(ref mut case) = &mut info.data.items[1] { |
| 840 |
case.description = Some(parse_str::<Ident>(description).unwrap()); |
| 841 |
} else { |
| 842 |
panic!("Test case should be the second one"); |
| 843 |
} |
| 844 |
|
| 845 |
let tokens = parametrize(item_fn.clone(), info); |
| 846 |
|
| 847 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 848 |
|
| 849 |
assert!(tests[0] |
| 850 |
.sig |
| 851 |
.ident |
| 852 |
.to_string() |
| 853 |
.ends_with(&format!("_{}", description))); |
| 854 |
} |
| 855 |
|
| 856 |
#[rstest] |
| 857 |
#[case::sync(false)] |
| 858 |
#[case::async_fn(true)] |
| 859 |
fn use_injected_test_attribute_to_mark_test_functions_if_any( |
| 860 |
#[case] is_async: bool, |
| 861 |
#[values( |
| 862 |
"#[test]", |
| 863 |
"#[other::test]", |
| 864 |
"#[very::complicated::path::test]", |
| 865 |
"#[prev]#[test]", |
| 866 |
"#[test]#[after]", |
| 867 |
"#[prev]#[other::test]" |
| 868 |
)] |
| 869 |
attributes: &str, |
| 870 |
) { |
| 871 |
let attributes = attrs(attributes); |
| 872 |
let (mut item_fn, mut info) = TestCaseBuilder::from(r#"fn test(s: String){}"#) |
| 873 |
.push_case(r#"String::from("3")"#) |
| 874 |
.set_async(is_async) |
| 875 |
.take(); |
| 876 |
info.arguments.set_test_attr(Some(TestAttr::InAttrs)); |
| 877 |
item_fn.attrs = attributes.clone(); |
| 878 |
item_fn.set_async(is_async); |
| 879 |
|
| 880 |
let tokens = parametrize(item_fn.clone(), info); |
| 881 |
|
| 882 |
let test = &TestsGroup::from(tokens).get_all_tests()[0]; |
| 883 |
|
| 884 |
assert_eq!(attributes, test.attrs); |
| 885 |
} |
| 886 |
|
| 887 |
#[rstest] |
| 888 |
#[case::sync(false, parse_quote! { #[test] })] |
| 889 |
fn add_default_test_attribute( |
| 890 |
#[case] is_async: bool, |
| 891 |
#[case] test_attribute: Attribute, |
| 892 |
#[values( |
| 893 |
"", |
| 894 |
"#[no_one]", |
| 895 |
"#[should_panic]", |
| 896 |
"#[should_panic]#[other]", |
| 897 |
"#[a::b::c]#[should_panic]" |
| 898 |
)] |
| 899 |
attributes: &str, |
| 900 |
) { |
| 901 |
let attributes = attrs(attributes); |
| 902 |
let (mut item_fn, info) = TestCaseBuilder::from( |
| 903 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#, |
| 904 |
) |
| 905 |
.push_case("42") |
| 906 |
.set_async(is_async) |
| 907 |
.take(); |
| 908 |
item_fn.attrs = attributes.clone(); |
| 909 |
|
| 910 |
let tokens = parametrize(item_fn, info); |
| 911 |
|
| 912 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 913 |
let (generated_attribute, old_attributes) = tests[0].attrs.split_last().unwrap(); |
| 914 |
|
| 915 |
assert_eq!(old_attributes, attributes.as_slice()); |
| 916 |
assert_eq!(generated_attribute, &test_attribute); |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn add_future_boilerplate_if_requested() { |
| 921 |
let (item_fn, mut info) = TestCaseBuilder::from( |
| 922 |
r#"async fn test(async_ref_u32: &u32, async_u32: u32,simple: u32) { }"#, |
| 923 |
) |
| 924 |
.take(); |
| 925 |
|
| 926 |
let mut arguments = ArgumentsInfo::default(); |
| 927 |
arguments.add_future(pat("async_ref_u32")); |
| 928 |
arguments.add_future(pat("async_u32")); |
| 929 |
|
| 930 |
info.arguments = arguments; |
| 931 |
|
| 932 |
let tokens = parametrize(item_fn.clone(), info); |
| 933 |
let test_function = TestsGroup::from(tokens).requested_test; |
| 934 |
|
| 935 |
let expected = parse_str::<syn::ItemFn>( |
| 936 |
r#"async fn test<'_async_ref_u32>( |
| 937 |
async_ref_u32: impl ::core::future::Future<Output = &'_async_ref_u32 u32>, |
| 938 |
async_u32: impl ::core::future::Future<Output = u32>, |
| 939 |
simple: u32 |
| 940 |
) |
| 941 |
{ } |
| 942 |
"#, |
| 943 |
) |
| 944 |
.unwrap(); |
| 945 |
|
| 946 |
assert_eq!(test_function.sig, expected.sig); |
| 947 |
} |
| 948 |
|
| 949 |
#[rstest] |
| 950 |
#[case::sync(false, false)] |
| 951 |
#[case::async_fn(true, true)] |
| 952 |
fn use_await_for_async_test_function(#[case] is_async: bool, #[case] use_await: bool) { |
| 953 |
let (item_fn, info) = |
| 954 |
TestCaseBuilder::from(r#"fn test(mut fix: String) { println!("user code") }"#) |
| 955 |
.set_async(is_async) |
| 956 |
.push_case(r#"String::from("3")"#) |
| 957 |
.take(); |
| 958 |
|
| 959 |
let tokens = parametrize(item_fn, info); |
| 960 |
|
| 961 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 962 |
|
| 963 |
let last_stmt = tests[0].block.stmts.last().unwrap(); |
| 964 |
|
| 965 |
assert_eq!(use_await, last_stmt.is_await()); |
| 966 |
} |
| 967 |
|
| 968 |
#[test] |
| 969 |
fn trace_arguments_value() { |
| 970 |
let (item_fn, info) = |
| 971 |
TestCaseBuilder::from(r#"#[trace] fn test(a_trace_me: i32, b_trace_me: i32) {}"#) |
| 972 |
.push_case(TestCase::from_iter(vec!["1", "2"])) |
| 973 |
.push_case(TestCase::from_iter(vec!["3", "4"])) |
| 974 |
.take(); |
| 975 |
|
| 976 |
let tokens = parametrize(item_fn, info); |
| 977 |
|
| 978 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 979 |
|
| 980 |
assert!(tests.len() > 0); |
| 981 |
for test in tests { |
| 982 |
for name in &["a_trace_me", "b_trace_me"] { |
| 983 |
assert_in!(test.block.display_code(), trace_argument_code_string(name)); |
| 984 |
} |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
#[test] |
| 989 |
fn trace_just_some_arguments_value() { |
| 990 |
let (item_fn, info) = |
| 991 |
TestCaseBuilder::from(r#"#[trace] fn test(a_trace_me: i32, b_no_trace_me: i32, c_no_trace_me: i32, d_trace_me: i32) {}"#) |
| 992 |
.push_case(TestCase::from_iter(vec!["1", "2", "1", "2"])) |
| 993 |
.push_case(TestCase::from_iter(vec!["3", "4", "3", "4"])) |
| 994 |
.add_notrace(to_pats!(["b_no_trace_me", "c_no_trace_me"])) |
| 995 |
.take(); |
| 996 |
|
| 997 |
let tokens = parametrize(item_fn, info); |
| 998 |
|
| 999 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1000 |
|
| 1001 |
assert!(tests.len() > 0); |
| 1002 |
for test in tests { |
| 1003 |
for should_be_present in &["a_trace_me", "d_trace_me"] { |
| 1004 |
assert_in!( |
| 1005 |
test.block.display_code(), |
| 1006 |
trace_argument_code_string(should_be_present) |
| 1007 |
); |
| 1008 |
} |
| 1009 |
for should_not_be_present in &["b_trace_me", "c_trace_me"] { |
| 1010 |
assert_not_in!( |
| 1011 |
test.block.display_code(), |
| 1012 |
trace_argument_code_string(should_not_be_present) |
| 1013 |
); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
#[test] |
| 1019 |
fn trace_just_one_case() { |
| 1020 |
let (item_fn, info) = |
| 1021 |
TestCaseBuilder::from(r#"fn test(a_no_trace_me: i32, b_trace_me: i32) {}"#) |
| 1022 |
.push_case(TestCase::from_iter(vec!["1", "2"])) |
| 1023 |
.push_case(TestCase::from_iter(vec!["3", "4"]).with_attrs(attrs("#[trace]"))) |
| 1024 |
.add_notrace(to_pats!(["a_no_trace_me"])) |
| 1025 |
.take(); |
| 1026 |
|
| 1027 |
let tokens = parametrize(item_fn, info); |
| 1028 |
|
| 1029 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1030 |
|
| 1031 |
assert_not_in!( |
| 1032 |
tests[0].block.display_code(), |
| 1033 |
trace_argument_code_string("b_trace_me") |
| 1034 |
); |
| 1035 |
assert_in!( |
| 1036 |
tests[1].block.display_code(), |
| 1037 |
trace_argument_code_string("b_trace_me") |
| 1038 |
); |
| 1039 |
assert_not_in!( |
| 1040 |
tests[1].block.display_code(), |
| 1041 |
trace_argument_code_string("a_no_trace_me") |
| 1042 |
); |
| 1043 |
} |
| 1044 |
|
| 1045 |
#[test] |
| 1046 |
fn use_global_await() { |
| 1047 |
let (item_fn, mut info) = TestCaseBuilder::from(r#"fn test(a: i32, b:i32, c:i32) {}"#) |
| 1048 |
.push_case(TestCase::from_iter(vec!["1", "2", "3"])) |
| 1049 |
.push_case(TestCase::from_iter(vec!["1", "2", "3"])) |
| 1050 |
.take(); |
| 1051 |
info.arguments.set_global_await(true); |
| 1052 |
info.arguments.add_future(pat("a")); |
| 1053 |
info.arguments.add_future(pat("b")); |
| 1054 |
|
| 1055 |
let tokens = parametrize(item_fn, info); |
| 1056 |
|
| 1057 |
let tests = TestsGroup::from(tokens); |
| 1058 |
|
| 1059 |
let code = tests.requested_test.block.display_code(); |
| 1060 |
|
| 1061 |
assert_in!(code, await_argument_code_string("a")); |
| 1062 |
assert_in!(code, await_argument_code_string("b")); |
| 1063 |
assert_not_in!(code, await_argument_code_string("c")); |
| 1064 |
} |
| 1065 |
|
| 1066 |
#[test] |
| 1067 |
fn use_selective_await() { |
| 1068 |
let (item_fn, mut info) = TestCaseBuilder::from(r#"fn test(a: i32, b:i32, c:i32) {}"#) |
| 1069 |
.push_case(TestCase::from_iter(vec!["1", "2", "3"])) |
| 1070 |
.push_case(TestCase::from_iter(vec!["1", "2", "3"])) |
| 1071 |
.take(); |
| 1072 |
info.arguments.set_future(pat("a"), FutureArg::Define); |
| 1073 |
info.arguments.set_future(pat("b"), FutureArg::Await); |
| 1074 |
|
| 1075 |
let tokens = parametrize(item_fn, info); |
| 1076 |
|
| 1077 |
let tests = TestsGroup::from(tokens); |
| 1078 |
|
| 1079 |
let code = tests.requested_test.block.display_code(); |
| 1080 |
|
| 1081 |
assert_not_in!(code, await_argument_code_string("a")); |
| 1082 |
assert_in!(code, await_argument_code_string("b")); |
| 1083 |
assert_not_in!(code, await_argument_code_string("c")); |
| 1084 |
} |
| 1085 |
|
| 1086 |
#[test] |
| 1087 |
fn render_context() { |
| 1088 |
let (item_fn, mut info) = |
| 1089 |
TestCaseBuilder::from(r#"fn test_with_context(ctx: Context, a: u32) {}"#) |
| 1090 |
.push_case(TestCase { |
| 1091 |
args: vec![expr("1")], |
| 1092 |
attrs: Default::default(), |
| 1093 |
description: Some(ident("my_description")), |
| 1094 |
}) |
| 1095 |
.push_case(TestCase { |
| 1096 |
args: vec![expr("2")], |
| 1097 |
attrs: Default::default(), |
| 1098 |
description: Some(ident("other_description")), |
| 1099 |
}) |
| 1100 |
.take(); |
| 1101 |
info.arguments.add_context(pat("ctx")); |
| 1102 |
|
| 1103 |
let tokens = parametrize(item_fn, info); |
| 1104 |
|
| 1105 |
let tests = TestsGroup::from(tokens); |
| 1106 |
|
| 1107 |
fn code(tests: &TestsGroup, id: usize) -> String { |
| 1108 |
tests.module.get_all_tests()[id].block.display_code() |
| 1109 |
} |
| 1110 |
|
| 1111 |
assert_in!( |
| 1112 |
code(&tests, 0), |
| 1113 |
r#"let ctx = Context::new(module_path!(), "test_with_context", Some("my_description"), Some(0usize));"# |
| 1114 |
.ast::<Stmt>() |
| 1115 |
.display_code() |
| 1116 |
); |
| 1117 |
assert_in!( |
| 1118 |
code(&tests, 1), |
| 1119 |
r#"let ctx = Context::new(module_path!(), "test_with_context", Some("other_description"), Some(1usize));"# |
| 1120 |
.ast::<Stmt>() |
| 1121 |
.display_code() |
| 1122 |
); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
mod matrix_cases_should { |
| 1127 |
use rstest_test::{assert_in, assert_not_in}; |
| 1128 |
|
| 1129 |
use crate::parse::{ |
| 1130 |
arguments::{ArgumentsInfo, FutureArg}, |
| 1131 |
rstest::RsTestData, |
| 1132 |
}; |
| 1133 |
|
| 1134 |
/// Should test matrix tests render without take in account MatrixInfo to RsTestInfo |
| 1135 |
/// transformation |
| 1136 |
use super::{assert_eq, *}; |
| 1137 |
|
| 1138 |
fn into_rstest_data(item_fn: &ItemFn) -> RsTestData { |
| 1139 |
RsTestData { |
| 1140 |
items: fn_args_pats(item_fn) |
| 1141 |
.cloned() |
| 1142 |
.map(|it| { |
| 1143 |
ValueList { |
| 1144 |
arg: it, |
| 1145 |
values: vec![], |
| 1146 |
} |
| 1147 |
.into() |
| 1148 |
}) |
| 1149 |
.collect(), |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
#[test] |
| 1154 |
fn create_a_module_named_as_test_function() { |
| 1155 |
let item_fn = "fn should_be_the_module_name(mut fix: String) {}".ast(); |
| 1156 |
let data = into_rstest_data(&item_fn); |
| 1157 |
|
| 1158 |
let tokens = matrix(item_fn.clone(), data.into()); |
| 1159 |
|
| 1160 |
let output = TestsGroup::from(tokens); |
| 1161 |
|
| 1162 |
assert_eq!(output.module.ident, "should_be_the_module_name"); |
| 1163 |
} |
| 1164 |
|
| 1165 |
#[test] |
| 1166 |
fn copy_user_function() { |
| 1167 |
let item_fn = |
| 1168 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#.ast(); |
| 1169 |
let data = into_rstest_data(&item_fn); |
| 1170 |
|
| 1171 |
let tokens = matrix(item_fn.clone(), data.into()); |
| 1172 |
|
| 1173 |
let mut output = TestsGroup::from(tokens); |
| 1174 |
let test_impl: Stmt = output.requested_test.block.stmts.last().cloned().unwrap(); |
| 1175 |
|
| 1176 |
output.requested_test.attrs = vec![]; |
| 1177 |
assert_eq!(output.requested_test.sig, item_fn.sig); |
| 1178 |
assert_eq!(test_impl.display_code(), item_fn.block.display_code()); |
| 1179 |
} |
| 1180 |
|
| 1181 |
#[test] |
| 1182 |
fn not_copy_user_function() { |
| 1183 |
let t_name = "test_name"; |
| 1184 |
let item_fn: ItemFn = format!( |
| 1185 |
"fn {}(fix: String) -> Result<i32, String> {{ Ok(42) }}", |
| 1186 |
t_name |
| 1187 |
) |
| 1188 |
.ast(); |
| 1189 |
let info = RsTestInfo { |
| 1190 |
data: RsTestData { |
| 1191 |
items: vec![values_list("fix", &["1"]).into()].into(), |
| 1192 |
}, |
| 1193 |
..Default::default() |
| 1194 |
}; |
| 1195 |
|
| 1196 |
let tokens = matrix(item_fn, info); |
| 1197 |
|
| 1198 |
let test = &TestsGroup::from(tokens).get_all_tests()[0]; |
| 1199 |
let inner_functions = extract_inner_functions(&test.block); |
| 1200 |
|
| 1201 |
assert_eq!(0, inner_functions.filter(|f| f.sig.ident == t_name).count()); |
| 1202 |
} |
| 1203 |
|
| 1204 |
#[test] |
| 1205 |
fn not_copy_should_panic_attribute() { |
| 1206 |
let item_fn = |
| 1207 |
r#"#[should_panic] fn with_should_panic(mut fix: String) { println!("user code") }"# |
| 1208 |
.ast(); |
| 1209 |
let info = RsTestInfo { |
| 1210 |
data: RsTestData { |
| 1211 |
items: vec![values_list("fix", &["1"]).into()].into(), |
| 1212 |
}, |
| 1213 |
..Default::default() |
| 1214 |
}; |
| 1215 |
|
| 1216 |
let tokens = matrix(item_fn, info); |
| 1217 |
|
| 1218 |
let output = TestsGroup::from(tokens); |
| 1219 |
|
| 1220 |
assert!(!format!("{:?}", output.requested_test.attrs).contains("should_panic")); |
| 1221 |
} |
| 1222 |
|
| 1223 |
#[test] |
| 1224 |
fn should_mark_test_with_given_attributes() { |
| 1225 |
let item_fn: ItemFn = r#"#[should_panic] #[other(value)] fn test(_s: String){}"#.ast(); |
| 1226 |
|
| 1227 |
let info = RsTestInfo { |
| 1228 |
data: RsTestData { |
| 1229 |
items: vec![values_list("fix", &["1"]).into()].into(), |
| 1230 |
}, |
| 1231 |
..Default::default() |
| 1232 |
}; |
| 1233 |
let tokens = matrix(item_fn.clone(), info); |
| 1234 |
|
| 1235 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1236 |
|
| 1237 |
// Sanity check |
| 1238 |
assert!(tests.len() > 0); |
| 1239 |
|
| 1240 |
for t in tests { |
| 1241 |
let end = t.attrs.len() - 2; |
| 1242 |
assert_eq!(item_fn.attrs, &t.attrs[0..end]); |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
#[test] |
| 1247 |
fn add_return_type_if_any() { |
| 1248 |
let item_fn: ItemFn = "fn function(fix: String) -> Result<i32, String> { Ok(42) }".ast(); |
| 1249 |
let info = RsTestInfo { |
| 1250 |
data: RsTestData { |
| 1251 |
items: vec![values_list("fix", &["1", "2", "3"]).into()].into(), |
| 1252 |
}, |
| 1253 |
..Default::default() |
| 1254 |
}; |
| 1255 |
|
| 1256 |
let tokens = matrix(item_fn.clone(), info); |
| 1257 |
|
| 1258 |
let tests = TestsGroup::from(tokens).get_tests(); |
| 1259 |
|
| 1260 |
assert_eq!(tests[0].sig.output, item_fn.sig.output); |
| 1261 |
assert_eq!(tests[1].sig.output, item_fn.sig.output); |
| 1262 |
assert_eq!(tests[2].sig.output, item_fn.sig.output); |
| 1263 |
} |
| 1264 |
|
| 1265 |
#[test] |
| 1266 |
fn mark_user_function_as_test() { |
| 1267 |
let item_fn = |
| 1268 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#.ast(); |
| 1269 |
let data = into_rstest_data(&item_fn); |
| 1270 |
|
| 1271 |
let tokens = matrix(item_fn.clone(), data.into()); |
| 1272 |
|
| 1273 |
let output = TestsGroup::from(tokens); |
| 1274 |
|
| 1275 |
let expected = parse2::<ItemFn>(quote! { |
| 1276 |
#[cfg(test)] |
| 1277 |
fn some() {} |
| 1278 |
}) |
| 1279 |
.unwrap() |
| 1280 |
.attrs; |
| 1281 |
|
| 1282 |
assert_eq!(expected, output.requested_test.attrs); |
| 1283 |
} |
| 1284 |
|
| 1285 |
#[test] |
| 1286 |
fn mark_module_as_test() { |
| 1287 |
let item_fn = |
| 1288 |
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#.ast(); |
| 1289 |
let data = into_rstest_data(&item_fn); |
| 1290 |
|
| 1291 |
let tokens = matrix(item_fn.clone(), data.into()); |
| 1292 |
|
| 1293 |
let output = TestsGroup::from(tokens); |
| 1294 |
|
| 1295 |
let expected = parse2::<ItemMod>(quote! { |
| 1296 |
#[cfg(test)] |
| 1297 |
mod some {} |
| 1298 |
}) |
| 1299 |
.unwrap() |
| 1300 |
.attrs; |
| 1301 |
|
| 1302 |
assert_eq!(expected, output.module.attrs); |
| 1303 |
} |
| 1304 |
|
| 1305 |
#[test] |
| 1306 |
fn with_just_one_arg() { |
| 1307 |
let arg_name = "fix"; |
| 1308 |
let info = RsTestInfo { |
| 1309 |
data: RsTestData { |
| 1310 |
items: vec![values_list(arg_name, &["1", "2", "3"]).into()].into(), |
| 1311 |
}, |
| 1312 |
..Default::default() |
| 1313 |
}; |
| 1314 |
|
| 1315 |
let item_fn = format!(r#"fn test({}: u32) {{ println!("user code") }}"#, arg_name).ast(); |
| 1316 |
|
| 1317 |
let tokens = matrix(item_fn, info); |
| 1318 |
|
| 1319 |
let tests = TestsGroup::from(tokens).get_tests(); |
| 1320 |
|
| 1321 |
assert_eq!(3, tests.len()); |
| 1322 |
assert!(&tests[0].sig.ident.to_string().starts_with("fix_")) |
| 1323 |
} |
| 1324 |
|
| 1325 |
#[rstest] |
| 1326 |
#[case::sync(false)] |
| 1327 |
#[case::async_fn(true)] |
| 1328 |
fn use_injected_test_attribute_to_mark_test_functions_if_any( |
| 1329 |
#[case] is_async: bool, |
| 1330 |
#[values( |
| 1331 |
"#[test]", |
| 1332 |
"#[other::test]", |
| 1333 |
"#[very::complicated::path::test]", |
| 1334 |
"#[prev]#[test]", |
| 1335 |
"#[test]#[after]", |
| 1336 |
"#[prev]#[other::test]" |
| 1337 |
)] |
| 1338 |
attributes: &str, |
| 1339 |
) { |
| 1340 |
let attributes = attrs(attributes); |
| 1341 |
let filter = attrs("#[allow(non_snake_case)]"); |
| 1342 |
let data = RsTestData { |
| 1343 |
items: vec![values_list("v", &["1", "2", "3"]).into()].into(), |
| 1344 |
}; |
| 1345 |
let mut item_fn: ItemFn = r#"fn test(v: u32) {{ println!("user code") }}"#.ast(); |
| 1346 |
item_fn.set_async(is_async); |
| 1347 |
item_fn.attrs = attributes.clone(); |
| 1348 |
let mut info: RsTestInfo = data.into(); |
| 1349 |
info.arguments.set_test_attr(Some(TestAttr::InAttrs)); |
| 1350 |
|
| 1351 |
let tokens = matrix(item_fn, info); |
| 1352 |
|
| 1353 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1354 |
|
| 1355 |
// Sanity check |
| 1356 |
assert!(tests.len() > 0); |
| 1357 |
|
| 1358 |
for test in tests { |
| 1359 |
let filtered: Vec<_> = test |
| 1360 |
.attrs |
| 1361 |
.into_iter() |
| 1362 |
.filter(|a| !filter.contains(a)) |
| 1363 |
.collect(); |
| 1364 |
assert_eq!(attributes, filtered); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
#[rstest] |
| 1369 |
#[case::sync(false, parse_quote! { #[test] })] |
| 1370 |
fn add_default_test_attribute( |
| 1371 |
#[case] is_async: bool, |
| 1372 |
#[case] test_attribute: Attribute, |
| 1373 |
#[values( |
| 1374 |
"", |
| 1375 |
"#[no_one]", |
| 1376 |
"#[should_panic]", |
| 1377 |
"#[should_panic]#[other]", |
| 1378 |
"#[a::b::c]#[should_panic]" |
| 1379 |
)] |
| 1380 |
attributes: &str, |
| 1381 |
) { |
| 1382 |
let attributes = attrs(attributes); |
| 1383 |
let data = RsTestData { |
| 1384 |
items: vec![values_list("v", &["1", "2", "3"]).into()].into(), |
| 1385 |
}; |
| 1386 |
|
| 1387 |
let mut item_fn: ItemFn = r#"fn test(v: u32) {{ println!("user code") }}"#.ast(); |
| 1388 |
item_fn.set_async(is_async); |
| 1389 |
item_fn.attrs = attributes.clone(); |
| 1390 |
|
| 1391 |
let tokens = matrix(item_fn, data.into()); |
| 1392 |
|
| 1393 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1394 |
|
| 1395 |
// Sanity check |
| 1396 |
assert!(tests.len() > 0); |
| 1397 |
|
| 1398 |
for test in tests { |
| 1399 |
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice()); |
| 1400 |
assert_eq!(test.attrs[test.attrs.len() - 1], test_attribute); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
#[test] |
| 1405 |
fn add_future_boilerplate_if_requested() { |
| 1406 |
let item_fn = r#"async fn test(async_ref_u32: &u32, async_u32: u32,simple: u32) { }"#.ast(); |
| 1407 |
|
| 1408 |
let mut arguments = ArgumentsInfo::default(); |
| 1409 |
arguments.add_future(pat("async_ref_u32")); |
| 1410 |
arguments.add_future(pat("async_u32")); |
| 1411 |
|
| 1412 |
let info = RsTestInfo { |
| 1413 |
arguments, |
| 1414 |
..Default::default() |
| 1415 |
}; |
| 1416 |
|
| 1417 |
let tokens = matrix(item_fn, info); |
| 1418 |
|
| 1419 |
let test_function = TestsGroup::from(tokens).requested_test; |
| 1420 |
|
| 1421 |
let expected = parse_str::<syn::ItemFn>( |
| 1422 |
r#"async fn test<'_async_ref_u32>( |
| 1423 |
async_ref_u32: impl ::core::future::Future<Output = &'_async_ref_u32 u32>, |
| 1424 |
async_u32: impl ::core::future::Future<Output = u32>, |
| 1425 |
simple: u32 |
| 1426 |
) |
| 1427 |
{ } |
| 1428 |
"#, |
| 1429 |
) |
| 1430 |
.unwrap(); |
| 1431 |
|
| 1432 |
assert_eq!(test_function.sig, expected.sig); |
| 1433 |
} |
| 1434 |
|
| 1435 |
#[rstest] |
| 1436 |
fn add_allow_non_snake_case( |
| 1437 |
#[values( |
| 1438 |
"", |
| 1439 |
"#[no_one]", |
| 1440 |
"#[should_panic]", |
| 1441 |
"#[should_panic]#[other]", |
| 1442 |
"#[a::b::c]#[should_panic]" |
| 1443 |
)] |
| 1444 |
attributes: &str, |
| 1445 |
) { |
| 1446 |
let attributes = attrs(attributes); |
| 1447 |
let non_snake_case = &attrs("#[allow(non_snake_case)]")[0]; |
| 1448 |
let data = RsTestData { |
| 1449 |
items: vec![values_list("v", &["1", "2", "3"]).into()].into(), |
| 1450 |
}; |
| 1451 |
|
| 1452 |
let mut item_fn: ItemFn = r#"fn test(v: u32) {{ println!("user code") }}"#.ast(); |
| 1453 |
item_fn.attrs = attributes.clone(); |
| 1454 |
|
| 1455 |
let tokens = matrix(item_fn, data.into()); |
| 1456 |
|
| 1457 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1458 |
|
| 1459 |
// Sanity check |
| 1460 |
assert!(tests.len() > 0); |
| 1461 |
|
| 1462 |
for test in tests { |
| 1463 |
assert_eq!(&test.attrs[test.attrs.len() - 2], non_snake_case); |
| 1464 |
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice()); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
#[rstest] |
| 1469 |
#[case::sync(false, false)] |
| 1470 |
#[case::async_fn(true, true)] |
| 1471 |
fn use_await_for_async_test_function(#[case] is_async: bool, #[case] use_await: bool) { |
| 1472 |
let data = RsTestData { |
| 1473 |
items: vec![values_list("v", &["1", "2", "3"]).into()].into(), |
| 1474 |
}; |
| 1475 |
|
| 1476 |
let mut item_fn: ItemFn = r#"fn test(v: u32) {{ println!("user code") }}"#.ast(); |
| 1477 |
item_fn.set_async(is_async); |
| 1478 |
if is_async { |
| 1479 |
item_fn.attrs.push(parse_quote!(#[async_std::test])); |
| 1480 |
} |
| 1481 |
|
| 1482 |
let tokens = matrix(item_fn, data.into()); |
| 1483 |
|
| 1484 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1485 |
|
| 1486 |
// Sanity check |
| 1487 |
assert!(tests.len() > 0); |
| 1488 |
|
| 1489 |
for test in tests { |
| 1490 |
let last_stmt = test.block.stmts.last().unwrap(); |
| 1491 |
assert_eq!(use_await, last_stmt.is_await()); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
#[test] |
| 1496 |
fn trace_arguments_value() { |
| 1497 |
let data = RsTestData { |
| 1498 |
items: vec![ |
| 1499 |
values_list("a_trace_me", &["1", "2"]).into(), |
| 1500 |
values_list("b_trace_me", &["3", "4"]).into(), |
| 1501 |
] |
| 1502 |
.into(), |
| 1503 |
}; |
| 1504 |
let item_fn: ItemFn = r#"#[trace] fn test(a_trace_me: u32, b_trace_me: u32) {}"#.ast(); |
| 1505 |
|
| 1506 |
let tokens = matrix(item_fn, data.into()); |
| 1507 |
|
| 1508 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1509 |
|
| 1510 |
assert!(tests.len() > 0); |
| 1511 |
for test in tests { |
| 1512 |
for name in &["a_trace_me", "b_trace_me"] { |
| 1513 |
assert_in!(test.block.display_code(), trace_argument_code_string(name)); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
#[test] |
| 1519 |
fn trace_just_some_arguments_value() { |
| 1520 |
let data = RsTestData { |
| 1521 |
items: vec![ |
| 1522 |
values_list("a_trace_me", &["1", "2"]).into(), |
| 1523 |
values_list("b_no_trace_me", &["3", "4"]).into(), |
| 1524 |
values_list("c_no_trace_me", &["5", "6"]).into(), |
| 1525 |
values_list("d_trace_me", &["7", "8"]).into(), |
| 1526 |
] |
| 1527 |
.into(), |
| 1528 |
}; |
| 1529 |
let mut attributes: RsTestAttributes = Default::default(); |
| 1530 |
attributes.add_notraces(vec![pat("b_no_trace_me"), pat("c_no_trace_me")]); |
| 1531 |
let item_fn: ItemFn = r#"#[trace] fn test(a_trace_me: u32, b_no_trace_me: u32, c_no_trace_me: u32, d_trace_me: u32) {}"#.ast(); |
| 1532 |
|
| 1533 |
let tokens = matrix( |
| 1534 |
item_fn, |
| 1535 |
RsTestInfo { |
| 1536 |
data, |
| 1537 |
attributes, |
| 1538 |
..Default::default() |
| 1539 |
}, |
| 1540 |
); |
| 1541 |
|
| 1542 |
let tests = TestsGroup::from(tokens).get_all_tests(); |
| 1543 |
|
| 1544 |
assert!(tests.len() > 0); |
| 1545 |
for test in tests { |
| 1546 |
for should_be_present in &["a_trace_me", "d_trace_me"] { |
| 1547 |
assert_in!( |
| 1548 |
test.block.display_code(), |
| 1549 |
trace_argument_code_string(should_be_present) |
| 1550 |
); |
| 1551 |
} |
| 1552 |
for should_not_be_present in &["b_no_trace_me", "c_no_trace_me"] { |
| 1553 |
assert_not_in!( |
| 1554 |
test.block.display_code(), |
| 1555 |
trace_argument_code_string(should_not_be_present) |
| 1556 |
); |
| 1557 |
} |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
#[test] |
| 1562 |
fn use_global_await() { |
| 1563 |
let item_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {}"#.ast(); |
| 1564 |
let data = RsTestData { |
| 1565 |
items: vec![ |
| 1566 |
values_list("a", &["1"]).into(), |
| 1567 |
values_list("b", &["2"]).into(), |
| 1568 |
values_list("c", &["3"]).into(), |
| 1569 |
] |
| 1570 |
.into(), |
| 1571 |
}; |
| 1572 |
let mut info = RsTestInfo { |
| 1573 |
data, |
| 1574 |
attributes: Default::default(), |
| 1575 |
arguments: Default::default(), |
| 1576 |
}; |
| 1577 |
info.arguments.set_global_await(true); |
| 1578 |
info.arguments.add_future(pat("a")); |
| 1579 |
info.arguments.add_future(pat("b")); |
| 1580 |
|
| 1581 |
let tokens = matrix(item_fn, info); |
| 1582 |
|
| 1583 |
let tests = TestsGroup::from(tokens); |
| 1584 |
|
| 1585 |
let code = tests.requested_test.block.display_code(); |
| 1586 |
|
| 1587 |
assert_in!(code, await_argument_code_string("a")); |
| 1588 |
assert_in!(code, await_argument_code_string("b")); |
| 1589 |
assert_not_in!(code, await_argument_code_string("c")); |
| 1590 |
} |
| 1591 |
|
| 1592 |
#[test] |
| 1593 |
fn use_selective_await() { |
| 1594 |
let item_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {}"#.ast(); |
| 1595 |
let data = RsTestData { |
| 1596 |
items: vec![ |
| 1597 |
values_list("a", &["1"]).into(), |
| 1598 |
values_list("b", &["2"]).into(), |
| 1599 |
values_list("c", &["3"]).into(), |
| 1600 |
] |
| 1601 |
.into(), |
| 1602 |
}; |
| 1603 |
let mut info = RsTestInfo { |
| 1604 |
data, |
| 1605 |
attributes: Default::default(), |
| 1606 |
arguments: Default::default(), |
| 1607 |
}; |
| 1608 |
|
| 1609 |
info.arguments.set_future(pat("a"), FutureArg::Define); |
| 1610 |
info.arguments.set_future(pat("b"), FutureArg::Await); |
| 1611 |
|
| 1612 |
let tokens = matrix(item_fn, info); |
| 1613 |
|
| 1614 |
let tests = TestsGroup::from(tokens); |
| 1615 |
|
| 1616 |
let code = tests.requested_test.block.display_code(); |
| 1617 |
|
| 1618 |
assert_not_in!(code, await_argument_code_string("a")); |
| 1619 |
assert_in!(code, await_argument_code_string("b")); |
| 1620 |
assert_not_in!(code, await_argument_code_string("c")); |
| 1621 |
} |
| 1622 |
|
| 1623 |
mod two_args_should { |
| 1624 |
/// Should test matrix tests render without take in account MatrixInfo to RsTestInfo |
| 1625 |
/// transformation |
| 1626 |
use super::{assert_eq, *}; |
| 1627 |
|
| 1628 |
fn fixture<'a>() -> (Vec<&'a str>, ItemFn, RsTestInfo) { |
| 1629 |
let names = vec!["first", "second"]; |
| 1630 |
( |
| 1631 |
names.clone(), |
| 1632 |
format!( |
| 1633 |
r#"fn test({}: u32, {}: u32) {{ println!("user code") }}"#, |
| 1634 |
names[0], names[1] |
| 1635 |
) |
| 1636 |
.ast(), |
| 1637 |
RsTestInfo { |
| 1638 |
data: RsTestData { |
| 1639 |
items: vec![ |
| 1640 |
values_list(names[0], &["1", "2", "3"]).into(), |
| 1641 |
values_list(names[1], &["1", "2"]).into(), |
| 1642 |
], |
| 1643 |
}, |
| 1644 |
..Default::default() |
| 1645 |
}, |
| 1646 |
) |
| 1647 |
} |
| 1648 |
|
| 1649 |
#[test] |
| 1650 |
fn contain_a_module_for_each_first_arg() { |
| 1651 |
let (names, item_fn, info) = fixture(); |
| 1652 |
|
| 1653 |
let tokens = matrix(item_fn, info); |
| 1654 |
|
| 1655 |
let modules = TestsGroup::from(tokens).module.get_modules().names(); |
| 1656 |
|
| 1657 |
let expected = (1..=3) |
| 1658 |
.map(|i| format!("{}_{}", names[0], i)) |
| 1659 |
.collect::<Vec<_>>(); |
| 1660 |
|
| 1661 |
assert_eq!(expected.len(), modules.len()); |
| 1662 |
for (e, m) in expected.into_iter().zip(modules.into_iter()) { |
| 1663 |
assert_in!(m, e); |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
#[test] |
| 1668 |
fn annotate_modules_with_allow_non_snake_name() { |
| 1669 |
let (_, item_fn, info) = fixture(); |
| 1670 |
let non_snake_case = &attrs("#[allow(non_snake_case)]")[0]; |
| 1671 |
|
| 1672 |
let tokens = matrix(item_fn, info); |
| 1673 |
|
| 1674 |
let modules = TestsGroup::from(tokens).module.get_modules(); |
| 1675 |
|
| 1676 |
for module in modules { |
| 1677 |
assert!(module.attrs.contains(&non_snake_case)); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
#[test] |
| 1682 |
fn create_all_tests() { |
| 1683 |
let (_, item_fn, info) = fixture(); |
| 1684 |
|
| 1685 |
let tokens = matrix(item_fn, info); |
| 1686 |
|
| 1687 |
let tests = TestsGroup::from(tokens).module.get_all_tests().names(); |
| 1688 |
|
| 1689 |
assert_eq!(6, tests.len()); |
| 1690 |
} |
| 1691 |
|
| 1692 |
#[test] |
| 1693 |
fn create_all_modules_with_the_same_functions() { |
| 1694 |
let (_, item_fn, info) = fixture(); |
| 1695 |
|
| 1696 |
let tokens = matrix(item_fn, info); |
| 1697 |
|
| 1698 |
let tests = TestsGroup::from(tokens) |
| 1699 |
.module |
| 1700 |
.get_modules() |
| 1701 |
.into_iter() |
| 1702 |
.map(|m| m.get_tests().names()) |
| 1703 |
.collect::<Vec<_>>(); |
| 1704 |
|
| 1705 |
assert_eq!(tests[0], tests[1]); |
| 1706 |
assert_eq!(tests[1], tests[2]); |
| 1707 |
} |
| 1708 |
|
| 1709 |
#[test] |
| 1710 |
fn test_name_should_contain_argument_name() { |
| 1711 |
let (names, item_fn, info) = fixture(); |
| 1712 |
|
| 1713 |
let tokens = matrix(item_fn, info); |
| 1714 |
|
| 1715 |
let tests = TestsGroup::from(tokens).module.get_modules()[0] |
| 1716 |
.get_tests() |
| 1717 |
.names(); |
| 1718 |
|
| 1719 |
let expected = (1..=2) |
| 1720 |
.map(|i| format!("{}_{}", names[1], i)) |
| 1721 |
.collect::<Vec<_>>(); |
| 1722 |
|
| 1723 |
assert_eq!(expected.len(), tests.len()); |
| 1724 |
for (e, m) in expected.into_iter().zip(tests.into_iter()) { |
| 1725 |
assert_in!(m, e); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
#[test] |
| 1731 |
fn three_args_should_create_all_function_4_mods_at_the_first_level_and_3_at_the_second() { |
| 1732 |
let (first, second, third) = ("first", "second", "third"); |
| 1733 |
let info = RsTestInfo { |
| 1734 |
data: RsTestData { |
| 1735 |
items: vec![ |
| 1736 |
values_list(first, &["1", "2", "3", "4"]).into(), |
| 1737 |
values_list(second, &["1", "2", "3"]).into(), |
| 1738 |
values_list(third, &["1", "2"]).into(), |
| 1739 |
], |
| 1740 |
}, |
| 1741 |
..Default::default() |
| 1742 |
}; |
| 1743 |
let item_fn = format!( |
| 1744 |
r#"fn test({}: u32, {}: u32, {}: u32) {{ println!("user code") }}"#, |
| 1745 |
first, second, third |
| 1746 |
) |
| 1747 |
.ast(); |
| 1748 |
|
| 1749 |
let tokens = matrix(item_fn, info); |
| 1750 |
|
| 1751 |
let tg = TestsGroup::from(tokens); |
| 1752 |
|
| 1753 |
assert_eq!(24, tg.module.get_all_tests().len()); |
| 1754 |
assert_eq!(4, tg.module.get_modules().len()); |
| 1755 |
assert_eq!(3, tg.module.get_modules()[0].get_modules().len()); |
| 1756 |
assert_eq!(3, tg.module.get_modules()[3].get_modules().len()); |
| 1757 |
assert_eq!( |
| 1758 |
2, |
| 1759 |
tg.module.get_modules()[0].get_modules()[0] |
| 1760 |
.get_tests() |
| 1761 |
.len() |
| 1762 |
); |
| 1763 |
assert_eq!( |
| 1764 |
2, |
| 1765 |
tg.module.get_modules()[3].get_modules()[1] |
| 1766 |
.get_tests() |
| 1767 |
.len() |
| 1768 |
); |
| 1769 |
} |
| 1770 |
|
| 1771 |
#[test] |
| 1772 |
fn pad_case_index() { |
| 1773 |
let item_fn: ItemFn = |
| 1774 |
r#"fn test(first: u32, second: u32, third: u32) { println!("user code") }"#.ast(); |
| 1775 |
let values = (1..=100).map(|i| i.to_string()).collect::<Vec<_>>(); |
| 1776 |
let info = RsTestInfo { |
| 1777 |
data: RsTestData { |
| 1778 |
items: vec![ |
| 1779 |
values_list("first", values.as_ref()).into(), |
| 1780 |
values_list("second", values[..10].as_ref()).into(), |
| 1781 |
values_list("third", values[..2].as_ref()).into(), |
| 1782 |
], |
| 1783 |
}, |
| 1784 |
..Default::default() |
| 1785 |
}; |
| 1786 |
|
| 1787 |
let tokens = matrix(item_fn.clone(), info); |
| 1788 |
|
| 1789 |
let tg = TestsGroup::from(tokens); |
| 1790 |
|
| 1791 |
let mods = tg.get_modules().names(); |
| 1792 |
|
| 1793 |
assert_in!(mods[0], "first_001"); |
| 1794 |
assert_in!(mods[99], "first_100"); |
| 1795 |
|
| 1796 |
let mods = tg.get_modules()[0].get_modules().names(); |
| 1797 |
|
| 1798 |
assert_in!(mods[0], "second_01"); |
| 1799 |
assert_in!(mods[9], "second_10"); |
| 1800 |
|
| 1801 |
let functions = tg.get_modules()[0].get_modules()[1].get_tests().names(); |
| 1802 |
|
| 1803 |
assert_in!(functions[0], "third_1"); |
| 1804 |
assert_in!(functions[1], "third_2"); |
| 1805 |
} |
| 1806 |
|
| 1807 |
#[test] |
| 1808 |
fn render_context() { |
| 1809 |
let item_fn: ItemFn = |
| 1810 |
r#"fn matrix_with_context(ctx: Context, first: u32, second: u32) { println!("user code") }"#.ast(); |
| 1811 |
let values = vec!["1".to_string(), "2".to_string()]; |
| 1812 |
let mut info = RsTestInfo { |
| 1813 |
data: RsTestData { |
| 1814 |
items: vec![ |
| 1815 |
values_list("first", values.as_ref()).into(), |
| 1816 |
values_list("second", values.as_ref()).into(), |
| 1817 |
], |
| 1818 |
}, |
| 1819 |
..Default::default() |
| 1820 |
}; |
| 1821 |
info.arguments.add_context(pat("ctx")); |
| 1822 |
|
| 1823 |
let tokens = matrix(item_fn.clone(), info); |
| 1824 |
|
| 1825 |
let tests = TestsGroup::from(tokens); |
| 1826 |
|
| 1827 |
fn code(tests: &TestsGroup, id: usize) -> String { |
| 1828 |
tests.module.get_all_tests()[id].block.display_code() |
| 1829 |
} |
| 1830 |
|
| 1831 |
for t in 0..tests.module.get_all_tests().len() { |
| 1832 |
assert_in!( |
| 1833 |
code(&tests, t), |
| 1834 |
r#"let ctx = Context::new(module_path!(), "matrix_with_context", None, None);"# |
| 1835 |
.ast::<Stmt>() |
| 1836 |
.display_code() |
| 1837 |
); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
mod complete_should { |
| 1843 |
use crate::parse::rstest::RsTestData; |
| 1844 |
|
| 1845 |
use super::{assert_eq, *}; |
| 1846 |
|
| 1847 |
fn rendered_case(fn_name: &str) -> TestsGroup { |
| 1848 |
let item_fn: ItemFn = format!( |
| 1849 |
r#" #[first] |
| 1850 |
#[second(arg)] |
| 1851 |
fn {}( |
| 1852 |
fix: u32, |
| 1853 |
a: f64, b: f32, |
| 1854 |
x: i32, y: i32) {{}}"#, |
| 1855 |
fn_name |
| 1856 |
) |
| 1857 |
.ast(); |
| 1858 |
let data = RsTestData { |
| 1859 |
items: vec![ |
| 1860 |
fixture("fix", &["2"]).into(), |
| 1861 |
ident("a").into(), |
| 1862 |
ident("b").into(), |
| 1863 |
vec!["1f64", "2f32"] |
| 1864 |
.into_iter() |
| 1865 |
.collect::<TestCase>() |
| 1866 |
.into(), |
| 1867 |
TestCase { |
| 1868 |
description: Some(ident("description")), |
| 1869 |
..vec!["3f64", "4f32"].into_iter().collect::<TestCase>() |
| 1870 |
} |
| 1871 |
.with_attrs(attrs("#[third]#[forth(other)]")) |
| 1872 |
.into(), |
| 1873 |
values_list("x", &["12", "-2"]).into(), |
| 1874 |
values_list("y", &["-3", "42"]).into(), |
| 1875 |
], |
| 1876 |
}; |
| 1877 |
|
| 1878 |
matrix(item_fn.clone(), data.into()).into() |
| 1879 |
} |
| 1880 |
|
| 1881 |
fn test_case() -> TestsGroup { |
| 1882 |
rendered_case("test_function") |
| 1883 |
} |
| 1884 |
|
| 1885 |
#[test] |
| 1886 |
fn use_function_name_as_outer_module() { |
| 1887 |
let rendered = rendered_case("should_be_the_outer_module_name"); |
| 1888 |
|
| 1889 |
assert_eq!(rendered.module.ident, "should_be_the_outer_module_name") |
| 1890 |
} |
| 1891 |
|
| 1892 |
#[test] |
| 1893 |
fn have_one_module_for_each_parametrized_case() { |
| 1894 |
let rendered = test_case(); |
| 1895 |
|
| 1896 |
assert_eq!( |
| 1897 |
vec!["case_1", "case_2_description"], |
| 1898 |
rendered |
| 1899 |
.get_modules() |
| 1900 |
.iter() |
| 1901 |
.map(|m| m.ident.to_string()) |
| 1902 |
.collect::<Vec<_>>() |
| 1903 |
); |
| 1904 |
} |
| 1905 |
|
| 1906 |
#[test] |
| 1907 |
fn implement_exactly_8_tests() { |
| 1908 |
let rendered = test_case(); |
| 1909 |
|
| 1910 |
assert_eq!(8, rendered.get_all_tests().len()); |
| 1911 |
} |
| 1912 |
|
| 1913 |
#[test] |
| 1914 |
fn implement_exactly_4_tests_in_each_module() { |
| 1915 |
let modules = test_case().module.get_modules(); |
| 1916 |
|
| 1917 |
assert_eq!(4, modules[0].get_all_tests().len()); |
| 1918 |
assert_eq!(4, modules[1].get_all_tests().len()); |
| 1919 |
} |
| 1920 |
|
| 1921 |
#[test] |
| 1922 |
fn assign_same_case_value_for_each_test() { |
| 1923 |
let modules = test_case().module.get_modules(); |
| 1924 |
|
| 1925 |
for f in modules[0].get_all_tests() { |
| 1926 |
let assignments = Assignments::collect_assignments(&f); |
| 1927 |
assert_eq!(assignments.0["a"], expr("1f64")); |
| 1928 |
assert_eq!(assignments.0["b"], expr("2f32")); |
| 1929 |
} |
| 1930 |
|
| 1931 |
for f in modules[1].get_all_tests() { |
| 1932 |
let assignments = Assignments::collect_assignments(&f); |
| 1933 |
assert_eq!(assignments.0["a"], expr("3f64")); |
| 1934 |
assert_eq!(assignments.0["b"], expr("4f32")); |
| 1935 |
} |
| 1936 |
} |
| 1937 |
|
| 1938 |
#[test] |
| 1939 |
fn assign_all_case_combination_in_tests() { |
| 1940 |
let modules = test_case().module.get_modules(); |
| 1941 |
|
| 1942 |
let cases = vec![("12", "-3"), ("12", "42"), ("-2", "-3"), ("-2", "42")]; |
| 1943 |
for module in modules { |
| 1944 |
for ((x, y), f) in cases.iter().zip(module.get_all_tests().iter()) { |
| 1945 |
let assignments = Assignments::collect_assignments(f); |
| 1946 |
assert_eq!(assignments.0["x"], expr(x)); |
| 1947 |
assert_eq!(assignments.0["y"], expr(y)); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
#[test] |
| 1953 |
fn mark_test_with_given_attributes() { |
| 1954 |
let modules = test_case().module.get_modules(); |
| 1955 |
let attrs = attrs("#[first]#[second(arg)]"); |
| 1956 |
|
| 1957 |
for f in modules[0].get_all_tests() { |
| 1958 |
let end = f.attrs.len() - 2; |
| 1959 |
assert_eq!(attrs, &f.attrs[..end]); |
| 1960 |
} |
| 1961 |
for f in modules[1].get_all_tests() { |
| 1962 |
assert_eq!(attrs, &f.attrs[..2]); |
| 1963 |
} |
| 1964 |
} |
| 1965 |
#[test] |
| 1966 |
fn should_add_attributes_given_in_the_test_case() { |
| 1967 |
let modules = test_case().module.get_modules(); |
| 1968 |
let attrs = attrs("#[third]#[forth(other)]"); |
| 1969 |
|
| 1970 |
for f in modules[1].get_all_tests() { |
| 1971 |
assert_eq!(attrs, &f.attrs[2..4]); |
| 1972 |
} |
| 1973 |
} |
| 1974 |
} |
| 1975 |
|
| 1976 |
mod test_attribute_should { |
| 1977 |
use super::*; |
| 1978 |
use crate::test::assert_eq; |
| 1979 |
|
| 1980 |
enum TestAttrStyle { |
| 1981 |
Explicit, |
| 1982 |
Implicit, |
| 1983 |
Omitted, |
| 1984 |
} |
| 1985 |
|
| 1986 |
fn make_test_fn(is_async: bool, attr_style: TestAttrStyle) -> (ItemFn, RsTestInfo) { |
| 1987 |
use std::fmt::Write as _; |
| 1988 |
let mut info = RsTestInfo::default(); |
| 1989 |
|
| 1990 |
let mut out = String::from("#[rstest]\n"); |
| 1991 |
match attr_style { |
| 1992 |
TestAttrStyle::Explicit => { |
| 1993 |
info.arguments.set_test_attr(Some(TestAttr::Explicit( |
| 1994 |
attr("#[my_explicit_test_attr]").into(), |
| 1995 |
))); |
| 1996 |
} |
| 1997 |
TestAttrStyle::Implicit => { |
| 1998 |
info.arguments.set_test_attr(Some(TestAttr::InAttrs)); |
| 1999 |
writeln!(&mut out, "#[implicit::test::runner::test]").unwrap(); |
| 2000 |
} |
| 2001 |
TestAttrStyle::Omitted => { |
| 2002 |
info.arguments.set_test_attr(None); |
| 2003 |
} |
| 2004 |
}; |
| 2005 |
if is_async { |
| 2006 |
out.push_str("async "); |
| 2007 |
} |
| 2008 |
out.push_str("fn test_attr_test() {}"); |
| 2009 |
|
| 2010 |
(out.ast(), info) |
| 2011 |
} |
| 2012 |
|
| 2013 |
/// Check if itemfns are equivalent: |
| 2014 |
/// |
| 2015 |
/// - attributes don't have to match |
| 2016 |
/// - generic tokens are not checked, but generic params must match |
| 2017 |
/// - blocks are not checked (they have been munged) |
| 2018 |
fn itemfn_near_match(expect: &ItemFn, result: &ItemFn) { |
| 2019 |
assert_eq!(expect.vis, result.vis); |
| 2020 |
assert_eq!(expect.sig.constness, result.sig.constness); |
| 2021 |
assert_eq!(expect.sig.asyncness, result.sig.asyncness); |
| 2022 |
assert_eq!(expect.sig.unsafety, result.sig.unsafety); |
| 2023 |
assert_eq!(expect.sig.abi, result.sig.abi); |
| 2024 |
assert_eq!(expect.sig.fn_token, result.sig.fn_token); |
| 2025 |
assert_eq!(expect.sig.ident, result.sig.ident); |
| 2026 |
assert_eq!(expect.sig.generics.params, result.sig.generics.params); |
| 2027 |
assert_eq!( |
| 2028 |
expect.sig.generics.where_clause, |
| 2029 |
result.sig.generics.where_clause |
| 2030 |
); |
| 2031 |
assert_eq!(expect.sig.paren_token, result.sig.paren_token); |
| 2032 |
assert_eq!(expect.sig.inputs, result.sig.inputs); |
| 2033 |
assert_eq!(expect.sig.variadic, result.sig.variadic); |
| 2034 |
assert_eq!(expect.sig.output, result.sig.output); |
| 2035 |
} |
| 2036 |
|
| 2037 |
#[test] |
| 2038 |
fn use_explicit_test_attr_when_sync() { |
| 2039 |
let (input, info) = make_test_fn(false, TestAttrStyle::Explicit); |
| 2040 |
let result = single(input.clone(), info).ast::<ItemFn>(); |
| 2041 |
|
| 2042 |
assert!(result |
| 2043 |
.attrs |
| 2044 |
.iter() |
| 2045 |
.any(|attr| attr_is(attr, "my_explicit_test_attr"))); |
| 2046 |
itemfn_near_match(&input, &result); |
| 2047 |
} |
| 2048 |
|
| 2049 |
#[test] |
| 2050 |
fn use_implicit_test_attr_when_sync() { |
| 2051 |
let (input, info) = make_test_fn(false, TestAttrStyle::Implicit); |
| 2052 |
let result = single(input.clone(), info).ast::<ItemFn>(); |
| 2053 |
|
| 2054 |
assert_eq!( |
| 2055 |
result |
| 2056 |
.attrs |
| 2057 |
.iter() |
| 2058 |
.find(|attr| attr_starts_with(attr, &parse_quote!(implicit))), |
| 2059 |
Some(&parse_quote!(#[implicit::test::runner::test])) |
| 2060 |
); |
| 2061 |
itemfn_near_match(&input, &result); |
| 2062 |
} |
| 2063 |
|
| 2064 |
#[test] |
| 2065 |
fn use_explicit_test_attr_when_async() { |
| 2066 |
let (input, info) = make_test_fn(true, TestAttrStyle::Explicit); |
| 2067 |
let result = single(input.clone(), info).ast::<ItemFn>(); |
| 2068 |
|
| 2069 |
assert!(result |
| 2070 |
.attrs |
| 2071 |
.iter() |
| 2072 |
.any(|attr| attr_is(attr, "my_explicit_test_attr"))); |
| 2073 |
itemfn_near_match(&input, &result); |
| 2074 |
} |
| 2075 |
|
| 2076 |
#[test] |
| 2077 |
fn use_implicit_test_attr_when_async() { |
| 2078 |
let (input, info) = make_test_fn(true, TestAttrStyle::Implicit); |
| 2079 |
let result = single(input.clone(), info).ast::<ItemFn>(); |
| 2080 |
|
| 2081 |
assert_eq!( |
| 2082 |
result |
| 2083 |
.attrs |
| 2084 |
.iter() |
| 2085 |
.find(|attr| attr_starts_with(attr, &parse_quote!(implicit))), |
| 2086 |
Some(&parse_quote!(#[implicit::test::runner::test])) |
| 2087 |
); |
| 2088 |
itemfn_near_match(&input, &result); |
| 2089 |
} |
| 2090 |
|
| 2091 |
#[test] |
| 2092 |
fn fallback_for_synchronous_test_when_missing() { |
| 2093 |
let (input, info) = make_test_fn(false, TestAttrStyle::Omitted); |
| 2094 |
let result = single(input.clone(), info).ast::<ItemFn>(); |
| 2095 |
|
| 2096 |
assert!(result.attrs.iter().any(|attr| attr_is(attr, "test"))); |
| 2097 |
itemfn_near_match(&input, &result); |
| 2098 |
} |
| 2099 |
} |