-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder.test.ts
More file actions
371 lines (290 loc) · 10.9 KB
/
builder.test.ts
File metadata and controls
371 lines (290 loc) · 10.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2025 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { beforeEach, describe, it } from "@std/testing/bdd";
import { ScopeInfoBuilder } from "./builder.ts";
import {
assertEquals,
assertNotStrictEquals,
assertStrictEquals,
} from "@std/assert";
describe("ScopeInfoBuilder", () => {
let builder: ScopeInfoBuilder;
beforeEach(() => {
builder = new ScopeInfoBuilder();
});
it("adds null OriginalScopes", () => {
const info = builder.addNullScope().addNullScope().build();
assertEquals(info.scopes, [null, null]);
});
it("builds simple OriginalScopes", () => {
const info = builder.startScope(0, 0).endScope(5, 10).build();
assertEquals(info.scopes[0]?.start, { line: 0, column: 0 });
assertEquals(info.scopes[0]?.end, { line: 5, column: 10 });
});
it("builds a simple nested OriginalScope", () => {
const info = builder.startScope(0, 0).startScope(5, 0).endScope(10, 0)
.endScope(15, 0).build();
assertStrictEquals(info.scopes[0]?.children.length, 1);
assertEquals(info.scopes[0].children[0].start, { line: 5, column: 0 });
assertEquals(info.scopes[0].children[0].end, { line: 10, column: 0 });
assertStrictEquals(info.scopes[0], info.scopes[0].children[0].parent);
});
describe("startScope", () => {
it("can set the name via option", () => {
const info = builder.startScope(0, 0, { name: "foo" }).endScope(5, 0)
.build();
assertStrictEquals(info.scopes[0]?.name, "foo");
});
it("can set kind via option", () => {
const info = builder.startScope(0, 0, { kind: "Global" }).endScope(10, 0)
.build();
assertStrictEquals(info.scopes[0]?.kind, "Global");
});
it("can set isStackFrame via option", () => {
const info = builder.startScope(0, 0, { isStackFrame: true }).endScope(
10,
0,
).build();
assertStrictEquals(info.scopes[0]?.isStackFrame, true);
});
it("can set variables via option", () => {
const info = builder.startScope(0, 0, { variables: ["a", "b"] }).endScope(
10,
0,
).build();
assertEquals(info.scopes[0]?.variables, ["a", "b"]);
});
it("copies the variables passed via options", () => {
const variables = ["a", "b"];
const info = builder.startScope(0, 0, { variables }).endScope(10, 0)
.build();
variables.push("c");
assertEquals(info.scopes[0]?.variables, ["a", "b"]);
});
});
describe("setScopeName", () => {
it("sets the name", () => {
const info = builder.startScope(0, 0).setScopeName("foo").endScope(5, 0)
.build();
assertStrictEquals(info.scopes[0]?.name, "foo");
});
it("does nothing when no scope is open", () => {
builder.setScopeName("ignored");
});
});
describe("setScopeKind", () => {
it("sets the kind", () => {
const info = builder.startScope(0, 0).setScopeKind("Global").endScope(
20,
0,
).build();
assertStrictEquals(info.scopes[0]?.kind, "Global");
});
it("does nothing when no scope is open", () => {
builder.setScopeKind("Function");
});
});
describe("setScopeStackFrame", () => {
it("sets the isStackFrame flag", () => {
const info = builder.startScope(0, 0).setScopeStackFrame(true).endScope(
10,
0,
)
.build();
assertStrictEquals(info.scopes[0]?.isStackFrame, true);
});
});
describe("setScopeVariables", () => {
it("sets variables", () => {
const info = builder.startScope(0, 0).setScopeVariables(["a", "b"])
.endScope(10, 0).build();
assertEquals(info.scopes[0]?.variables, ["a", "b"]);
});
it("creates a copy of the variables", () => {
const variables = ["a", "b"];
const info = builder.startScope(0, 0).setScopeVariables(variables)
.endScope(10, 0).build();
variables.push("c");
assertEquals(info.scopes[0]?.variables, ["a", "b"]);
});
});
describe("endScope", () => {
it("does nothing when no scope is open", () => {
builder.endScope(10, 0);
});
});
it("builds a simple generated range", () => {
const info = builder.startRange(0, 0).endRange(0, 20).build();
assertEquals(info.ranges[0]?.start, { line: 0, column: 0 });
assertEquals(info.ranges[0]?.end, { line: 0, column: 20 });
});
it("builds a simple nested range", () => {
const info = builder.startRange(0, 0).startRange(5, 0).endRange(10, 0)
.endRange(15, 0).build();
assertStrictEquals(info.ranges[0]?.children.length, 1);
assertEquals(info.ranges[0].children[0].start, { line: 5, column: 0 });
assertEquals(info.ranges[0].children[0].end, { line: 10, column: 0 });
assertStrictEquals(info.ranges[0], info.ranges[0].children[0].parent);
});
describe("startRange", () => {
it("sets the definition scope when it's provided as a number", () => {
const info = builder.startScope(0, 0, { key: 0 }).endScope(10, 0)
.startRange(0, 0, {
scopeKey: 0,
}).endRange(0, 10).build();
assertStrictEquals(info.scopes[0], info.ranges[0].originalScope);
});
it("sets the definition scope when it's provided directly", () => {
const scope = builder.startScope(0, 0).endScope(10, 0).lastScope();
const info = builder.startRange(0, 0, { scope: scope! }).endRange(0, 10)
.build();
assertStrictEquals(info.scopes[0], info.ranges[0].originalScope);
assertStrictEquals(info.ranges[0].originalScope, scope);
});
it("can set isStackFrame via option", () => {
const info = builder.startRange(0, 0, { isStackFrame: true }).endRange(
10,
0,
).build();
assertStrictEquals(info.ranges[0]?.isStackFrame, true);
});
it("can set isHidden via option", () => {
const info = builder.startRange(0, 0, { isHidden: true }).endRange(
10,
0,
).build();
assertStrictEquals(info.ranges[0]?.isHidden, true);
});
it("can set simple values via option", () => {
const info = builder.startRange(0, 0, { values: ["a", null, "b"] })
.endRange(0, 10).build();
assertEquals(info.ranges[0]?.values, ["a", null, "b"]);
});
it("can set the callSite position via option", () => {
const info = builder.startRange(0, 0, {
callSite: { line: 10, column: 20, sourceIndex: 0 },
}).endRange(0, 10).build();
assertEquals(info.ranges[0].callSite, {
line: 10,
column: 20,
sourceIndex: 0,
});
});
});
describe("setRangeDefinitionScope", () => {
it("sets the definition scope when it's provided directly", () => {
const scope = builder.startScope(0, 0).endScope(10, 0).lastScope()!;
const info = builder.startRange(0, 0).setRangeDefinitionScope(scope)
.endRange(0, 10).build();
assertStrictEquals(info.scopes[0], info.ranges[0].originalScope);
assertStrictEquals(info.ranges[0].originalScope, scope);
});
it("does nothing when no range is on the stack", () => {
const scope = builder.startScope(0, 0).endScope(10, 0).lastScope()!;
builder.setRangeDefinitionScope(scope);
});
});
describe("setRangeDefinitionScopeKey", () => {
it("sets the definition scope when it's provided directly", () => {
builder.startScope(0, 0, { key: "my key" }).endScope(10, 0);
const info = builder.startRange(0, 0).setRangeDefinitionScopeKey("my key")
.endRange(0, 10).build();
assertStrictEquals(info.ranges[0].originalScope, info.scopes[0]);
});
it("does nothing when no range is on the stack", () => {
builder.setRangeDefinitionScopeKey("foo");
});
});
describe("setRangeStackFrame", () => {
it("sets the isStackFrame flag", () => {
const info = builder.startRange(0, 0).setRangeStackFrame(true).endRange(
10,
0,
).build();
assertStrictEquals(info.ranges[0]?.isStackFrame, true);
});
});
describe("setRangeHidden", () => {
it("sets the isHidden flag", () => {
const info = builder.startRange(0, 0).setRangeHidden(true).endRange(
10,
0,
).build();
assertStrictEquals(info.ranges[0]?.isHidden, true);
});
});
describe("setRangeValues", () => {
it("sets the values", () => {
const info = builder.startRange(0, 0).setRangeValues(["a", null, null])
.endRange(0, 10).build();
assertEquals(info.ranges[0]?.values, ["a", null, null]);
});
it("does nothing when no range is on the stack", () => {
builder.setRangeValues(["a", null, "b"]);
});
});
describe("setRangeCallSite", () => {
it("sets the callSite", () => {
const info = builder.startRange(0, 0).setRangeCallSite({
line: 10,
column: 20,
sourceIndex: 0,
}).endRange(0, 10).build();
assertEquals(info.ranges[0].callSite, {
line: 10,
column: 20,
sourceIndex: 0,
});
});
it("does nothing when no range is on the stack", () => {
builder.setRangeCallSite({ line: 10, column: 20, sourceIndex: 0 });
});
});
describe("endRange", () => {
it("does nothing when no range is open", () => {
builder.endRange(0, 20);
});
});
describe("currentScope", () => {
it("returns 'null' when no scope is on the stack", () => {
assertStrictEquals(builder.currentScope(), null);
});
it("returns the currently open scope (top-level)", () => {
builder.startScope(0, 0);
assertNotStrictEquals(builder.currentScope(), null);
});
it("returns the currently open scope (nested)", () => {
builder.startScope(0, 0).startScope(10, 0);
assertEquals(builder.currentScope()?.start, { line: 10, column: 0 });
});
});
describe("lastScope", () => {
it("returns 'null' when no scope was closed yet", () => {
assertStrictEquals(builder.lastScope(), null);
});
it("returns the last closed scope (top-level)", () => {
builder.startScope(0, 0).endScope(10, 0);
assertNotStrictEquals(builder.lastScope(), null);
});
it("returns the last closed scope (nested)", () => {
builder.startScope(0, 0).startScope(10, 0).endScope(20, 0);
assertEquals(builder.lastScope()?.start, { line: 10, column: 0 });
});
it("returns the last closed scope after starting new ones", () => {
builder.startScope(0, 0).startScope(10, 0).endScope(20, 0).startScope(
30,
0,
);
assertEquals(builder.lastScope()?.start, { line: 10, column: 0 });
});
});
describe("scope key", () => {
it("can set the scope key via options", () => {
builder.startScope(0, 0, { key: "my custom key" }).endScope(10, 0);
builder.startRange(0, 0, { scopeKey: "my custom key" }).endRange(0, 10);
const info = builder.build();
assertStrictEquals(info.ranges[0].originalScope, info.scopes[0]);
});
});
});