This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathutil-test.ts
More file actions
201 lines (200 loc) · 7.26 KB
/
util-test.ts
File metadata and controls
201 lines (200 loc) · 7.26 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
197
198
199
200
201
import * as assert from 'assert'
import {
getMatchingPropertyCount,
getPropertyCount,
isGlobalTSFile,
isSymbolDescriptorMatch,
JSONPTR,
path2uri,
uri2path,
} from '../util'
describe('util', () => {
describe('JSONPTR', () => {
it('should escape JSON Pointer components', () => {
const uri = 'file:///foo/~bar'
const pointer = JSONPTR`/changes/${uri}/-`
assert.equal(pointer, '/changes/file:~1~1~1foo~1~0bar/-')
})
})
describe('getMatchingPropertyCount()', () => {
it('should return a score of 4 if 4 properties match', () => {
const score = getMatchingPropertyCount(
{
containerName: 'ts',
kind: 'interface',
name: 'Program',
package: undefined,
},
{
containerKind: 'module',
containerName: 'ts',
kind: 'interface',
name: 'Program',
package: undefined,
}
)
assert.equal(score, 4)
})
it('should return a score of 0.6 if a string property is 60% similar', () => {
const score = getMatchingPropertyCount(
{
filePath: 'lib/foo.d.ts',
},
{
filePath: 'src/foo.ts',
}
)
assert.equal(score, 0.6)
})
it('should return a score of 4 if 4 properties match and 1 does not', () => {
const score = getMatchingPropertyCount(
{
containerKind: '',
containerName: 'util',
kind: 'var',
name: 'colors',
package: undefined,
},
{
containerKind: '',
containerName: '',
kind: 'var',
name: 'colors',
package: undefined,
}
)
assert.equal(score, 4)
})
it('should return a score of 3 if 3 properties match deeply', () => {
const score = getMatchingPropertyCount(
{
name: 'a',
kind: 'class',
package: { name: 'mypkg' },
containerKind: undefined,
},
{
kind: 'class',
name: 'a',
containerKind: '',
containerName: '',
package: { name: 'mypkg' },
}
)
assert.equal(score, 3)
})
})
describe('getPropertyCount()', () => {
it('should return the amount of leaf properties', () => {
const count = getPropertyCount({
name: 'a', // 1
kind: 'class', // 2
package: {
name: 'mypkg', // 3
},
containerKind: '', // 4
})
assert.equal(count, 4)
})
})
describe('isSymbolDescriptorMatch()', () => {
it('should return true for a matching query', () => {
const matches = isSymbolDescriptorMatch(
{
containerKind: undefined,
containerName: 'ts',
kind: 'interface',
name: 'Program',
filePath: 'foo/bar.ts',
package: undefined,
},
{
containerKind: 'module',
containerName: 'ts',
kind: 'interface',
name: 'Program',
filePath: 'foo/bar.ts',
package: undefined,
}
)
assert.equal(matches, true)
})
it('should return true for a matching query with PackageDescriptor', () => {
const matches = isSymbolDescriptorMatch(
{
name: 'a',
kind: 'class',
package: { name: 'mypkg' },
filePath: 'foo/bar.ts',
containerKind: undefined,
},
{
kind: 'class',
name: 'a',
containerKind: '',
containerName: '',
filePath: 'foo/bar.ts',
package: { name: 'mypkg' },
}
)
assert.equal(matches, true)
})
})
describe('isGlobalTSFile()', () => {
it('should match the synthetic reference to tsdlib when using importHelpers', () => {
assert.equal(isGlobalTSFile('/node_modules/tslib/tslib.d.ts'), true)
})
it('should not include non-declaration files', () => {
assert.equal(isGlobalTSFile('/node_modules/@types/node/Readme.MD'), false)
})
it('should include some libraries from @types with global declarations', () => {
assert.equal(isGlobalTSFile('/node_modules/@types/node/index.d.ts'), true)
assert.equal(isGlobalTSFile('/node_modules/@types/jest/index.d.ts'), true)
assert.equal(isGlobalTSFile('/node_modules/@types/jasmine/index.d.ts'), true)
assert.equal(isGlobalTSFile('/node_modules/@types/mocha/index.d.ts'), true)
})
})
describe('path2uri()', () => {
it('should throw an error if a non-absolute uri is passed in', () => {
assert.throws(() => path2uri('baz/qux'))
})
it('should convert a Unix file path to a URI', () => {
const uri = path2uri('/baz/qux')
assert.equal(uri, 'file:///baz/qux')
})
it('should convert a Windows file path to a URI', () => {
const uri = path2uri('C:\\baz\\qux')
assert.equal(uri, 'file:///C:/baz/qux')
})
it('should encode special characters', () => {
const uri = path2uri('/💩')
assert.equal(uri, 'file:///%F0%9F%92%A9')
})
it('should encode unreserved special characters', () => {
const uri = path2uri('/@baz')
assert.equal(uri, 'file:///%40baz')
})
})
describe('uri2path()', () => {
it('should convert a Unix file URI to a file path', () => {
const filePath = uri2path('file:///baz/qux')
assert.equal(filePath, '/baz/qux')
})
it('should convert a Windows file URI to a file path', () => {
const filePath = uri2path('file:///c:/baz/qux')
assert.equal(filePath, 'c:\\baz\\qux')
})
it('should convert a Windows file URI with uppercase drive letter to a file path', () => {
const filePath = uri2path('file:///C:/baz/qux')
assert.equal(filePath, 'C:\\baz\\qux')
})
it('should decode special characters', () => {
const filePath = uri2path('file:///%F0%9F%92%A9')
assert.equal(filePath, '/💩')
})
it('should decode unreserved special characters', () => {
const filePath = uri2path('file:///%40foo')
assert.equal(filePath, '/@foo')
})
})
})