Skip to content

Commit 8815fa3

Browse files
committed
Actually call argument-less methods.
I accidentally broke this in an earlier patch.
1 parent 6740a21 commit 8815fa3

11 files changed

Lines changed: 124 additions & 36 deletions

File tree

analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ linter:
3838
- always_specify_types
3939
- annotate_overrides
4040
# - avoid_annotating_with_dynamic # conflicts with always_specify_types
41-
# - avoid_as # conflicts with NNBD
41+
# - avoid_as # no longer relevant with null safety
4242
- avoid_bool_literals_in_conditional_expressions
4343
# - avoid_catches_without_on_clauses # we do this commonly
4444
# - avoid_catching_errors # we do this commonly

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.19
2+
3+
* Fixed a bug introduced in 0.1.17 where methods without arguments were
4+
no longer being called.
5+
16
## 0.1.18
27

38
* Null safe requires Dart 2.12.

packages/pigeon/lib/dart_generator.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void _writeFlutterApi(
8888
}) {
8989
assert(api.location == ApiLocation.flutter);
9090
final String nullTag = opt.isNullSafe ? '?' : '';
91+
final String unwrapOperator = opt.isNullSafe ? '!' : '';
9192
indent.write('abstract class ${api.name} ');
9293
indent.scoped('{', '}', () {
9394
for (Method func in api.methods) {
@@ -106,10 +107,10 @@ void _writeFlutterApi(
106107
indent.writeln(
107108
'const BasicMessageChannel<Object$nullTag> channel =',
108109
);
110+
final String channelName = channelNameFunc == null
111+
? makeChannelName(api, func)
112+
: channelNameFunc(func);
109113
indent.nest(2, () {
110-
final String channelName = channelNameFunc == null
111-
? makeChannelName(api, func)
112-
: channelNameFunc(func);
113114
indent.writeln(
114115
'BasicMessageChannel<Object$nullTag>(\'$channelName\', StandardMessageCodec());',
115116
);
@@ -134,16 +135,16 @@ void _writeFlutterApi(
134135
: func.returnType == 'void'
135136
? 'return;'
136137
: 'return null;';
137-
indent.write('if (message == null) ');
138-
indent.scoped('{', '}', () {
139-
indent.writeln(emptyReturnStatement);
140-
});
141138
String call;
142139
if (argType == 'void') {
140+
indent.writeln('// ignore message');
143141
call = 'api.${func.name}()';
144142
} else {
145143
indent.writeln(
146-
'final $argType input = $argType.decode(message);',
144+
'assert(message != null, \'Argument for $channelName was null. Expected $argType.\');',
145+
);
146+
indent.writeln(
147+
'final $argType input = $argType.decode(message$unwrapOperator);',
147148
);
148149
call = 'api.${func.name}(input)';
149150
}
@@ -213,7 +214,6 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
213214
if (klass.fields.isNotEmpty) {
214215
indent.writeln('');
215216
}
216-
indent.writeln('// ignore: unused_element');
217217
indent.write('Object encode() ');
218218
indent.scoped('{', '}', () {
219219
indent.writeln(
@@ -232,7 +232,6 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
232232
indent.writeln('return pigeonMap;');
233233
});
234234
indent.writeln('');
235-
indent.writeln('// ignore: unused_element');
236235
indent.write(
237236
'static ${klass.name} decode(Object message) ',
238237
);

packages/pigeon/lib/generator_tools.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'dart:io';
77
import 'dart:mirrors';
88
import 'ast.dart';
99

10-
/// The current version of pigeon.
11-
const String pigeonVersion = '0.1.18';
10+
/// The current version of pigeon. This must match the version in pubspec.yaml.
11+
const String pigeonVersion = '0.1.19';
1212

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

packages/pigeon/mock_handler_tester/test/message.dart

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
// Autogenerated from Pigeon (v0.1.15), do not edit directly.
1+
// Autogenerated from Pigeon (v0.1.19), do not edit directly.
22
// See also: https://pub.dev/packages/pigeon
33
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import
44
// @dart = 2.8
55
import 'dart:async';
66
import 'dart:typed_data' show Uint8List, Int32List, Int64List, Float64List;
7+
78
import 'package:flutter/services.dart';
89

910
class SearchReply {
1011
String result;
1112
String error;
1213

13-
// ignore: unused_element
1414
Object encode() {
1515
final Map<Object, Object> pigeonMap = <Object, Object>{};
1616
pigeonMap['result'] = result;
1717
pigeonMap['error'] = error;
1818
return pigeonMap;
1919
}
2020

21-
// ignore: unused_element
2221
static SearchReply decode(Object message) {
2322
final Map<Object, Object> pigeonMap = message as Map<Object, Object>;
2423
return SearchReply()
@@ -32,7 +31,6 @@ class SearchRequest {
3231
int anInt;
3332
bool aBool;
3433

35-
// ignore: unused_element
3634
Object encode() {
3735
final Map<Object, Object> pigeonMap = <Object, Object>{};
3836
pigeonMap['query'] = query;
@@ -41,7 +39,6 @@ class SearchRequest {
4139
return pigeonMap;
4240
}
4341

44-
// ignore: unused_element
4542
static SearchRequest decode(Object message) {
4643
final Map<Object, Object> pigeonMap = message as Map<Object, Object>;
4744
return SearchRequest()
@@ -54,14 +51,12 @@ class SearchRequest {
5451
class Nested {
5552
SearchRequest request;
5653

57-
// ignore: unused_element
5854
Object encode() {
5955
final Map<Object, Object> pigeonMap = <Object, Object>{};
6056
pigeonMap['request'] = request == null ? null : request.encode();
6157
return pigeonMap;
6258
}
6359

64-
// ignore: unused_element
6560
static Nested decode(Object message) {
6661
final Map<Object, Object> pigeonMap = message as Map<Object, Object>;
6762
return Nested()
@@ -81,9 +76,8 @@ abstract class FlutterSearchApi {
8176
channel.setMessageHandler(null);
8277
} else {
8378
channel.setMessageHandler((Object message) async {
84-
if (message == null) {
85-
return null;
86-
}
79+
assert(message != null,
80+
'Argument for dev.flutter.pigeon.FlutterSearchApi.search was null. Expected SearchRequest.');
8781
final SearchRequest input = SearchRequest.decode(message);
8882
final SearchReply output = api.search(input);
8983
return output.encode();
@@ -121,6 +115,30 @@ class NestedApi {
121115
}
122116

123117
class Api {
118+
Future<void> initialize() async {
119+
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
120+
'dev.flutter.pigeon.Api.initialize', StandardMessageCodec());
121+
final Map<Object, Object> replyMap =
122+
await channel.send(null) as Map<Object, Object>;
123+
if (replyMap == null) {
124+
throw PlatformException(
125+
code: 'channel-error',
126+
message: 'Unable to establish connection on channel.',
127+
details: null,
128+
);
129+
} else if (replyMap['error'] != null) {
130+
final Map<Object, Object> error =
131+
replyMap['error'] as Map<Object, Object>;
132+
throw PlatformException(
133+
code: error['code'] as String,
134+
message: error['message'] as String,
135+
details: error['details'],
136+
);
137+
} else {
138+
// noop
139+
}
140+
}
141+
124142
Future<SearchReply> search(SearchRequest arg) async {
125143
final Object encoded = arg.encode();
126144
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(

packages/pigeon/mock_handler_tester/test/test.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Autogenerated from Pigeon (v0.1.15), do not edit directly.
1+
// Autogenerated from Pigeon (v0.1.19), do not edit directly.
22
// See also: https://pub.dev/packages/pigeon
33
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import
44
// @dart = 2.8
@@ -19,9 +19,8 @@ abstract class TestNestedApi {
1919
channel.setMockMessageHandler(null);
2020
} else {
2121
channel.setMockMessageHandler((Object message) async {
22-
if (message == null) {
23-
return <Object, Object>{};
24-
}
22+
assert(message != null,
23+
'Argument for dev.flutter.pigeon.NestedApi.search was null. Expected Nested.');
2524
final Nested input = Nested.decode(message);
2625
final SearchReply output = api.search(input);
2726
return <Object, Object>{'result': output.encode()};
@@ -32,18 +31,31 @@ abstract class TestNestedApi {
3231
}
3332

3433
abstract class TestHostApi {
34+
void initialize();
3535
SearchReply search(SearchRequest arg);
3636
static void setup(TestHostApi api) {
37+
{
38+
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
39+
'dev.flutter.pigeon.Api.initialize', StandardMessageCodec());
40+
if (api == null) {
41+
channel.setMockMessageHandler(null);
42+
} else {
43+
channel.setMockMessageHandler((Object message) async {
44+
// ignore message
45+
api.initialize();
46+
return <Object, Object>{};
47+
});
48+
}
49+
}
3750
{
3851
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
3952
'dev.flutter.pigeon.Api.search', StandardMessageCodec());
4053
if (api == null) {
4154
channel.setMockMessageHandler(null);
4255
} else {
4356
channel.setMockMessageHandler((Object message) async {
44-
if (message == null) {
45-
return <Object, Object>{};
46-
}
57+
assert(message != null,
58+
'Argument for dev.flutter.pigeon.Api.search was null. Expected SearchRequest.');
4759
final SearchRequest input = SearchRequest.decode(message);
4860
final SearchReply output = api.search(input);
4961
return <Object, Object>{'result': output.encode()};

packages/pigeon/mock_handler_tester/test/widget_test.dart

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:io';
6+
7+
import 'package:flutter/services.dart';
58
import 'package:flutter_test/flutter_test.dart';
69

710
import 'message.dart';
811
import 'test.dart';
912

1013
class Mock implements TestHostApi {
11-
bool didCall = false;
14+
List<String> log = <String>[];
15+
16+
@override
17+
void initialize() {
18+
log.add('initialize');
19+
}
20+
1221
@override
1322
SearchReply search(SearchRequest arg) {
14-
didCall = true;
23+
log.add('search');
1524
return SearchReply()..result = arg.query;
1625
}
1726
}
@@ -46,7 +55,48 @@ void main() {
4655
final Mock mock = Mock();
4756
TestHostApi.setup(mock);
4857
final SearchReply reply = await api.search(SearchRequest()..query = 'foo');
49-
expect(mock.didCall, true);
58+
expect(mock.log, <String>['search']);
5059
expect(reply.result, 'foo');
5160
});
61+
62+
test('no-arg calls', () async {
63+
final Api api = Api();
64+
final Mock mock = Mock();
65+
TestHostApi.setup(mock);
66+
await api.initialize();
67+
expect(mock.log, <String>['initialize']);
68+
});
69+
70+
test(
71+
'calling methods with null',
72+
() async {
73+
final Mock mock = Mock();
74+
TestHostApi.setup(mock);
75+
expect(
76+
await const BasicMessageChannel<Object>(
77+
'dev.flutter.pigeon.Api.initialize',
78+
StandardMessageCodec(),
79+
).send(null),
80+
isEmpty,
81+
);
82+
try {
83+
await const BasicMessageChannel<Object>(
84+
'dev.flutter.pigeon.Api.search',
85+
StandardMessageCodec(),
86+
).send(null) as Map<Object, Object>;
87+
expect(true, isFalse); // should not reach here
88+
} catch (error) {
89+
expect(error, isAssertionError);
90+
expect(
91+
error.toString(),
92+
contains(
93+
'Argument for dev.flutter.pigeon.Api.search was null. Expected SearchRequest.',
94+
),
95+
);
96+
}
97+
expect(mock.log, <String>['initialize']);
98+
},
99+
// TODO(ianh): skip can be removed after first stable release in 2021
100+
skip: Platform.environment['CHANNEL'] == 'stable',
101+
);
52102
}

packages/pigeon/pigeons/message.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SearchReply {
2020

2121
@HostApi(dartHostTestHandler: 'TestHostApi')
2222
abstract class Api {
23+
void initialize();
2324
SearchReply search(SearchRequest request);
2425
}
2526

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pigeon
2-
version: 0.1.18
2+
version: 0.1.19 # This must match the version in lib/generator_tools.dart
33
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
44
homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
55
dependencies:

packages/pigeon/test/dart_generator_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ void main() {
144144
final StringBuffer sink = StringBuffer();
145145
generateDart(DartOptions(), root, sink);
146146
final String code = sink.toString();
147-
expect(code, isNot(matches('=.*doSomething')));
147+
// The next line verifies that we're not setting a variable to the value of "doSomething", but
148+
// ignores the line where we assert the value of the argument isn't null, since on that line
149+
// we mention "doSomething" in the assertion message.
150+
expect(code, isNot(matches('[^!]=.*doSomething')));
148151
expect(code, contains('doSomething('));
149152
expect(code, isNot(contains('.encode()')));
150153
});

0 commit comments

Comments
 (0)