aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts
blob: 2b25eb705dd35743f432df559326249c34ba4244 (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
import * as assert from 'assert';
import * as vscode from 'vscode';

import { SuggestionApplicability } from '../../../utils/diagnostics/rust';
import SuggestedFix from '../../../utils/diagnostics/SuggestedFix';

const location1 = new vscode.Location(
    vscode.Uri.file('/file/1'),
    new vscode.Range(new vscode.Position(1, 2), new vscode.Position(3, 4)),
);

const location2 = new vscode.Location(
    vscode.Uri.file('/file/2'),
    new vscode.Range(new vscode.Position(5, 6), new vscode.Position(7, 8)),
);

describe('SuggestedFix', () => {
    describe('isEqual', () => {
        it('should treat identical instances as equal', () => {
            const suggestion1 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            const suggestion2 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            assert(suggestion1.isEqual(suggestion2));
        });

        it('should treat instances with different titles as inequal', () => {
            const suggestion1 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            const suggestion2 = new SuggestedFix(
                'Not the same title!',
                location1,
                'With this!',
            );

            assert(!suggestion1.isEqual(suggestion2));
        });

        it('should treat instances with different replacements as inequal', () => {
            const suggestion1 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            const suggestion2 = new SuggestedFix(
                'Replace me!',
                location1,
                'With something else!',
            );

            assert(!suggestion1.isEqual(suggestion2));
        });

        it('should treat instances with different locations as inequal', () => {
            const suggestion1 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            const suggestion2 = new SuggestedFix(
                'Replace me!',
                location2,
                'With this!',
            );

            assert(!suggestion1.isEqual(suggestion2));
        });

        it('should treat instances with different applicability as inequal', () => {
            const suggestion1 = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
                SuggestionApplicability.MachineApplicable,
            );

            const suggestion2 = new SuggestedFix(
                'Replace me!',
                location2,
                'With this!',
                SuggestionApplicability.HasPlaceholders,
            );

            assert(!suggestion1.isEqual(suggestion2));
        });
    });

    describe('toCodeAction', () => {
        it('should map a simple suggestion', () => {
            const suggestion = new SuggestedFix(
                'Replace me!',
                location1,
                'With this!',
            );

            const codeAction = suggestion.toCodeAction();
            assert.strictEqual(codeAction.kind, vscode.CodeActionKind.QuickFix);
            assert.strictEqual(codeAction.title, 'Replace me!');
            assert.strictEqual(codeAction.isPreferred, false);

            const edit = codeAction.edit;
            if (!edit) {
                assert.fail('Code Action edit unexpectedly missing');
                return;
            }

            const editEntries = edit.entries();
            assert.strictEqual(editEntries.length, 1);

            const [[editUri, textEdits]] = editEntries;
            assert.strictEqual(editUri.toString(), location1.uri.toString());

            assert.strictEqual(textEdits.length, 1);
            const [textEdit] = textEdits;

            assert(textEdit.range.isEqual(location1.range));
            assert.strictEqual(textEdit.newText, 'With this!');
        });
    });
});