1 import * as assert from 'assert';
2 import * as vscode from 'vscode';
4 import SuggestedFix from '../../../utils/diagnostics/SuggestedFix';
5 import SuggestedFixCollection from '../../../utils/diagnostics/SuggestedFixCollection';
7 const uri1 = vscode.Uri.file('/file/1');
8 const uri2 = vscode.Uri.file('/file/2');
10 const mockDocument1 = ({
12 } as unknown) as vscode.TextDocument;
14 const mockDocument2 = ({
16 } as unknown) as vscode.TextDocument;
18 const range1 = new vscode.Range(
19 new vscode.Position(1, 2),
20 new vscode.Position(3, 4),
22 const range2 = new vscode.Range(
23 new vscode.Position(5, 6),
24 new vscode.Position(7, 8),
27 const diagnostic1 = new vscode.Diagnostic(range1, 'First diagnostic');
28 const diagnostic2 = new vscode.Diagnostic(range2, 'Second diagnostic');
30 // This is a mutable object so return a fresh instance every time
31 function suggestion1(): SuggestedFix {
32 return new SuggestedFix(
34 new vscode.Location(uri1, range1),
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);
44 // Specify the document and range that exactly matches
45 const codeActions = suggestedFixes.provideCodeActions(
50 assert.strictEqual(codeActions.length, 1);
51 const [codeAction] = codeActions;
52 assert.strictEqual(codeAction.title, suggestion1().title);
54 const { diagnostics } = codeAction;
56 return assert.fail('Diagnostics unexpectedly missing');
59 assert.strictEqual(diagnostics.length, 1);
60 assert.strictEqual(diagnostics[0], diagnostic1);
63 it('should not return code actions for different ranges', () => {
64 const suggestedFixes = new SuggestedFixCollection();
65 suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
67 const codeActions = suggestedFixes.provideCodeActions(
72 assert(!codeActions || codeActions.length === 0);
75 it('should not return code actions for different documents', () => {
76 const suggestedFixes = new SuggestedFixCollection();
77 suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
79 const codeActions = suggestedFixes.provideCodeActions(
84 assert(!codeActions || codeActions.length === 0);
87 it('should not return code actions that have been cleared', () => {
88 const suggestedFixes = new SuggestedFixCollection();
89 suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
90 suggestedFixes.clear();
92 const codeActions = suggestedFixes.provideCodeActions(
97 assert(!codeActions || codeActions.length === 0);
100 it('should merge identical suggestions together', () => {
101 const suggestedFixes = new SuggestedFixCollection();
103 // Add the same suggestion for two diagnostics
104 suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic1);
105 suggestedFixes.addSuggestedFixForDiagnostic(suggestion1(), diagnostic2);
107 const codeActions = suggestedFixes.provideCodeActions(
112 assert.strictEqual(codeActions.length, 1);
113 const [codeAction] = codeActions;
114 const { diagnostics } = codeAction;
117 return assert.fail('Diagnostics unexpectedly missing');
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);