forked from microsoft/vscode-json-languageservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.test.ts
More file actions
196 lines (176 loc) · 6.54 KB
/
hover.test.ts
File metadata and controls
196 lines (176 loc) · 6.54 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
188
189
190
191
192
193
194
195
196
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Hover, Position, TextDocument, getLanguageService, JSONSchema, LanguageServiceParams } from '../jsonLanguageService';
suite('JSON Hover', () => {
async function testComputeInfo(value: string, schema: JSONSchema, position: Position, serviceParams?: LanguageServiceParams): Promise<Hover> {
const uri = 'test://test.json';
const schemaUri = "http://myschemastore/test1";
if (!serviceParams) {
serviceParams = { schemaRequestService: requestService };
}
const service = getLanguageService(serviceParams);
service.configure({ schemas: [{ fileMatch: ["*.json"], uri: schemaUri, schema }] });
const document = TextDocument.create(uri, 'json', 0, value);
const jsonDoc = service.parseJSONDocument(document);
const hover = await service.doHover(document, position, jsonDoc);
assert(hover, 'expected hover to be returned');
return hover;
}
let requestService = function (uri: string): Promise<string> {
return Promise.reject<string>('Resource not found');
};
test('Simple schema', async function () {
const content = '{"a": 42, "b": "hello", "c": false, "complex-description": false}';
const schema: JSONSchema = {
type: 'object',
description: 'a very special object',
properties: {
'a': {
type: 'number',
description: 'A'
},
'b': {
type: 'string',
description: 'B'
},
'c': {
type: 'boolean',
description: 'C'
},
'complex-description': {
type: 'boolean',
description: 'For example:\n\n<script>\n alert(1)\n</script>\n\n Test [1]'
},
}
};
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
assert.deepEqual(result.contents, ['a very special object']);
});
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
assert.deepEqual(result.contents, ['A']);
});
await testComputeInfo(content, schema, { line: 0, character: 32 }).then((result) => {
assert.deepEqual(result.contents, ['C']);
});
await testComputeInfo(content, schema, { line: 0, character: 37 }).then((result) => {
assert.deepEqual(result.contents, ['For example:\\\n\\\n\\<script\\>\\\n alert\\(1\\)\\\n\\</script\\>\\\n\\\n Test \\[1\\]']);
});
await testComputeInfo(content, schema, { line: 0, character: 7 }).then((result) => {
assert.deepEqual(result.contents, ['A']);
});
});
test('Nested schema', async function () {
const content = '{"a": 42, "b": "hello"}';
const schema: JSONSchema = {
oneOf: [{
type: 'object',
description: 'a very special object',
properties: {
'a': {
type: 'number',
description: 'A'
},
'b': {
type: 'string',
title: 'B',
description: 'It\'s B'
},
}
}, {
type: 'array'
}]
};
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
assert.deepEqual(result.contents, ['a very special object']);
});
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
assert.deepEqual(result.contents, ['A']);
});
await testComputeInfo(content, schema, { line: 0, character: 10 }).then((result) => {
assert.deepEqual(result.contents, ['B\n\nIt\'s B']);
});
});
test('Enum description', async function () {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop1': {
description: "prop1",
enum: ['e1', 'e2', 'e3'],
enumDescriptions: ['E1', 'E2', 'E3'],
},
'prop2': {
description: "prop2",
enum: [null, 1, false],
enumDescriptions: ['null', 'one', 'wrong'],
},
'prop3': {
title: "title",
markdownDescription: "*prop3*",
description: "prop3",
enum: [null, 1],
markdownEnumDescriptions: ['Set to `null`', 'Set to `1`'],
}
}
};
await testComputeInfo('{ "prop1": "e1', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['prop1\n\n`e1`: E1']);
});
await testComputeInfo('{ "prop2": null', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['prop2\n\n`null`: null']);
});
await testComputeInfo('{ "prop2": 1', schema, { line: 0, character: 11 }).then(result => {
assert.deepEqual(result.contents, ['prop2\n\n`1`: one']);
});
await testComputeInfo('{ "prop2": false', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['prop2\n\n`false`: wrong']);
});
await testComputeInfo('{ "prop3": null', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['title\n\n*prop3*\n\n`null`: Set to `null`']);
});
});
test('Multiline descriptions', async function () {
const schema: JSONSchema = {
type: 'object',
properties: {
'prop1': {
description: "line1\nline2\n\nline3\n\n\nline4\n",
},
'prop2': {
description: "line1\r\nline2\r\n\r\nline3",
}
}
};
await testComputeInfo('{ "prop1": "e1', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['line1\\\nline2\\\n\\\nline3\\\n\\\n\\\nline4']);
});
await testComputeInfo('{ "prop2": "e1', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['line1\r\\\nline2\r\\\n\r\\\nline3']);
});
});
test('Contribution descriptions', async function () {
const serviceParams: LanguageServiceParams = {
contributions: [{
async getInfoContribution(uri, location) {
return [`${uri}: ${location.join('/')}`];
},
collectDefaultCompletions(uri, results) {
return Promise.resolve();
},
collectPropertyCompletions(uri, location, currentWord, addValue, isLast, results) {
return Promise.resolve();
},
collectValueCompletions(uri, location, propertyKey, results) {
return Promise.resolve();
}
}]
};
let result = await testComputeInfo('{ "prop1": [ 1, false ] }', {}, { line: 0, character: 3 }, serviceParams);
assert.deepEqual(result.contents, ['test://test.json: prop1']);
result = await testComputeInfo('{ "prop1": [ 1, false ] }', {}, { line: 0, character: 13 }, serviceParams);
assert.deepEqual(result.contents, ['test://test.json: prop1/0']);
});
});