forked from dart-lang/webdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable_scope_test.dart
301 lines (263 loc) · 10.4 KB
/
variable_scope_test.dart
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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@TestOn('vm')
@Timeout(Duration(minutes: 2))
library;
import 'package:dwds/src/debugging/dart_scope.dart';
import 'package:dwds/src/services/chrome_proxy_service.dart';
import 'package:test/test.dart';
import 'package:test_common/logging.dart';
import 'package:test_common/test_sdk_configuration.dart';
import 'package:vm_service/vm_service.dart';
import 'fixtures/context.dart';
import 'fixtures/project.dart';
import 'fixtures/utilities.dart';
void main() {
// set to true for debug logging.
final debug = false;
final provider = TestSdkConfigurationProvider(verbose: debug);
tearDownAll(provider.dispose);
final context = TestContext(TestProject.testScopes, provider);
setUpAll(() async {
setCurrentLogWriter(debug: debug);
await context.setUp(
testSettings: TestSettings(verboseCompiler: debug),
);
});
tearDownAll(() async {
await context.tearDown();
});
group('temporary variable regular expression', () {
setUpAll(() => setCurrentLogWriter(debug: debug));
test('matches correctly for pre-patterns temporary variables', () {
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't4$'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't4$0'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't4$10'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't4$0'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't1'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't10'), isTrue);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r'__t$TL'), isTrue);
expect(
previousDdcTemporaryVariableRegExp.hasMatch(r'__t$StringN'),
isTrue,
);
expect(
previousDdcTemporaryVariableRegExp
.hasMatch(r'__t$IdentityMapOfString$T'),
isTrue,
);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't'), isFalse);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't10foo'), isFalse);
expect(previousDdcTemporaryVariableRegExp.hasMatch(r't$10foo'), isFalse);
});
test('matches correctly for post-patterns temporary variables', () {
expect(ddcTemporaryVariableRegExp.hasMatch(r't$364$'), isTrue);
expect(ddcTemporaryVariableRegExp.hasMatch(r't$364$0'), isTrue);
expect(ddcTemporaryVariableRegExp.hasMatch(r't$364$10'), isTrue);
expect(ddcTemporaryVariableRegExp.hasMatch(r't$364$0'), isTrue);
expect(ddcTemporaryVariableRegExp.hasMatch(r't$361'), isTrue);
expect(ddcTemporaryVariableRegExp.hasMatch(r't$36$350$350'), isTrue);
expect(
ddcTemporaryVariableRegExp.hasMatch(r't$36$350$354$35isSet'),
isTrue,
);
expect(
ddcTemporaryVariableRegExp.hasMatch(r't$36$35variable$35isSet'),
isTrue,
);
expect(
ddcTemporaryVariableRegExp.hasMatch(r'synthetic$35variable'),
isTrue,
);
expect(ddcTemporaryTypeVariableRegExp.hasMatch(r'__t$TL'), isTrue);
expect(ddcTemporaryTypeVariableRegExp.hasMatch(r'__t$StringN'), isTrue);
expect(
ddcTemporaryTypeVariableRegExp.hasMatch(r'__t$IdentityMapOfString$T'),
isTrue,
);
expect(ddcTemporaryVariableRegExp.hasMatch(r't'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r'this'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r'\$this'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r't10'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r't10foo'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r'ten'), isFalse);
expect(ddcTemporaryVariableRegExp.hasMatch(r'my$3635variable'), isFalse);
});
});
group('variable scope', () {
late ChromeProxyService service;
VM vm;
String? isolateId;
late Stream<Event> stream;
ScriptList scripts;
late ScriptRef mainScript;
Stack stack;
// TODO: Be able to set breakpoints before start/reload so we can exercise
// things that aren't in recurring loops.
/// Support function for pausing and returning the stack at a line.
Future<Stack> breakAt(String breakpointId, ScriptRef scriptRef) async {
final lineNumber =
await context.findBreakpointLine(breakpointId, isolateId!, scriptRef);
final bp =
await service.addBreakpoint(isolateId!, scriptRef.id!, lineNumber);
// Wait for breakpoint to trigger.
await stream
.firstWhere((event) => event.kind == EventKind.kPauseBreakpoint);
// Remove breakpoint so it doesn't impact other tests.
await service.removeBreakpoint(isolateId!, bp.id!);
final stack = await service.getStack(isolateId!);
return stack;
}
Future<Instance> getInstance(InstanceRef ref) async {
final result = await service.getObject(isolateId!, ref.id!);
expect(result, isA<Instance>());
return result as Instance;
}
void expectDartObject(String variableName, Instance instance) {
expect(
instance,
isA<Instance>().having(
(instance) => instance.classRef!.name,
'$variableName: classRef.name',
isNot(
isIn(['NativeJavaScriptObject', 'JavaScriptObject', 'NativeError']),
),
),
);
}
Future<void> expectDartVariables(
Map<String?, InstanceRef?> variables,
) async {
for (final name in variables.keys) {
final instance = await getInstance(variables[name]!);
expectDartObject(name!, instance);
}
}
Map<String?, InstanceRef?> getFrameVariables(Frame frame) {
return <String?, InstanceRef?>{
for (final variable in frame.vars!)
variable.name: variable.value as InstanceRef?,
};
}
setUpAll(() => setCurrentLogWriter(debug: debug));
setUp(() async {
service = context.service;
vm = await service.getVM();
isolateId = vm.isolates!.first.id;
scripts = await service.getScripts(isolateId!);
await service.streamListen('Debug');
stream = service.onEvent('Debug');
mainScript = scripts.scripts!
.firstWhere((each) => each.uri!.contains('main.dart'));
});
tearDown(() async {
await service.resume(isolateId!);
});
test('variables in static function', () async {
stack = await breakAt('staticFunction', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
expect(variableNames, containsAll(['formal']));
});
test('variables in static async function', () async {
stack = await breakAt('staticAsyncFunction', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
final variableValues =
variableNames.map((name) => variables[name]?.valueAsString).toList();
expect(
variableNames,
containsAll(['myLocal', 'value']),
);
expect(
variableValues,
containsAll(['a local value', 'arg1']),
);
});
test('variables in static async loop function', () async {
stack = await breakAt('staticAsyncLoopFunction', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
final variableValues =
variableNames.map((name) => variables[name]?.valueAsString).toList();
expect(
variableNames,
containsAll(['i', 'myLocal', 'value']),
);
// Ensure the loop variable, i, is captued correctly. The value from the
// first iteration should be captured by the saved closure.
expect(
variableValues,
containsAll(['1', 'my local value', 'arg2']),
);
});
test('variables in function', () async {
stack = await breakAt('nestedFunction', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
expect(
variableNames,
containsAll([
'aClass',
'another',
'intLocalInMain',
'local',
'localThatsNull',
'nestedFunction',
'parameter',
'testClass',
]),
);
});
test('variables in closure nested in method', () async {
stack = await breakAt('nestedClosure', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
expect(
variableNames,
[
'closureLocalInsideMethod',
'local',
'parameter',
'this',
],
);
});
test('variables in method', () async {
stack = await breakAt('printMethod', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
expect(variableNames, [
'this',
]);
});
test('variables in extension method', () async {
stack = await breakAt('extension', mainScript);
final variables = getFrameVariables(stack.frames!.first);
await expectDartVariables(variables);
final variableNames = variables.keys.toList()..sort();
// Note: '$this' should change to 'this', and 'return' should
// disappear after debug symbols are available.
// https://github.com/dart-lang/webdev/issues/1371
expect(variableNames, ['\$this', 'ret', 'return']);
});
test('evaluateJsOnCallFrame', () async {
stack = await breakAt('nestedFunction', mainScript);
final debugger = await service.debuggerFuture;
final parameter =
await debugger.evaluateJsOnCallFrameIndex(0, 'parameter');
expect(parameter.value, matches(RegExp(r'\d+ world')));
final ticks = await debugger.evaluateJsOnCallFrameIndex(1, 'ticks');
// We don't know how many ticks there were before we stopped, but it should
// be a positive number.
expect(ticks.value, isPositive);
});
});
}