aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-14 15:43:39 +0100
committerAleksey Kladov <[email protected]>2020-07-14 15:44:20 +0100
commitb9070cc64e0767d2a8bde5084a61f46e2e804f5b (patch)
tree11eff9785ddae6ac25b9b74543fb36180bd20d82 /crates/ra_hir_ty/src/tests.rs
parent5a25cc28204c6d02a5108b13568ec71914f096a5 (diff)
Refactor the test of diagnostic tests
Diffstat (limited to 'crates/ra_hir_ty/src/tests.rs')
-rw-r--r--crates/ra_hir_ty/src/tests.rs408
1 files changed, 0 insertions, 408 deletions
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index 27f5a60bf..d57b3f288 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -20,7 +20,6 @@ use hir_def::{
20 AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, 20 AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
21}; 21};
22use hir_expand::{db::AstDatabase, InFile}; 22use hir_expand::{db::AstDatabase, InFile};
23use insta::assert_snapshot;
24use ra_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt}; 23use ra_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
25use ra_syntax::{ 24use ra_syntax::{
26 algo, 25 algo,
@@ -341,410 +340,3 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
341 assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events) 340 assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
342 } 341 }
343} 342}
344
345#[test]
346fn no_such_field_diagnostics() {
347 let diagnostics = TestDB::with_files(
348 r"
349 //- /lib.rs
350 struct S { foo: i32, bar: () }
351 impl S {
352 fn new() -> S {
353 S {
354 foo: 92,
355 baz: 62,
356 }
357 }
358 }
359 ",
360 )
361 .diagnostics()
362 .0;
363
364 assert_snapshot!(diagnostics, @r###"
365 "baz: 62": no such field
366 "{\n foo: 92,\n baz: 62,\n }": Missing structure fields:
367 - bar
368 "###
369 );
370}
371
372#[test]
373fn no_such_field_with_feature_flag_diagnostics() {
374 let diagnostics = TestDB::with_files(
375 r#"
376 //- /lib.rs crate:foo cfg:feature=foo
377 struct MyStruct {
378 my_val: usize,
379 #[cfg(feature = "foo")]
380 bar: bool,
381 }
382
383 impl MyStruct {
384 #[cfg(feature = "foo")]
385 pub(crate) fn new(my_val: usize, bar: bool) -> Self {
386 Self { my_val, bar }
387 }
388
389 #[cfg(not(feature = "foo"))]
390 pub(crate) fn new(my_val: usize, _bar: bool) -> Self {
391 Self { my_val }
392 }
393 }
394 "#,
395 )
396 .diagnostics()
397 .0;
398
399 assert_snapshot!(diagnostics, @r###""###);
400}
401
402#[test]
403fn no_such_field_enum_with_feature_flag_diagnostics() {
404 let diagnostics = TestDB::with_files(
405 r#"
406 //- /lib.rs crate:foo cfg:feature=foo
407 enum Foo {
408 #[cfg(not(feature = "foo"))]
409 Buz,
410 #[cfg(feature = "foo")]
411 Bar,
412 Baz
413 }
414
415 fn test_fn(f: Foo) {
416 match f {
417 Foo::Bar => {},
418 Foo::Baz => {},
419 }
420 }
421 "#,
422 )
423 .diagnostics()
424 .0;
425
426 assert_snapshot!(diagnostics, @r###""###);
427}
428
429#[test]
430fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() {
431 let diagnostics = TestDB::with_files(
432 r#"
433 //- /lib.rs crate:foo cfg:feature=foo
434 struct S {
435 #[cfg(feature = "foo")]
436 foo: u32,
437 #[cfg(not(feature = "foo"))]
438 bar: u32,
439 }
440
441 impl S {
442 #[cfg(feature = "foo")]
443 fn new(foo: u32) -> Self {
444 Self { foo }
445 }
446 #[cfg(not(feature = "foo"))]
447 fn new(bar: u32) -> Self {
448 Self { bar }
449 }
450 }
451 "#,
452 )
453 .diagnostics()
454 .0;
455
456 assert_snapshot!(diagnostics, @r###""###);
457}
458
459#[test]
460fn no_such_field_with_feature_flag_diagnostics_on_block_expr() {
461 let diagnostics = TestDB::with_files(
462 r#"
463 //- /lib.rs crate:foo cfg:feature=foo
464 struct S {
465 #[cfg(feature = "foo")]
466 foo: u32,
467 #[cfg(not(feature = "foo"))]
468 bar: u32,
469 }
470
471 impl S {
472 fn new(bar: u32) -> Self {
473 #[cfg(feature = "foo")]
474 {
475 Self { foo: bar }
476 }
477 #[cfg(not(feature = "foo"))]
478 {
479 Self { bar }
480 }
481 }
482 }
483 "#,
484 )
485 .diagnostics()
486 .0;
487
488 assert_snapshot!(diagnostics, @r###""###);
489}
490
491#[test]
492fn no_such_field_with_feature_flag_diagnostics_on_struct_fields() {
493 let diagnostics = TestDB::with_files(
494 r#"
495 //- /lib.rs crate:foo cfg:feature=foo
496 struct S {
497 #[cfg(feature = "foo")]
498 foo: u32,
499 #[cfg(not(feature = "foo"))]
500 bar: u32,
501 }
502
503 impl S {
504 fn new(val: u32) -> Self {
505 Self {
506 #[cfg(feature = "foo")]
507 foo: val,
508 #[cfg(not(feature = "foo"))]
509 bar: val,
510 }
511 }
512 }
513 "#,
514 )
515 .diagnostics()
516 .0;
517
518 assert_snapshot!(diagnostics, @r###""###);
519}
520
521#[test]
522fn no_such_field_with_type_macro() {
523 let diagnostics = TestDB::with_files(
524 r"
525 macro_rules! Type {
526 () => { u32 };
527 }
528
529 struct Foo {
530 bar: Type![],
531 }
532 impl Foo {
533 fn new() -> Self {
534 Foo { bar: 0 }
535 }
536 }
537 ",
538 )
539 .diagnostics()
540 .0;
541
542 assert_snapshot!(diagnostics, @r###""###);
543}
544
545#[test]
546fn missing_record_pat_field_diagnostic() {
547 let diagnostics = TestDB::with_files(
548 r"
549 //- /lib.rs
550 struct S { foo: i32, bar: () }
551 fn baz(s: S) {
552 let S { foo: _ } = s;
553 }
554 ",
555 )
556 .diagnostics()
557 .0;
558
559 assert_snapshot!(diagnostics, @r###"
560 "{ foo: _ }": Missing structure fields:
561 - bar
562 "###
563 );
564}
565
566#[test]
567fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
568 let diagnostics = TestDB::with_files(
569 r"
570 //- /lib.rs
571 struct S { foo: i32, bar: () }
572 fn baz(s: S) -> i32 {
573 match s {
574 S { foo, .. } => foo,
575 }
576 }
577 ",
578 )
579 .diagnostics()
580 .0;
581
582 assert_snapshot!(diagnostics, @"");
583}
584
585#[test]
586fn missing_unsafe_diagnostic_with_raw_ptr() {
587 let diagnostics = TestDB::with_files(
588 r"
589//- /lib.rs
590fn missing_unsafe() {
591 let x = &5 as *const usize;
592 let y = *x;
593}
594",
595 )
596 .diagnostics()
597 .0;
598
599 assert_snapshot!(diagnostics, @r#""*x": This operation is unsafe and requires an unsafe function or block"#);
600}
601
602#[test]
603fn missing_unsafe_diagnostic_with_unsafe_call() {
604 let diagnostics = TestDB::with_files(
605 r"
606//- /lib.rs
607unsafe fn unsafe_fn() {
608 let x = &5 as *const usize;
609 let y = *x;
610}
611
612fn missing_unsafe() {
613 unsafe_fn();
614}
615",
616 )
617 .diagnostics()
618 .0;
619
620 assert_snapshot!(diagnostics, @r#""unsafe_fn()": This operation is unsafe and requires an unsafe function or block"#);
621}
622
623#[test]
624fn missing_unsafe_diagnostic_with_unsafe_method_call() {
625 let diagnostics = TestDB::with_files(
626 r"
627struct HasUnsafe;
628
629impl HasUnsafe {
630 unsafe fn unsafe_fn(&self) {
631 let x = &5 as *const usize;
632 let y = *x;
633 }
634}
635
636fn missing_unsafe() {
637 HasUnsafe.unsafe_fn();
638}
639
640",
641 )
642 .diagnostics()
643 .0;
644
645 assert_snapshot!(diagnostics, @r#""HasUnsafe.unsafe_fn()": This operation is unsafe and requires an unsafe function or block"#);
646}
647
648#[test]
649fn no_missing_unsafe_diagnostic_with_raw_ptr_in_unsafe_block() {
650 let diagnostics = TestDB::with_files(
651 r"
652fn nothing_to_see_move_along() {
653 let x = &5 as *const usize;
654 unsafe {
655 let y = *x;
656 }
657}
658",
659 )
660 .diagnostics()
661 .0;
662
663 assert_snapshot!(diagnostics, @"");
664}
665
666#[test]
667fn missing_unsafe_diagnostic_with_raw_ptr_outside_unsafe_block() {
668 let diagnostics = TestDB::with_files(
669 r"
670fn nothing_to_see_move_along() {
671 let x = &5 as *const usize;
672 unsafe {
673 let y = *x;
674 }
675 let z = *x;
676}
677",
678 )
679 .diagnostics()
680 .0;
681
682 assert_snapshot!(diagnostics, @r#""*x": This operation is unsafe and requires an unsafe function or block"#);
683}
684
685#[test]
686fn no_missing_unsafe_diagnostic_with_unsafe_call_in_unsafe_block() {
687 let diagnostics = TestDB::with_files(
688 r"
689unsafe fn unsafe_fn() {
690 let x = &5 as *const usize;
691 let y = *x;
692}
693
694fn nothing_to_see_move_along() {
695 unsafe {
696 unsafe_fn();
697 }
698}
699",
700 )
701 .diagnostics()
702 .0;
703
704 assert_snapshot!(diagnostics, @"");
705}
706
707#[test]
708fn no_missing_unsafe_diagnostic_with_unsafe_method_call_in_unsafe_block() {
709 let diagnostics = TestDB::with_files(
710 r"
711struct HasUnsafe;
712
713impl HasUnsafe {
714 unsafe fn unsafe_fn() {
715 let x = &5 as *const usize;
716 let y = *x;
717 }
718}
719
720fn nothing_to_see_move_along() {
721 unsafe {
722 HasUnsafe.unsafe_fn();
723 }
724}
725
726",
727 )
728 .diagnostics()
729 .0;
730
731 assert_snapshot!(diagnostics, @"");
732}
733
734#[test]
735fn break_outside_of_loop() {
736 let diagnostics = TestDB::with_files(
737 r"
738 //- /lib.rs
739 fn foo() {
740 break;
741 }
742 ",
743 )
744 .diagnostics()
745 .0;
746
747 assert_snapshot!(diagnostics, @r###""break": break outside of loop
748 "###
749 );
750}