Skip to content

Commit dd419d8

Browse files
[pigeon] Fix enum parameters in Dart test
Fixes the handling of enum parameters in the Dart test class generation for testing host APIs. Fixes flutter/flutter#105742
1 parent a09ee60 commit dd419d8

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.4
2+
3+
* [dart] Fixes enum parameter handling in Dart test API class.
4+
15
## 4.2.3
26

37
* [java] Adds assert `args != null`.

packages/pigeon/lib/dart_generator.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ void _writeFlutterApi(
276276
bool isMockHandler = false,
277277
}) {
278278
assert(api.location == ApiLocation.flutter);
279+
final List<String> customEnumNames =
280+
root.enums.map((Enum x) => x.name).toList();
279281
String codecName = _standardMessageCodec;
280282
if (getCodecClasses(api, root).isNotEmpty) {
281283
codecName = _getCodecName(api);
@@ -358,8 +360,14 @@ void _writeFlutterApi(
358360
_makeGenericTypeArguments(arg.type);
359361
final String castCall = _makeGenericCastCall(arg.type);
360362

361-
indent.writeln(
362-
'final $argType? $argName = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
363+
final String leftHandSide = 'final $argType? $argName';
364+
if (customEnumNames.contains(arg.type.baseName)) {
365+
indent.writeln(
366+
'$leftHandSide = $argsArray[$count] == null ? null : $argType.values[$argsArray[$count] as int];');
367+
} else {
368+
indent.writeln(
369+
'$leftHandSide = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
370+
}
363371
if (!arg.type.isNullable) {
364372
indent.writeln(
365373
"assert($argName != null, 'Argument for $channelName was null, expected non-null $argType.');");

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:mirrors';
99
import 'ast.dart';
1010

1111
/// The current version of pigeon. This must match the version in pubspec.yaml.
12-
const String pigeonVersion = '4.2.3';
12+
const String pigeonVersion = '4.2.4';
1313

1414
/// Read all the content from [stdin] to a String.
1515
String readStdin() {

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
5-
version: 4.2.3 # This must match the version in lib/generator_tools.dart
5+
version: 4.2.4 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

packages/pigeon/test/dart_generator_test.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,4 +1309,51 @@ name: foobar
13091309
final String code = sink.toString();
13101310
expect(code, contains('extends StandardMessageCodec'));
13111311
});
1312+
1313+
test('host test code handles enums', () {
1314+
final Root root = Root(
1315+
apis: <Api>[
1316+
Api(
1317+
name: 'Api',
1318+
location: ApiLocation.host,
1319+
dartHostTestHandler: 'ApiMock',
1320+
methods: <Method>[
1321+
Method(
1322+
name: 'doit',
1323+
returnType: const TypeDeclaration.voidDeclaration(),
1324+
arguments: <NamedType>[
1325+
NamedType(
1326+
type: const TypeDeclaration(
1327+
baseName: 'Enum',
1328+
isNullable: false,
1329+
),
1330+
name: 'anEnum')
1331+
])
1332+
])
1333+
],
1334+
classes: <Class>[],
1335+
enums: <Enum>[
1336+
Enum(
1337+
name: 'Enum',
1338+
members: <String>[
1339+
'one',
1340+
'two',
1341+
],
1342+
)
1343+
],
1344+
);
1345+
final StringBuffer sink = StringBuffer();
1346+
generateTestDart(
1347+
const DartOptions(),
1348+
root,
1349+
sink,
1350+
dartOutPath: 'code.dart',
1351+
testOutPath: 'test.dart',
1352+
);
1353+
final String testCode = sink.toString();
1354+
expect(
1355+
testCode,
1356+
contains(
1357+
'final Enum? arg_anEnum = args[0] == null ? null : Enum.values[args[0] as int]'));
1358+
});
13121359
}

0 commit comments

Comments
 (0)