forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyamlCodeActions.test.ts
More file actions
187 lines (169 loc) · 7.94 KB
/
yamlCodeActions.test.ts
File metadata and controls
187 lines (169 loc) · 7.94 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import * as chai from 'chai';
import { YamlCodeActions } from '../src/languageservice/services/yamlCodeActions';
import {
ClientCapabilities,
CodeAction,
CodeActionContext,
CodeActionParams,
Command,
Range,
TextDocumentIdentifier,
TextEdit,
WorkspaceEdit,
} from 'vscode-languageserver';
import { setupTextDocument, TEST_URI } from './utils/testHelper';
import { createDiagnosticWithData, createExpectedError, createUnusedAnchorDiagnostic } from './utils/verifyError';
import { YamlCommands } from '../src/commands';
import { LanguageSettings } from '../src';
const expect = chai.expect;
chai.use(sinonChai);
const JSON_SCHEMA_LOCAL = 'file://some/path/schema.json';
const JSON_SCHEMA2_LOCAL = 'file://some/path/schema2.json';
describe('CodeActions Tests', () => {
const sandbox = sinon.createSandbox();
let clientCapabilities: ClientCapabilities;
beforeEach(() => {
clientCapabilities = {};
});
afterEach(() => {
sandbox.restore();
});
describe('JumpToSchema tests', () => {
it('should not provide any actions if there are no diagnostics', () => {
const doc = setupTextDocument('');
const params: CodeActionParams = {
context: CodeActionContext.create(undefined),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
expect(result).to.be.undefined;
});
it('should provide action if diagnostic has uri for schema', () => {
const doc = setupTextDocument('');
const diagnostics = [createDiagnosticWithData('foo', 0, 0, 0, 0, 1, JSON_SCHEMA_LOCAL, JSON_SCHEMA_LOCAL)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
clientCapabilities.window = { showDocument: { support: true } };
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
const codeAction = CodeAction.create(
'Jump to schema location (schema.json)',
Command.create('JumpToSchema', YamlCommands.JUMP_TO_SCHEMA, JSON_SCHEMA_LOCAL)
);
codeAction.diagnostics = diagnostics;
expect(result[0]).to.deep.equal(codeAction);
});
it('should provide multiple action if diagnostic has uri for multiple schemas', () => {
const doc = setupTextDocument('');
const diagnostics = [
createDiagnosticWithData('foo', 0, 0, 0, 0, 1, JSON_SCHEMA_LOCAL, [JSON_SCHEMA_LOCAL, JSON_SCHEMA2_LOCAL]),
];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
clientCapabilities.window = { showDocument: { support: true } };
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
const codeAction = CodeAction.create(
'Jump to schema location (schema.json)',
Command.create('JumpToSchema', YamlCommands.JUMP_TO_SCHEMA, JSON_SCHEMA_LOCAL)
);
const codeAction2 = CodeAction.create(
'Jump to schema location (schema2.json)',
Command.create('JumpToSchema', YamlCommands.JUMP_TO_SCHEMA, JSON_SCHEMA2_LOCAL)
);
codeAction.diagnostics = diagnostics;
codeAction2.diagnostics = diagnostics;
expect(result[0]).to.deep.equal(codeAction);
expect(result[1]).to.deep.equal(codeAction2);
});
});
describe('Convert TAB to Spaces', () => {
it('should add "Convert TAB to Spaces" CodeAction', () => {
const doc = setupTextDocument('foo:\n\t- bar');
const diagnostics = [createExpectedError('Using tabs can lead to unpredictable results', 1, 0, 1, 1, 1, JSON_SCHEMA_LOCAL)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
expect(result).to.has.length(2);
expect(result[0].title).to.be.equal('Convert Tab to Spaces');
expect(WorkspaceEdit.is(result[0].edit)).to.be.true;
expect(result[0].edit.changes[TEST_URI]).deep.equal([TextEdit.replace(Range.create(1, 0, 1, 1), ' ')]);
});
it('should support current indentation chars settings', () => {
const doc = setupTextDocument('foo:\n\t- bar');
const diagnostics = [createExpectedError('Using tabs can lead to unpredictable results', 1, 0, 1, 1, 1, JSON_SCHEMA_LOCAL)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
actions.configure({ indentation: ' ' } as LanguageSettings);
const result = actions.getCodeAction(doc, params);
expect(result[0].title).to.be.equal('Convert Tab to Spaces');
expect(result[0].edit.changes[TEST_URI]).deep.equal([TextEdit.replace(Range.create(1, 0, 1, 1), ' ')]);
});
it('should provide "Convert all Tabs to Spaces"', () => {
const doc = setupTextDocument('foo:\n\t\t\t- bar\n\t\t');
const diagnostics = [createExpectedError('Using tabs can lead to unpredictable results', 1, 0, 1, 3, 1, JSON_SCHEMA_LOCAL)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
expect(result[1].title).to.be.equal('Convert all Tabs to Spaces');
expect(result[1].edit.changes[TEST_URI]).deep.equal([
TextEdit.replace(Range.create(1, 0, 1, 3), ' '),
TextEdit.replace(Range.create(2, 0, 2, 2), ' '),
]);
});
});
describe('Remove Unused Anchor', () => {
it('should generate proper action', () => {
const doc = setupTextDocument('foo: &bar bar\n');
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
expect(result[0].title).to.be.equal('Delete unused anchor: &bar');
expect(result[0].edit.changes[TEST_URI]).deep.equal([TextEdit.del(Range.create(0, 5, 0, 10))]);
});
it('should delete all whitespace after unused anchor', () => {
const doc = setupTextDocument('foo: &bar \tbar\n');
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
textDocument: TextDocumentIdentifier.create(TEST_URI),
};
const actions = new YamlCodeActions(clientCapabilities);
const result = actions.getCodeAction(doc, params);
expect(result[0].title).to.be.equal('Delete unused anchor: &bar');
expect(result[0].edit.changes[TEST_URI]).deep.equal([TextEdit.del(Range.create(0, 5, 0, 13))]);
});
});
});