-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
201 lines (169 loc) · 4.41 KB
/
test.js
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 assert from 'node:assert/strict'
import test from 'node:test'
import {VFile} from 'vfile'
import {location} from 'vfile-location'
test('location', async function () {
assert.deepEqual(
Object.keys(await import('vfile-location')).sort(),
['location'],
'should expose the public api'
)
let place = location('')
assert.equal(
typeof place.toOffset,
'function',
'should expose `toOffset` for `doc`'
)
assert.equal(
typeof place.toOffset,
'function',
'should expose `toPoint` for `doc`'
)
place = location(new VFile())
assert.equal(
typeof place.toOffset,
'function',
'should expose `toOffset` for `file`'
)
assert.equal(
typeof place.toOffset,
'function',
'should expose `toPoint` for `file`'
)
})
test('toOffset(point)', function () {
const place = location('foo\nbar\nbaz')
assert.equal(
place.toOffset({line: undefined, column: undefined}),
undefined,
'should return `undefined` for invalid input'
)
assert.equal(
place.toOffset({line: 4, column: 2}),
undefined,
'should return `undefined` for out of bounds input'
)
assert.equal(
place.toOffset({line: 2, column: 2}),
5,
'should return an offset (#1)'
)
assert.equal(
place.toOffset({line: 1, column: 1}),
0,
'should return an offset (#2)'
)
assert.equal(
place.toOffset({line: 3, column: 4}),
11,
'should return an offset (#3)'
)
assert.equal(
location('').toOffset({line: 1, column: 1}),
0,
'should support empty document'
)
})
test('toPoint(offset)', function () {
const place = location('foo\nbar\nbaz')
assert.deepEqual(
place.toPoint(-1),
undefined,
'should return nothing for invalid input'
)
assert.deepEqual(
place.toPoint(12),
undefined,
'should return nothing for out of bounds input'
)
assert.deepEqual(
place.toPoint(0),
{line: 1, column: 1, offset: 0},
'should return a point (#1)'
)
assert.deepEqual(
place.toPoint(11),
{line: 3, column: 4, offset: 11},
'should return a point (#2)'
)
assert.deepEqual(
location('').toPoint(0),
{line: 1, column: 1, offset: 0},
'should support empty document'
)
})
test('other tests', function () {
let place = location('foo')
assert.deepEqual(
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[{line: 1, column: 4, offset: 3}, undefined, undefined],
'should return points for offsets around an EOF w/o EOLs'
)
assert.deepEqual(
[
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, undefined, undefined],
'should return offsets for points around an EOF w/o EOLs'
)
place = location('foo\n')
assert.deepEqual(
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[
{line: 1, column: 4, offset: 3},
{line: 2, column: 1, offset: 4},
undefined
],
'should return points for offsets around an EOF EOL'
)
assert.deepEqual(
[
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 4, undefined],
'should return offsets for points around an EOF EOL'
)
place = location('foo\rbar')
assert.deepEqual(
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[
{line: 1, column: 4, offset: 3},
{line: 2, column: 1, offset: 4},
{line: 2, column: 2, offset: 5}
],
'should return points for offsets around carriage returns'
)
assert.deepEqual(
[
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 4, 5],
'should return offsets for points around carriage returns'
)
place = location('foo\r\nbar')
assert.deepEqual(
[place.toPoint(3), place.toPoint(4), place.toPoint(5), place.toPoint(6)],
[
{line: 1, column: 4, offset: 3},
{line: 1, column: 5, offset: 4},
{line: 2, column: 1, offset: 5},
{line: 2, column: 2, offset: 6}
],
'should return points for offsets around carriage returns + line feeds'
)
assert.deepEqual(
[
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 5, 6],
'should return offsets for points around carriage returns + line feeds'
)
})