Skip to content

[pigeon] Fix enum parameters in Dart test #2749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ task:
- 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:
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 10 additions & 2 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ void _writeFlutterApi(
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);
Expand Down Expand Up @@ -358,8 +360,14 @@ void _writeFlutterApi(
_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.');");
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'dart:mirrors';
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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
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"
Expand Down
47 changes: 47 additions & 0 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1309,4 +1309,51 @@ name: foobar
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]'));
});
}