]> git.lizzy.rs Git - rust.git/blob - editors/code/src/test/utils/diagnotics/SuggestedFixCollection.test.ts
4c1467b5760279e74917f8c9af169c2a88f1b33c
[rust.git] / editors / code / src / test / utils / diagnotics / SuggestedFixCollection.test.ts
1 import * as assert from 'assert';
2 import * as vscode from 'vscode';
3
4 import SuggestedFix from '../../../utils/diagnostics/SuggestedFix';
5 import SuggestedFixCollection from '../../../utils/diagnostics/SuggestedFixCollection';
6
7 const uri1 = vscode.Uri.file('/file/1');
8 const uri2 = vscode.Uri.file('/file/2');
9
10 const mockDocument1 = ({
11     uri: uri1,
12 } as unknown) as vscode.TextDocument;
13
14 const mockDocument2 = ({
15     uri: uri2,
16 } as unknown) as vscode.TextDocument;
17
18 const range1 = new vscode.Range(
19     new vscode.Position(1, 2),
20     new vscode.Position(3, 4),
21 );
22 const range2 = new vscode.Range(
23     new vscode.Position(5, 6),
24     new vscode.Position(7, 8),
25 );
26
27 const diagnostic1 = new vscode.Diagnostic(range1, 'First diagnostic');
28 const diagnostic2 = new vscode.Diagnostic(range2, 'Second diagnostic');
29
30 // This is a mutable object so return a fresh instance every time
31 function suggestion1(): SuggestedFix {
32     return new SuggestedFix(
33         'Replace me!',
34         new vscode.Location(uri1, range1),
35         'With this!',
36     );
37 }
38
39 describe('SuggestedFixCollection', () => {
40     it('should add a suggestion then return it as a code action', () => {
41         const suggestedFixes = new SuggestedFixCollection();
42         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
43
44         // Specify the document and range that exactly matches
45         const codeActions = suggestedFixes.provideCodeActions(
46             mockDocument1,
47             range1,
48         );
49
50         assert.strictEqual(codeActions.length, 1);
51         const [codeAction] = codeActions;
52         assert.strictEqual(codeAction.title, suggestion1().title);
53
54         const { diagnostics } = codeAction;
55         if (!diagnostics) {
56             return assert.fail('Diagnostics unexpectedly missing');
57         }
58
59         assert.strictEqual(diagnostics.length, 1);
60         assert.strictEqual(diagnostics[0], diagnostic1);
61     });
62
63     it('should not return code actions for different ranges', () => {
64         const suggestedFixes = new SuggestedFixCollection();
65         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
66
67         const codeActions = suggestedFixes.provideCodeActions(
68             mockDocument1,
69             range2,
70         );
71
72         assert(!codeActions || codeActions.length === 0);
73     });
74
75     it('should not return code actions for different documents', () => {
76         const suggestedFixes = new SuggestedFixCollection();
77         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
78
79         const codeActions = suggestedFixes.provideCodeActions(
80             mockDocument2,
81             range1,
82         );
83
84         assert(!codeActions || codeActions.length === 0);
85     });
86
87     it('should not return code actions that have been cleared', () => {
88         const suggestedFixes = new SuggestedFixCollection();
89         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
90         suggestedFixes.clear();
91
92         const codeActions = suggestedFixes.provideCodeActions(
93             mockDocument1,
94             range1,
95         );
96
97         assert(!codeActions || codeActions.length === 0);
98     });
99
100     it('should merge identical suggestions together', () => {
101         const suggestedFixes = new SuggestedFixCollection();
102
103         // Add the same suggestion for two diagnostics
104         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
105         suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic2);
106
107         const codeActions = suggestedFixes.provideCodeActions(
108             mockDocument1,
109             range1,
110         );
111
112         assert.strictEqual(codeActions.length, 1);
113         const [codeAction] = codeActions;
114         const { diagnostics } = codeAction;
115
116         if (!diagnostics) {
117             return assert.fail('Diagnostics unexpectedly missing');
118         }
119
120         // We should be associated with both diagnostics
121         assert.strictEqual(diagnostics.length, 2);
122         assert.strictEqual(diagnostics[0], diagnostic1);
123         assert.strictEqual(diagnostics[1], diagnostic2);
124     });
125 });