| 1 |
use std::path::Path; |
| 2 |
pub use unindent::Unindent; |
| 3 |
|
| 4 |
use super::resources; |
| 5 |
use mytest::*; |
| 6 |
use rstest_test::{assert_in, assert_not_in, Project, Stringable, TestResults}; |
| 7 |
|
| 8 |
const SRC_LIB_RS: &str = { |
| 9 |
const SEP: u8 = std::path::MAIN_SEPARATOR as u8; |
| 10 |
const SRC_LIB_RS: [u8; 11] = [ |
| 11 |
SEP, b's', b'r', b'c', SEP, b'l', b'i', b'b', b'.', b'r', b's', |
| 12 |
]; |
| 13 |
unsafe { std::str::from_utf8_unchecked(&SRC_LIB_RS) } |
| 14 |
}; |
| 15 |
|
| 16 |
fn prj(res: &str) -> Project { |
| 17 |
let path = Path::new("fixture").join(res); |
| 18 |
crate::prj().set_code_file(resources(path)) |
| 19 |
} |
| 20 |
|
| 21 |
fn run_test(res: &str) -> (std::process::Output, String) { |
| 22 |
let prj = prj(res); |
| 23 |
( |
| 24 |
prj.run_tests().unwrap(), |
| 25 |
prj.get_name().to_owned().to_string(), |
| 26 |
) |
| 27 |
} |
| 28 |
|
| 29 |
mod should { |
| 30 |
use rstest_test::{assert_regex, CountMessageOccurrence}; |
| 31 |
|
| 32 |
use super::*; |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn use_input_fixtures() { |
| 36 |
let (output, _) = run_test("simple_injection.rs"); |
| 37 |
|
| 38 |
TestResults::new().ok("success").fail("fail").assert(output); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn create_a_struct_that_return_the_fixture() { |
| 43 |
let (output, _) = run_test("fixture_struct.rs"); |
| 44 |
|
| 45 |
TestResults::new() |
| 46 |
.ok("resolve_new") |
| 47 |
.ok("resolve_default") |
| 48 |
.ok("injected_new") |
| 49 |
.ok("injected_default") |
| 50 |
.assert(output); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn be_accessible_from_other_module() { |
| 55 |
let (output, _) = run_test("from_other_module.rs"); |
| 56 |
|
| 57 |
TestResults::new().ok("struct_access").assert(output); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn not_show_any_warning() { |
| 62 |
let (output, _) = run_test("no_warning.rs"); |
| 63 |
|
| 64 |
assert_not_in!(output.stderr.str(), "warning:"); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn rename() { |
| 69 |
let (output, _) = run_test("rename.rs"); |
| 70 |
|
| 71 |
TestResults::new().ok("test").assert(output); |
| 72 |
} |
| 73 |
|
| 74 |
mod accept_and_return { |
| 75 |
use super::*; |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn impl_traits() { |
| 79 |
let (output, _) = run_test("impl.rs"); |
| 80 |
|
| 81 |
TestResults::new() |
| 82 |
.ok("base_impl_return") |
| 83 |
.ok("nested_impl_return") |
| 84 |
.ok("nested_multiple_impl_return") |
| 85 |
.ok("base_impl_input") |
| 86 |
.ok("nested_impl_input") |
| 87 |
.ok("nested_multiple_impl_input") |
| 88 |
.assert(output); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn dyn_traits() { |
| 93 |
let (output, _) = run_test("dyn.rs"); |
| 94 |
|
| 95 |
TestResults::new() |
| 96 |
.ok("test_dyn_box") |
| 97 |
.ok("test_dyn_ref") |
| 98 |
.ok("test_dyn_box_resolve") |
| 99 |
.ok("test_dyn_ref_resolve") |
| 100 |
.assert(output); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[rstest] |
| 105 |
#[case::base("async_fixture.rs")] |
| 106 |
#[case::use_global("await_complete_fixture.rs")] |
| 107 |
#[case::use_selective("await_partial_fixture.rs")] |
| 108 |
fn resolve_async_fixture(#[case] code: &str) { |
| 109 |
let prj = prj(code); |
| 110 |
prj.add_dependency("async-std", r#"{version="*", features=["attributes"]}"#); |
| 111 |
|
| 112 |
let output = prj.run_tests().unwrap(); |
| 113 |
|
| 114 |
TestResults::new() |
| 115 |
.ok("default_is_async") |
| 116 |
.ok("use_async_fixture") |
| 117 |
.ok("use_async_impl_output") |
| 118 |
.ok("use_async_nest_fixture_default") |
| 119 |
.ok("use_async_nest_fixture_injected") |
| 120 |
.ok("use_async_nest_fixture_with_default") |
| 121 |
.ok("use_two_args_mix_fixture") |
| 122 |
.ok("use_two_args_mix_fixture_inject_first") |
| 123 |
.ok("use_two_args_mix_fixture_inject_both") |
| 124 |
.assert(output); |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn resolve_fixture_generics_by_fixture_input() { |
| 129 |
let (output, _) = run_test("resolve.rs"); |
| 130 |
|
| 131 |
TestResults::new() |
| 132 |
.ok("test_u32") |
| 133 |
.ok("test_i32") |
| 134 |
.assert(output); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn use_defined_return_type_if_any() { |
| 139 |
let (output, _) = run_test("defined_return_type.rs"); |
| 140 |
|
| 141 |
TestResults::new() |
| 142 |
.ok("resolve") |
| 143 |
.ok("resolve_partial") |
| 144 |
.ok("resolve_attrs") |
| 145 |
.ok("resolve_partial_attrs") |
| 146 |
.assert(output); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn clean_up_default_from_unused_generics() { |
| 151 |
let (output, _) = run_test("clean_up_default_generics.rs"); |
| 152 |
|
| 153 |
TestResults::new() |
| 154 |
.ok("resolve") |
| 155 |
.ok("resolve_partial") |
| 156 |
.assert(output); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn apply_partial_fixture() { |
| 161 |
let (output, _) = run_test("partial.rs"); |
| 162 |
|
| 163 |
TestResults::new() |
| 164 |
.ok("default") |
| 165 |
.ok("t_partial_1") |
| 166 |
.ok("t_partial_2") |
| 167 |
.ok("t_complete") |
| 168 |
.assert(output); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn apply_partial_fixture_from_value_attribute() { |
| 173 |
let (output, _) = run_test("partial_in_attr.rs"); |
| 174 |
|
| 175 |
TestResults::new() |
| 176 |
.ok("default") |
| 177 |
.ok("t_partial_1") |
| 178 |
.ok("t_partial_2") |
| 179 |
.ok("t_complete") |
| 180 |
.assert(output); |
| 181 |
} |
| 182 |
|
| 183 |
#[rstest] |
| 184 |
#[case::compact_form("default.rs")] |
| 185 |
#[case::attrs_form("default_in_attrs.rs")] |
| 186 |
fn use_input_values_if_any(#[case] file: &str) { |
| 187 |
let (output, _) = run_test(file); |
| 188 |
|
| 189 |
TestResults::new() |
| 190 |
.ok("test_simple") |
| 191 |
.ok("test_simple_changed") |
| 192 |
.ok("test_double") |
| 193 |
.ok("test_double_changed") |
| 194 |
.ok("test_mixed") |
| 195 |
.assert(output); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn convert_literal_string_for_default_values() { |
| 200 |
let (output, _) = run_test("default_conversion.rs"); |
| 201 |
|
| 202 |
assert_regex!( |
| 203 |
"Cannot parse 'error' to get [a-z:_0-9]*MyType", |
| 204 |
output.stdout.str() |
| 205 |
); |
| 206 |
|
| 207 |
TestResults::new() |
| 208 |
.ok("test_base") |
| 209 |
.ok("test_byte_array") |
| 210 |
.ok("test_convert_custom") |
| 211 |
.fail("test_fail_conversion") |
| 212 |
.assert(output); |
| 213 |
} |
| 214 |
|
| 215 |
#[rstest] |
| 216 |
#[case("once.rs")] |
| 217 |
#[case::no_return("once_no_return.rs")] |
| 218 |
#[case::defined_type("once_defined_type.rs")] |
| 219 |
fn accept_once_attribute_and_call_fixture_just_once(#[case] fname: &str) { |
| 220 |
let project = prj(fname).with_nocapture(); |
| 221 |
|
| 222 |
let output = project.run_tests().unwrap(); |
| 223 |
|
| 224 |
// Just to see the errors if fixture doesn't compile |
| 225 |
assert_in!(output.stderr.str(), "Exec fixture() just once"); |
| 226 |
|
| 227 |
let occurrences = output.stderr.str().count("Exec fixture() just once"); |
| 228 |
|
| 229 |
assert_eq!(1, occurrences); |
| 230 |
} |
| 231 |
|
| 232 |
mod show_correct_errors { |
| 233 |
use super::*; |
| 234 |
use std::process::Output; |
| 235 |
|
| 236 |
use rstest::{fixture, rstest}; |
| 237 |
|
| 238 |
#[fixture] |
| 239 |
#[once] |
| 240 |
fn errors_rs() -> (Output, String) { |
| 241 |
run_test("errors.rs") |
| 242 |
} |
| 243 |
|
| 244 |
#[rstest] |
| 245 |
fn when_cannot_resolve_fixture() { |
| 246 |
let (output, name) = run_test("errors_unresolvable.rs"); |
| 247 |
|
| 248 |
assert_in!(output.stderr.str(), "error[E0433]: "); |
| 249 |
assert_in!( |
| 250 |
output.stderr.str(), |
| 251 |
format!( |
| 252 |
r#" |
| 253 |
--> {name}{SRC_LIB_RS}:9:33 |
| 254 |
| |
| 255 |
9 | fn error_cannot_resolve_fixture(no_fixture: u32) {{"# |
| 256 |
) |
| 257 |
.unindent() |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
#[rstest] |
| 262 |
fn on_mismatched_types_inner(errors_rs: &(Output, String)) { |
| 263 |
let (output, name) = errors_rs.clone(); |
| 264 |
|
| 265 |
assert_in!( |
| 266 |
output.stderr.str(), |
| 267 |
format!( |
| 268 |
r#" |
| 269 |
error[E0308]: mismatched types |
| 270 |
--> {name}{SRC_LIB_RS}:10:18 |
| 271 |
| |
| 272 |
10 | let a: u32 = ""; |
| 273 |
"# |
| 274 |
) |
| 275 |
.unindent() |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
#[rstest] |
| 280 |
fn on_mismatched_types_argument(errors_rs: &(Output, String)) { |
| 281 |
let (output, name) = errors_rs.clone(); |
| 282 |
|
| 283 |
assert_in!( |
| 284 |
output.stderr.str(), |
| 285 |
format!( |
| 286 |
r#" |
| 287 |
error[E0308]: mismatched types |
| 288 |
--> {name}{SRC_LIB_RS}:17:29 |
| 289 |
"# |
| 290 |
) |
| 291 |
.unindent() |
| 292 |
); |
| 293 |
|
| 294 |
assert_in!( |
| 295 |
output.stderr.str(), |
| 296 |
r#" |
| 297 |
17 | fn error_fixture_wrong_type(fixture: String) {} |
| 298 |
| ^^^^^^"# |
| 299 |
.unindent() |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
#[rstest] |
| 304 |
fn on_invalid_fixture(errors_rs: &(Output, String)) { |
| 305 |
let (output, name) = errors_rs.clone(); |
| 306 |
|
| 307 |
assert_in!( |
| 308 |
output.stderr.str(), |
| 309 |
format!( |
| 310 |
" |
| 311 |
error: Missed argument: 'not_a_fixture' should be a test function argument. |
| 312 |
--> {name}{SRC_LIB_RS}:19:11 |
| 313 |
| |
| 314 |
19 | #[fixture(not_a_fixture(24))] |
| 315 |
| ^^^^^^^^^^^^^ |
| 316 |
" |
| 317 |
) |
| 318 |
.unindent() |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
#[rstest] |
| 323 |
fn on_duplicate_fixture_argument(errors_rs: &(Output, String)) { |
| 324 |
let (output, name) = errors_rs.clone(); |
| 325 |
|
| 326 |
assert_in!( |
| 327 |
output.stderr.str(), |
| 328 |
format!( |
| 329 |
r#" |
| 330 |
error: Duplicate argument: 'f' is already defined. |
| 331 |
--> {name}{SRC_LIB_RS}:32:23 |
| 332 |
| |
| 333 |
32 | #[fixture(f("first"), f("second"))] |
| 334 |
| ^ |
| 335 |
"# |
| 336 |
) |
| 337 |
.unindent() |
| 338 |
); |
| 339 |
} |
| 340 |
|
| 341 |
#[rstest] |
| 342 |
fn on_destruct_implicit_fixture(errors_rs: &(Output, String)) { |
| 343 |
let (output, name) = errors_rs.clone(); |
| 344 |
|
| 345 |
assert_in!( |
| 346 |
output.stderr.str(), |
| 347 |
format!( |
| 348 |
r#" |
| 349 |
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute. |
| 350 |
--> {name}{SRC_LIB_RS}:48:35 |
| 351 |
| |
| 352 |
48 | fn error_destruct_without_resolve(T(a): T) {{}} |
| 353 |
| ^^^^^^^ |
| 354 |
"# |
| 355 |
) |
| 356 |
.unindent() |
| 357 |
); |
| 358 |
} |
| 359 |
|
| 360 |
#[rstest] |
| 361 |
fn on_destruct_explicit_fixture_without_from(errors_rs: &(Output, String)) { |
| 362 |
let (output, name) = errors_rs.clone(); |
| 363 |
|
| 364 |
assert_in!( |
| 365 |
output.stderr.str(), |
| 366 |
format!( |
| 367 |
r#" |
| 368 |
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute. |
| 369 |
--> {name}{SRC_LIB_RS}:51:57 |
| 370 |
| |
| 371 |
51 | fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {{}} |
| 372 |
| ^^^^^^^ |
| 373 |
"# |
| 374 |
) |
| 375 |
.unindent() |
| 376 |
); |
| 377 |
assert_eq!( |
| 378 |
1, |
| 379 |
output.stderr.str().count("51 | fn error_destruct_without") |
| 380 |
) |
| 381 |
} |
| 382 |
|
| 383 |
#[fixture] |
| 384 |
#[once] |
| 385 |
fn errors_once_rs() -> (Output, String) { |
| 386 |
run_test("errors_once.rs") |
| 387 |
} |
| 388 |
|
| 389 |
#[rstest] |
| 390 |
fn deny_docs() { |
| 391 |
let (output, _) = run_test("deny_docs.rs"); |
| 392 |
|
| 393 |
assert_not_in!(output.stdout.str(), "missing_docs"); |
| 394 |
|
| 395 |
TestResults::new().ok("success").fail("fail").assert(output); |
| 396 |
} |
| 397 |
|
| 398 |
#[rstest] |
| 399 |
fn once_async(errors_once_rs: &(Output, String)) { |
| 400 |
let (output, name) = errors_once_rs.clone(); |
| 401 |
assert_in!( |
| 402 |
output.stderr.str(), |
| 403 |
format!( |
| 404 |
r#" |
| 405 |
error: Cannot apply #[once] to async fixture. |
| 406 |
--> {name}{SRC_LIB_RS}:4:1 |
| 407 |
| |
| 408 |
4 | #[once] |
| 409 |
"# |
| 410 |
) |
| 411 |
.unindent() |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
#[rstest] |
| 416 |
fn once_generic_type(errors_once_rs: &(Output, String)) { |
| 417 |
let (output, name) = errors_once_rs.clone(); |
| 418 |
|
| 419 |
assert_in!( |
| 420 |
output.stderr.str(), |
| 421 |
format!( |
| 422 |
r#" |
| 423 |
error: Cannot apply #[once] on generic fixture. |
| 424 |
--> {name}{SRC_LIB_RS}:9:1 |
| 425 |
| |
| 426 |
9 | #[once] |
| 427 |
"# |
| 428 |
) |
| 429 |
.unindent() |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
#[rstest] |
| 434 |
fn once_generic_impl(errors_once_rs: &(Output, String)) { |
| 435 |
let (output, name) = errors_once_rs.clone(); |
| 436 |
assert_in!( |
| 437 |
output.stderr.str(), |
| 438 |
format!( |
| 439 |
r#" |
| 440 |
error: Cannot apply #[once] on generic fixture. |
| 441 |
--> {name}{SRC_LIB_RS}:15:1 |
| 442 |
| |
| 443 |
15 | #[once] |
| 444 |
"# |
| 445 |
) |
| 446 |
.unindent() |
| 447 |
); |
| 448 |
} |
| 449 |
|
| 450 |
#[rstest] |
| 451 |
fn once_on_not_sync_type(errors_once_rs: &(Output, String)) { |
| 452 |
let (output, name) = errors_once_rs.clone(); |
| 453 |
assert_in!( |
| 454 |
output.stderr.str(), |
| 455 |
format!( |
| 456 |
r#" |
| 457 |
error[E0277]: `Cell<u32>` cannot be shared between threads safely |
| 458 |
--> {name}{SRC_LIB_RS}:20:1 |
| 459 |
| |
| 460 |
20 | #[fixture] |
| 461 |
| ^^^^^^^^^^ `Cell<u32>` cannot be shared between threads safely |
| 462 |
"# |
| 463 |
) |
| 464 |
.unindent(), |
| 465 |
); |
| 466 |
} |
| 467 |
} |
| 468 |
} |