[pigeon] Fix enum parameters in Dart test (#2749)
diff --git a/.cirrus.yml b/.cirrus.yml
index 5b73ffb..ef93b57 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -118,6 +118,18 @@
- else
- ./script/tool_runner.sh version-check --check-for-missing-changes --pr-labels="$CIRRUS_PR_LABELS"
- fi
+ publishable_bug_workaround_script:
+ # Workaround for https://github.com/dart-lang/pub/issues/3618
+ # TODO(stuartmorgan): Remove this once that issue is fixed.
+ - cd packages/pigeon
+ - pushd mock_handler_tester; flutter pub get; popd
+ - pushd e2e_tests/test_objc; flutter pub get; popd
+ - pushd platform_tests/ios_swift_unit_tests; flutter pub get; popd
+ - pushd platform_tests/windows_unit_tests; flutter pub get; popd
+ - pushd platform_tests/ios_unit_tests; flutter pub get; popd
+ - pushd platform_tests/flutter_null_safe_unit_tests; flutter pub get; popd
+ - pushd platform_tests/android_kotlin_unit_tests; flutter pub get; popd
+ - pushd platform_tests/android_unit_tests; flutter pub get; popd
publishable_script: ./script/tool_runner.sh publish-check --allow-pre-release
- name: dart_unit_tests
env:
diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md
index ddf96c8..cdebafe 100644
--- a/packages/pigeon/CHANGELOG.md
+++ b/packages/pigeon/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 4.2.5
+
+* [dart] Fixes enum parameter handling in Dart test API class.
+
## 4.2.4
* [kotlin] Fixes Kotlin generated sync host api error.
diff --git a/packages/pigeon/lib/dart_generator.dart b/packages/pigeon/lib/dart_generator.dart
index 70da7ca..f87297a 100644
--- a/packages/pigeon/lib/dart_generator.dart
+++ b/packages/pigeon/lib/dart_generator.dart
@@ -276,6 +276,8 @@
bool isMockHandler = false,
}) {
assert(api.location == ApiLocation.flutter);
+ final List<String> customEnumNames =
+ root.enums.map((Enum x) => x.name).toList();
String codecName = _standardMessageCodec;
if (getCodecClasses(api, root).isNotEmpty) {
codecName = _getCodecName(api);
@@ -358,8 +360,14 @@
_makeGenericTypeArguments(arg.type);
final String castCall = _makeGenericCastCall(arg.type);
- indent.writeln(
- 'final $argType? $argName = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
+ final String leftHandSide = 'final $argType? $argName';
+ if (customEnumNames.contains(arg.type.baseName)) {
+ indent.writeln(
+ '$leftHandSide = $argsArray[$count] == null ? null : $argType.values[$argsArray[$count] as int];');
+ } else {
+ indent.writeln(
+ '$leftHandSide = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
+ }
if (!arg.type.isNullable) {
indent.writeln(
"assert($argName != null, 'Argument for $channelName was null, expected non-null $argType.');");
diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart
index bc58a70..7c988f8 100644
--- a/packages/pigeon/lib/generator_tools.dart
+++ b/packages/pigeon/lib/generator_tools.dart
@@ -9,7 +9,7 @@
import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml.
-const String pigeonVersion = '4.2.4';
+const String pigeonVersion = '4.2.5';
/// Read all the content from [stdin] to a String.
String readStdin() {
diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml
index 42674d6..7d5060f 100644
--- a/packages/pigeon/pubspec.yaml
+++ b/packages/pigeon/pubspec.yaml
@@ -2,7 +2,7 @@
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
-version: 4.2.4 # This must match the version in lib/generator_tools.dart
+version: 4.2.5 # This must match the version in lib/generator_tools.dart
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/pigeon/test/dart_generator_test.dart b/packages/pigeon/test/dart_generator_test.dart
index 582b29e..1c16311 100644
--- a/packages/pigeon/test/dart_generator_test.dart
+++ b/packages/pigeon/test/dart_generator_test.dart
@@ -1309,4 +1309,51 @@
final String code = sink.toString();
expect(code, contains('extends StandardMessageCodec'));
});
+
+ test('host test code handles enums', () {
+ final Root root = Root(
+ apis: <Api>[
+ Api(
+ name: 'Api',
+ location: ApiLocation.host,
+ dartHostTestHandler: 'ApiMock',
+ methods: <Method>[
+ Method(
+ name: 'doit',
+ returnType: const TypeDeclaration.voidDeclaration(),
+ arguments: <NamedType>[
+ NamedType(
+ type: const TypeDeclaration(
+ baseName: 'Enum',
+ isNullable: false,
+ ),
+ name: 'anEnum')
+ ])
+ ])
+ ],
+ classes: <Class>[],
+ enums: <Enum>[
+ Enum(
+ name: 'Enum',
+ members: <String>[
+ 'one',
+ 'two',
+ ],
+ )
+ ],
+ );
+ final StringBuffer sink = StringBuffer();
+ generateTestDart(
+ const DartOptions(),
+ root,
+ sink,
+ dartOutPath: 'code.dart',
+ testOutPath: 'test.dart',
+ );
+ final String testCode = sink.toString();
+ expect(
+ testCode,
+ contains(
+ 'final Enum? arg_anEnum = args[0] == null ? null : Enum.values[args[0] as int]'));
+ });
}