Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Unified Diff: tests/lib/protobuf/protobuf_client_test.dart

Issue 10595002: Protocol Buffer runtime library and 'protoc' plugin (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Work around http://code.google.com/p/dart/issues/detail?id=3806 Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/lib/lib.status ('k') | tests/lib/protobuf/test_util.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/protobuf/protobuf_client_test.dart
diff --git a/tests/lib/protobuf/protobuf_client_test.dart b/tests/lib/protobuf/protobuf_client_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e79d5c9f6607ac3c8c8fe5832d84037be5406b2a
--- /dev/null
+++ b/tests/lib/protobuf/protobuf_client_test.dart
@@ -0,0 +1,1763 @@
+// Copyright (c) 2012, 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.
+
+#library("protobuftest");
+#import('../../../lib/protobuf/runtime/Protobuf_client.dart');
+#source('../../../lib/protobuf/plugin/protoc/descriptor.pb.dart');
+#source('test_util.dart');
+#source('unittest.pb.dart');
+#source('unittest_import.pb.dart');
+
+void main() {
+ // CodedBufferReader tests
+ testReadVarint();
+ testReadWholeMessage();
+ testSkipWholeMessage();
+ testReadHugeBlob();
+ testReadMaliciouslyLargeBlob();
+ testMaliciousRecursion();
+ testSizeLimit();
+ testReadInvalidUtf8();
+ testInvalidTag();
+
+ // CodedBufferWriter tests
+ testWriteWholeMessage();
+ testWriteWholePackedFieldsMessage();
+ testWriteMessageWithNegativeEnumValue();
+
+ // Wire protocol tests
+ testBuffer();
+ testSerialization();
+ testSerializationPacked();
+ testSerializeExtensions();
+ testSerializePackedExtensions();
+ testSerializationPackedWithoutGetSerializedSize();
+ testParseExtensions();
+ testParsePackedExtensions();
+ testExtensionsSerializedSize();
+
+ // GeneratedMessage tests
+ testMessageOrBuilder();
+ testUsingBuilderMultipleTimes();
+ testProtosShareRepeatedArraysIfDidntChange();
+ testRepeatedArraysAreImmutable();
+ testSettersRejectNull();
+ testRepeatedSetters();
+ testRepeatedSettersRejectNull();
+ testRepeatedAppend();
+ testRepeatedAppendRejectsNull();
+ testSettingForeignMessageUsingBuilder();
+ testSettingRepeatedForeignMessageUsingBuilder();
+ testDefaults();
+ testClear();
+ testEnumInterface();
+ testEnumMap();
+ testParsePackedToUnpacked();
+ testParseUnpackedToPacked();
+ testExtensionMessageOrBuilder();
+ testExtensionRepeatedSetters();
+ testExtensionDefaults();
+ testClearExtension();
+ testExtensionCopy();
+ testExtensionMergeFrom();
+ testToBuilder();
+ testRecursiveMessageDefaultInstance();
+ testSerialize();
+ testSerializePartial();
+ testEnumValues();
+ testBadExtension();
+
+ // JSON tests
+ testOutput();
+ testBase64Encode();
+ testBase64Decode();
+ testParse();
+ testExtensionsOutput();
+ testExtensionsParse();
+
+ // Message tests
+ setUpMessageTests();
+ testMergeFrom();
+ testRequired();
+ testRequiredForeign();
+ testRequiredExtension();
+ testUninitializedException();
+ testBuildPartial();
+ testNestedUninitializedException();
+ testBuildNestedPartial();
+ testParseUnititialized();
+ testParseNestedUnititialized();
+
+ // UnknownFieldSet tests
+ setUpUnknownFieldSetTests();
+ testVarint();
+ testFixed32();
+ testFixed64();
+ testLengthDelimited();
+ testGroup();
+ testSerialize2();
+ testCopyFrom();
+ testMergeFrom2();
+ testClear2();
+ testEmpty();
+ testClearMessage();
+ testParseKnownAndUnknown();
+ testWrongTypeTreatedAsUnknown();
+ testUnknownExtensions();
+ testWrongExtensionTypeTreatedAsUnknown();
+ testParseUnknownEnumValue();
+ testLargeVarint();
+ testEqualsAndHashCode();
+}
+
+void testReadVarint() {
+ _assertReadRawVarint([0x00], [0x00, 0x00, 0x00, 0x00]);
+ _assertReadRawVarint([0x01], [0x01, 0x00, 0x00, 0x00]);
+ _assertReadRawVarint([0x7f], [0x7f, 0x00, 0x00, 0x00]);
+ _assertReadRawVarint([0xa2, 0x74], [0xa2, 0x74, 0x00, 0x00]);
+ _assertReadRawVarint([0xbe, 0xf7, 0x92, 0x84, 0x0b], [0xbe, 0xf7, 0x92,
+ 0x84, 0x0b, 0x00, 0x00, 0x00, 0x00]);
+ _assertReadRawVarint(
+ [0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01],
+ [0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01, 0x00]);
+ _assertReadVarintFailure(<int>[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
+ 0x80, 0x80, 0x80, 0x00]);
+ _assertReadVarintFailure(<int>[0x80]);
+}
+
+void _assertReadRawVarint(List<int> expected, List<int> src) {
+ CodedBufferReader reader = new CodedBufferReader.fromBuffer(src);
+ Expect.listEquals(expected, reader.readRawVarint());
+}
+
+void _assertReadVarintFailure(List<int> src) {
+ try {
+ CodedBufferReader reader = new CodedBufferReader.fromBuffer(src);
+ reader.readRawVarint();
+ Expect.fail("Should throw exception");
+ } catch (InvalidProtocolBufferException e) {
+ // success
+ }
+}
+
+void testReadWholeMessage() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+ List<int> rawBytes = message.writeToBuffer();
+ Expect.equals(rawBytes.length, message.getSerializedSize());
+
+ Protobuf_Unittest_TestAllTypes message2 =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes);
+ assertAllFieldsSet(message2);
+}
+
+/** Tests skipField(). */
+void testSkipWholeMessage() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+ List<int> rawBytes = message.writeToBuffer();
+
+ // Create two parallel inputs. Parse one as unknown fields while using
+ // skipField() to skip each field on the other. Expect the same tags.
+ CodedBufferReader input1 = new CodedBufferReader.fromBuffer(rawBytes);
+ CodedBufferReader input2 = new CodedBufferReader.fromBuffer(rawBytes);
+ UnknownFieldSet_Builder unknownFields = new UnknownFieldSet_Builder();
+
+ while (true) {
+ int input1Tag = input1.readTag();
+ int input2Tag = input2.readTag();
+ Expect.equals(input1Tag, input2Tag);
+ if (input1Tag == 0) {
+ break;
+ }
+ unknownFields.mergeFieldFromBuffer(input1Tag, input1);
+ input2.skipField(input2Tag);
+ }
+}
+
+void testReadHugeBlob() {
+ // Allocate and initialize a 1MB blob.
+ List<int> blob = new List<int>(1 << 20);
+ for (int i = 0; i < blob.length; i++) {
+ blob[i] = i % 256;
+ }
+
+ // Make a message containing it.
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ setAllFields(builder);
+ builder.optionalBytes = blob;
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+
+ Protobuf_Unittest_TestAllTypes message2 =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(message.writeToBuffer());
+
+ Expect.listEquals(message.optionalBytes, message2.optionalBytes);
+}
+
+void testReadMaliciouslyLargeBlob() {
+ CodedBufferWriter output = new CodedBufferWriter(38);
+
+ int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
+ output.writeRawBytes(PbCodec.int32ToBytes(tag));
+ output.writeInt32NoTag(0x7FFFFFFF);
+ // Pad with a few random bytes.
+ List<int> randomBits = <int>[];
+ randomBits.insertRange(0, 32, 47);
+ output.writeRawBytes(randomBits);
+
+ CodedBufferReader input = new CodedBufferReader.fromBuffer(output.buffer);
+ Expect.equals(tag, input.readTag());
+
+ try {
+ input.readBytes();
+ Expect.fail("Should have thrown an exception!");
+ } catch (InvalidProtocolBufferException e) {
+ // success.
+ }
+}
+
+void testMaliciousRecursion() {
+ List<int> data64 = _makeRecursiveMessage(64).writeToBuffer();
+ List<int> data65 = _makeRecursiveMessage(65).writeToBuffer();
+
+ _assertMessageDepth(
+ Protobuf_Unittest_TestRecursiveMessage.parseFromBuffer(data64), 64);
+
+ try {
+ Protobuf_Unittest_TestRecursiveMessage.parseFromBuffer(data65);
+ Expect.fail("Should have thrown an exception!");
+ } catch (InvalidProtocolBufferException e) {
+ // success.
+ }
+
+ CodedBufferReader input = new CodedBufferReader.fromBuffer(data64, 8);
+ try {
+ // uncomfortable alternative to below...
+ new Protobuf_Unittest_TestRecursiveMessage_Builder()
+ .mergeFromCodedBufferReader(input);
+ Expect.fail("Should have thrown an exception!");
+ } catch (InvalidProtocolBufferException e) {
+ // success.
+ }
+}
+
+Protobuf_Unittest_TestRecursiveMessage _makeRecursiveMessage(int depth) {
+ if (depth == 0) {
+ Protobuf_Unittest_TestRecursiveMessage_Builder builder =
+ new Protobuf_Unittest_TestRecursiveMessage_Builder();
+ builder.i = 5;
+ return builder.build();
+ } else {
+ Protobuf_Unittest_TestRecursiveMessage_Builder builder =
+ new Protobuf_Unittest_TestRecursiveMessage_Builder();
+ builder.a = _makeRecursiveMessage(depth - 1);
+ return builder.build();
+ }
+}
+
+void _assertMessageDepth(Protobuf_Unittest_TestRecursiveMessage message,
+ int depth) {
+ if (depth == 0) {
+ Expect.isFalse(message.hasA());
+ Expect.equals(5, message.i);
+ } else {
+ Expect.isTrue(message.hasA());
+ _assertMessageDepth(message.a, depth - 1);
+ }
+}
+
+void testSizeLimit() {
+ CodedBufferReader input = new CodedBufferReader.fromBuffer(
+ getAllSet().writeToBuffer(), sizeLimit: 16);
+
+ try {
+ // uncomfortable alternative to below...
+ new Protobuf_Unittest_TestAllTypes_Builder()
+ .mergeFromCodedBufferReader(input);
+ Expect.fail("Size limit too small, should have thrown an exception!");
+ } catch (InvalidProtocolBufferException e) {
+ // success.
+ }
+}
+
+/**
+ * Tests that if we read an string that contains invalid UTF-8, no exception
+ * is thrown. Instead, the invalid bytes are replaced with the Unicode
+ * "replacement character" U+FFFD.
+ */
+void testReadInvalidUtf8() {
+ CodedBufferWriter writer = new CodedBufferWriter(3);
+ writer.writeBytes(1, [0x80]);
+
+ CodedBufferReader input = new CodedBufferReader.fromBuffer(writer.buffer);
+ Expect.equals(
+ WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED),
+ input.readTag());
+ String text = input.readString();
+ Expect.equals(0xfffd, text.charCodeAt(0));
+}
+
+void testInvalidTag() {
+ // Any tag number which corresponds to field number zero is invalid and
+ // should throw InvalidProtocolBufferException.
+ for (int i = 0; i < 8; i++) {
+ try {
+ new CodedBufferReader.fromBuffer([i]).readTag();
+ Expect.fail("Should have thrown an exception.");
+ } catch (InvalidProtocolBufferException e) {
+ // success
+ }
+ }
+}
+
+void testWriteWholeMessage() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+
+ List<int> rawBytes = message.writeToBuffer();
+ Expect.listEquals(goldenMessage, rawBytes);
+}
+
+void testWriteWholePackedFieldsMessage() {
+ List<int> rawBytes = getPackedSet().writeToBuffer();
+ Expect.listEquals(goldenPackedMessage, rawBytes);
+}
+
+void testWriteMessageWithNegativeEnumValue() {
+ Protobuf_Unittest_SparseEnumMessage_Builder builder =
+ new Protobuf_Unittest_SparseEnumMessage_Builder();
+ builder.sparseEnum = Protobuf_Unittest_TestSparseEnum.SPARSE_E;
+ Protobuf_Unittest_SparseEnumMessage message = builder.build();
+ Expect.isTrue(message.sparseEnum.value < 0, "enum.value should be -53452");
+ List<int> buffer = message.writeToBuffer();
+ Protobuf_Unittest_SparseEnumMessage message2 =
+ Protobuf_Unittest_SparseEnumMessage.parseFromBuffer(buffer);
+ Expect.equals(Protobuf_Unittest_TestSparseEnum.SPARSE_E,
+ message2.sparseEnum, "should resolve back to SPARSE_E");
+
+}
+
+void testBuffer() {
+ GoogleProtobuf_DescriptorProto_ExtensionRange_Builder builder =
+ new GoogleProtobuf_DescriptorProto_ExtensionRange_Builder();
+ builder.start = 1;
+ builder.end = 10000;
+ GoogleProtobuf_DescriptorProto_ExtensionRange range = builder.build();
+
+ List<int> wireFormat = range.writeToBuffer();
+ Expect.equals(8, wireFormat[0]); // tag 1, type 0 (varint)
+ Expect.equals(1, wireFormat[1]); // value 1
+ Expect.equals(16, wireFormat[2]); // tag 2, type 0 (varint)
+ Expect.equals(144, wireFormat[3]); // value 10000 = (78 << 7) + (144 & 0x7f);
+ Expect.equals(78, wireFormat[4]);
+
+ // Set values manually
+ wireFormat[1] = 120;
+ wireFormat[3] = 185;
+ wireFormat[4] = 96;
+
+ GoogleProtobuf_DescriptorProto_ExtensionRange range2 =
+ GoogleProtobuf_DescriptorProto_ExtensionRange.parseFromBuffer(wireFormat);
+ Expect.equals(120, range2.start);
+ Expect.equals(12345, range2.end);
+}
+
+void testSerialization() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+
+ List<int> rawBytes = message.writeToBuffer();
+ Expect.equals(rawBytes.length, message.getSerializedSize());
+
+ Protobuf_Unittest_TestAllTypes message2 =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes);
+ assertAllFieldsSet(message2);
+}
+
+
+void testSerializationPacked() {
+ Protobuf_Unittest_TestPackedTypes message = getPackedSet();
+
+ List<int> rawBytes = message.writeToBuffer();
+ Expect.equals(rawBytes.length, message.getSerializedSize());
+
+ Protobuf_Unittest_TestPackedTypes message2 =
+ Protobuf_Unittest_TestPackedTypes.parseFromBuffer(rawBytes);
+ assertPackedFieldsSet(message2);
+}
+
+void testSerializeExtensions() {
+ Protobuf_Unittest_TestAllExtensions message = getAllExtensionsSet();
+ List<int> rawBytes = message.writeToBuffer();
+ Expect.equals(rawBytes.length, message.getSerializedSize());
+
+ Protobuf_Unittest_TestAllTypes message2 =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes);
+ assertAllFieldsSet(message2);
+}
+
+void testSerializePackedExtensions() {
+ Protobuf_Unittest_TestPackedExtensions message = getPackedExtensionsSet();
+ List<int> rawBytes = message.writeToBuffer();
+
+ Protobuf_Unittest_TestPackedTypes message2 = getPackedSet();
+ List<int> rawBytes2 = message2.writeToBuffer();
+
+ Expect.listEquals(rawBytes, rawBytes2);
+}
+
+void testSerializationPackedWithoutGetSerializedSize() {
+ // At one point, the Java impl. had a bug where size was not computed
+ // unless the getSerializedSize() was called on the message outside
+ // of the writing of the message.
+ Protobuf_Unittest_TestPackedTypes message = getPackedSet();
+
+ // Directly construct a CodedBufferWriter around a buffer, in case
+ // writeToBuffer() invokes getSerializedSize();
+ CodedBufferWriter writer = new CodedBufferWriter(10000);
+ message.writeToCodedBufferWriter(writer);
+
+ List<int> buffer = writer.buffer.getRange(0, writer.length);
+ Protobuf_Unittest_TestPackedTypes message2 =
+ Protobuf_Unittest_TestPackedTypes.parseFromBuffer(buffer);
+
+ assertPackedFieldsSet(message2);
+}
+
+void testParseExtensions() {
+ // TestAllTypes and TestAllExtensions should have compatible wire formats,
+ // so if we serialize a TestAllTypes then parse it as TestAllExtensions
+ // it should work.
+
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+ List<int> rawBytes = message.writeToBuffer();
+
+ ExtensionRegistry registry = getExtensionRegistry();
+
+ Protobuf_Unittest_TestAllExtensions message2 =
+ Protobuf_Unittest_TestAllExtensions.parseFromBuffer(rawBytes, registry);
+
+ assertAllExtensionsSet(message2);
+}
+
+void testParsePackedExtensions() {
+ // Ensure that packed extensions can be properly parsed.
+ Protobuf_Unittest_TestPackedExtensions message = getPackedExtensionsSet();
+ List<int> rawBytes = message.writeToBuffer();
+
+ ExtensionRegistry registry = getExtensionRegistry();
+
+ Protobuf_Unittest_TestPackedExtensions message2 =
+ Protobuf_Unittest_TestPackedExtensions.parseFromBuffer(rawBytes,
+ registry);
+
+ assertPackedExtensionsSet(message2);
+}
+
+void testExtensionsSerializedSize() {
+ Expect.equals(getAllSet().getSerializedSize(),
+ getAllExtensionsSet().getSerializedSize());
+}
+
+void testMessageOrBuilder() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ setAllFields(builder);
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+ assertAllFieldsSet(message);
+}
+
+void testUsingBuilderMultipleTimes() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ // primitive field scalar and repeated
+ builder.optionalSfixed64 = 100;
+ builder.repeatedInt32.add(100);
+ // enum field scalar and repeated
+ builder.optionalImportEnum = Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR;
+ builder.repeatedImportEnum.add(
+ Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR);
+ // proto field scalar and repeated
+ Protobuf_Unittest_ForeignMessage_Builder optionalForeignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ optionalForeignMessageBuilder.c = 1;
+ builder.optionalForeignMessage = optionalForeignMessageBuilder.build();
+ builder.repeatedForeignMessage.add(optionalForeignMessageBuilder.build());
+
+ Protobuf_Unittest_TestAllTypes value1 = builder.build();
+
+ Expect.equals(100, value1.optionalSfixed64);
+ Expect.equals(100, value1.repeatedInt32[0]);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR,
+ value1.optionalImportEnum);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR,
+ value1.repeatedImportEnum[0]);
+ Expect.equals(1, value1.optionalForeignMessage.c);
+ Expect.equals(1, value1.repeatedForeignMessage[0].c);
+
+ // Make sure that builder didn't update previously created values
+ builder.optionalSfixed64 = 200;
+ builder.repeatedInt32[0] = 200;
+ builder.optionalImportEnum = Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO;
+ builder.repeatedImportEnum[0] =
+ Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO;
+ optionalForeignMessageBuilder.c = 2;
+ builder.optionalForeignMessage = optionalForeignMessageBuilder.build();
+ builder.repeatedForeignMessage[0] = optionalForeignMessageBuilder.build();
+
+ Protobuf_Unittest_TestAllTypes value2 = builder.build();
+
+ // Make sure value1 didn't change.
+ Expect.equals(100, value1.optionalSfixed64);
+ Expect.equals(100, value1.repeatedInt32[0]);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR,
+ value1.optionalImportEnum);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR,
+ value1.repeatedImportEnum[0]);
+ Expect.equals(1, value1.optionalForeignMessage.c);
+ Expect.equals(1, value1.repeatedForeignMessage[0].c);
+
+ // Make sure value2 is correct
+ Expect.equals(200, value2.optionalSfixed64);
+ Expect.equals(200, value2.repeatedInt32[0]);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO,
+ value2.optionalImportEnum);
+ Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO,
+ value2.repeatedImportEnum[0]);
+ Expect.equals(2, value2.optionalForeignMessage.c);
+ Expect.equals(2, value2.repeatedForeignMessage[0].c);
+}
+
+void testProtosShareRepeatedArraysIfDidntChange() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ builder.repeatedInt32.add(100);
+ builder.repeatedImportEnum
+ .add(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR);
+ builder.repeatedForeignMessage
+ .add(Protobuf_Unittest_ForeignMessage.defaultInstance);
+
+ Protobuf_Unittest_TestAllTypes value1 = builder.build();
+ Protobuf_Unittest_TestAllTypes value2 = value1.toBuilder().build();
+
+ Expect.listEquals(value1.repeatedInt32, value2.repeatedInt32);
+ Expect.listEquals(value1.repeatedImportEnum, value2.repeatedImportEnum);
+ Expect.listEquals(value1.repeatedForeignMessage,
+ value2.repeatedForeignMessage);
+}
+
+void testRepeatedArraysAreImmutable() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ builder.repeatedInt32.add(100);
+ builder.repeatedImportEnum.add(
+ Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR);
+ builder.repeatedForeignMessage.add(
+ Protobuf_Unittest_ForeignMessage.defaultInstance);
+
+ Protobuf_Unittest_TestAllTypes value = builder.build();
+ _assertIsUnmodifiable(value.repeatedInt32);
+ _assertIsUnmodifiable(value.repeatedImportEnum);
+ _assertIsUnmodifiable(value.repeatedForeignMessage);
+ // Following line fails. Default repeating values are not properly being
+ // converted into immutable lists (could be a generic empty list).
+ _assertIsUnmodifiable(value.repeatedFloat);
+}
+
+void _assertIsUnmodifiable(List list) {
+ try {
+ list.clear();
+ Expect.fail("List wasn't immutable");
+ } catch (UnsupportedOperationException e) {
+ // good
+ }
+}
+
+void testSettersRejectNull() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ try {
+ builder.optionalString = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.optionalBytes = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.optionalNestedMessage = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.optionalNestedMessage =null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.optionalNestedEnum = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedString.add(null);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedBytes.add(null);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedNestedMessage.add(null);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedNestedMessage.add(null);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedNestedEnum.add(null);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+}
+
+void testRepeatedSetters() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ setAllFields(builder);
+ modifyRepeatedFields(builder);
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+ assertRepeatedFieldsModified(message);
+}
+
+void testRepeatedSettersRejectNull() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+
+ builder.repeatedString.add("one");
+ builder.repeatedString.add("two");
+ try {
+ builder.repeatedString[1] = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ builder.repeatedBytes.add("one".charCodes());
+ builder.repeatedBytes.add("two".charCodes());
+ try {
+ builder.repeatedBytes[1] = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ Protobuf_Unittest_TestAllTypes_NestedMessage_Builder repeatedNestedBuilder =
+ new Protobuf_Unittest_TestAllTypes_NestedMessage_Builder();
+ repeatedNestedBuilder.bb = 318;
+ builder.repeatedNestedMessage.add(repeatedNestedBuilder.build());
+ repeatedNestedBuilder.bb = 456;
+ builder.repeatedNestedMessage.add(repeatedNestedBuilder.build());
+
+ try {
+ builder.repeatedNestedMessage[1] = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.repeatedNestedMessage[1] = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ builder.repeatedNestedEnum
+ .add(Protobuf_Unittest_TestAllTypes_NestedEnum.FOO);
+ builder.repeatedNestedEnum
+ .add(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR);
+ try {
+ builder.repeatedNestedEnum[1] = null;
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+}
+
+void testRepeatedAppend() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+
+ builder.repeatedInt32.addAll([1, 2, 3, 4]);
+ builder.repeatedForeignEnum.addAll([
+ Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ]);
+
+ Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 12;
+ builder.repeatedForeignMessage.addAll([foreignMessageBuilder.build()]);
+
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+ Expect.listEquals([1, 2, 3, 4], message.repeatedInt32, "s.b. [1,2,3,4]");
+ Expect.listEquals([Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ],
+ message.repeatedForeignEnum);
+ Expect.equals(1, message.repeatedForeignMessage.length);
+ Expect.equals(12, message.repeatedForeignMessage[0].c);
+}
+
+void testRepeatedAppendRejectsNull() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+
+ Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 12;
+
+ try {
+ builder.repeatedForeignMessage.addAll([foreignMessageBuilder.build(),
+ null]);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ try {
+ builder.repeatedForeignEnum.addAll([
+ Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ, null]);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ try {
+ builder.repeatedString.addAll(["one", null]);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+
+ try {
+ builder.repeatedBytes.addAll(["one".charCodes(), null]);
+ Expect.fail("Exception was not thrown");
+ } catch (NullPointerException e) {
+ // We expect this exception.
+ }
+}
+
+void testSettingForeignMessageUsingBuilder() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ // Pass builder for foreign message instance.
+ Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 123;
+ builder.optionalForeignMessage = foreignMessageBuilder.build();
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+
+ builder = new Protobuf_Unittest_TestAllTypes_Builder();
+ // Pass builder for foreign message instance.
+ foreignMessageBuilder = new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 123;
+ builder.optionalForeignMessage = foreignMessageBuilder.build();
+ Protobuf_Unittest_TestAllTypes expectedMessage = builder.build();
+
+ // TODO: In Java version, ngd says:
+ // Upgrade to using real #equals method once implemented
+ Expect.stringEquals(expectedMessage.toString(), message.toString());
+}
+
+void testSettingRepeatedForeignMessageUsingBuilder() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ // Pass builder for foreign message instance.
+ Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 456;
+ builder.repeatedForeignMessage.add(foreignMessageBuilder.build());
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+
+ builder = new Protobuf_Unittest_TestAllTypes_Builder();
+ // Pass builder for foreign message instance.
+ foreignMessageBuilder = new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 456;
+ builder.repeatedForeignMessage.add(foreignMessageBuilder.build());
+ Protobuf_Unittest_TestAllTypes expectedMessage = builder.build();
+
+ // TODO(ngd): Upgrade to using real #equals method once implemented
+ Expect.stringEquals(expectedMessage.toString(), message.toString());
+}
+
+void testDefaults() {
+ assertClear(Protobuf_Unittest_TestAllTypes.defaultInstance);
+ assertClear((new Protobuf_Unittest_TestAllTypes_Builder()).build());
+
+ Protobuf_Unittest_TestExtremeDefaultValues message =
+ Protobuf_Unittest_TestExtremeDefaultValues.defaultInstance;
+
+ Expect.stringEquals("\u1234", message.utf8String);
+ Expect.identical(double.INFINITY, message.infDouble);
+ Expect.identical(double.NEGATIVE_INFINITY, message.negInfDouble);
+ Expect.isTrue(message.nanDouble.isNaN());
+ Expect.identical(double.INFINITY, message.infFloat);
+ Expect.identical(double.NEGATIVE_INFINITY, message.negInfFloat);
+ Expect.isTrue(message.nanFloat.isNaN());
+ Expect.stringEquals("? ? ?? ?? ??? ??/ ??-", message.cppTrigraph);
+}
+
+void testClear() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+
+ assertClear(builder.build());
+ setAllFields(builder);
+ builder.clear();
+ assertClear(builder.build());
+}
+
+void testEnumInterface() {
+ Expect.isTrue(
+ Protobuf_Unittest_TestAllTypes.defaultInstance.defaultNestedEnum
+ is ProtobufEnum);
+}
+
+void testEnumMap() {
+ for (Protobuf_Unittest_ForeignEnum value in
+ Protobuf_Unittest_ForeignEnum.values) {
+ Expect.equals(value, Protobuf_Unittest_ForeignEnum.valueOf(value.value));
+ }
+ Expect.isNull(Protobuf_Unittest_ForeignEnum.valueOf(12345));
+}
+
+void testParsePackedToUnpacked() {
+ Protobuf_Unittest_TestUnpackedTypes_Builder builder =
+ new Protobuf_Unittest_TestUnpackedTypes_Builder();
+ Protobuf_Unittest_TestUnpackedTypes message =
+ builder.mergeFromBuffer(getPackedSet().writeToBuffer()).build();
+ assertUnpackedFieldsSet(message);
+}
+
+void testParseUnpackedToPacked() {
+ Protobuf_Unittest_TestPackedTypes_Builder builder =
+ new Protobuf_Unittest_TestPackedTypes_Builder();
+ Protobuf_Unittest_TestPackedTypes message =
+ builder.mergeFromBuffer(getUnpackedSet().writeToBuffer()).build();
+ assertPackedFieldsSet(message);
+}
+
+// =================================================================
+// Extensions.
+void testExtensionMessageOrBuilder() {
+ Protobuf_Unittest_TestAllExtensions_Builder builder =
+ new Protobuf_Unittest_TestAllExtensions_Builder();
+ setAllExtensions(builder);
+ Protobuf_Unittest_TestAllExtensions message = builder.build();
+ assertAllExtensionsSet(message);
+}
+
+void testExtensionRepeatedSetters() {
+ Protobuf_Unittest_TestAllExtensions_Builder builder =
+ new Protobuf_Unittest_TestAllExtensions_Builder();
+ setAllExtensions(builder);
+ modifyRepeatedExtensions(builder);
+ Protobuf_Unittest_TestAllExtensions message = builder.build();
+ assertRepeatedExtensionsModified(message);
+}
+
+void testExtensionDefaults() {
+ assertExtensionsClear(Protobuf_Unittest_TestAllExtensions
+ .defaultInstance);
+ assertExtensionsClear(new Protobuf_Unittest_TestAllExtensions_Builder()
+ .build());
+}
+
+void testClearExtension() {
+ // clearExtension() is not actually used in TestUtil, so try it manually.
+ var builder = new Protobuf_Unittest_TestAllExtensions_Builder();
+ builder.setExtension(Unittest.optionalInt32Extension, 1);
+ builder.clearExtension(Unittest.optionalInt32Extension);
+ Expect.isFalse(builder.hasExtension(
+ Unittest.optionalInt32Extension));
+
+ builder = new Protobuf_Unittest_TestAllExtensions_Builder();
+ builder.addExtension(Unittest.repeatedInt32Extension, 1);
+ builder.clearExtension(Unittest.repeatedInt32Extension);
+ Expect.equals(0, builder.getExtensionCount(
+ Unittest.repeatedInt32Extension));
+}
+
+void testExtensionCopy() {
+ Protobuf_Unittest_TestAllExtensions original = getAllExtensionsSet();
+
+ Protobuf_Unittest_TestAllExtensions_Builder builder =
+ new Protobuf_Unittest_TestAllExtensions_Builder();
+ builder.mergeFromMessage(original);
+ Protobuf_Unittest_TestAllExtensions copy = builder.build();
+ assertAllExtensionsSet(copy);
+}
+
+void testExtensionMergeFrom() {
+ Protobuf_Unittest_TestAllExtensions_Builder builder =
+ new Protobuf_Unittest_TestAllExtensions_Builder();
+ builder.setExtension(Unittest.optionalInt32Extension, 1);
+ Protobuf_Unittest_TestAllExtensions original = builder.build();
+ Protobuf_Unittest_TestAllExtensions merged =
+ new Protobuf_Unittest_TestAllExtensions_Builder().
+ mergeFromMessage(original).build();
+ Expect.isTrue(merged.hasExtension(
+ Unittest.optionalInt32Extension));
+ Expect.equals(
+ 1, merged.getExtension(
+ Unittest.optionalInt32Extension));
+}
+
+void testToBuilder() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ setAllFields(builder);
+ Protobuf_Unittest_TestAllTypes message = builder.build();
+ assertAllFieldsSet(message);
+ assertAllFieldsSet(message.toBuilder().build());
+}
+
+void testRecursiveMessageDefaultInstance() {
+ Protobuf_Unittest_TestRecursiveMessage message =
+ Protobuf_Unittest_TestRecursiveMessage.defaultInstance;
+ Expect.isNotNull(message);
+ Expect.isNotNull(message.a);
+ Expect.equals(message.a, message);
+}
+
+void testSerialize() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ setAllFields(builder);
+ Protobuf_Unittest_TestAllTypes expected = builder.build();
+ List<int> out = expected.writeToBuffer();
+ Protobuf_Unittest_TestAllTypes actual =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(out);
+ Expect.equals(expected, actual);
+}
+
+void testSerializePartial() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ Protobuf_Unittest_TestAllTypes expected = builder.buildPartial();
+ List<int> out = expected.writeToBuffer();
+ Protobuf_Unittest_TestAllTypes actual =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(out);
+ Expect.equals(expected, actual);
+}
+
+void testEnumValues() {
+ Expect.setEquals([
+ Protobuf_Unittest_TestAllTypes_NestedEnum.FOO,
+ Protobuf_Unittest_TestAllTypes_NestedEnum.BAR,
+ Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ],
+ Protobuf_Unittest_TestAllTypes_NestedEnum.values);
+ Expect.equals(1, Protobuf_Unittest_TestAllTypes_NestedEnum.FOO.value);
+ Expect.equals(2, Protobuf_Unittest_TestAllTypes_NestedEnum.BAR.value);
+ Expect.equals(3, Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ.value);
+}
+
+void testBadExtension() {
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ try {
+ builder.setExtension(Unittest.optionalInt32Extension,
+ 101);
+ Expect.fail("Extension range not set. Should throw exception");
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ builder.getExtension(Unittest.optionalInt32Extension);
+ Expect.fail("Extension range not set. Should throw exception");
+ } catch (IllegalArgumentException e) {
+ }
+}
+
+final String TEST_ALL_TYPES_JSON = '{"1":101,"2":"102","3":103,"4":"104",'
+ '"5":105,"6":"106","7":107,"8":"108","9":109,"10":"110","11":111.0,'
+ '"12":112.0,"13":true,"14":"115","15":"MTE2","16":{"17":117},'
+ '"18":{"1":118},"19":{"1":119},"20":{"1":120},"21":3,"22":6,"23":9,'
+ '"24":"124","25":"125","31":[201,301],"32":["202","302"],'
+ '"33":[203,303],"34":["204","304"],"35":[205,305],"36":["206","306"],'
+ '"37":[207,307],"38":["208","308"],"39":[209,309],"40":["210","310"],'
+ '"41":[211.0,311.0],"42":[212.0,312.0],"43":[true,false],'
+ '"44":["215","315"],"45":["MjE2","MzE2"],"46":[{"47":217},{"47":317}],'
+ '"48":[{"1":218},{"1":318}],"49":[{"1":219},{"1":319}],'
+ '"50":[{"1":220},{"1":320}],"51":[2,3],"52":[5,6],"53":[8,9],'
+ '"54":["224","324"],"55":["225","325"],"61":401,"62":"402","63":403,'
+ '"64":"404","65":405,"66":"406","67":407,"68":"408","69":409,'
+ '"70":"410","71":411.0,"72":412.0,"73":false,"74":"415","75":"NDE2",'
+ '"81":1,"82":4,"83":7,"84":"424","85":"425"}';
+
+void testOutput() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+ String json = message.writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON, json);
+
+ // Test empty list
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.repeatedBool.clear();
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll('[true,false]', '[]'),
+ json);
+
+ // test negative number
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalInt32 = -1234567;
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':101,', ':-1234567,'),
+ json);
+
+ // 64-bit numbers are quoted
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalInt64 = 9007199254740992;
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"102",',
+ ':"9007199254740992",'), json);
+
+ // Quotes, backslashes, and control characters in strings are quoted
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalString = "a\u0000b\u0001cd\\e\"fg";
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"115",',
+ ':"a\\u0000b\\u0001cd\\\\e\\"fg",'), json);
+}
+
+void testBase64Encode() {
+ Protobuf_Unittest_TestAllTypes message = getAllSet();
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalBytes = "Hello, world".charCodes();
+ String json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxk",'), json);
+
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalBytes = "Hello, world!".charCodes();
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxkIQ==",'), json);
+
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalBytes = "Hello, world!!".charCodes();
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxkISE=",'), json);
+
+ // An empty list should not appear in the output
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalBytes = [];
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll('"15":"MTE2",',
+ ''), json);
+
+ builder =
+ Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message);
+ builder.optionalBytes = "a".charCodes();
+ json = builder.build().writeToJson();
+ Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"YQ==",'), json);
+}
+
+void testBase64Decode() {
+ String json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxk",');
+ Protobuf_Unittest_TestAllTypes message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.stringEquals("Hello, world", new String.fromCharCodes(message.optionalBytes));
+
+ json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxkIQ==",');
+ message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.stringEquals("Hello, world!", new String.fromCharCodes(message.optionalBytes));
+
+ json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"SGVsbG8sIHdvcmxkISE=",');
+ message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.stringEquals("Hello, world!!", new String.fromCharCodes(message.optionalBytes));
+
+ // Remove optionalBytes tag, reads back as empty list
+ json = TEST_ALL_TYPES_JSON.replaceAll('"15":"MTE2",',
+ '');
+ message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.listEquals([], message.optionalBytes);
+
+ // Keep optionalBytes tag, set data to empty string, get back empty list
+ json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"",');
+ message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.listEquals([], message.optionalBytes);
+
+ json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",',
+ ':"YQ==",');
+ message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(json);
+ Expect.stringEquals("a", new String.fromCharCodes(message.optionalBytes));
+}
+
+void testParse() {
+ Protobuf_Unittest_TestAllTypes message =
+ Protobuf_Unittest_TestAllTypes.parseFromJson(TEST_ALL_TYPES_JSON);
+ Protobuf_Unittest_TestAllTypes expected = getAllSet();
+ Expect.equals(expected, message);
+}
+
+void testExtensionsOutput() {
+ Protobuf_Unittest_TestAllExtensions message = getAllExtensionsSet();
+ String json = message.writeToJson();
+ Expect.equals(TEST_ALL_TYPES_JSON, json);
+}
+
+void testExtensionsParse() {
+ ExtensionRegistry registry = getExtensionRegistry();
+ Protobuf_Unittest_TestAllExtensions message =
+ Protobuf_Unittest_TestAllExtensions.parseFromJson(TEST_ALL_TYPES_JSON,
+ registry);
+ Protobuf_Unittest_TestAllExtensions expected = getAllExtensionsSet();
+ Expect.equals(expected, message);
+}
+
+ Protobuf_Unittest_TestAllTypes MERGE_SOURCE;
+ Protobuf_Unittest_TestAllTypes MERGE_DEST;
+ String MERGE_RESULT_TEXT = """
+optionalInt32: 1
+optionalInt64: 2
+optionalString: baz
+optionalForeignMessage: {
+ c: 3
+}
+repeatedString: bar
+repeatedString: qux
+""";
+
+void setUpMessageTests() {
+ Protobuf_Unittest_TestAllTypes_Builder mergeSourceBuilder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ mergeSourceBuilder.optionalInt32 = 1;
+ mergeSourceBuilder.optionalString = "foo";
+ mergeSourceBuilder.optionalForeignMessage =
+ Protobuf_Unittest_ForeignMessage.defaultInstance;
+ mergeSourceBuilder.repeatedString.add("bar");
+ MERGE_SOURCE = mergeSourceBuilder.build();
+
+ Protobuf_Unittest_TestAllTypes_Builder mergeDestBuilder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ mergeDestBuilder.optionalInt64 = 2;
+ mergeDestBuilder.optionalString = "baz";
+ mergeDestBuilder.optionalForeignMessage =
+ Protobuf_Unittest_ForeignMessage.defaultInstance;
+ Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder =
+ new Protobuf_Unittest_ForeignMessage_Builder();
+ foreignMessageBuilder.c = 3;
+ mergeDestBuilder.optionalForeignMessage = foreignMessageBuilder.build();
+ mergeDestBuilder.repeatedString.add("qux");
+ MERGE_DEST = mergeDestBuilder.build();
+
+ TEST_REQUIRED_UNINITIALIZED =
+ Protobuf_Unittest_TestRequired.defaultInstance;
+
+ Protobuf_Unittest_TestRequired_Builder testRequiredBuilder =
+ new Protobuf_Unittest_TestRequired_Builder();
+ testRequiredBuilder.a = 1;
+ testRequiredBuilder.b = 2;
+ testRequiredBuilder.c = 3;
+ TEST_REQUIRED_INITIALIZED = testRequiredBuilder.build();
+}
+
+void testMergeFrom() {
+ // was:
+ //Protobuf_Unittest_TestAllTypes result =
+ // TestAllTypes.newBuilder(MERGE_DEST)
+ // .mergeFrom(MERGE_SOURCE).build();
+ // less succinct now...
+ Protobuf_Unittest_TestAllTypes_Builder builder =
+ new Protobuf_Unittest_TestAllTypes_Builder();
+ // TODO add (builder construction) to the message class?
+ Protobuf_Unittest_TestAllTypes result =
+ builder.mergeFromMessage(MERGE_SOURCE)
+ // TODO can we create a builder from a message instance?
+ .mergeFromMessage(MERGE_DEST).build();
+
+ Expect.equals(MERGE_RESULT_TEXT, result.toString());
+}
+
+// void testMergeFromDynamic() {} // UNSUPPORTED no Dynamic implementation
+// void testDynamicMergeFrom() {} // UNSUPPORTED no Dynamic implementation
+
+Protobuf_Unittest_TestRequired TEST_REQUIRED_UNINITIALIZED;
+Protobuf_Unittest_TestRequired TEST_REQUIRED_INITIALIZED;
+
+void testRequired() {
+ Protobuf_Unittest_TestRequired_Builder builder =
+ new Protobuf_Unittest_TestRequired_Builder();
+
+ Expect.isFalse(builder.isInitialized(), "no required fields set");
+ builder.a = 1;
+ Expect.isFalse(builder.isInitialized(), "single required field set");
+ builder.b = 1;
+ Expect.isFalse(builder.isInitialized(), "all but one required field set");
+ builder.c = 1;
+ Expect.isTrue(builder.isInitialized(), "required fields set");
+}
+
+void testRequiredForeign() {
+ Protobuf_Unittest_TestRequiredForeign_Builder builder =
+ new Protobuf_Unittest_TestRequiredForeign_Builder();
+ Expect.isTrue(builder.isInitialized(),
+ "TestRequiredForeign without children should be initialized");
+
+ builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED;
+ Expect.isFalse(builder.isInitialized(),
+ "TestRequiredForeign with optional TEST_REQUIRED_UNINITIALIZED "
+ "should not be initialized");
+
+ builder.optionalMessage = TEST_REQUIRED_INITIALIZED;
+ Expect.isTrue(builder.isInitialized(),
+ "TestRequiredForeign with optional TEST_REQUIRED_INITIALIZED "
+ "should be initialized");
+
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ Expect.isFalse(builder.isInitialized(),
+ "TestRequiredForeign with repeating TEST_REQUIRED_UNINITIALIZED "
+ "should not be initialized");
+
+ builder.repeatedMessage[0] = TEST_REQUIRED_INITIALIZED;
+ Expect.isTrue(builder.isInitialized(),
+ "TestRequiredForeign with repeating TEST_REQUIRED_INITIALIZED "
+ "should be initialized");
+}
+
+void testRequiredExtension() {
+ Protobuf_Unittest_TestAllExtensions_Builder builder =
+ new Protobuf_Unittest_TestAllExtensions_Builder();
+ Expect.isTrue(builder.isInitialized());
+
+ builder.setExtension(Protobuf_Unittest_TestRequired.single,
+ TEST_REQUIRED_UNINITIALIZED);
+ Expect.isFalse(builder.isInitialized());
+
+ builder.setExtension(Protobuf_Unittest_TestRequired.single,
+ TEST_REQUIRED_INITIALIZED);
+ Expect.isTrue(builder.isInitialized());
+
+ builder.addExtension(Protobuf_Unittest_TestRequired.multi,
+ TEST_REQUIRED_UNINITIALIZED);
+ Expect.isFalse(builder.isInitialized());
+
+ builder.getExtension(Protobuf_Unittest_TestRequired.multi)[0] =
+ TEST_REQUIRED_INITIALIZED;
+ Expect.isTrue(builder.isInitialized());
+}
+
+// void testRequiredDynamic() {} // UNSUPPORTED no Dynamic implementation
+// void testRequiredDynamicForeign() {} // UNSUPPORTED no Dynamic impl
+
+void testUninitializedException() {
+ try {
+ new Protobuf_Unittest_TestRequired_Builder().build();
+ Expect.fail("Should have thrown an exception.");
+ } catch (UninitializedMessageException e) {
+ Expect.equals("Message missing required fields: a, b, c", e.message);
+ }
+}
+
+void testBuildPartial() {
+ // We're mostly testing that no exception is thrown.
+ Protobuf_Unittest_TestRequired message =
+ new Protobuf_Unittest_TestRequired_Builder().buildPartial();
+ Expect.isFalse(message.isInitialized());
+}
+
+void testNestedUninitializedException() {
+ try {
+ Protobuf_Unittest_TestRequiredForeign_Builder builder =
+ new Protobuf_Unittest_TestRequiredForeign_Builder();
+ builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED;
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ builder.build();
+ Expect.fail("Should have thrown an exception.");
+ } catch (UninitializedMessageException e) {
+ // NOTE: error message differs from Java in that
+ // fields are referenced using Dart fieldnames r.t.
+ // proto field names.
+ Expect.equals(
+ "Message missing required fields: "
+ "optionalMessage.a, "
+ "optionalMessage.b, "
+ "optionalMessage.c, "
+ "repeatedMessage[0].a, "
+ "repeatedMessage[0].b, "
+ "repeatedMessage[0].c, "
+ "repeatedMessage[1].a, "
+ "repeatedMessage[1].b, "
+ "repeatedMessage[1].c",
+ e.message);
+ }
+}
+
+void testBuildNestedPartial() {
+ // We're mostly testing that no exception is thrown.
+ Protobuf_Unittest_TestRequiredForeign_Builder builder =
+ new Protobuf_Unittest_TestRequiredForeign_Builder();
+ builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED;
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ Protobuf_Unittest_TestRequiredForeign message = builder.buildPartial();
+ Expect.isFalse(message.isInitialized());
+}
+
+void testParseUnititialized() {
+ try {
+ Protobuf_Unittest_TestRequired.parseFromBuffer([]);
+ Expect.fail("Should have thrown an exception.");
+ } catch (UninitializedMessageException e) {
+ Expect.equals("Message missing required fields: a, b, c", e.message);
+ }
+}
+
+void testParseNestedUnititialized() {
+ Protobuf_Unittest_TestRequiredForeign_Builder builder =
+ new Protobuf_Unittest_TestRequiredForeign_Builder();
+ builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED;
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED);
+ List<int> buffer = builder.buildPartial().writeToBuffer();
+
+ try {
+ Protobuf_Unittest_TestRequiredForeign.parseFromBuffer(buffer);
+ Expect.fail("Should have thrown an exception.");
+ } catch (UninitializedMessageException e) {
+ // NOTE: error message differs from Java in that
+ // fields are referenced using Dart fieldnames r.t.
+ // proto field names.
+ Expect.equals(
+ "Message missing required fields: "
+ "optionalMessage.a, "
+ "optionalMessage.b, "
+ "optionalMessage.c, "
+ "repeatedMessage[0].a, "
+ "repeatedMessage[0].b, "
+ "repeatedMessage[0].c, "
+ "repeatedMessage[1].a, "
+ "repeatedMessage[1].b, "
+ "repeatedMessage[1].c",
+ e.message);
+ }
+}
+
+Protobuf_Unittest_TestEmptyMessage emptyMessage;
+Protobuf_Unittest_TestAllTypes testAllTypes;
+UnknownFieldSet unknownFields;
+List<int> allFieldsData;
+
+void setUpUnknownFieldSetTests() {
+ testAllTypes = getAllSet();
+ allFieldsData = testAllTypes.writeToBuffer();
+ emptyMessage =
+ Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(allFieldsData);
+ unknownFields = emptyMessage.unknownFields;
+}
+
+UnknownFieldSet_Field getField(String name) {
+ int tagNumber = testAllTypes.getTagNumber(name);
+ assert(unknownFields.hasField(tagNumber));
+ return unknownFields.getField(tagNumber);
+}
+
+void testVarint() {
+ UnknownFieldSet_Field optionalInt32 = getField("optionalInt32");
+ Expect.equals(testAllTypes.optionalInt32,
+ optionalInt32.varintList[0].toInt());
+}
+
+void testFixed32() {
+ UnknownFieldSet_Field optionalFixed32 = getField("optionalFixed32");
+ Expect.equals(testAllTypes.optionalFixed32, optionalFixed32.fixed32List[0]);
+}
+
+void testFixed64() {
+ UnknownFieldSet_Field optionalFixed64 = getField("optionalFixed64");
+ Expect.equals(testAllTypes.optionalFixed64,
+ optionalFixed64.fixed64List[0].toInt());
+}
+
+void testLengthDelimited() {
+ UnknownFieldSet_Field optionalBytes = getField("optionalBytes");
+ Expect.listEquals(testAllTypes.optionalBytes,
+ optionalBytes.lengthDelimitedList[0]);
+}
+
+void testGroup() {
+ int tagNumberA = Protobuf_Unittest_TestAllTypes_OptionalGroup
+ .defaultInstance.getTagNumber("a");
+ Expect.isTrue(tagNumberA != null);
+
+ UnknownFieldSet_Field optionalGroupField = getField("optionalGroup");
+ Expect.equals(1, optionalGroupField.groupList.length);
+ UnknownFieldSet group = optionalGroupField.groupList[0];
+ Expect.isTrue(group.hasField(tagNumberA));
+ Expect.equals(testAllTypes.optionalGroup.a, group.getField(tagNumberA)
+ .varintList[0].toInt());
+}
+
+void testSerialize2() {
+ List<int> data = emptyMessage.writeToBuffer();
+ Expect.listEquals(allFieldsData, data);
+}
+
+void testCopyFrom() {
+ Protobuf_Unittest_TestEmptyMessage message =
+ Protobuf_Unittest_TestEmptyMessage.newBuilder()
+ .mergeFromMessage(emptyMessage).build();
+ Expect.stringEquals(emptyMessage.toString(), message.toString());
+ Expect.isFalse(emptyMessage.toString().isEmpty());
+}
+
+void testMergeFrom2() {
+ // source
+ UnknownFieldSet sourceFieldSet = new UnknownFieldSet_Builder()
+ .addField(2, new UnknownFieldSet_Field_Builder().addVarint(2).build())
+ .addField(3, new UnknownFieldSet_Field_Builder().addVarint(3).build())
+ .build();
+
+ Protobuf_Unittest_TestEmptyMessage_Builder sourceBuilder =
+ Protobuf_Unittest_TestEmptyMessage.newBuilder();
+ sourceBuilder.unknownFields = sourceFieldSet;
+ Protobuf_Unittest_TestEmptyMessage source = sourceBuilder.build();
+
+ // destination
+ UnknownFieldSet destinationFieldSet = new UnknownFieldSet_Builder()
+ .addField(1, new UnknownFieldSet_Field_Builder().addVarint(1).build())
+ .addField(3, new UnknownFieldSet_Field_Builder().addVarint(4).build())
+ .build();
+
+ Protobuf_Unittest_TestEmptyMessage_Builder destinationBuilder =
+ Protobuf_Unittest_TestEmptyMessage.newBuilder();
+ destinationBuilder.unknownFields = destinationFieldSet;
+ destinationBuilder.mergeFromMessage(source);
+ Protobuf_Unittest_TestEmptyMessage destination = destinationBuilder.build();
+
+ Expect.stringEquals(
+ "1: 1\n"
+ "2: 2\n"
+ "3: 4\n"
+ "3: 3\n",
+ destination.toString());
+}
+
+void testClear2() {
+ UnknownFieldSet_Builder fsb = new UnknownFieldSet_Builder(unknownFields);
+ fsb.clear();
+ UnknownFieldSet fields = fsb.build();
+ Expect.isTrue(fields.asMap().isEmpty());
+}
+
+void testEmpty() {
+ UnknownFieldSet fields = (new UnknownFieldSet_Builder()).build();
+ Expect.isTrue(fields.asMap().isEmpty());
+}
+
+void testClearMessage() {
+ Protobuf_Unittest_TestEmptyMessage_Builder messageBuilder =
+ Protobuf_Unittest_TestEmptyMessage.newBuilder();
+ messageBuilder.mergeFromMessage(emptyMessage);
+ messageBuilder.clear();
+ Protobuf_Unittest_TestEmptyMessage message = messageBuilder.build();
+ Expect.equals(0, message.getSerializedSize());
+}
+
+void testParseKnownAndUnknown() {
+ // Test mixing known and unknown fields when parsing.
+ UnknownFieldSet fields = (new UnknownFieldSet_Builder(unknownFields))
+ .addField(123456,
+ (new UnknownFieldSet_Field_Builder()).addVarint(654321).build())
+ .build();
+
+ CodedBufferWriter writer =
+ new CodedBufferWriter(fields.getSerializedSize());
+ fields.writeToCodedBufferWriter(writer);
+
+ Protobuf_Unittest_TestAllTypes destination =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(writer.buffer);
+
+ assertAllFieldsSet(destination);
+ Expect.equals(1, destination.unknownFields.asMap().length);
+
+ UnknownFieldSet_Field field =
+ destination.unknownFields.getField(123456);
+ Expect.equals(1, field.varintList.length);
+ Expect.equals(654321, field.varintList[0].toInt());
+}
+
+// Constructs a protocol buffer which contains fields with all the same
+// numbers as allFieldsData except that each field is some other wire
+// type.
+List<int> getBizarroData() {
+ UnknownFieldSet_Builder bizarroFieldsBuilder =
+ new UnknownFieldSet_Builder();
+
+ UnknownFieldSet_Field_Builder varintField =
+ (new UnknownFieldSet_Field_Builder()).addVarint(1);
+
+ UnknownFieldSet_Field_Builder fixed32Field =
+ (new UnknownFieldSet_Field_Builder()).addFixed32(1);
+
+ unknownFields.asMap().forEach(void _(int tag, UnknownFieldSet_Field value) {
+ if (value.varintList.isEmpty()) {
+ // Original field is not a varint, so use a varint.
+ bizarroFieldsBuilder.addField(tag, varintField.build());
+ } else {
+ // Original field *is* a varint, so use something else.
+ bizarroFieldsBuilder.addField(tag, fixed32Field.build());
+ }
+ });
+ UnknownFieldSet bizarroFields = bizarroFieldsBuilder.build();
+ CodedBufferWriter writer =
+ new CodedBufferWriter(bizarroFields.getSerializedSize());
+ bizarroFields.writeToCodedBufferWriter(writer);
+ return writer.buffer;
+}
+
+void testWrongTypeTreatedAsUnknown() {
+ // Test that fields of the wrong wire type are treated like unknown fields
+ // when parsing.
+ List<int> bizarroData = getBizarroData();
+ Protobuf_Unittest_TestAllTypes allTypesMessage =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(bizarroData);
+ Protobuf_Unittest_TestEmptyMessage emptyMessage_ =
+ Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(bizarroData);
+ // All fields should have been interpreted as unknown, so the debug strings
+ // should be the same.
+ Expect.stringEquals(emptyMessage_.toString(), allTypesMessage.toString());
+}
+
+void testUnknownExtensions() {
+ // Make sure fields are properly parsed to the UnknownFieldSet even when
+ // they are declared as extension numbers.
+ Protobuf_Unittest_TestEmptyMessageWithExtensions message =
+ Protobuf_Unittest_TestEmptyMessageWithExtensions
+ .parseFromBuffer(allFieldsData);
+
+ Expect.equals(unknownFields.asMap().length,
+ message.unknownFields.asMap().length);
+ Expect.listEquals(allFieldsData, message.writeToBuffer());
+}
+
+void testWrongExtensionTypeTreatedAsUnknown() {
+ // Test that fields of the wrong wire type are treated like unknown fields
+ // when parsing extensions.
+
+ List<int> bizarroData = getBizarroData();
+ Protobuf_Unittest_TestAllExtensions allExtensionsMessage =
+ Protobuf_Unittest_TestAllExtensions.parseFromBuffer(bizarroData);
+ Protobuf_Unittest_TestEmptyMessage emptyMessage_ =
+ Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(bizarroData);
+
+ // All fields should have been interpreted as unknown, so the debug strings
+ // should be the same.
+ Expect.stringEquals(emptyMessage_.toString(),
+ allExtensionsMessage.toString());
+}
+
+void testParseUnknownEnumValue() {
+ int singularFieldNum = testAllTypes.getTagNumber("optionalNestedEnum");
+ int repeatedFieldNum = testAllTypes.getTagNumber("repeatedNestedEnum");
+ Expect.isNotNull(singularFieldNum);
+ Expect.isNotNull(repeatedFieldNum);
+
+ UnknownFieldSet fieldSet = (new UnknownFieldSet_Builder())
+ .addField(singularFieldNum, (new UnknownFieldSet_Field_Builder())
+ .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR.value)
+ .addVarint(5).build())
+ .addField(repeatedFieldNum, (new UnknownFieldSet_Field_Builder())
+ .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.FOO.value)
+ .addVarint(4)
+ .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ.value)
+ .addVarint(6).build())
+ .build();
+
+ CodedBufferWriter writer =
+ new CodedBufferWriter(fieldSet.getSerializedSize());
+ fieldSet.writeToCodedBufferWriter(writer);
+ {
+ Protobuf_Unittest_TestAllTypes message =
+ Protobuf_Unittest_TestAllTypes.parseFromBuffer(writer.buffer);
+ Expect.equals(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR,
+ message.optionalNestedEnum);
+ Expect.listEquals([Protobuf_Unittest_TestAllTypes_NestedEnum.FOO,
+ Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ],
+ message.repeatedNestedEnum);
+ Expect.equals(1,
+ message.unknownFields.getField(singularFieldNum).varintList.length);
+ Expect.equals(5,
+ message.unknownFields.getField(singularFieldNum).varintList[0].toInt());
+ Expect.equals(2,
+ message.unknownFields.getField(repeatedFieldNum).varintList.length);
+ Expect.equals(4,
+ message.unknownFields.getField(repeatedFieldNum).varintList[0].toInt());
+ Expect.equals(6,
+ message.unknownFields.getField(repeatedFieldNum).varintList[1].toInt());
+ }
+ {
+ Protobuf_Unittest_TestAllExtensions message =
+ Protobuf_Unittest_TestAllExtensions.parseFromBuffer(writer.buffer,
+ getExtensionRegistry());
+ Expect.equals(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR,
+ message.getExtension(
+ Unittest.optionalNestedEnumExtension));
+
+ Expect.listEquals([Protobuf_Unittest_TestAllTypes_NestedEnum.FOO,
+ Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ],
+ message.getExtension(
+ Unittest.repeatedNestedEnumExtension));
+ Expect.equals(1,
+ message.unknownFields.getField(singularFieldNum).varintList.length);
+ Expect.equals(5,
+ message.unknownFields.getField(singularFieldNum).varintList[0].toInt());
+ Expect.equals(2,
+ message.unknownFields.getField(repeatedFieldNum).varintList.length);
+ Expect.equals(4,
+ message.unknownFields.getField(repeatedFieldNum).varintList[0].toInt());
+ Expect.equals(6,
+ message.unknownFields.getField(repeatedFieldNum).varintList[1].toInt());
+ }
+}
+
+void testLargeVarint() {
+ Packed64 maxint = new Packed64(0x7FFFFFFF, 0xFFFFFFFF);
+ UnknownFieldSet unknownFieldSet = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder())
+ .addVarint(maxint).build())
+ .build();
+ CodedBufferWriter writer =
+ new CodedBufferWriter(unknownFieldSet.getSerializedSize());
+ unknownFieldSet.writeToCodedBufferWriter(writer);
+
+ var parsed = new UnknownFieldSet_Builder();
+ parsed.mergeFromCodedBufferReader(
+ new CodedBufferReader.fromBuffer(writer.buffer));
+ var field = parsed.build().getField(1);
+ Expect.equals(1, field.varintList.length);
+ Expect.equals(new Packed64(0x7FFFFFFF, 0xFFFFFFFF).toString(),
+ field.varintList[0].toString());
+}
+
+void testEqualsAndHashCode() {
+ UnknownFieldSet a = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder())
+ .addFixed32(1).build()).build();
+
+ UnknownFieldSet b = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder())
+ .addFixed64(new Packed64.fromInt(1)).build()).build();
+
+ UnknownFieldSet c = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder()).addVarint(1).build())
+ .build();
+
+ UnknownFieldSet d = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder())
+ .addLengthDelimited([]).build()).build();
+
+ UnknownFieldSet e = (new UnknownFieldSet_Builder())
+ .addField(1, (new UnknownFieldSet_Field_Builder())
+ .addGroup(unknownFields).build()).build();
+
+ _checkEqualsIsConsistent(a);
+ _checkEqualsIsConsistent(b);
+ _checkEqualsIsConsistent(c);
+ _checkEqualsIsConsistent(d);
+ _checkEqualsIsConsistent(e);
+
+ _checkNotEqual(a, b);
+ _checkNotEqual(a, c);
+ _checkNotEqual(a, d);
+ _checkNotEqual(a, e);
+ _checkNotEqual(b, c);
+ _checkNotEqual(b, d);
+ _checkNotEqual(b, e);
+ _checkNotEqual(c, d);
+ _checkNotEqual(c, e);
+ _checkNotEqual(d, e);
+
+ UnknownFieldSet f1 = (new UnknownFieldSet_Builder())
+ .addField(1,
+ (new UnknownFieldSet_Field_Builder()).addLengthDelimited([1, 2])
+ .build()).build();
+ UnknownFieldSet f2 = (new UnknownFieldSet_Builder())
+ .addField(1,
+ (new UnknownFieldSet_Field_Builder()).addLengthDelimited([2, 1])
+ .build()).build();
+
+ _checkEqualsIsConsistent(f1);
+ _checkEqualsIsConsistent(f2);
+
+ _checkNotEqual(f1, f2);
+}
+
+/**
+ * Asserts that the given field sets are not equal and have different
+ * hash codes.
+ *
+ * N.B.: It is valid for non-equal objects to have the same hash code, so
+ * this test is more strict than necessary. However, in the test cases
+ * identifies, the hash codes should differ, and as a matter of principle
+ * hash collisions should be relatively rare.
+ */
+void _checkNotEqual(UnknownFieldSet s1, UnknownFieldSet s2) {
+ String equalsError =
+ "${s1.toString()} should not be equal to ${s2.toString}";
+ Expect.isFalse(s1 == s2, equalsError);
+ Expect.isFalse(s2 == s1, equalsError);
+
+ Expect.isFalse(
+ s1.hashCode() == s2.hashCode(),
+ "${s1.toString()} should have a different hash code "
+ "from ${s2.toString()}");
+}
+
+/**
+ * Asserts that the given field sets are equal and have identical hash codes.
+ */
+void _checkEqualsIsConsistent(UnknownFieldSet set) {
+ // Object should be equal to itself.
+ Expect.equals(set, set);
+
+ // Object should be equal to a copy of itself.
+ UnknownFieldSet copy = (new UnknownFieldSet_Builder(set)).build();
+ Expect.equals(set, copy);
+ Expect.equals(copy, set);
+ Expect.equals(set.hashCode(), copy.hashCode());
+}
+
+
« no previous file with comments | « tests/lib/lib.status ('k') | tests/lib/protobuf/test_util.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698