diff options
Diffstat (limited to 'editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts')
-rw-r--r-- | editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts b/editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts new file mode 100644 index 000000000..6c7f436f3 --- /dev/null +++ b/editors/code/src/test/utils/diagnotics/SuggestedFix.test.ts | |||
@@ -0,0 +1,133 @@ | |||
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 | return assert.fail('Code Action edit unexpectedly missing'); | ||
118 | } | ||
119 | |||
120 | const editEntries = edit.entries(); | ||
121 | assert.strictEqual(editEntries.length, 1); | ||
122 | |||
123 | const [[editUri, textEdits]] = editEntries; | ||
124 | assert.strictEqual(editUri.toString(), location1.uri.toString()); | ||
125 | |||
126 | assert.strictEqual(textEdits.length, 1); | ||
127 | const [textEdit] = textEdits; | ||
128 | |||
129 | assert(textEdit.range.isEqual(location1.range)); | ||
130 | assert.strictEqual(textEdit.newText, 'With this!'); | ||
131 | }); | ||
132 | }); | ||
133 | }); | ||