|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | + |
| 8 | +import { getLanguageService, JSONSchema, TextDocument, ClientCapabilities, CompletionList, CompletionItemKind, Position, MarkupContent } from '../jsonLanguageService'; |
| 9 | +import { repeat } from '../utils/strings'; |
| 10 | +import { DefinitionLink } from 'vscode-languageserver-types'; |
| 11 | + |
| 12 | +suite('JSON Find Definitions', () => { |
| 13 | + const testFindDefinitionFor = function (value: string, expected: {offset: number, length: number} | null): PromiseLike<void> { |
| 14 | + const offset = value.indexOf('|'); |
| 15 | + value = value.substr(0, offset) + value.substr(offset + 1); |
| 16 | + |
| 17 | + const ls = getLanguageService({ clientCapabilities: ClientCapabilities.LATEST }); |
| 18 | + const document = TextDocument.create('test://test/test.json', 'json', 0, value); |
| 19 | + const position = Position.create(0, offset); |
| 20 | + const jsonDoc = ls.parseJSONDocument(document); |
| 21 | + return ls.findDefinition(document, position, jsonDoc).then(list => { |
| 22 | + if (expected) { |
| 23 | + assert.notDeepEqual(list, []); |
| 24 | + const startOffset = list[0].targetRange.start.character; |
| 25 | + assert.equal(startOffset, expected.offset); |
| 26 | + assert.equal(list[0].targetRange.end.character - startOffset, expected.length); |
| 27 | + } else { |
| 28 | + assert.deepEqual(list, []); |
| 29 | + } |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + test('FindDefinition invalid ref', async function () { |
| 34 | + await testFindDefinitionFor('{|}', null); |
| 35 | + await testFindDefinitionFor('{"name": |"John"}', null); |
| 36 | + await testFindDefinitionFor('{"|name": "John"}', null); |
| 37 | + await testFindDefinitionFor('{"name": "|John"}', null); |
| 38 | + await testFindDefinitionFor('{"name": "John", "$ref": "#/|john/name"}', null); |
| 39 | + await testFindDefinitionFor('{"name": "John", "$ref|": "#/name"}', null); |
| 40 | + await testFindDefinitionFor('{"name": "John", "$ref": "#/|"}', null); |
| 41 | + }); |
| 42 | + |
| 43 | + test('FindDefinition valid ref', async function () { |
| 44 | + await testFindDefinitionFor('{"name": "John", "$ref": "#/n|ame"}', {offset: 9, length: 6}); |
| 45 | + await testFindDefinitionFor('{"name": "John", "$ref": "|#/name"}', {offset: 9, length: 6}); |
| 46 | + await testFindDefinitionFor('{"name": "John", "$ref": |"#/name"}', {offset: 9, length: 6}); |
| 47 | + await testFindDefinitionFor('{"name": "John", "$ref": "#/name"|}', {offset: 9, length: 6}); |
| 48 | + await testFindDefinitionFor('{"name": "John", "$ref": "#/name|"}', {offset: 9, length: 6}); |
| 49 | + await testFindDefinitionFor('{"name": "John", "$ref": "#|"}', {offset: 0, length: 29}); |
| 50 | + |
| 51 | + const doc = (ref: string) => `{"foo": ["bar", "baz"],"": 0,"a/b": 1,"c%d": 2,"e^f": 3,"i\\\\j": 5,"k\\"l": 6," ": 7,"m~n": 8, "$ref": "|${ref}"}`; |
| 52 | + await testFindDefinitionFor(doc('#'), {offset: 0, length: 105}); |
| 53 | + await testFindDefinitionFor(doc('#/foo'), {offset: 8, length: 14}); |
| 54 | + await testFindDefinitionFor(doc('#/foo/0'), {offset: 9, length: 5}); |
| 55 | + await testFindDefinitionFor(doc('#/foo/1'), {offset: 16, length: 5}); |
| 56 | + await testFindDefinitionFor(doc('#/foo/01'), null); |
| 57 | + await testFindDefinitionFor(doc('#/'), {offset: 27, length: 1}); |
| 58 | + await testFindDefinitionFor(doc('#/a~1b'), {offset: 36, length: 1}); |
| 59 | + await testFindDefinitionFor(doc('#/c%d'), {offset: 45, length: 1}); |
| 60 | + await testFindDefinitionFor(doc('#/e^f'), {offset: 54, length: 1}); |
| 61 | + await testFindDefinitionFor(doc('#/i\\\\j'), {offset: 64, length: 1}); |
| 62 | + await testFindDefinitionFor(doc('#/k\\"l'), {offset: 74, length: 1}); |
| 63 | + await testFindDefinitionFor(doc('#/ '), {offset: 81, length: 1}); |
| 64 | + await testFindDefinitionFor(doc('#/m~0n'), {offset: 90, length: 1}); |
| 65 | + }); |
| 66 | +}); |
0 commit comments