summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs
blob: 7312bca32fed99151c7fe9711a1437f3ba5b4aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#[cfg(not(feature = "in-rust-tree"))]
mod sourcegen;

use expect_test::Expect;
use ide_db::{
    assists::AssistResolveStrategy,
    base_db::{fixture::WithFixture, SourceDatabaseExt},
    RootDatabase,
};
use stdx::trim_indent;
use test_utils::{assert_eq_text, extract_annotations};

use crate::{DiagnosticsConfig, ExprFillDefaultMode, Severity};

/// Takes a multi-file input fixture with annotated cursor positions,
/// and checks that:
///  * a diagnostic is produced
///  * the first diagnostic fix trigger range touches the input cursor position
///  * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
#[track_caller]
pub(crate) fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
    check_nth_fix(0, ra_fixture_before, ra_fixture_after);
}
/// Takes a multi-file input fixture with annotated cursor positions,
/// and checks that:
///  * a diagnostic is produced
///  * every diagnostic fixes trigger range touches the input cursor position
///  * that the contents of the file containing the cursor match `after` after each diagnostic fix is applied
pub(crate) fn check_fixes(ra_fixture_before: &str, ra_fixtures_after: Vec<&str>) {
    for (i, ra_fixture_after) in ra_fixtures_after.iter().enumerate() {
        check_nth_fix(i, ra_fixture_before, ra_fixture_after)
    }
}

#[track_caller]
fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
    let after = trim_indent(ra_fixture_after);

    let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
    let mut conf = DiagnosticsConfig::default();
    conf.expr_fill_default = ExprFillDefaultMode::Default;
    let diagnostic =
        super::diagnostics(&db, &conf, &AssistResolveStrategy::All, file_position.file_id)
            .pop()
            .expect("no diagnostics");
    let fix = &diagnostic.fixes.expect("diagnostic misses fixes")[nth];
    let actual = {
        let source_change = fix.source_change.as_ref().unwrap();
        let file_id = *source_change.source_file_edits.keys().next().unwrap();
        let mut actual = db.file_text(file_id).to_string();

        for edit in source_change.source_file_edits.values() {
            edit.apply(&mut actual);
        }
        actual
    };

    assert!(
        fix.target.contains_inclusive(file_position.offset),
        "diagnostic fix range {:?} does not touch cursor position {:?}",
        fix.target,
        file_position.offset
    );
    assert_eq_text!(&after, &actual);
}

/// Checks that there's a diagnostic *without* fix at `$0`.
pub(crate) fn check_no_fix(ra_fixture: &str) {
    let (db, file_position) = RootDatabase::with_position(ra_fixture);
    let diagnostic = super::diagnostics(
        &db,
        &DiagnosticsConfig::default(),
        &AssistResolveStrategy::All,
        file_position.file_id,
    )
    .pop()
    .unwrap();
    assert!(diagnostic.fixes.is_none(), "got a fix when none was expected: {:?}", diagnostic);
}

pub(crate) fn check_expect(ra_fixture: &str, expect: Expect) {
    let (db, file_id) = RootDatabase::with_single_file(ra_fixture);
    let diagnostics = super::diagnostics(
        &db,
        &DiagnosticsConfig::default(),
        &AssistResolveStrategy::All,
        file_id,
    );
    expect.assert_debug_eq(&diagnostics)
}

#[track_caller]
pub(crate) fn check_diagnostics(ra_fixture: &str) {
    let mut config = DiagnosticsConfig::default();
    config.disabled.insert("inactive-code".to_string());
    check_diagnostics_with_config(config, ra_fixture)
}

#[track_caller]
pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
    let (db, files) = RootDatabase::with_many_files(ra_fixture);
    for file_id in files {
        let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);

        let expected = extract_annotations(&*db.file_text(file_id));
        let mut actual = diagnostics
            .into_iter()
            .map(|d| {
                let mut annotation = String::new();
                if let Some(fixes) = &d.fixes {
                    assert!(!fixes.is_empty());
                    annotation.push_str("💡 ")
                }
                annotation.push_str(match d.severity {
                    Severity::Error => "error",
                    Severity::WeakWarning => "weak",
                });
                annotation.push_str(": ");
                annotation.push_str(&d.message);
                (d.range, annotation)
            })
            .collect::<Vec<_>>();
        actual.sort_by_key(|(range, _)| range.start());
        assert_eq!(expected, actual);
    }
}

#[test]
fn test_disabled_diagnostics() {
    let mut config = DiagnosticsConfig::default();
    config.disabled.insert("unresolved-module".into());

    let (db, file_id) = RootDatabase::with_single_file(r#"mod foo;"#);

    let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
    assert!(diagnostics.is_empty());

    let diagnostics = super::diagnostics(
        &db,
        &DiagnosticsConfig::default(),
        &AssistResolveStrategy::All,
        file_id,
    );
    assert!(!diagnostics.is_empty());
}