]> git.lizzy.rs Git - rust.git/blob - editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts
Migrate tests from extension to rust
[rust.git] / editors / code / src / test / utils / diagnotics / SuggestedFix.test.ts
1 import * as assert from 'assert';
2 import * as vscode from 'vscode';
3
4 import { SuggestionApplicability } from '../../../utils/diagnostics/rust';
5 import SuggestedFix from '../../../utils/diagnostics/SuggestedFix';
6
7 const location1 = new vscode.Location(
8     vscode.Uri.file('/file/1'),
9     new vscode.Range(new vscode.Position(1, 2), new vscode.Position(3, 4)),
10 );
11
12 const location2 = new vscode.Location(
13     vscode.Uri.file('/file/2'),
14     new vscode.Range(new vscode.Position(5, 6), new vscode.Position(7, 8)),
15 );
16
17 describe('SuggestedFix', () => {
18     describe('isEqual', () => {
19         it('should treat identical instances as equal', () => {
20             const suggestion1 = new SuggestedFix(
21                 'Replace me!',
22                 location1,
23                 'With this!',
24             );
25
26             const suggestion2 = new SuggestedFix(
27                 'Replace me!',
28                 location1,
29                 'With this!',
30             );
31
32             assert(suggestion1.isEqual(suggestion2));
33         });
34
35         it('should treat instances with different titles as inequal', () => {
36             const suggestion1 = new SuggestedFix(
37                 'Replace me!',
38                 location1,
39                 'With this!',
40             );
41
42             const suggestion2 = new SuggestedFix(
43                 'Not the same title!',
44                 location1,
45                 'With this!',
46             );
47
48             assert(!suggestion1.isEqual(suggestion2));
49         });
50
51         it('should treat instances with different replacements as inequal', () => {
52             const suggestion1 = new SuggestedFix(
53                 'Replace me!',
54                 location1,
55                 'With this!',
56             );
57
58             const suggestion2 = new SuggestedFix(
59                 'Replace me!',
60                 location1,
61                 'With something else!',
62             );
63
64             assert(!suggestion1.isEqual(suggestion2));
65         });
66
67         it('should treat instances with different locations as inequal', () => {
68             const suggestion1 = new SuggestedFix(
69                 'Replace me!',
70                 location1,
71                 'With this!',
72             );
73
74             const suggestion2 = new SuggestedFix(
75                 'Replace me!',
76                 location2,
77                 'With this!',
78             );
79
80             assert(!suggestion1.isEqual(suggestion2));
81         });
82
83         it('should treat instances with different applicability as inequal', () => {
84             const suggestion1 = new SuggestedFix(
85                 'Replace me!',
86                 location1,
87                 'With this!',
88                 SuggestionApplicability.MachineApplicable,
89             );
90
91             const suggestion2 = new SuggestedFix(
92                 'Replace me!',
93                 location2,
94                 'With this!',
95                 SuggestionApplicability.HasPlaceholders,
96             );
97
98             assert(!suggestion1.isEqual(suggestion2));
99         });
100     });
101
102     describe('toCodeAction', () => {
103         it('should map a simple suggestion', () => {
104             const suggestion = new SuggestedFix(
105                 'Replace me!',
106                 location1,
107                 'With this!',
108             );
109
110             const codeAction = suggestion.toCodeAction();
111             assert.strictEqual(codeAction.kind, vscode.CodeActionKind.QuickFix);
112             assert.strictEqual(codeAction.title, 'Replace me!');
113             assert.strictEqual(codeAction.isPreferred, false);
114
115             const edit = codeAction.edit;
116             if (!edit) {
117                 assert.fail('Code Action edit unexpectedly missing');
118                 return;
119             }
120
121             const editEntries = edit.entries();
122             assert.strictEqual(editEntries.length, 1);
123
124             const [[editUri, textEdits]] = editEntries;
125             assert.strictEqual(editUri.toString(), location1.uri.toString());
126
127             assert.strictEqual(textEdits.length, 1);
128             const [textEdit] = textEdits;
129
130             assert(textEdit.range.isEqual(location1.range));
131             assert.strictEqual(textEdit.newText, 'With this!');
132         });
133     });
134 });