Skip to content

Commit 4c522a2

Browse files
authored
Add a test for Class inspection (#2310)
1 parent 546a037 commit 4c522a2

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
@Tags(['daily'])
6+
@TestOn('vm')
7+
@Timeout(Duration(minutes: 2))
8+
9+
import 'package:test/test.dart';
10+
import 'package:test_common/logging.dart';
11+
import 'package:test_common/test_sdk_configuration.dart';
12+
import 'package:vm_service/vm_service.dart';
13+
14+
import '../fixtures/context.dart';
15+
import '../fixtures/project.dart';
16+
import '../fixtures/utilities.dart';
17+
import 'common/test_inspector.dart';
18+
19+
void main() {
20+
// Enable verbose logging for debugging.
21+
final debug = false;
22+
23+
final provider = TestSdkConfigurationProvider(
24+
verbose: debug,
25+
);
26+
27+
final context =
28+
TestContext(TestProject.testExperimentWithSoundNullSafety, provider);
29+
final testInspector = TestInspector(context);
30+
31+
late VmService service;
32+
late Stream<Event> stream;
33+
late String isolateId;
34+
late ScriptRef mainScript;
35+
36+
onBreakPoint(breakPointId, body) => testInspector.onBreakPoint(
37+
stream,
38+
isolateId,
39+
mainScript,
40+
breakPointId,
41+
body,
42+
);
43+
44+
getObject(instanceId) => service.getObject(isolateId, instanceId);
45+
46+
group('Class |', () {
47+
tearDownAll(provider.dispose);
48+
49+
for (var compilationMode in CompilationMode.values) {
50+
group('$compilationMode |', () {
51+
setUpAll(() async {
52+
setCurrentLogWriter(debug: debug);
53+
await context.setUp(
54+
testSettings: TestSettings(
55+
compilationMode: compilationMode,
56+
enableExpressionEvaluation: true,
57+
verboseCompiler: debug,
58+
),
59+
);
60+
service = context.debugConnection.vmService;
61+
62+
final vm = await service.getVM();
63+
isolateId = vm.isolates!.first.id!;
64+
final scripts = await service.getScripts(isolateId);
65+
66+
await service.streamListen('Debug');
67+
stream = service.onEvent('Debug');
68+
69+
mainScript = scripts.scripts!
70+
.firstWhere((each) => each.uri!.contains('main.dart'));
71+
});
72+
73+
tearDownAll(() async {
74+
await context.tearDown();
75+
});
76+
77+
setUp(() => setCurrentLogWriter(debug: debug));
78+
tearDown(() => service.resume(isolateId));
79+
80+
group('calling getObject for an existent class', () {
81+
test('returns the correct class representation', () async {
82+
await onBreakPoint('testClass1Case1', (event) async {
83+
// classes|dart:core|Object_Diagnosticable
84+
final result = await getObject(
85+
'classes|org-dartlang-app:///web/main.dart|GreeterClass',
86+
);
87+
final clazz = result as Class?;
88+
expect(clazz!.name, equals('GreeterClass'));
89+
expect(
90+
clazz.fields!.map((field) => field.name),
91+
unorderedEquals([
92+
'greeteeName',
93+
'useFrench',
94+
]),
95+
);
96+
expect(
97+
clazz.functions!.map((fn) => fn.name),
98+
containsAll([
99+
'sayHello',
100+
'greetInEnglish',
101+
'greetInFrench',
102+
]),
103+
);
104+
});
105+
});
106+
});
107+
108+
group('calling getObject for a non-existent class', () {
109+
// TODO(https://github.com/dart-lang/webdev/issues/2297): Ideally we
110+
// should throw an error in this case for the client to catch instead
111+
// of returning an empty class.
112+
test('returns an empty class representation', () async {
113+
await onBreakPoint('testClass1Case1', (event) async {
114+
final result = await getObject(
115+
'classes|dart:core|Object_Diagnosticable',
116+
);
117+
final clazz = result as Class?;
118+
expect(clazz!.name, equals('Object_Diagnosticable'));
119+
expect(clazz.fields, isEmpty);
120+
expect(clazz.functions, isEmpty);
121+
});
122+
});
123+
});
124+
});
125+
}
126+
});
127+
}

fixtures/_experimentSound/web/main.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void main() {
2020
testPattern([3.14, 'b']);
2121
testPattern([0, 1]);
2222
testPattern2();
23+
print('Classes');
24+
testClass();
2325
});
2426

2527
document.body!.appendText('Program is running!');
@@ -55,6 +57,11 @@ void printNestedNamedLocalRecord() {
5557
print(record); // Breakpoint: printNestedNamedLocalRecord
5658
}
5759

60+
void testClass() {
61+
final greeter = GreeterClass(greeteeName: 'Charlie Brown');
62+
greeter.sayHello();
63+
}
64+
5865
String testPattern(Object obj) {
5966
switch (obj) {
6067
case [var a, int n] || [int n, var a] when n == 1 && a is String:
@@ -73,3 +80,25 @@ String testPattern2() {
7380
print(firstCat); // Breakpoint: testPattern2Case2
7481
return '$dog, $firstCat, $secondCat';
7582
}
83+
84+
class GreeterClass {
85+
final String greeteeName;
86+
final bool useFrench;
87+
88+
GreeterClass({
89+
this.greeteeName = 'Snoopy',
90+
this.useFrench = false,
91+
});
92+
93+
void sayHello() {
94+
useFrench ? greetInFrench() : greetInEnglish();
95+
}
96+
97+
void greetInEnglish() {
98+
print('Hello $greeteeName'); // Breakpoint: testClass1Case1
99+
}
100+
101+
void greetInFrench() {
102+
print('Bonjour $greeteeName');
103+
}
104+
}

0 commit comments

Comments
 (0)