chore(unified_analytics): remove dart:mirrors usage in test/event_test.dart (#2602)
`dart:mirrors` has been marked `@deprecated` on the bleeding-edge Dart SDK (`main` branch, [CL 541360](https://dart-review.googlesource.com/c/sdk/+/541360)) ahead of its planned removal in Dart 4.0 ([dart-lang/sdk#44489](https://github.com/dart-lang/sdk/issues/44489)).
### Changes
- Replace `reflectClass(Event).declarations` (`dart:mirrors`) in `pkgs/unified_analytics/test/event_test.dart` with `Isolate.resolvePackageUriSync` (`dart:isolate`), stripping comments (`//` and `/* ... */`) before matching `Event.<name>(` constructor identifiers and verifying exact unordered equality against `DashEvent.values` (excluding `DashEvent.ideEvent`) and `eventsAccountedForInTests`.
- `pubspec.yaml` and `CHANGELOG.md` are already at `8.0.19-wip`, and since this only touches `test/event_test.dart`, no new changelog entry is needed.
diff --git a/pkgs/unified_analytics/test/event_test.dart b/pkgs/unified_analytics/test/event_test.dart
index a96bcda..569e682 100644
--- a/pkgs/unified_analytics/test/event_test.dart
+++ b/pkgs/unified_analytics/test/event_test.dart
@@ -2,7 +2,8 @@
// 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 'dart:mirrors';
+import 'dart:io';
+import 'dart:isolate';
import 'package:test/test.dart';
import 'package:unified_analytics/src/enums.dart';
@@ -1180,21 +1181,33 @@
});
test('Confirm all constructors were checked', () {
- var constructorCount = 0;
- for (final declaration in reflectClass(Event).declarations.keys) {
- // Count public constructors but omit private constructors
- if (declaration.toString().contains('Event.') &&
- !declaration.toString().contains('Event._')) {
- constructorCount++;
- }
- }
+ final eventFile = File.fromUri(
+ Isolate.resolvePackageUriSync(
+ Uri.parse('package:unified_analytics/src/event.dart'),
+ )!,
+ );
+ final sourceWithoutComments = eventFile.readAsStringSync().replaceAll(
+ RegExp(r'//.*|/\*[\s\S]*?\*/'),
+ '',
+ );
+ final constructorNames = RegExp(
+ r'^\s*(?:factory\s+)?Event\.(?!_|fromJson\b)([a-zA-Z0-9_]+)\(',
+ multiLine: true,
+ ).allMatches(sourceWithoutComments).map((m) => m.group(1)!).toList();
+
+ expect(
+ constructorNames,
+ unorderedEquals(
+ DashEvent.values.map((e) => e.name).where((name) => name != 'ideEvent'),
+ ),
+ );
// Change this integer below if your PR either adds or removes
// an Event constructor
final eventsAccountedForInTests = 35;
expect(
eventsAccountedForInTests,
- constructorCount,
+ constructorNames.length,
reason:
'If you added or removed an event constructor, '
'ensure you have updated '