aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/test/utils/diagnotics/rust.test.ts
blob: b555a481986e09d6fce9164521d32333623c7c30 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as assert from 'assert';
import * as fs from 'fs';
import * as vscode from 'vscode';

import {
    MappedRustDiagnostic,
    mapRustDiagnosticToVsCode,
    RustDiagnostic,
    SuggestionApplicability
} from '../../../utils/diagnostics/rust';

function loadDiagnosticFixture(name: string): RustDiagnostic {
    const jsonText = fs
        .readFileSync(
            // We're actually in our JavaScript output directory, climb out
            `${__dirname}/../../../../src/test/fixtures/rust-diagnostics/${name}.json`
        )
        .toString();

    return JSON.parse(jsonText);
}

function mapFixtureToVsCode(name: string): MappedRustDiagnostic {
    const rd = loadDiagnosticFixture(name);
    const mapResult = mapRustDiagnosticToVsCode(rd);

    if (!mapResult) {
        return assert.fail('Mapping unexpectedly failed');
    }
    return mapResult;
}

describe('mapRustDiagnosticToVsCode', () => {
    it('should map an incompatible type for trait error', () => {
        const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
            'error/E0053'
        );

        assert.strictEqual(
            diagnostic.severity,
            vscode.DiagnosticSeverity.Error
        );
        assert.strictEqual(diagnostic.source, 'rustc');
        assert.strictEqual(
            diagnostic.message,
            [
                `method \`next\` has an incompatible type for trait`,
                `expected type \`fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>\``,
                `   found type \`fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>\``
            ].join('\n')
        );
        assert.strictEqual(diagnostic.code, 'E0053');
        assert.strictEqual(diagnostic.tags, undefined);

        // No related information
        assert.deepStrictEqual(diagnostic.relatedInformation, []);

        // There are no suggested fixes
        assert.strictEqual(suggestedFixes.length, 0);
    });

    it('should map an unused variable warning', () => {
        const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
            'warning/unused_variables'
        );

        assert.strictEqual(
            diagnostic.severity,
            vscode.DiagnosticSeverity.Warning
        );
        assert.strictEqual(
            diagnostic.message,
            [
                'unused variable: `foo`',
                '#[warn(unused_variables)] on by default'
            ].join('\n')
        );
        assert.strictEqual(diagnostic.code, 'unused_variables');
        assert.strictEqual(diagnostic.source, 'rustc');
        assert.deepStrictEqual(diagnostic.tags, [
            vscode.DiagnosticTag.Unnecessary
        ]);

        // No related information
        assert.deepStrictEqual(diagnostic.relatedInformation, []);

        // One suggested fix available to prefix the variable
        assert.strictEqual(suggestedFixes.length, 1);
        const [suggestedFix] = suggestedFixes;
        assert.strictEqual(
            suggestedFix.title,
            'consider prefixing with an underscore: `_foo`'
        );
        assert.strictEqual(
            suggestedFix.applicability,
            SuggestionApplicability.MachineApplicable
        );
    });

    it('should map a wrong number of parameters error', () => {
        const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
            'error/E0061'
        );

        assert.strictEqual(
            diagnostic.severity,
            vscode.DiagnosticSeverity.Error
        );
        assert.strictEqual(
            diagnostic.message,
            'this function takes 2 parameters but 3 parameters were supplied'
        );
        assert.strictEqual(diagnostic.code, 'E0061');
        assert.strictEqual(diagnostic.source, 'rustc');
        assert.strictEqual(diagnostic.tags, undefined);

        // One related information for the original definition
        const relatedInformation = diagnostic.relatedInformation;
        if (!relatedInformation) {
            return assert.fail('Related information unexpectedly undefined');
        }
        assert.strictEqual(relatedInformation.length, 1);
        const [related] = relatedInformation;
        assert.strictEqual(related.message, 'defined here');

        // There are no suggested fixes
        assert.strictEqual(suggestedFixes.length, 0);
    });

    it('should map a Clippy copy pass by ref warning', () => {
        const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
            'clippy/trivially_copy_pass_by_ref'
        );

        assert.strictEqual(
            diagnostic.severity,
            vscode.DiagnosticSeverity.Warning
        );
        assert.strictEqual(diagnostic.source, 'clippy');
        assert.strictEqual(
            diagnostic.message,
            [
                'this argument is passed by reference, but would be more efficient if passed by value',
                '#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]',
                'for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref'
            ].join('\n')
        );
        assert.strictEqual(diagnostic.code, 'trivially_copy_pass_by_ref');
        assert.strictEqual(diagnostic.tags, undefined);

        // One related information for the lint definition
        const relatedInformation = diagnostic.relatedInformation;
        if (!relatedInformation) {
            return assert.fail('Related information unexpectedly undefined');
        }
        assert.strictEqual(relatedInformation.length, 1);
        const [related] = relatedInformation;
        assert.strictEqual(related.message, 'lint level defined here');

        // One suggested fix to pass by value
        assert.strictEqual(suggestedFixes.length, 1);
        const [suggestedFix] = suggestedFixes;
        assert.strictEqual(
            suggestedFix.title,
            'consider passing by value instead: `self`'
        );
        // Clippy does not mark this with any applicability
        assert.strictEqual(
            suggestedFix.applicability,
            SuggestionApplicability.Unspecified
        );
    });
});