-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathyamlCodeLens.test.ts
188 lines (174 loc) · 7.38 KB
/
yamlCodeLens.test.ts
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
188
/*---------------------------------------------------------------------------------------------
* 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 { YamlCodeLens } from '../src/languageservice/services/yamlCodeLens';
import { YAMLSchemaService } from '../src/languageservice/services/yamlSchemaService';
import { setupTextDocument } from './utils/testHelper';
import { JSONSchema } from '../src/languageservice/jsonSchema';
import { CodeLens, Command, Range } from 'vscode-languageserver-protocol';
import { YamlCommands } from '../src/commands';
import { TelemetryImpl } from '../src/languageserver/telemetry';
import { Telemetry } from '../src/languageservice/yamlLanguageService';
const expect = chai.expect;
chai.use(sinonChai);
describe('YAML CodeLens', () => {
const sandbox = sinon.createSandbox();
let yamlSchemaService: sinon.SinonStubbedInstance<YAMLSchemaService>;
let telemetryStub: sinon.SinonStubbedInstance<TelemetryImpl>;
let telemetry: Telemetry;
beforeEach(() => {
yamlSchemaService = sandbox.createStubInstance(YAMLSchemaService);
telemetryStub = sandbox.createStubInstance(TelemetryImpl);
telemetry = telemetryStub;
});
afterEach(() => {
sandbox.restore();
});
function createCommand(title: string, command: string, arg: string): Command {
return {
title,
command,
arguments: [arg],
};
}
function createCodeLens(title: string, command: string, arg: string): CodeLens {
const lens = CodeLens.create(Range.create(0, 0, 0, 0));
lens.command = createCommand(title, command, arg);
return lens;
}
it('should provides CodeLens with jumpToSchema command', async () => {
const doc = setupTextDocument('foo: bar');
const schema: JSONSchema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).is.not.empty;
expect(result[0].command).is.not.undefined;
expect(result[0].command).is.deep.equal(
createCommand('schema.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});
it('should place CodeLens at beginning of the file and it has command', async () => {
const doc = setupTextDocument('foo: bar');
const schema: JSONSchema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].range).is.deep.equal(Range.create(0, 0, 0, 0));
expect(result[0].command).is.deep.equal(
createCommand('schema.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});
it('should place one CodeLens at beginning of the file for multiple documents', async () => {
const doc = setupTextDocument('foo: bar\n---\nfoo: bar');
const schema: JSONSchema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result.length).to.eq(1);
expect(result[0].range).is.deep.equal(Range.create(0, 0, 0, 0));
expect(result[0].command).is.deep.equal(
createCommand('schema.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});
it('command name should contains schema title', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
title: 'fooBar',
} as JSONSchema;
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].command).is.deep.equal(
createCommand('fooBar (schema.json)', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});
it('command name should contains schema title and description', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
url: 'some://url/to/schema.json',
title: 'fooBar',
description: 'fooBarDescription',
} as JSONSchema;
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result[0].command).is.deep.equal(
createCommand('fooBar - fooBarDescription (schema.json)', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});
it('should provide lens for oneOf schemas', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
oneOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
} as JSONSchema;
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
it('should provide lens for allOf schemas', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
allOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
} as JSONSchema;
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
it('should provide lens for anyOf schemas', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
anyOf: [
{
url: 'some://url/schema1.json',
},
{
url: 'some://url/schema2.json',
},
],
} as JSONSchema;
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens(yamlSchemaService as unknown as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc);
expect(result).has.length(2);
expect(result).is.deep.equal([
createCodeLens('schema1.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema1.json'),
createCodeLens('schema2.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/schema2.json'),
]);
});
});