-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathpatterns_inspection_common.dart
163 lines (135 loc) · 5.28 KB
/
patterns_inspection_common.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
// Copyright (c) 2023, 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.
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 'test_inspector.dart';
void runTests({
required TestSdkConfigurationProvider provider,
required CompilationMode compilationMode,
required bool canaryFeatures,
required bool debug,
}) {
final context =
TestContext(TestProject.testExperimentWithSoundNullSafety, provider);
final testInspector = TestInspector(context);
late VmService service;
late Stream<Event> stream;
late String isolateId;
late ScriptRef mainScript;
onBreakPoint(breakPointId, body) => testInspector.onBreakPoint(
stream,
isolateId,
mainScript,
breakPointId,
body,
);
getInstanceRef(frame, expression) =>
testInspector.getInstanceRef(isolateId, frame, expression);
getFields(instanceRef, {offset, count}) => testInspector
.getFields(isolateId, instanceRef, offset: offset, count: count);
getFrameVariables(Frame frame) =>
testInspector.getFrameVariables(isolateId, frame);
group('$compilationMode |', () {
setUpAll(() async {
setCurrentLogWriter(debug: debug);
await context.setUp(
compilationMode: compilationMode,
enableExpressionEvaluation: true,
verboseCompiler: debug,
experiments: ['records', 'patterns'],
canaryFeatures: canaryFeatures,
);
service = context.debugConnection.vmService;
final vm = await service.getVM();
isolateId = vm.isolates!.first.id!;
final scripts = await service.getScripts(isolateId);
await service.streamListen('Debug');
stream = service.onEvent('Debug');
mainScript = scripts.scripts!
.firstWhere((each) => each.uri!.contains('main.dart'));
});
tearDownAll(() async {
await context.tearDown();
});
setUp(() => setCurrentLogWriter(debug: debug));
tearDown(() => service.resume(isolateId));
test('pattern match case 1', () async {
await onBreakPoint('testPatternCase1', (event) async {
final frame = event.topFrame!;
expect(await getFrameVariables(frame), {
'obj': matchListInstance(type: 'Object'),
'a': matchPrimitiveInstance(kind: InstanceKind.kString, value: 'a'),
'n': matchPrimitiveInstance(kind: InstanceKind.kDouble, value: 1),
});
});
});
test('pattern match case 2', () async {
await onBreakPoint('testPatternCase2', (event) async {
final frame = event.topFrame!;
expect(await getFrameVariables(frame), {
'obj': matchListInstance(type: 'Object'),
'a': matchPrimitiveInstance(kind: InstanceKind.kString, value: 'b'),
'n': matchPrimitiveInstance(kind: InstanceKind.kDouble, value: 3.14),
});
});
});
test('pattern match default case', () async {
await onBreakPoint('testPatternDefault', (event) async {
final frame = event.topFrame!;
final frameIndex = frame.index!;
final instanceRef = await getInstanceRef(frameIndex, 'obj');
expect(await getFields(instanceRef), [0, 1]);
expect(await getFrameVariables(frame), {
'obj': matchListInstance(type: 'int'),
});
});
});
test('stepping through pattern match', () async {
await onBreakPoint('callTestPattern1', (Event event) async {
var previousLocation = event.topFrame!.location;
for (var step in [
// Make sure we step into the callee.
for (var i = 0; i < 4; i++) 'Into',
// Make a few steps inside the callee.
for (var i = 0; i < 4; i++) 'Over',
]) {
await service.resume(isolateId, step: step);
event = await stream
.firstWhere((e) => e.kind == EventKind.kPauseInterrupted);
if (step == 'Over') {
expect(event.topFrame!.code!.name, 'testPattern');
}
final location = event.topFrame!.location;
expect(location, isNot(equals(previousLocation)));
previousLocation = location;
}
});
});
test('before instantiation of pattern-matching variables', () async {
await onBreakPoint('testPattern2Case1', (event) async {
final frame = event.topFrame!;
expect(
await getFrameVariables(frame),
{'dog': matchPrimitiveInstance(kind: 'String', value: 'Prismo')},
);
});
});
test('after instantiation of pattern-matching variables', () async {
await onBreakPoint('testPattern2Case2', (event) async {
final frame = event.topFrame!;
final vars = await getFrameVariables(frame);
expect(vars, {
'dog': matchPrimitiveInstance(kind: 'String', value: 'Prismo'),
'cats': matchListInstance(type: 'String'),
'firstCat': matchPrimitiveInstance(kind: 'String', value: 'Garfield'),
'secondCat': matchPrimitiveInstance(kind: 'String', value: 'Tom'),
});
});
});
});
}