-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathlabel-templates.test.ts
More file actions
353 lines (319 loc) · 13.3 KB
/
Copy pathlabel-templates.test.ts
File metadata and controls
353 lines (319 loc) · 13.3 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
/* 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/. */
import {
expandPattern,
reverseModifier,
reverseBlinkSnake,
compilePatternToRegex,
discoverAutoLabels,
resolveAllLabels,
} from 'firefox-profiler/utils/label-templates';
import type { AutoLabel } from 'firefox-profiler/utils/label-templates';
// ---------------------------------------------------------------------------
// expandPattern
// ---------------------------------------------------------------------------
describe('expandPattern', function () {
it('substitutes a plain variable', function () {
expect(
expandPattern('{Class}.{method}', {
Class: 'Element',
method: 'querySelector',
})
).toBe('Element.querySelector');
});
it('leaves unrelated text intact', function () {
expect(expandPattern('no_vars_here', {})).toBe('no_vars_here');
});
it('throws when a referenced variable is not provided', function () {
expect(() =>
expandPattern('{Class}.{method}', { Class: 'Element' })
).toThrow('Template variable "method" not provided');
});
it('throws when a modifier is used in a label template', function () {
expect(() =>
expandPattern('{Class:blink_snake}', { Class: 'Element' })
).toThrow('Template modifier ":blink_snake" is not supported');
});
});
// ---------------------------------------------------------------------------
// reverseSnake / reverseModifier
// ---------------------------------------------------------------------------
describe('reverseBlinkSnake', function () {
it('reverses a single Pascal word', function () {
expect(reverseBlinkSnake('element')).toBe('Element');
});
it('reverses a multi-word PascalCase name', function () {
expect(reverseBlinkSnake('document_fragment')).toBe('DocumentFragment');
expect(reverseBlinkSnake('event_target')).toBe('EventTarget');
});
it('uses the special-tokens list to recover all-uppercase fragments', function () {
expect(reverseBlinkSnake('html_element')).toBe('HTMLElement');
expect(reverseBlinkSnake('html_image_element')).toBe('HTMLImageElement');
expect(reverseBlinkSnake('css_style_sheet')).toBe('CSSStyleSheet');
expect(reverseBlinkSnake('xml_http_request')).toBe('XmlHttpRequest');
});
it('uses the special-tokens list to recover mixed-case fragments', function () {
expect(reverseBlinkSnake('webgl_rendering_context')).toBe(
'WebGLRenderingContext'
);
expect(reverseBlinkSnake('webgl2_rendering_context')).toBe(
'WebGL2RenderingContext'
);
expect(reverseBlinkSnake('xpath_evaluator')).toBe('XPathEvaluator');
});
it('handles trailing digit-bearing tokens', function () {
expect(reverseBlinkSnake('canvas_rendering_context_2d')).toBe(
'CanvasRenderingContext2D'
);
expect(reverseBlinkSnake('canvas_2d')).toBe('Canvas2D');
});
});
describe('reverseModifier', function () {
it('reverses :pascal by lowercasing the first letter', function () {
expect(reverseModifier('QuerySelector', 'pascal')).toBe('querySelector');
expect(reverseModifier('Fill', 'pascal')).toBe('fill');
});
it('reverses :blink_snake using the special-tokens list', function () {
expect(reverseModifier('html_input_element', 'blink_snake')).toBe(
'HTMLInputElement'
);
});
it('returns the value unchanged when no modifier is given', function () {
expect(reverseModifier('Element', undefined)).toBe('Element');
});
it('throws on an unknown modifier', function () {
expect(() => reverseModifier('foo', 'upper')).toThrow(
'Unknown template modifier: upper'
);
});
});
// ---------------------------------------------------------------------------
// compilePatternToRegex
// ---------------------------------------------------------------------------
describe('compilePatternToRegex', function () {
it('compiles a Mozilla-style operation pattern', function () {
const { regex, vars } = compilePatternToRegex(
'mozilla::dom::{Class}_Binding::{method}('
);
expect(vars).toEqual([
{ name: 'Class', modifier: undefined },
{ name: 'method', modifier: undefined },
]);
const m = 'mozilla::dom::Element_Binding::querySelector(args)'.match(regex);
expect(m).not.toBeNull();
expect(m![1]).toBe('Element');
expect(m![2]).toBe('querySelector');
});
it('compiles a Blink-style pattern with :blink_snake and :pascal', function () {
const { regex, vars } = compilePatternToRegex(
'blink::(anonymous namespace)::v8_{Class:blink_snake}::{method:pascal}Operation'
);
expect(vars).toEqual([
{ name: 'Class', modifier: 'blink_snake' },
{ name: 'method', modifier: 'pascal' },
]);
const m =
'blink::(anonymous namespace)::v8_html_image_element::SetSrcOperation'.match(
regex
);
expect(m).not.toBeNull();
expect(m![1]).toBe('html_image_element');
expect(m![2]).toBe('SetSrc');
});
it('compiles a WebKit-style pattern', function () {
const { regex } = compilePatternToRegex(
'WebCore::js{Class}PrototypeFunction_{method}'
);
const m = 'WebCore::jsElementPrototypeFunction_querySelector(args)'.match(
regex
);
expect(m).not.toBeNull();
expect(m![1]).toBe('Element');
expect(m![2]).toBe('querySelector');
});
it('refuses to match when the literal text does not appear', function () {
const { regex } = compilePatternToRegex(
'mozilla::dom::{Class}_Binding::{method}('
);
expect('mozilla::dom::Element_Other::foo('.match(regex)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// discoverAutoLabels / resolveAllLabels
// ---------------------------------------------------------------------------
const DOM_OPERATION_AUTO: AutoLabel = {
nameTemplate: '{Class}.{method}',
funcPrefixTemplates: [
'mozilla::dom::{Class}_Binding::{method}(',
"blink::`anonymous namespace'::v8_{Class:blink_snake}::{method:pascal}Operation",
'blink::(anonymous namespace)::v8_{Class:blink_snake}::{method:pascal}Operation',
'WebCore::js{Class}PrototypeFunction_{method}',
],
};
const DOM_SETTER_AUTO: AutoLabel = {
nameTemplate: 'set {Class}.{prop}',
funcPrefixTemplates: [
'mozilla::dom::{Class}_Binding::set_{prop}(',
"blink::`anonymous namespace'::v8_{Class:blink_snake}::{prop:pascal}AttributeSetCallback",
'blink::(anonymous namespace)::v8_{Class:blink_snake}::{prop:pascal}AttributeSetCallback',
'WebCore::setJS{Class}_{prop}(',
],
};
describe('discoverAutoLabels', function () {
it('discovers a Mozilla-style operation', function () {
const labels = discoverAutoLabels(
[DOM_OPERATION_AUTO],
['mozilla::dom::Element_Binding::querySelector(args)']
);
expect(labels).toHaveLength(1);
expect(labels[0].name).toBe('Element.querySelector');
expect(labels[0].funcPrefixes).toEqual([
'mozilla::dom::Element_Binding::querySelector(',
]);
});
it('discovers a Blink-style operation, recovering Class via the special-tokens list', function () {
const labels = discoverAutoLabels(
[DOM_OPERATION_AUTO],
['blink::(anonymous namespace)::v8_html_image_element::ClickOperation']
);
expect(labels).toHaveLength(1);
expect(labels[0].name).toBe('HTMLImageElement.click');
expect(labels[0].funcPrefixes).toEqual([
'blink::(anonymous namespace)::v8_html_image_element::ClickOperation',
]);
});
it('collects every observed engine form under one label entry', function () {
const labels = discoverAutoLabels(
[DOM_OPERATION_AUTO],
[
'mozilla::dom::Element_Binding::querySelector(args)',
'WebCore::jsElementPrototypeFunction_querySelector(JSC::JSGlobalObject*)',
]
);
expect(labels).toHaveLength(1);
expect(labels[0].name).toBe('Element.querySelector');
expect(labels[0].funcPrefixes).toEqual([
'mozilla::dom::Element_Binding::querySelector(',
'WebCore::jsElementPrototypeFunction_querySelector',
]);
});
it('deduplicates identical observed prefixes', function () {
const labels = discoverAutoLabels(
[DOM_OPERATION_AUTO],
[
'mozilla::dom::Element_Binding::querySelector(args)',
'mozilla::dom::Element_Binding::querySelector(other_args)',
]
);
expect(labels).toHaveLength(1);
expect(labels[0].funcPrefixes).toEqual([
'mozilla::dom::Element_Binding::querySelector(',
]);
});
it('discovers a setter using a separate auto_labels entry', function () {
const labels = discoverAutoLabels(
[DOM_SETTER_AUTO],
['mozilla::dom::Element_Binding::set_id(args)']
);
expect(labels).toHaveLength(1);
expect(labels[0].name).toBe('set Element.id');
});
it('does not match a binding setter as a dom_operation method', function () {
// `{method}` (camelCase, no modifier) must not swallow the `set_` of
// `set_innerHTML`, otherwise dom_operation would synthesize a stray
// "Element.set_innerHTML" label alongside dom_setter's "set Element.innerHTML".
const labels = discoverAutoLabels(
[DOM_OPERATION_AUTO, DOM_SETTER_AUTO],
['mozilla::dom::Element_Binding::set_innerHTML(args)']
);
expect(labels.map((l) => l.name)).toEqual(['set Element.innerHTML']);
});
});
describe('resolveAllLabels', function () {
// When two `[[auto_labels]]` entries synthesize a label with the same name
// (e.g. a generic `{Class}.{method}` template and a specific
// `CanvasRenderingContext2D.{method}` template both yielding
// `CanvasRenderingContext2D.fill`), every matched funcPrefix should end up
// on the merged label — not just the prefixes from whichever auto_labels
// entry happened to be processed last. Previously the second entry
// overwrote the first in `byName`, dropping the direct Blink call's prefix
// (`blink::Canvas2DRecorderContext::fill(`) and leaving only the V8 wrapper
// prefix, so direct C++ calls to `fill()` never got the label.
const CANVAS_SPECIFIC_AUTO: AutoLabel = {
nameTemplate: 'CanvasRenderingContext2D.{method}',
funcPrefixTemplates: [
"blink::`anonymous namespace'::v8_canvas_rendering_context_2d::{method:pascal}Operation",
'blink::Canvas2DRecorderContext::{method}(',
],
};
it('merges prefixes when a generic and a specific auto_label produce the same name', function () {
// Order of funcNames matters for catching the bug. When the C++ direct
// call (only matched by CANVAS_SPECIFIC_AUTO) is processed first, the
// `discovered` Map ends up with the specific entry inserted before the
// generic entry. `[...discovered.values()]` then iterates specific
// before generic, and the buggy `byName.set(l.name, l)` overwrites the
// specific entry (with both prefixes) with the generic entry (with
// only the V8 wrapper prefix). Putting the V8 wrapper first hides the
// bug because the specific entry is then the last value iterated and
// wins the overwrite by accident.
const resolved = resolveAllLabels(
[DOM_OPERATION_AUTO, CANVAS_SPECIFIC_AUTO],
[],
[
// Direct Blink C++ call: matches only CANVAS_SPECIFIC_AUTO. Without
// the merge fix this prefix gets dropped when the generic-auto
// entry overwrites the specific one in `byName`.
'blink::Canvas2DRecorderContext::fill() (canvas_2d_recorder_context.cc)',
// V8 wrapper: matches both DOM_OPERATION_AUTO (via the generic
// v8_{Class:blink_snake}::{method:pascal}Operation template) and
// CANVAS_SPECIFIC_AUTO (via its method-only template). Both
// synthesize the name `CanvasRenderingContext2D.fill`.
"blink::`anonymous namespace'::v8_canvas_rendering_context_2d::FillOperationCallback(args)",
]
);
const fill = resolved.find(
(l) => l.name === 'CanvasRenderingContext2D.fill'
);
expect(fill).toBeDefined();
expect(fill!.funcPrefixes).toEqual(
expect.arrayContaining([
"blink::`anonymous namespace'::v8_canvas_rendering_context_2d::FillOperation",
'blink::Canvas2DRecorderContext::fill(',
])
);
// And the result must have exactly one entry for this name, not one per
// contributing auto_labels entry.
expect(
resolved.filter((l) => l.name === 'CanvasRenderingContext2D.fill')
).toHaveLength(1);
});
it('appends explicit [[labels]] prefixes onto an auto-discovered label of the same name', function () {
const resolved = resolveAllLabels(
[DOM_OPERATION_AUTO],
[
{
name: 'Element.querySelector',
funcPrefixes: ['SomeExtraPrefix::querySelector('],
},
],
['mozilla::dom::Element_Binding::querySelector(args)']
);
const qs = resolved.find((l) => l.name === 'Element.querySelector');
expect(qs).toBeDefined();
expect(qs!.funcPrefixes).toEqual([
'mozilla::dom::Element_Binding::querySelector(',
'SomeExtraPrefix::querySelector(',
]);
});
it('passes through an explicit-only label when nothing auto-discovers the same name', function () {
const resolved = resolveAllLabels(
[DOM_OPERATION_AUTO],
[{ name: 'Custom Label', funcPrefixes: ['some::prefix('] }],
[]
);
expect(resolved).toEqual([
{ name: 'Custom Label', funcPrefixes: ['some::prefix('] },
]);
});
});