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
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
tarrinneal committed Mar 28, 2024
commit 66b79863d20cb70a4661921851ffa9eda8b7913e
12 changes: 1 addition & 11 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
final String genericType = _makeGenericTypeArguments(field.type);
final String castCall = _makeGenericCastCall(field.type);
final String nullableTag = field.type.isNullable ? '?' : '';
if (field.type.isClass) {
final String nonNullValue = '$resultAt! as ${field.type.baseName}';
if (field.type.isNullable) {
indent.format('''
$resultAt != null
\t\t? $nonNullValue
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(nonNullValue);
}
} else if (field.type.isEnum) {
if (field.type.isEnum) {
final String nonNullValue =
'${field.type.baseName}.values[$resultAt! as int]';
if (field.type.isNullable) {
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
});
} else {
indent.addScoped('val wrapped: List<Any?> = try {', '}', () {
indent.writeScoped('val wrapped: List<Any?> = try {', '}', () {
if (returnType.isVoid) {
indent.writeln(call);
indent.writeln('listOf<Any?>(null)');
Expand Down
7 changes: 7 additions & 0 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,13 @@ List<Error> _validateAst(Root root, String source) {
lineNumber: _calculateLineNumberNullable(source, field.offset),
));
}
if (customEnums.contains(typeArgument.baseName)) {
result.add(Error(
message:
'Enum types aren\'t supported in type arguments in "${field.name}" in class "${classDefinition.name}".',
lineNumber: _calculateLineNumberNullable(source, field.offset),
));
}
}
if (!(validTypes.contains(field.type.baseName) ||
customClasses.contains(field.type.baseName) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ class AllNullableTypes {
result[13] != null ? AnEnum.values[result[13]! as int] : null,
aNullableString: result[14] as String?,
aNullableObject: result[15],
allNullableTypes:
result[16] != null ? result[16]! as AllNullableTypes : null,
allNullableTypes: result[16] as AllNullableTypes?,
);
}
}
Expand Down Expand Up @@ -358,10 +357,9 @@ class AllClassesWrapper {
result as List<Object?>;
return AllClassesWrapper(
allNullableTypes: result[0]! as AllNullableTypes,
allNullableTypesWithoutRecursion: result[1] != null
? result[1]! as AllNullableTypesWithoutRecursion
: null,
allTypes: result[2] != null ? result[2]! as AllTypes : null,
allNullableTypesWithoutRecursion:
result[1] as AllNullableTypesWithoutRecursion?,
allTypes: result[2] as AllTypes?,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class MessageNested {
static MessageNested decode(Object result) {
result as List<Object?>;
return MessageNested(
request: result[0] != null ? result[0]! as MessageSearchRequest : null,
request: result[0] as MessageSearchRequest?,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class NullFieldsSearchReply {
result: result[0] as String?,
error: result[1] as String?,
indices: (result[2] as List<Object?>?)?.cast<int?>(),
request: result[3] != null ? result[3]! as NullFieldsSearchRequest : null,
request: result[3] as NullFieldsSearchRequest?,
type: result[4] != null
? NullFieldsSearchReplyType.values[result[4]! as int]
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ void main() {
});

test('test reply decode with values', () {
final NullFieldsSearchReply reply = NullFieldsSearchReply.decode(<dynamic>[
'result',
'error',
<int>[1, 2, 3],
<dynamic>[
'query',
1,
],
NullFieldsSearchReplyType.success.index,
]);
final NullFieldsSearchReply reply =
NullFieldsSearchReply.decode(NullFieldsSearchReply(
result: 'result',
error: 'error',
indices: <int>[1, 2, 3],
request: NullFieldsSearchRequest(
query: 'query',
identifier: 1,
),
type: NullFieldsSearchReplyType.success,
).encode());

expect(reply.result, 'result');
expect(reply.error, 'error');
Expand Down Expand Up @@ -118,22 +119,21 @@ void main() {
});

test('test reply encode with values', () {
final NullFieldsSearchRequest request =
NullFieldsSearchRequest(query: 'query', identifier: 1);
final NullFieldsSearchReply reply = NullFieldsSearchReply(
result: 'result',
error: 'error',
indices: <int>[1, 2, 3],
request: NullFieldsSearchRequest(query: 'query', identifier: 1),
request: request,
type: NullFieldsSearchReplyType.success,
);

expect(reply.encode(), <Object?>[
'result',
'error',
<int>[1, 2, 3],
<Object?>[
'query',
1,
],
request,
NullFieldsSearchReplyType.success.index,
]);
});
Expand Down
9 changes: 5 additions & 4 deletions packages/pigeon/test/cpp_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ void main() {
expect(
code,
contains(
'nullable_nested_ ? EncodableValue(nullable_nested_->ToEncodableList()) '
': EncodableValue()'));
'nullable_nested_ ? CustomEncodableValue(*nullable_nested_) : EncodableValue())'));

// Serialization should use push_back, not initializer lists, to avoid
// copies.
Expand Down Expand Up @@ -805,13 +804,15 @@ void main() {
'non_nullable_nested_ = std::make_unique<Nested>(value_arg);'));
// Serialization uses the value directly.
expect(code, contains('EncodableValue(non_nullable_bool_)'));
expect(code, contains('non_nullable_nested_->ToEncodableList()'));
expect(code, contains('CustomEncodableValue(non_nullable_nested_)'));

// Serialization should use push_back, not initializer lists, to avoid
// copies.
expect(code, contains('list.reserve(4)'));
expect(
code, contains('list.push_back(EncodableValue(non_nullable_bool_))'));
code,
contains(
'list.push_back(CustomEncodableValue(non_nullable_nested_))'));
}
});

Expand Down
12 changes: 6 additions & 6 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ void main() {
expect(
code,
contains(
'nested?.encode(),',
'nested,',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
code,
contains(
'nested: result[0] != null ? Input.decode(result[0]! as List<Object?>) : null',
'nested: result[0] as Input?',
),
);
});
Expand Down Expand Up @@ -295,13 +295,13 @@ void main() {
expect(
code,
contains(
'nested.encode(),',
'nested,',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
code,
contains(
'nested: Input.decode(result[0]! as List<Object?>)',
'nested: result[0]! as Input',
),
);
});
Expand Down
7 changes: 2 additions & 5 deletions packages/pigeon/test/java_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,8 @@ void main() {
expect(code, contains('public static final class Outer'));
expect(code, contains('public static final class Nested'));
expect(code, contains('private @Nullable Nested nested;'));
expect(
code,
contains(
'(nested == null) ? null : Nested.fromList((ArrayList<Object>) nested)'));
expect(code, contains('add((nested == null) ? null : nested.toList());'));
;
expect(code, contains('add(nested);'));
});

test('gen one async Host Api', () {
Expand Down
34 changes: 16 additions & 18 deletions packages/pigeon/test/kotlin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main() {
final String code = sink.toString();
expect(code, contains('data class Foobar ('));
expect(code, contains('val field1: Long? = null'));
expect(code, contains('fun fromList(list: List<Any?>): Foobar'));
expect(code, contains('fun fromList(__pigeon_list: List<Any?>): Foobar'));
expect(code, contains('fun toList(): List<Any?>'));
});

Expand Down Expand Up @@ -132,9 +132,10 @@ void main() {
expect(code, contains('data class Bar ('));
expect(code, contains('val field1: Foo,'));
expect(code, contains('val field2: String'));
expect(code, contains('fun fromList(list: List<Any?>): Bar'));
expect(code, contains('val field1 = Foo.ofRaw(list[0] as Int)!!\n'));
expect(code, contains('val field2 = list[1] as String\n'));
expect(code, contains('fun fromList(__pigeon_list: List<Any?>): Bar'));
expect(
code, contains('val field1 = Foo.ofRaw(__pigeon_list[0] as Int)!!\n'));
expect(code, contains('val field2 = __pigeon_list[1] as String\n'));
expect(code, contains('fun toList(): List<Any?>'));
});

Expand Down Expand Up @@ -236,11 +237,10 @@ void main() {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val inputArg = args[0] as Input
var wrapped: List<Any?>
try {
wrapped = listOf<Any?>(api.doSomething(inputArg))
val wrapped: List<Any?> = try {
listOf<Any?>(api.doSomething(inputArg))
} catch (exception: Throwable) {
wrapped = wrapError(exception)
wrapError(exception)
}
reply.reply(wrapped)
}
Expand Down Expand Up @@ -390,7 +390,7 @@ void main() {
expect(
code,
contains(
'val aInt = list[1].let { if (it is Int) it.toLong() else it as Long }'));
'val aInt = __pigeon_list[1].let { if (it is Int) it.toLong() else it as Long }'));
expect(code, contains('val aNullableBool: Boolean? = null'));
expect(code, contains('val aNullableInt: Long? = null'));
expect(code, contains('val aNullableDouble: Double? = null'));
Expand All @@ -402,7 +402,7 @@ void main() {
expect(
code,
contains(
'val aNullableInt = list[9].let { if (it is Int) it.toLong() else it as Long? }'));
'val aNullableInt = __pigeon_list[9].let { if (it is Int) it.toLong() else it as Long? }'));
});

test('gen one flutter api', () {
Expand Down Expand Up @@ -589,8 +589,8 @@ void main() {
);
final String code = sink.toString();
expect(code, contains('fun doSomething(): Output'));
expect(code, contains('wrapped = listOf<Any?>(api.doSomething())'));
expect(code, contains('wrapped = wrapError(exception)'));
expect(code, contains('listOf<Any?>(api.doSomething())'));
expect(code, contains('wrapError(exception)'));
expect(code, contains('reply(wrapped)'));
});

Expand Down Expand Up @@ -730,10 +730,8 @@ void main() {
expect(code, contains('data class Outer'));
expect(code, contains('data class Nested'));
expect(code, contains('val nested: Nested? = null'));
expect(code, contains('fun fromList(list: List<Any?>): Outer'));
expect(
code, contains('val nested: Nested? = (list[0] as List<Any?>?)?.let'));
expect(code, contains('Nested.fromList(it)'));
expect(code, contains('fun fromList(__pigeon_list: List<Any?>): Outer'));
expect(code, contains('val nested = __pigeon_list[0] as Nested?'));
expect(code, contains('fun toList(): List<Any?>'));
});

Expand Down Expand Up @@ -1089,7 +1087,7 @@ void main() {
);
final String code = sink.toString();
expect(code, contains('fun doit(): List<Long?>'));
expect(code, contains('wrapped = listOf<Any?>(api.doit())'));
expect(code, contains('listOf<Any?>(api.doit())'));
expect(code, contains('reply.reply(wrapped)'));
});

Expand Down Expand Up @@ -1167,7 +1165,7 @@ void main() {
code,
contains(
'val yArg = args[1].let { if (it is Int) it.toLong() else it as Long }'));
expect(code, contains('wrapped = listOf<Any?>(api.add(xArg, yArg))'));
expect(code, contains('listOf<Any?>(api.add(xArg, yArg))'));
expect(code, contains('reply.reply(wrapped)'));
});

Expand Down
10 changes: 5 additions & 5 deletions packages/pigeon/test/objc_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,10 @@ void main() {
);
final String code = sink.toString();
expect(code, contains('@implementation Foobar'));
expect(code,
contains('pigeonResult.aBool = GetNullableObjectAtIndex(list, 0);'));
expect(
code,
contains(
'pigeonResult.aBool = GetNullableObjectAtIndex(__pigeon_list, 0);'));
});

test('nested class header', () {
Expand Down Expand Up @@ -603,9 +605,7 @@ void main() {
expect(
code,
contains(
'pigeonResult.nested = [Input nullableFromList:(GetNullableObjectAtIndex(list, 0))];'));
expect(
code, contains('self.nested ? [self.nested toList] : [NSNull null]'));
'pigeonResult.nested = GetNullableObjectAtIndex(__pigeon_list, 0);'));
});

test('prefix class header', () {
Expand Down
8 changes: 5 additions & 3 deletions packages/pigeon/test/swift_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void main() {
final String code = sink.toString();
expect(code, contains('struct Foobar'));
expect(code, contains('var field1: Int64? = nil'));
expect(code, contains('static func fromList(_ list: [Any?]) -> Foobar?'));
expect(code,
contains('static func fromList(_ __pigeon_list: [Any?]) -> Foobar?'));
expect(code, contains('func toList() -> [Any?]'));
expect(code, isNot(contains('if (')));
});
Expand Down Expand Up @@ -572,8 +573,9 @@ void main() {
expect(code, contains('struct Outer'));
expect(code, contains('struct Nested'));
expect(code, contains('var nested: Nested? = nil'));
expect(code, contains('static func fromList(_ list: [Any?]) -> Outer?'));
expect(code, contains('nested = Nested.fromList(nestedList)'));
expect(code,
contains('static func fromList(_ __pigeon_list: [Any?]) -> Outer?'));
expect(code, contains('nested = nestedList as Nested'));
expect(code, contains('func toList() -> [Any?]'));
expect(code, isNot(contains('if (')));
// Single-element list serializations should not have a trailing comma.
Expand Down