Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.4

* [dart] Fixes enum parameter handling in Dart test API class.

## 4.2.3

* [java] Adds assert `args != null`.
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.3';
const String pigeonVersion = '4.2.4';

/// 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.3 # This must match the version in lib/generator_tools.dart
version: 4.2.4 # 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]'));
});
}