Skip to content

Commit e072cdb

Browse files
mkustermannCommit Queue
authored andcommitted
[vm] Add test to ensure class hierarchy is present in output of analyze_snapshot
go/dart-ama Change-Id: I533ca49d0e27a59f2f2763cc14c69485a470c281 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408561 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent fc34e6e commit e072cdb

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

runtime/tests/vm/dart/analyze_snapshot_binary_test.dart

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,24 @@ Future<void> testAOT(String dillPath,
122122
.toSet()
123123
.containsAll(['snapshot_data', 'objects', 'metadata']));
124124

125-
final objects = (analyzerJson['objects'] as List).map((o) => o as Map);
125+
final objects = (analyzerJson['objects'] as List).map((o) => o as Map).toList();
126+
final classes = objects.where((o) => o['type'] == 'Class').toList();
127+
final classnames = <int, String>{};
128+
final superclass = <int, int>{};
129+
final implementedInterfaces = <int, List<int>>{};
130+
for (final klass in classes) {
131+
final id = klass['id'];
132+
superclass[id] = klass['super_class'] as int;
133+
classnames[id] = klass['name'];
134+
implementedInterfaces[id] = [
135+
for (final superTypeId in klass['interfaces'] as List? ?? [])
136+
objects[superTypeId]['type_class']!,
137+
];
138+
}
126139

127140
// Find MethodChannel class.
128-
final objList = objects
129-
.where((o) => o['type'] == 'Class' && o['name'] == 'MethodChannel')
130-
.toList();
131-
Expect.isTrue(
132-
objList.length == 1, 'one MethodChannel class must exist in output');
133-
final int objId = objList.first['id'];
141+
final methodChannelId =
142+
classnames.entries.singleWhere((e) => e.value == 'MethodChannel').key;
134143

135144
// Find string instance.
136145
final stringList = objects
@@ -144,16 +153,28 @@ Future<void> testAOT(String dillPath,
144153
final instanceList = objects
145154
.where((o) =>
146155
o['type'] == 'Instance' &&
147-
o['class'] == objId &&
156+
o['class'] == methodChannelId &&
148157
o['references'].contains(stringObjId))
149158
.toList();
150-
Expect.isTrue(instanceList.length == 1, '''one instance of MethodChannel
159+
Expect.isTrue(instanceList.length == 1, '''one instance of MethodChannel
151160
with reference to "constChannel1" must exist in output''');
152161

162+
// Test class hierarchy information
163+
final myBaseClassId =
164+
classnames.entries.singleWhere((e) => e.value == 'MyBase').key;
165+
final mySubClassId =
166+
classnames.entries.singleWhere((e) => e.value == 'MySub').key;
167+
final myInterfaceClassId =
168+
classnames.entries.singleWhere((e) => e.value == 'MyInterface').key;
169+
170+
Expect.equals(myBaseClassId, superclass[mySubClassId]);
171+
Expect.equals(myInterfaceClassId, implementedInterfaces[mySubClassId]!.single);
172+
153173
Expect.isTrue(analyzerJson['metadata'].containsKey('analyzer_version'),
154174
'snapshot analyzer version must be reported');
155175
Expect.isTrue(analyzerJson['metadata']['analyzer_version'] == 2,
156176
'invalid snapshot analyzer version');
177+
157178
});
158179
}
159180

runtime/tests/vm/dart/analyze_snapshot_program.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ final channel2 = MethodChannel("channel2");
1616
const constChannel = MethodChannel("constChannel1");
1717
const constChannel2 = MethodChannel("constChannel2");
1818

19+
class MyBase {}
20+
21+
class MyInterface {}
22+
23+
class MySub extends MyBase implements MyInterface {}
24+
1925
main() {
2026
final mcs = [channel1, channel2, constChannel, constChannel2];
2127
for (int i = 0; i < mcs.length; ++i) {
2228
mcs[i].dump();
2329
}
30+
print('Class: ${MySub()}');
2431
}

0 commit comments

Comments
 (0)