forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontains.spec.js
More file actions
324 lines (275 loc) · 9.9 KB
/
Copy pathcontains.spec.js
File metadata and controls
324 lines (275 loc) · 9.9 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import {
containsPosition,
containsLocation,
nodeContainsPosition
} from "../utils/contains";
function getTestLoc() {
return {
start: {
line: 10,
column: 2
},
end: {
line: 12,
column: 10
}
};
}
// AstPosition.column is typed as a number, but many parts of this test set it
// to undefined. Using zero instead causes test failures, and allowing it to be
// undefined causes many flow errors in code manipulating AstPosition.
// Fake a coercion of undefined to number as a workaround for now.
function undefinedColumn(): number {
return (undefined: any);
}
function startPos(lineOffset, columnOffset) {
const { start } = getTestLoc();
return {
line: start.line + lineOffset,
column: start.column + columnOffset
};
}
function endPos(lineOffset, columnOffset) {
const { end } = getTestLoc();
return {
line: end.line + lineOffset,
column: end.column + columnOffset
};
}
function startLine(lineOffset = 0) {
const { start } = getTestLoc();
return {
line: start.line + lineOffset,
column: undefinedColumn()
};
}
function endLine(lineOffset = 0) {
const { end } = getTestLoc();
return {
line: end.line + lineOffset,
column: undefinedColumn()
};
}
function testContains(pos, bool) {
const loc = getTestLoc();
expect(containsPosition(loc, pos)).toEqual(bool);
}
function testContainsPosition(pos, bool) {
const loc = getTestLoc();
expect(nodeContainsPosition({ loc }, pos)).toEqual(bool);
}
describe("containsPosition", () => {
describe("location and postion both with the column criteria", () => {
it("should contain position within the location range", () =>
testContains(startPos(1, 1), true));
it("should not contain position out of the start line", () =>
testContains(startPos(-1, 0), false));
it("should not contain position out of the start column", () =>
testContains(startPos(0, -1), false));
it(`should contain position on the same start line and
within the start column`, () => testContains(startPos(0, 1), true));
it("should not contain position out of the end line", () =>
testContains(endPos(1, 0), false));
it("should not contain position out of the end column", () =>
testContains(endPos(0, 1), false));
// eslint-disable-next-line max-len
it("should contain position on the same end line and within the end column", () =>
testContains(endPos(0, -1), true));
});
describe("position without the column criterion", () => {
it("should contain position on the same start line", () =>
testContains(startLine(0), true));
it("should contain position on the same end line", () =>
testContains(endLine(0), true));
});
describe("location without the column criterion", () => {
it("should contain position on the same start line", () => {
const loc = getTestLoc();
loc.start.column = undefinedColumn();
const pos = {
line: loc.start.line,
column: 1
};
expect(containsPosition(loc, pos)).toEqual(true);
});
it("should contain position on the same end line", () => {
const loc = getTestLoc();
loc.end.column = undefinedColumn();
const pos = {
line: loc.end.line,
column: 1
};
expect(containsPosition(loc, pos)).toEqual(true);
});
});
describe("location and postion both without the column criterion", () => {
it("should contain position on the same start line", () => {
const loc = getTestLoc();
loc.start.column = undefinedColumn();
const pos = startLine();
expect(containsPosition(loc, pos)).toEqual(true);
});
it("should contain position on the same end line", () => {
const loc = getTestLoc();
loc.end.column = undefinedColumn();
const pos = endLine();
expect(containsPosition(loc, pos)).toEqual(true);
});
});
});
describe("containsLocation", () => {
describe("locations both with the column criteria", () => {
it("should contian location within the range", () => {
const locA = getTestLoc();
const locB = {
start: startPos(1, 1),
end: endPos(-1, -1)
};
expect(containsLocation(locA, locB)).toEqual(true);
});
it("should not contian location out of the start line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.start.line--;
expect(containsLocation(locA, locB)).toEqual(false);
});
it("should not contian location out of the start column", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.start.column--;
expect(containsLocation(locA, locB)).toEqual(false);
});
it("should not contian location out of the end line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.end.line++;
expect(containsLocation(locA, locB)).toEqual(false);
});
it("should not contian location out of the end column", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.end.column++;
expect(containsLocation(locA, locB)).toEqual(false);
});
it(`should contain location on the same start line and
within the start column`, () => {
const locA = getTestLoc();
const locB = {
start: startPos(0, 1),
end: endPos(-1, -1)
};
expect(containsLocation(locA, locB)).toEqual(true);
});
it(`should contain location on the same end line and
within the end column`, () => {
const locA = getTestLoc();
const locB = {
start: startPos(1, 1),
end: endPos(0, -1)
};
expect(containsLocation(locA, locB)).toEqual(true);
});
});
describe("location A without the column criterion", () => {
it("should contain location on the same start line", () => {
const locA = getTestLoc();
locA.start.column = undefinedColumn();
const locB = getTestLoc();
expect(containsLocation(locA, locB)).toEqual(true);
});
it("should contain location on the same end line", () => {
const locA = getTestLoc();
locA.end.column = undefinedColumn();
const locB = getTestLoc();
expect(containsLocation(locA, locB)).toEqual(true);
});
});
describe("location B without the column criterion", () => {
it("should contain location on the same start line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.start.column = undefinedColumn();
expect(containsLocation(locA, locB)).toEqual(true);
});
it("should contain location on the same end line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locB.end.column = undefinedColumn();
expect(containsLocation(locA, locB)).toEqual(true);
});
});
describe("locations both without the column criteria", () => {
it("should contain location on the same start line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locA.start.column = undefinedColumn();
locB.start.column = undefinedColumn();
expect(containsLocation(locA, locB)).toEqual(true);
});
it("should contain location on the same end line", () => {
const locA = getTestLoc();
const locB = getTestLoc();
locA.end.column = undefinedColumn();
locB.end.column = undefinedColumn();
expect(containsLocation(locA, locB)).toEqual(true);
});
});
});
describe("nodeContainsPosition", () => {
describe("node and position both with the column criteria", () => {
it("should contian position within the range", () =>
testContainsPosition(startPos(1, 1), true));
it("should not contian position out of the start line", () =>
testContainsPosition(startPos(-1, 0), false));
it("should not contian position out of the start column", () =>
testContainsPosition(startPos(0, -1), false));
it("should not contian position out of the end line", () =>
testContainsPosition(endPos(1, 0), false));
it("should not contian position out of the end column", () =>
testContainsPosition(endPos(0, 1), false));
it(`should contain position on the same start line and
within the start column`, () =>
testContainsPosition(startPos(0, 1), true));
it(`should contain position on the same end line and
within the end column`, () =>
testContainsPosition(endPos(0, -1), true));
});
describe("node without the column criterion", () => {
it("should contain position on the same start line", () => {
const loc = getTestLoc();
loc.start.column = undefinedColumn();
const pos = startPos(0, -1);
expect(nodeContainsPosition({ loc }, pos)).toEqual(true);
});
it("should contain position on the same end line", () => {
const loc = getTestLoc();
loc.end.column = undefinedColumn();
const pos = startPos(0, 1);
expect(nodeContainsPosition({ loc }, pos)).toEqual(true);
});
});
describe("position without the column criterion", () => {
it("should contain position on the same start line", () =>
testContainsPosition(startLine(), true));
it("should contain position on the same end line", () =>
testContainsPosition(endLine(), true));
});
describe("node and position both without the column criteria", () => {
it("should contain position on the same start line", () => {
const loc = getTestLoc();
loc.start.column = undefinedColumn();
const pos = startLine();
expect(nodeContainsPosition({ loc }, pos)).toEqual(true);
});
it("should contain position on the same end line", () => {
const loc = getTestLoc();
loc.end.column = undefinedColumn();
const pos = endLine();
expect(nodeContainsPosition({ loc }, pos)).toEqual(true);
});
});
});