Update test, move default bool value logic to compile time
diff --git a/protoc_plugin/Makefile b/protoc_plugin/Makefile
index a7556e3..a1b6270 100644
--- a/protoc_plugin/Makefile
+++ b/protoc_plugin/Makefile
@@ -47,6 +47,7 @@
nested_extension \
non_nested_extension \
oneof \
+ optional_bool \
reserved_names \
reserved_names_extension \
reserved_names_message \
diff --git a/protoc_plugin/lib/src/protobuf_field.dart b/protoc_plugin/lib/src/protobuf_field.dart
index 7f8dad6..9b14e98 100644
--- a/protoc_plugin/lib/src/protobuf_field.dart
+++ b/protoc_plugin/lib/src/protobuf_field.dart
@@ -291,7 +291,7 @@
if (isRepeated) return 'null';
switch (descriptor.type) {
case FieldDescriptorProto_Type.TYPE_BOOL:
- return _getDefaultAsBoolExpr('false');
+ return _getDefaultAsBoolExpr();
case FieldDescriptorProto_Type.TYPE_INT32:
case FieldDescriptorProto_Type.TYPE_UINT32:
case FieldDescriptorProto_Type.TYPE_SINT32:
@@ -313,7 +313,7 @@
assert(!isRepeated);
switch (descriptor.type) {
case FieldDescriptorProto_Type.TYPE_BOOL:
- return _getDefaultAsBoolExpr(null);
+ return _getDefaultAsBoolExpr();
case FieldDescriptorProto_Type.TYPE_FLOAT:
case FieldDescriptorProto_Type.TYPE_DOUBLE:
if (!descriptor.hasDefaultValue()) {
@@ -379,11 +379,12 @@
}
}
- String _getDefaultAsBoolExpr(String noDefault) {
- if (descriptor.hasDefaultValue() && 'false' != descriptor.defaultValue) {
+ String _getDefaultAsBoolExpr() {
+ if (descriptor.hasDefaultValue()) {
return descriptor.defaultValue;
+ } else {
+ return 'false';
}
- return noDefault;
}
String _getDefaultAsStringExpr(String noDefault) {
diff --git a/protoc_plugin/test/all_tests.dart b/protoc_plugin/test/all_tests.dart
index d85093f..02ff1e1 100755
--- a/protoc_plugin/test/all_tests.dart
+++ b/protoc_plugin/test/all_tests.dart
@@ -34,6 +34,7 @@
import 'omit_field_names_test.dart' as omit_field_names;
import 'omit_message_names_test.dart' as omit_message_names;
import 'oneof_test.dart' as oneof;
+import 'optional_bool.dart' as optional_bool;
import 'proto3_optional_test.dart' as proto3_optional;
import 'protoc_options_test.dart' as protoc_options;
import 'repeated_field_test.dart' as repeated_field;
@@ -76,6 +77,7 @@
omit_field_names.main();
omit_message_names.main();
oneof.main();
+ optional_bool.main();
proto3_optional.main();
protoc_options.main();
repeated_field.main();
diff --git a/protoc_plugin/test/optional_bool.dart b/protoc_plugin/test/optional_bool.dart
new file mode 100644
index 0000000..350a613
--- /dev/null
+++ b/protoc_plugin/test/optional_bool.dart
@@ -0,0 +1,20 @@
+import 'package:test/test.dart';
+
+import '../out/protos/optional_bool.pb.dart';
+
+void main() {
+ test('Non optional bool default', () {
+ var msg = OptionalBoolTest();
+ expect(msg.nonOptionalBool, false);
+ });
+
+ test('Optional bool default', () {
+ var msg = OptionalBoolTest();
+ expect(msg.optionalBool, false);
+ });
+
+ test('Optional bool, default specified', () {
+ var msg = OptionalBoolTest();
+ expect(msg.optionalBoolWithDefault, true);
+ });
+}
diff --git a/protoc_plugin/test/protos/optional_bool.proto b/protoc_plugin/test/protos/optional_bool.proto
new file mode 100644
index 0000000..2c46209
--- /dev/null
+++ b/protoc_plugin/test/protos/optional_bool.proto
@@ -0,0 +1,14 @@
+// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+syntax = "proto2";
+
+package third_party.dart.protoc_plugin.test.protos;
+
+message OptionalBoolTest {
+ required bool non_optional_bool = 1;
+ optional bool optional_bool = 2;
+ optional bool optional_bool_with_default = 3 [default = true];
+}
+
diff --git a/protoc_plugin/test/protos/proto3_optional.proto b/protoc_plugin/test/protos/proto3_optional.proto
index 3064d19..af1af78 100644
--- a/protoc_plugin/test/protos/proto3_optional.proto
+++ b/protoc_plugin/test/protos/proto3_optional.proto
@@ -11,8 +11,6 @@
int32 non_optional_field = 2;
optional Submessage optional_submessage = 3;
Submessage non_optional_submessage = 4;
- bool non_optional_bool = 5;
- optional bool optional_bool = 6;
}
message Submessage {