diff options
author | Aleksey Kladov <[email protected]> | 2020-07-14 11:05:50 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-14 11:05:50 +0100 |
commit | af6984f6a6fad56f24345c03d018ac29549b169d (patch) | |
tree | c41f12adf77fd07801b55776a1aa8ab6f7ffa66e | |
parent | 7b2ce1a54f75ba5d933521ba8780aaf1ffeef407 (diff) |
Data-driven diagnostics tests
-rw-r--r-- | crates/ra_hir_ty/src/diagnostics.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/diagnostics/expr.rs | 169 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/test_db.rs | 2 |
3 files changed, 93 insertions, 100 deletions
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 3623b8569..49a616c6f 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs | |||
@@ -244,3 +244,25 @@ impl AstDiagnostic for MismatchedArgCount { | |||
244 | ast::CallExpr::cast(node).unwrap() | 244 | ast::CallExpr::cast(node).unwrap() |
245 | } | 245 | } |
246 | } | 246 | } |
247 | |||
248 | #[cfg(test)] | ||
249 | fn check_diagnostics(ra_fixture: &str) { | ||
250 | use ra_db::{fixture::WithFixture, FileId}; | ||
251 | use ra_syntax::TextRange; | ||
252 | use rustc_hash::FxHashMap; | ||
253 | |||
254 | use crate::test_db::TestDB; | ||
255 | |||
256 | let db = TestDB::with_files(ra_fixture); | ||
257 | let annotations = db.extract_annotations(); | ||
258 | |||
259 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); | ||
260 | db.diag(|d| { | ||
261 | // FXIME: macros... | ||
262 | let file_id = d.source().file_id.original_file(&db); | ||
263 | let range = d.syntax_node(&db).text_range(); | ||
264 | actual.entry(file_id).or_default().push((range, d.message().to_owned())); | ||
265 | }); | ||
266 | |||
267 | assert_eq!(annotations, actual); | ||
268 | } | ||
diff --git a/crates/ra_hir_ty/src/diagnostics/expr.rs b/crates/ra_hir_ty/src/diagnostics/expr.rs index 239be779f..277ace180 100644 --- a/crates/ra_hir_ty/src/diagnostics/expr.rs +++ b/crates/ra_hir_ty/src/diagnostics/expr.rs | |||
@@ -376,146 +376,117 @@ pub fn record_pattern_missing_fields( | |||
376 | 376 | ||
377 | #[cfg(test)] | 377 | #[cfg(test)] |
378 | mod tests { | 378 | mod tests { |
379 | use expect::{expect, Expect}; | 379 | use crate::diagnostics::check_diagnostics; |
380 | use ra_db::fixture::WithFixture; | ||
381 | |||
382 | use crate::{diagnostics::MismatchedArgCount, test_db::TestDB}; | ||
383 | |||
384 | fn check_diagnostic(ra_fixture: &str, expect: Expect) { | ||
385 | let msg = TestDB::with_single_file(ra_fixture).0.diagnostic::<MismatchedArgCount>().0; | ||
386 | expect.assert_eq(&msg); | ||
387 | } | ||
388 | |||
389 | fn check_no_diagnostic(ra_fixture: &str) { | ||
390 | let (s, diagnostic_count) = | ||
391 | TestDB::with_single_file(ra_fixture).0.diagnostic::<MismatchedArgCount>(); | ||
392 | |||
393 | assert_eq!(0, diagnostic_count, "expected no diagnostic, found one: {}", s); | ||
394 | } | ||
395 | 380 | ||
396 | #[test] | 381 | #[test] |
397 | fn simple_free_fn_zero() { | 382 | fn simple_free_fn_zero() { |
398 | check_diagnostic( | 383 | check_diagnostics( |
399 | r" | 384 | r#" |
400 | fn zero() {} | 385 | fn zero() {} |
401 | fn f() { zero(1); } | 386 | fn f() { zero(1); } |
402 | ", | 387 | //^^^^^^^ Expected 0 arguments, found 1 |
403 | expect![["\"zero(1)\": Expected 0 arguments, found 1\n"]], | 388 | "#, |
404 | ); | 389 | ); |
405 | 390 | ||
406 | check_no_diagnostic( | 391 | check_diagnostics( |
407 | r" | 392 | r#" |
408 | fn zero() {} | 393 | fn zero() {} |
409 | fn f() { zero(); } | 394 | fn f() { zero(); } |
410 | ", | 395 | "#, |
411 | ); | 396 | ); |
412 | } | 397 | } |
413 | 398 | ||
414 | #[test] | 399 | #[test] |
415 | fn simple_free_fn_one() { | 400 | fn simple_free_fn_one() { |
416 | check_diagnostic( | 401 | check_diagnostics( |
417 | r" | 402 | r#" |
418 | fn one(arg: u8) {} | 403 | fn one(arg: u8) {} |
419 | fn f() { one(); } | 404 | fn f() { one(); } |
420 | ", | 405 | //^^^^^ Expected 1 argument, found 0 |
421 | expect![["\"one()\": Expected 1 argument, found 0\n"]], | 406 | "#, |
422 | ); | 407 | ); |
423 | 408 | ||
424 | check_no_diagnostic( | 409 | check_diagnostics( |
425 | r" | 410 | r#" |
426 | fn one(arg: u8) {} | 411 | fn one(arg: u8) {} |
427 | fn f() { one(1); } | 412 | fn f() { one(1); } |
428 | ", | 413 | "#, |
429 | ); | 414 | ); |
430 | } | 415 | } |
431 | 416 | ||
432 | #[test] | 417 | #[test] |
433 | fn method_as_fn() { | 418 | fn method_as_fn() { |
434 | check_diagnostic( | 419 | check_diagnostics( |
435 | r" | 420 | r#" |
436 | struct S; | 421 | struct S; |
437 | impl S { | 422 | impl S { fn method(&self) {} } |
438 | fn method(&self) {} | 423 | |
439 | } | 424 | fn f() { |
440 | 425 | S::method(); | |
441 | fn f() { | 426 | } //^^^^^^^^^^^ Expected 1 argument, found 0 |
442 | S::method(); | 427 | "#, |
443 | } | ||
444 | ", | ||
445 | expect![["\"S::method()\": Expected 1 argument, found 0\n"]], | ||
446 | ); | 428 | ); |
447 | 429 | ||
448 | check_no_diagnostic( | 430 | check_diagnostics( |
449 | r" | 431 | r#" |
450 | struct S; | 432 | struct S; |
451 | impl S { | 433 | impl S { fn method(&self) {} } |
452 | fn method(&self) {} | ||
453 | } | ||
454 | 434 | ||
455 | fn f() { | 435 | fn f() { |
456 | S::method(&S); | 436 | S::method(&S); |
457 | S.method(); | 437 | S.method(); |
458 | } | 438 | } |
459 | ", | 439 | "#, |
460 | ); | 440 | ); |
461 | } | 441 | } |
462 | 442 | ||
463 | #[test] | 443 | #[test] |
464 | fn method_with_arg() { | 444 | fn method_with_arg() { |
465 | check_diagnostic( | 445 | check_diagnostics( |
466 | r" | 446 | r#" |
467 | struct S; | 447 | struct S; |
468 | impl S { | 448 | impl S { fn method(&self, arg: u8) {} } |
469 | fn method(&self, arg: u8) {} | ||
470 | } | ||
471 | 449 | ||
472 | fn f() { | 450 | fn f() { |
473 | S.method(); | 451 | S.method(); |
474 | } | 452 | } //^^^^^^^^^^ Expected 1 argument, found 0 |
475 | ", | 453 | "#, |
476 | expect![["\"S.method()\": Expected 1 argument, found 0\n"]], | ||
477 | ); | 454 | ); |
478 | 455 | ||
479 | check_no_diagnostic( | 456 | check_diagnostics( |
480 | r" | 457 | r#" |
481 | struct S; | 458 | struct S; |
482 | impl S { | 459 | impl S { fn method(&self, arg: u8) {} } |
483 | fn method(&self, arg: u8) {} | ||
484 | } | ||
485 | 460 | ||
486 | fn f() { | 461 | fn f() { |
487 | S::method(&S, 0); | 462 | S::method(&S, 0); |
488 | S.method(1); | 463 | S.method(1); |
489 | } | 464 | } |
490 | ", | 465 | "#, |
491 | ); | 466 | ); |
492 | } | 467 | } |
493 | 468 | ||
494 | #[test] | 469 | #[test] |
495 | fn tuple_struct() { | 470 | fn tuple_struct() { |
496 | check_diagnostic( | 471 | check_diagnostics( |
497 | r" | 472 | r#" |
498 | struct Tup(u8, u16); | 473 | struct Tup(u8, u16); |
499 | fn f() { | 474 | fn f() { |
500 | Tup(0); | 475 | Tup(0); |
501 | } | 476 | } //^^^^^^ Expected 2 arguments, found 1 |
502 | ", | 477 | "#, |
503 | expect![["\"Tup(0)\": Expected 2 arguments, found 1\n"]], | ||
504 | ) | 478 | ) |
505 | } | 479 | } |
506 | 480 | ||
507 | #[test] | 481 | #[test] |
508 | fn enum_variant() { | 482 | fn enum_variant() { |
509 | check_diagnostic( | 483 | check_diagnostics( |
510 | r" | 484 | r#" |
511 | enum En { | 485 | enum En { Variant(u8, u16), } |
512 | Variant(u8, u16), | 486 | fn f() { |
513 | } | 487 | En::Variant(0); |
514 | fn f() { | 488 | } //^^^^^^^^^^^^^^ Expected 2 arguments, found 1 |
515 | En::Variant(0); | 489 | "#, |
516 | } | ||
517 | ", | ||
518 | expect![["\"En::Variant(0)\": Expected 2 arguments, found 1\n"]], | ||
519 | ) | 490 | ) |
520 | } | 491 | } |
521 | } | 492 | } |
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs index ddafd0ea1..0bd29f435 100644 --- a/crates/ra_hir_ty/src/test_db.rs +++ b/crates/ra_hir_ty/src/test_db.rs | |||
@@ -94,7 +94,7 @@ impl TestDB { | |||
94 | panic!("Can't find module for file") | 94 | panic!("Can't find module for file") |
95 | } | 95 | } |
96 | 96 | ||
97 | fn diag<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { | 97 | pub(crate) fn diag<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { |
98 | let crate_graph = self.crate_graph(); | 98 | let crate_graph = self.crate_graph(); |
99 | for krate in crate_graph.iter() { | 99 | for krate in crate_graph.iter() { |
100 | let crate_def_map = self.crate_def_map(krate); | 100 | let crate_def_map = self.crate_def_map(krate); |