Skip to content

Commit 3a795dd

Browse files
committed
test(protoc_plugin): replace dart:mirrors in reserved_names_test
Inspect reserved member names via package:analyzer instead of dart:mirrors, fail with StateError when a target declaration is missing (dropping the dead ReadonlyMessageMixin check), and resolve dart:collection's MapMixin typedef so MapBase coverage is restored.
1 parent fd39182 commit 3a795dd

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎protoc_plugin/pubspec.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
pub_semver: ^2.2.0
1919

2020
dev_dependencies:
21+
analyzer: '>=13.1.0 <15.0.0'
2122
lints: '>=5.0.0 <7.0.0'
2223
test: ^1.16.0
2324

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright (c) 2013, 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+
@TestOn('vm')
6+
library;
7+
8+
import 'dart:isolate' show Isolate;
9+
10+
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
11+
import 'package:analyzer/dart/analysis/results.dart';
12+
import 'package:analyzer/dart/analysis/session.dart';
13+
import 'package:analyzer/dart/element/element.dart';
14+
import 'package:analyzer/dart/element/type.dart';
15+
import 'package:path/path.dart' as p;
16+
import 'package:protobuf/meta.dart'
17+
show GeneratedMessage_reservedNames, ProtobufEnum_reservedNames;
18+
import 'package:protoc_plugin/mixins.dart' show findMixin;
19+
import 'package:test/test.dart';
20+
21+
void main() {
22+
late final AnalysisSession session;
23+
24+
setUpAll(() {
25+
final packageEntryUri =
26+
Isolate.resolvePackageUriSync(
27+
Uri.parse('package:protoc_plugin/mixins.dart'),
28+
)!;
29+
final packageRoot = p.dirname(p.dirname(packageEntryUri.toFilePath()));
30+
final collection = AnalysisContextCollection(includedPaths: [packageRoot]);
31+
addTearDown(collection.dispose);
32+
session = collection.contextFor(packageRoot).currentSession;
33+
});
34+
35+
test('GeneratedMessage reserved names are up to date', () async {
36+
final actual = Set<String>.from(GeneratedMessage_reservedNames);
37+
final expected = await _findMemberNames(
38+
session,
39+
'package:protobuf/src/protobuf/internal.dart',
40+
'GeneratedMessage',
41+
);
42+
43+
expect(actual.toList()..sort(), equals(expected.toList()..sort()));
44+
});
45+
46+
test('ProtobufEnum reserved names are up to date', () async {
47+
final actual = Set<String>.from(ProtobufEnum_reservedNames);
48+
final expected = await _findMemberNames(
49+
session,
50+
'package:protobuf/src/protobuf/internal.dart',
51+
'ProtobufEnum',
52+
);
53+
54+
expect(actual.toList()..sort(), equals(expected.toList()..sort()));
55+
});
56+
57+
test('PbMapMixin reserved names are up to date', () async {
58+
final meta = findMixin('PbMapMixin')!;
59+
final actual = Set<String>.from(meta.findReservedNames());
60+
61+
final expected = await _findMemberNames(
62+
session,
63+
meta.importFrom,
64+
'PbMapMixin',
65+
);
66+
expected
67+
..addAll(await _findMemberNames(session, 'dart:collection', 'MapMixin'))
68+
..removeAll(GeneratedMessage_reservedNames);
69+
70+
// `MapMixin.retype` was removed in Dart 2, but remains reserved in
71+
// `PbMapMixin` for backwards compatibility (#636).
72+
expect(
73+
actual.difference({'retype'}).toList()..sort(),
74+
equals(expected.toList()..sort()),
75+
);
76+
});
77+
78+
test('throws StateError when target declaration is missing', () async {
79+
await expectLater(
80+
_findMemberNames(
81+
session,
82+
'package:protobuf/src/protobuf/internal.dart',
83+
'ReadonlyMessageMixin',
84+
),
85+
throwsA(isA<StateError>()),
86+
);
87+
});
88+
}
89+
90+
/// Returns the names of public constructors, explicit accessors, and
91+
/// non-static methods of [typeName] in [libraryUri] and its superclasses.
92+
Future<Set<String>> _findMemberNames(
93+
AnalysisSession session,
94+
String libraryUri,
95+
String typeName,
96+
) async {
97+
final libraryResult = await session.getLibraryByUri(libraryUri);
98+
if (libraryResult is! LibraryElementResult) {
99+
throw StateError('Failed to resolve library $libraryUri: $libraryResult');
100+
}
101+
102+
final library = libraryResult.element;
103+
final aliasedType = library.getTypeAlias(typeName)?.aliasedType;
104+
final target =
105+
library.getClass(typeName) ??
106+
library.getMixin(typeName) ??
107+
(aliasedType is InterfaceType ? aliasedType.element : null);
108+
if (target == null) {
109+
throw StateError(
110+
'No class, mixin, or interface typedef "$typeName" found in $libraryUri.',
111+
);
112+
}
113+
114+
final result = <String>{};
115+
for (
116+
InterfaceElement? current = target;
117+
current != null;
118+
current = current.supertype?.element
119+
) {
120+
result.addAll(
121+
_declaredMemberNames(
122+
current,
123+
aliasName: identical(current, target) ? typeName : null,
124+
),
125+
);
126+
}
127+
return result;
128+
}
129+
130+
Iterable<String> _declaredMemberNames(
131+
InterfaceElement element, {
132+
String? aliasName,
133+
}) sync* {
134+
final interfaceName = aliasName ?? element.name;
135+
if (element is MixinElement && !element.isPrivate && interfaceName != null) {
136+
yield interfaceName;
137+
}
138+
139+
for (final constructor in element.constructors) {
140+
if (constructor.isPrivate) continue;
141+
final name = constructor.name == 'new' ? interfaceName : constructor.name;
142+
if (name != null) yield name;
143+
}
144+
145+
for (final executable in [
146+
...element.methods,
147+
...element.getters,
148+
...element.setters,
149+
]) {
150+
if (executable.isPrivate || executable.isStatic) continue;
151+
final name = switch (executable) {
152+
MethodElement(isOriginDeclaration: true, :final name) ||
153+
PropertyAccessorElement(isOriginDeclaration: true, :final name) => name,
154+
_ => null,
155+
};
156+
if (name != null) yield name;
157+
}
158+
}

0 commit comments

Comments
 (0)