55
66import * as assert from 'assert' ;
77
8- import { getLanguageService , Range , TextDocument , ClientCapabilities } from '../jsonLanguageService' ;
8+ import {
9+ ClientCapabilities ,
10+ DocumentLink ,
11+ getLanguageService ,
12+ JSONSchema ,
13+ Range ,
14+ TextDocument ,
15+ } from '../jsonLanguageService' ;
16+ import { resolve as resolvePath } from 'path' ;
917
1018suite ( 'JSON Find Links' , ( ) => {
1119 const testFindLinksFor = function ( value : string , expected : { offset : number , length : number , target : number } | null ) : PromiseLike < void > {
@@ -26,6 +34,20 @@ suite('JSON Find Links', () => {
2634 } ) ;
2735 } ;
2836
37+ let requestService = function ( uri : string ) : Promise < string > {
38+ return Promise . reject < string > ( 'Resource not found' ) ;
39+ } ;
40+
41+ function testFindLinksWithSchema ( document : TextDocument , schema : JSONSchema ) : PromiseLike < DocumentLink [ ] > {
42+ const schemaUri = "http://myschemastore/test1" ;
43+
44+ const ls = getLanguageService ( { clientCapabilities : ClientCapabilities . LATEST } ) ;
45+ ls . configure ( { schemas : [ { fileMatch : [ "*.json" ] , uri : schemaUri , schema } ] } ) ;
46+ const jsonDoc = ls . parseJSONDocument ( document ) ;
47+
48+ return ls . findLinks ( document , jsonDoc ) ;
49+ }
50+
2951 test ( 'FindDefinition invalid ref' , async function ( ) {
3052 await testFindLinksFor ( '{}' , null ) ;
3153 await testFindLinksFor ( '{"name": "John"}' , null ) ;
@@ -52,4 +74,38 @@ suite('JSON Find Links', () => {
5274 await testFindLinksFor ( doc ( '#/ ' ) , { target : 81 , offset : 102 , length : 3 } ) ;
5375 await testFindLinksFor ( doc ( '#/m~0n' ) , { target : 90 , offset : 102 , length : 6 } ) ;
5476 } ) ;
77+
78+ test ( 'URI reference link' , async function ( ) {
79+ const relativePath = './src/test/fixtures/uri-reference.txt' ;
80+ const content = `{"stringProp": "string-value", "uriProp": "${ relativePath } ", "uriPropNotFound": "./does/not/exist.txt"}` ;
81+ const document = TextDocument . create ( 'test://test.json' , 'json' , 0 , content ) ;
82+ const schema : JSONSchema = {
83+ type : 'object' ,
84+ properties : {
85+ 'stringProp' : {
86+ type : 'string' ,
87+ } ,
88+ 'uriProp' : {
89+ type : 'string' ,
90+ format : 'uri-reference'
91+ } ,
92+ 'uriPropNotFound' : {
93+ type : 'string' ,
94+ format : 'uri-reference'
95+ }
96+ }
97+ } ;
98+ await testFindLinksWithSchema ( document , schema ) . then ( ( links ) => {
99+ assert . notDeepEqual ( links , [ ] ) ;
100+
101+ const absPath = resolvePath ( relativePath ) ;
102+ assert . equal ( links [ 0 ] . target , `file://${ absPath } ` ) ;
103+
104+ const startOffset = content . indexOf ( relativePath ) ;
105+ const endOffset = startOffset + relativePath . length ;
106+ const range = Range . create ( document . positionAt ( startOffset ) , document . positionAt ( endOffset ) ) ;
107+ assert . deepEqual ( links [ 0 ] . range , range ) ;
108+ } ) ;
109+ } ) ;
110+
55111} ) ;
0 commit comments