Skip to content

Commit e34f5b7

Browse files
authored
Migrate debugger_test and instance_test to null-safety (#1708)
1 parent 098a25a commit e34f5b7

File tree

4 files changed

+81
-69
lines changed

4 files changed

+81
-69
lines changed

dwds/lib/src/debugging/debugger.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Debugger extends Domain {
197197
});
198198

199199
handleErrorIfPresent(await _remoteDebugger.enablePage());
200-
handleErrorIfPresent(await _remoteDebugger.enable() as WipResponse);
200+
await _remoteDebugger.enable();
201201

202202
// Enable collecting information about async frames when paused.
203203
handleErrorIfPresent(await _remoteDebugger

dwds/test/debugger_test.dart

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
@TestOn('vm')
86
import 'dart:async';
97

@@ -13,8 +11,9 @@ import 'package:dwds/src/debugging/inspector.dart';
1311
import 'package:dwds/src/debugging/location.dart';
1412
import 'package:dwds/src/debugging/skip_list.dart';
1513
import 'package:dwds/src/loaders/strategy.dart';
14+
import 'package:logging/logging.dart';
1615
import 'package:test/test.dart';
17-
import 'package:vm_service/vm_service.dart';
16+
import 'package:vm_service/vm_service.dart' hide LogRecord;
1817
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'
1918
show CallFrame, DebuggerPausedEvent, StackTrace, WipCallFrame, WipScript;
2019

@@ -23,12 +22,12 @@ import 'fixtures/debugger_data.dart';
2322
import 'fixtures/fakes.dart';
2423

2524
final context = TestContext();
26-
AppInspector inspector;
27-
Debugger debugger;
28-
FakeWebkitDebugger webkitDebugger;
29-
StreamController<DebuggerPausedEvent> pausedController;
30-
Locations locations;
31-
SkipLists skipLists;
25+
late AppInspector inspector;
26+
late Debugger debugger;
27+
late FakeWebkitDebugger webkitDebugger;
28+
late StreamController<DebuggerPausedEvent> pausedController;
29+
late Locations locations;
30+
late SkipLists skipLists;
3231

3332
class TestStrategy extends FakeStrategy {
3433
@override
@@ -92,7 +91,7 @@ void main() async {
9291
skipLists = SkipLists();
9392
debugger = await Debugger.create(
9493
webkitDebugger,
95-
null,
94+
(_, __) {},
9695
locations,
9796
skipLists,
9897
root,
@@ -109,8 +108,8 @@ void main() async {
109108
expect(frames, isNotNull);
110109
expect(frames, isNotEmpty);
111110

112-
final firstFrame = frames[0];
113-
final frame1Variables = firstFrame.vars.map((each) => each.name).toList();
111+
final firstFrameVars = frames[0].vars!;
112+
final frame1Variables = firstFrameVars.map((each) => each.name).toList();
114113
expect(frame1Variables, ['a', 'b']);
115114
});
116115

@@ -164,6 +163,13 @@ void main() async {
164163
expect(frames[4].kind, FrameKind.kAsyncCausal);
165164
});
166165

166+
setUp(() {
167+
// We need to provide an Isolate so that the code doesn't bail out on a null
168+
// check before it has a chance to throw.
169+
inspector = FakeInspector(fakeIsolate: simpleIsolate);
170+
debugger.updateInspector(inspector);
171+
});
172+
167173
group('errors', () {
168174
setUp(() {
169175
// We need to provide an Isolate so that the code doesn't bail out on a null
@@ -175,17 +181,18 @@ void main() async {
175181
test('errors in the zone are caught and logged', () async {
176182
// Add a DebuggerPausedEvent with a null parameter to provoke an error.
177183
pausedController.sink.add(DebuggerPausedEvent({
184+
'method': '',
178185
'params': {
179186
'reason': 'other',
180187
'callFrames': [
181-
null,
188+
{'callFrameId': '', 'functionName': ''},
182189
],
183190
}
184191
}));
185192
expect(
186193
Debugger.logger.onRecord,
187-
emitsThrough(predicate(
188-
(log) => log.message == 'Error calculating Dart frames')));
194+
emitsThrough(predicate((LogRecord log) =>
195+
log.message == 'Error calculating Dart frames')));
189196
});
190197
});
191198
}

dwds/test/fixtures/fakes.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ class FakeWebkitDebugger implements WebkitDebugger {
182182
Stream<TargetCrashedEvent> get onTargetCrashed => Stream.empty();
183183

184184
@override
185-
Future<WipResponse> pause() async => WipResponse({});
185+
Future<WipResponse> pause() async => fakeWipResponse;
186186

187187
@override
188-
Future<WipResponse> resume() async => WipResponse({});
188+
Future<WipResponse> resume() async => fakeWipResponse;
189189

190190
@override
191191
Map<String, WipScript> get scripts => _scripts!;
@@ -200,27 +200,27 @@ class FakeWebkitDebugger implements WebkitDebugger {
200200
if (command == 'Runtime.getProperties') {
201201
return results[resultsReturned++];
202202
}
203-
return WipResponse({});
203+
return fakeWipResponse;
204204
}
205205

206206
@override
207207
Future<WipResponse> setPauseOnExceptions(PauseState state) async =>
208-
WipResponse({});
208+
fakeWipResponse;
209209

210210
@override
211211
Future<WipResponse> removeBreakpoint(String breakpointId) async =>
212-
WipResponse({});
212+
fakeWipResponse;
213213

214214
@override
215215
Future<WipResponse> stepInto({Map<String, dynamic>? params}) async =>
216-
WipResponse({});
216+
fakeWipResponse;
217217

218218
@override
219-
Future<WipResponse> stepOut() async => WipResponse({});
219+
Future<WipResponse> stepOut() async => fakeWipResponse;
220220

221221
@override
222222
Future<WipResponse> stepOver({Map<String, dynamic>? params}) async =>
223-
WipResponse({});
223+
fakeWipResponse;
224224

225225
@override
226226
Stream<ConsoleAPIEvent> get onConsoleAPICalled => Stream.empty();
@@ -251,10 +251,10 @@ class FakeWebkitDebugger implements WebkitDebugger {
251251
[];
252252

253253
@override
254-
Future<WipResponse> enablePage() async => WipResponse({});
254+
Future<WipResponse> enablePage() async => fakeWipResponse;
255255

256256
@override
257-
Future<WipResponse> pageReload() async => WipResponse({});
257+
Future<WipResponse> pageReload() async => fakeWipResponse;
258258
}
259259

260260
/// Fake execution context that is needed for id only
@@ -365,3 +365,8 @@ class FakeAssetReader implements AssetReader {
365365
return contents;
366366
}
367367
}
368+
369+
final fakeWipResponse = WipResponse({
370+
'id': 1,
371+
'result': {'fake': ''}
372+
});

dwds/test/instance_test.dart

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
5+
import 'dart:async';
66

77
import 'package:dwds/src/connections/debug_connection.dart';
88
import 'package:dwds/src/debugging/debugger.dart';
@@ -20,8 +20,8 @@ final context = TestContext(
2020
WipConnection get tabConnection => context.tabConnection;
2121

2222
void main() {
23-
AppInspector inspector;
24-
Debugger debugger;
23+
late AppInspector inspector;
24+
late Debugger debugger;
2525

2626
setUpAll(() async {
2727
await context.setUp();
@@ -54,9 +54,9 @@ void main() {
5454
final remoteObject = await libraryPublicFinal();
5555
final nullVariable = await inspector.loadField(remoteObject, 'notFinal');
5656
final ref = await inspector.instanceRefFor(nullVariable);
57-
expect(ref.valueAsString, 'null');
57+
expect(ref!.valueAsString, 'null');
5858
expect(ref.kind, InstanceKind.kNull);
59-
final classRef = ref.classRef;
59+
final classRef = ref.classRef!;
6060
expect(classRef.name, 'Null');
6161
expect(classRef.id, 'classes|dart:core|Null');
6262
});
@@ -65,9 +65,9 @@ void main() {
6565
final remoteObject = await libraryPublicFinal();
6666
final count = await inspector.loadField(remoteObject, 'count');
6767
final ref = await inspector.instanceRefFor(count);
68-
expect(ref.valueAsString, '0');
68+
expect(ref!.valueAsString, '0');
6969
expect(ref.kind, InstanceKind.kDouble);
70-
final classRef = ref.classRef;
70+
final classRef = ref.classRef!;
7171
expect(classRef.name, 'Double');
7272
expect(classRef.id, 'classes|dart:core|Double');
7373
});
@@ -76,8 +76,8 @@ void main() {
7676
final remoteObject = await libraryPublicFinal();
7777
final count = await inspector.loadField(remoteObject, 'myselfField');
7878
final ref = await inspector.instanceRefFor(count);
79-
expect(ref.kind, InstanceKind.kPlainInstance);
80-
final classRef = ref.classRef;
79+
expect(ref!.kind, InstanceKind.kPlainInstance);
80+
final classRef = ref.classRef!;
8181
expect(classRef.name, 'MyTestClass<dynamic>');
8282
expect(
8383
classRef.id,
@@ -87,11 +87,11 @@ void main() {
8787

8888
test('for closure', () async {
8989
final remoteObject = await libraryPublicFinal();
90-
final properties = await debugger.getProperties(remoteObject.objectId);
90+
final properties = await debugger.getProperties(remoteObject.objectId!);
9191
final closure =
9292
properties.firstWhere((property) => property.name == 'closure');
93-
final instanceRef = await inspector.instanceRefFor(closure.value);
94-
final functionName = instanceRef.closureFunction.name;
93+
final instanceRef = await inspector.instanceRefFor(closure.value!);
94+
final functionName = instanceRef!.closureFunction!.name;
9595
// Older SDKs do not contain function names
9696
if (functionName != 'Closure') {
9797
expect(functionName, 'someFunction');
@@ -102,40 +102,40 @@ void main() {
102102
test('for a list', () async {
103103
final remoteObject = await libraryPublic();
104104
final ref = await inspector.instanceRefFor(remoteObject);
105-
expect(ref.length, greaterThan(0));
105+
expect(ref!.length, greaterThan(0));
106106
expect(ref.kind, InstanceKind.kList);
107-
expect(ref.classRef.name, 'List<String>');
107+
expect(ref.classRef!.name, 'List<String>');
108108
});
109109

110110
test('for map', () async {
111111
final remoteObject =
112112
await inspector.jsEvaluate(libraryVariableExpression('map'));
113113
final ref = await inspector.instanceRefFor(remoteObject);
114-
expect(ref.length, 2);
114+
expect(ref!.length, 2);
115115
expect(ref.kind, InstanceKind.kMap);
116-
expect(ref.classRef.name, 'LinkedMap<Object, Object>');
116+
expect(ref.classRef!.name, 'LinkedMap<Object, Object>');
117117
});
118118

119119
test('for an IdentityMap', () async {
120120
final remoteObject =
121121
await inspector.jsEvaluate(libraryVariableExpression('identityMap'));
122122
final ref = await inspector.instanceRefFor(remoteObject);
123-
expect(ref.length, 2);
123+
expect(ref!.length, 2);
124124
expect(ref.kind, InstanceKind.kMap);
125-
expect(ref.classRef.name, 'IdentityMap<String, int>');
125+
expect(ref.classRef!.name, 'IdentityMap<String, int>');
126126
});
127127
});
128128

129129
group('instance', () {
130130
test('for class object', () async {
131131
final remoteObject = await libraryPublicFinal();
132132
final instance = await inspector.instanceFor(remoteObject);
133-
expect(instance.kind, InstanceKind.kPlainInstance);
134-
final classRef = instance.classRef;
133+
expect(instance!.kind, InstanceKind.kPlainInstance);
134+
final classRef = instance.classRef!;
135135
expect(classRef, isNotNull);
136136
expect(classRef.name, 'MyTestClass<dynamic>');
137137
final fieldNames =
138-
instance.fields.map((boundField) => boundField.decl.name).toList();
138+
instance.fields!.map((boundField) => boundField.decl!.name).toList();
139139
expect(fieldNames, [
140140
'_privateField',
141141
'abstractField',
@@ -146,54 +146,54 @@ void main() {
146146
'notFinal',
147147
'tornOff',
148148
]);
149-
for (var field in instance.fields) {
150-
expect(field.decl.declaredType, isNotNull);
149+
for (var field in instance.fields!) {
150+
expect(field.decl!.declaredType, isNotNull);
151151
}
152152
});
153153

154154
test('for closure', () async {
155155
final remoteObject = await libraryPublicFinal();
156-
final properties = await debugger.getProperties(remoteObject.objectId);
156+
final properties = await debugger.getProperties(remoteObject.objectId!);
157157
final closure =
158158
properties.firstWhere((property) => property.name == 'closure');
159-
final instance = await inspector.instanceFor(closure.value);
160-
expect(instance.kind, InstanceKind.kClosure);
161-
expect(instance.classRef.name, 'Closure');
159+
final instance = await inspector.instanceFor(closure.value!);
160+
expect(instance!.kind, InstanceKind.kClosure);
161+
expect(instance.classRef!.name, 'Closure');
162162
});
163163

164164
test('for a nested class', () async {
165165
final libraryRemoteObject = await libraryPublicFinal();
166166
final fieldRemoteObject =
167167
await inspector.loadField(libraryRemoteObject, 'myselfField');
168168
final instance = await inspector.instanceFor(fieldRemoteObject);
169-
expect(instance.kind, InstanceKind.kPlainInstance);
170-
final classRef = instance.classRef;
169+
expect(instance!.kind, InstanceKind.kPlainInstance);
170+
final classRef = instance.classRef!;
171171
expect(classRef, isNotNull);
172172
expect(classRef.name, 'MyTestClass<dynamic>');
173173
});
174174

175175
test('for a list', () async {
176176
final remote = await libraryPublic();
177177
final instance = await inspector.instanceFor(remote);
178-
expect(instance.kind, InstanceKind.kList);
179-
final classRef = instance.classRef;
178+
expect(instance!.kind, InstanceKind.kList);
179+
final classRef = instance.classRef!;
180180
expect(classRef, isNotNull);
181181
expect(classRef.name, 'List<String>');
182-
final first = instance.elements[0];
182+
final first = instance.elements![0];
183183
expect(first.valueAsString, 'library');
184184
});
185185

186186
test('for a map', () async {
187187
final remote =
188188
await inspector.jsEvaluate(libraryVariableExpression('map'));
189189
final instance = await inspector.instanceFor(remote);
190-
expect(instance.kind, InstanceKind.kMap);
191-
final classRef = instance.classRef;
190+
expect(instance!.kind, InstanceKind.kMap);
191+
final classRef = instance.classRef!;
192192
expect(classRef.name, 'LinkedMap<Object, Object>');
193-
final first = instance.associations[0].value as InstanceRef;
193+
final first = instance.associations![0].value as InstanceRef;
194194
expect(first.kind, InstanceKind.kList);
195195
expect(first.length, 3);
196-
final second = instance.associations[1].value as InstanceRef;
196+
final second = instance.associations![1].value as InstanceRef;
197197
expect(second.kind, InstanceKind.kString);
198198
expect(second.valueAsString, 'something');
199199
});
@@ -202,10 +202,10 @@ void main() {
202202
final remote =
203203
await inspector.jsEvaluate(libraryVariableExpression('identityMap'));
204204
final instance = await inspector.instanceFor(remote);
205-
expect(instance.kind, InstanceKind.kMap);
206-
final classRef = instance.classRef;
205+
expect(instance!.kind, InstanceKind.kMap);
206+
final classRef = instance.classRef!;
207207
expect(classRef.name, 'IdentityMap<String, int>');
208-
final first = instance.associations[0].value;
208+
final first = instance.associations![0].value;
209209
expect(first.valueAsString, '1');
210210
});
211211

@@ -214,12 +214,12 @@ void main() {
214214
final remote =
215215
await inspector.jsEvaluate(libraryVariableExpression('notAList'));
216216
final instance = await inspector.instanceFor(remote);
217-
expect(instance.kind, InstanceKind.kPlainInstance);
218-
final classRef = instance.classRef;
217+
expect(instance!.kind, InstanceKind.kPlainInstance);
218+
final classRef = instance.classRef!;
219219
expect(classRef.name, 'NotReallyAList');
220220
expect(instance.elements, isNull);
221-
final field = instance.fields.first;
222-
expect(field.decl.name, '_internal');
221+
final field = instance.fields!.first;
222+
expect(field.decl!.name, '_internal');
223223
});
224224
});
225225
}

0 commit comments

Comments
 (0)