public void EmptyMessageFieldDistinctFromMissingMessageField() { // This demonstrates what we're really interested in... var message1 = new TestAllTypes { SingleForeignMessage = new ForeignMessage() }; var message2 = new TestAllTypes(); // SingleForeignMessage is null EqualityTester.AssertInequality(message1, message2); }
public UnknownFieldSetTest() { descriptor = TestAllTypes.Descriptor; allFields = TestUtil.GetAllSet(); allFieldsData = allFields.ToByteString(); emptyMessage = TestEmptyMessage.ParseFrom(allFieldsData); unknownFields = emptyMessage.UnknownFields; }
public void DefaultValues() { // Single fields var message = new TestAllTypes(); Assert.AreEqual(false, message.SingleBool); Assert.AreEqual(ByteString.Empty, message.SingleBytes); Assert.AreEqual(0.0, message.SingleDouble); Assert.AreEqual(0, message.SingleFixed32); Assert.AreEqual(0L, message.SingleFixed64); Assert.AreEqual(0.0f, message.SingleFloat); Assert.AreEqual(ForeignEnum.FOREIGN_UNSPECIFIED, message.SingleForeignEnum); Assert.IsNull(message.SingleForeignMessage); Assert.AreEqual(ImportEnum.IMPORT_ENUM_UNSPECIFIED, message.SingleImportEnum); Assert.IsNull(message.SingleImportMessage); Assert.AreEqual(0, message.SingleInt32); Assert.AreEqual(0L, message.SingleInt64); Assert.AreEqual(TestAllTypes.Types.NestedEnum.NESTED_ENUM_UNSPECIFIED, message.SingleNestedEnum); Assert.IsNull(message.SingleNestedMessage); Assert.IsNull(message.SinglePublicImportMessage); Assert.AreEqual(0, message.SingleSfixed32); Assert.AreEqual(0L, message.SingleSfixed64); Assert.AreEqual(0, message.SingleSint32); Assert.AreEqual(0L, message.SingleSint64); Assert.AreEqual("", message.SingleString); Assert.AreEqual(0U, message.SingleUint32); Assert.AreEqual(0UL, message.SingleUint64); // Repeated fields Assert.AreEqual(0, message.RepeatedBool.Count); Assert.AreEqual(0, message.RepeatedBytes.Count); Assert.AreEqual(0, message.RepeatedDouble.Count); Assert.AreEqual(0, message.RepeatedFixed32.Count); Assert.AreEqual(0, message.RepeatedFixed64.Count); Assert.AreEqual(0, message.RepeatedFloat.Count); Assert.AreEqual(0, message.RepeatedForeignEnum.Count); Assert.AreEqual(0, message.RepeatedForeignMessage.Count); Assert.AreEqual(0, message.RepeatedImportEnum.Count); Assert.AreEqual(0, message.RepeatedImportMessage.Count); Assert.AreEqual(0, message.RepeatedNestedEnum.Count); Assert.AreEqual(0, message.RepeatedNestedMessage.Count); Assert.AreEqual(0, message.RepeatedPublicImportMessage.Count); Assert.AreEqual(0, message.RepeatedSfixed32.Count); Assert.AreEqual(0, message.RepeatedSfixed64.Count); Assert.AreEqual(0, message.RepeatedSint32.Count); Assert.AreEqual(0, message.RepeatedSint64.Count); Assert.AreEqual(0, message.RepeatedString.Count); Assert.AreEqual(0, message.RepeatedUint32.Count); Assert.AreEqual(0, message.RepeatedUint64.Count); // Oneof fields Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual("", message.OneofString); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); }
public void AllSingleFields() { var message = new TestAllTypes { SingleBool = true, SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), SingleDouble = 23.5, SingleFixed32 = 23, SingleFixed64 = 1234567890123, SingleFloat = 12.25f, SingleForeignEnum = ForeignEnum.FOREIGN_BAR, SingleForeignMessage = new ForeignMessage { C = 10 }, SingleImportEnum = ImportEnum.IMPORT_BAZ, SingleImportMessage = new ImportMessage { D = 20 }, SingleInt32 = 100, SingleInt64 = 3210987654321, SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, SinglePublicImportMessage = new PublicImportMessage { E = 54 }, SingleSfixed32 = -123, SingleSfixed64 = -12345678901234, SingleSint32 = -456, SingleSint64 = -12345678901235, SingleString = "test\twith\ttabs", SingleUint32 = uint.MaxValue, SingleUint64 = ulong.MaxValue, }; var actualText = JsonFormatter.Default.Format(message); // Fields in numeric order var expectedText = "{ " + "'singleInt32': 100, " + "'singleInt64': '3210987654321', " + "'singleUint32': 4294967295, " + "'singleUint64': '18446744073709551615', " + "'singleSint32': -456, " + "'singleSint64': '-12345678901235', " + "'singleFixed32': 23, " + "'singleFixed64': '1234567890123', " + "'singleSfixed32': -123, " + "'singleSfixed64': '-12345678901234', " + "'singleFloat': 12.25, " + "'singleDouble': 23.5, " + "'singleBool': true, " + "'singleString': 'test\\twith\\ttabs', " + "'singleBytes': 'AQIDBA==', " + "'singleNestedMessage': { 'bb': 35 }, " + "'singleForeignMessage': { 'c': 10 }, " + "'singleImportMessage': { 'd': 20 }, " + "'singleNestedEnum': 'FOO', " + "'singleForeignEnum': 'FOREIGN_BAR', " + "'singleImportEnum': 'IMPORT_BAZ', " + "'singlePublicImportMessage': { 'e': 54 }" + " }"; AssertJson(expectedText, actualText); }
public void CloneOneofField() { var original = new TestAllTypes { OneofNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var clone = original.Clone(); Assert.AreNotSame(original, clone); Assert.AreEqual(original, clone); // We should have cloned the message original.OneofNestedMessage.Bb = 30; Assert.AreNotEqual(original, clone); }
public void CloneRepeatedMessageField() { var original = new TestAllTypes { RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 20 } } }; var clone = original.Clone(); Assert.AreNotSame(original, clone); Assert.AreNotSame(original.RepeatedNestedMessage, clone.RepeatedNestedMessage); Assert.AreNotSame(original.RepeatedNestedMessage[0], clone.RepeatedNestedMessage[0]); Assert.AreEqual(original, clone); clone.RepeatedNestedMessage[0].Bb = 30; Assert.AreNotEqual(original, clone); }
public static void SetSingular(TestAllTypes allTypes, int offsetIndex = 0) { allTypes.SingleInt32 = IntValue(offsetIndex); allTypes.SingleInt64 = IntValue(offsetIndex); allTypes.SingleUint32 = UintValue(offsetIndex); allTypes.SingleUint64 = UintValue(offsetIndex); allTypes.SingleSint32 = IntValue(offsetIndex); allTypes.SingleSint64 = IntValue(offsetIndex); allTypes.SingleFixed32 = UintValue(offsetIndex); allTypes.SingleFixed64 = UintValue(offsetIndex); allTypes.SingleSfixed32 = IntValue(offsetIndex); allTypes.SingleSfixed64 = IntValue(offsetIndex); allTypes.SingleFloat = FloatValue(offsetIndex); allTypes.SingleDouble = FloatValue(offsetIndex); allTypes.SingleBool = BoolValue(offsetIndex); allTypes.SingleString = StringValue(offsetIndex); allTypes.SingleBytes = ByteValue(offsetIndex); }
public void CloneSingleMessageField() { var original = new TestAllTypes { SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var clone = original.Clone(); Assert.AreNotSame(original, clone); Assert.AreNotSame(original.SingleNestedMessage, clone.SingleNestedMessage); Assert.AreEqual(original, clone); clone.SingleNestedMessage.Bb = 30; Assert.AreNotEqual(original, clone); }
public void Any_RegularMessage() { var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var original = Any.Pack(message); var json = formatter.Format(original); // This is tested in JsonFormatterTest var parser = new JsonParser(new JsonParser.Settings(10, registry)); Assert.AreEqual(original, parser.Parse <Any>(json)); string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessage\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllTypes\" }"; Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson)); }
public void BuildModifyAndRebuild() { TestAllTypes.Builder b1 = new TestAllTypes.Builder(); b1.SetDefaultInt32(1); b1.AddRepeatedInt32(2); b1.SetOptionalForeignMessage(ForeignMessage.DefaultInstance); TestAllTypes m1 = b1.Build(); b1.SetDefaultInt32(5); b1.AddRepeatedInt32(6); b1.SetOptionalForeignMessage(b1.OptionalForeignMessage.ToBuilder().SetC(7)); TestAllTypes m2 = b1.Build(); Assert.AreEqual("{\"optional_foreign_message\":{},\"repeated_int32\":[2],\"default_int32\":1}", Extensions.ToJson(m1)); Assert.AreEqual("{\"optional_foreign_message\":{\"c\":7},\"repeated_int32\":[2,6],\"default_int32\":5}", Extensions.ToJson(m2)); }
public void InvalidSurrogatePairsFail() { // Note: don't use TestCase for these, as the strings can't be reliably represented // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/ // Lone low surrogate var message = new TestAllTypes { SingleString = "a\uDC01b" }; Assert.Throws <ArgumentException>(() => JsonFormatter.Default.Format(message)); // Lone high surrogate message = new TestAllTypes { SingleString = "a\uD801b" }; Assert.Throws <ArgumentException>(() => JsonFormatter.Default.Format(message)); }
public void ParseExtensions() { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serealize a TestAllTypes then parse it as TestAllExtensions // it should work. TestAllTypes message = TestUtil.GetAllSet(); ByteString rawBytes = message.ToByteString(); ExtensionRegistry registry = ExtensionRegistry.CreateInstance(); TestUtil.RegisterAllExtensions(registry); registry = registry.AsReadOnly(); TestAllExtensions message2 = TestAllExtensions.ParseFrom(rawBytes, registry); TestUtil.AssertAllExtensionsSet(message2); }
public void RepeatedAppend() { TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); builder.AddRangeRepeatedInt32(new int[] { 1, 2, 3, 4 }); builder.AddRangeRepeatedForeignEnum((new ForeignEnum[] { ForeignEnum.FOREIGN_BAZ })); ForeignMessage foreignMessage = ForeignMessage.CreateBuilder().SetC(12).Build(); builder.AddRangeRepeatedForeignMessage(new ForeignMessage[] { foreignMessage }); TestAllTypes message = builder.Build(); TestUtil.AssertEqual(message.RepeatedInt32List, new int[] { 1, 2, 3, 4 }); TestUtil.AssertEqual(message.RepeatedForeignEnumList, new ForeignEnum[] { ForeignEnum.FOREIGN_BAZ }); Assert.AreEqual(1, message.RepeatedForeignMessageCount); Assert.AreEqual(12, message.GetRepeatedForeignMessage(0).C); }
public void WriteWholeMessage() { TestAllTypes message = TestUtil.GetAllSet(); byte[] rawBytes = message.ToByteArray(); TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes); // Try different block sizes. for (int blockSize = 1; blockSize < 256; blockSize *= 2) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, blockSize); message.WriteTo(output); output.Flush(); TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray()); } }
public void SizeLimit() { // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't // apply to the latter case. MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray()); CodedInputStream input = CodedInputStream.CreateInstance(ms); input.SetSizeLimit(16); try { TestAllTypes.ParseFrom(input); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } }
public void TestSerializeAndParse() { TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); builder.SetOptionalInt32(1234); builder.SetOptionalString("hello"); builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance); builder.SetOneofUint32(0U); ByteString data = builder.Build().ToByteString(); TestAllTypes message = TestAllTypes.ParseFrom(data); Assert.AreEqual(1234, message.OptionalInt32); Assert.AreEqual("hello", message.OptionalString); Assert.AreEqual(ByteString.Empty, message.OptionalBytes); Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum); Assert.IsTrue(message.HasOptionalNestedMessage); Assert.AreEqual(0, message.OptionalNestedMessage.Bb); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase); }
public void ShouldGenerateEventsInDeeplyNestedSnapshotBasedInstance() { var root = DeeplyNested(); root.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[1] = DeeplyNested(); root.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[3] = DeeplyNested(); root.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[1].AllTypes.AllTypes.AllTypes.RepeatedTestAllTypesMessage.Add(DeeplyNested()); root.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[1].AllTypes.AllTypes.AllTypes.RepeatedTestAllTypesMessage.Add(DeeplyNested()); var allTypes = root.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[1].AllTypes.AllTypes.AllTypes.RepeatedTestAllTypesMessage[1].AllTypes.AllTypes.AllTypes; ApplyAllChanges(allTypes); var snapshot = root.GenerateSnapshot(); var target = new TestAllTypes(); target.ApplyEvents(snapshot); var nested = target.AllTypes.AllTypes.AllTypes.MapInt32TestAllTypesMessage[1].AllTypes.AllTypes.AllTypes.RepeatedTestAllTypesMessage[1].AllTypes.AllTypes.AllTypes; UpdateNestedMessages(nested, 2); UpdateNestedMessages(nested.OneofAllTypes, 5); UpdateNestedMessages(nested.AllTypes, 6); // verify events are stable var targetEvents = target.GenerateEvents(); var target2 = new TestAllTypes(); target2.ApplyEvents(snapshot); target2.ApplyEvents(targetEvents); Assert.Equal(target, target2); Assert.NotEqual(allTypes, target2); // verify snapshot is stable var targetSnapshot = target.GenerateSnapshot(); var target3 = new TestAllTypes(); target3.ApplyEvents(targetSnapshot); Assert.Equal(target, target3); Assert.NotEqual(allTypes, target3); }
public void Oneof() { var message = new TestAllTypes(); var descriptor = TestAllTypes.Descriptor; Assert.AreEqual(1, descriptor.Oneofs.Count); var oneof = descriptor.Oneofs[0]; Assert.AreEqual("oneof_field", oneof.Name); Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message)); message.OneofString = "foo"; Assert.AreSame(descriptor.Fields[TestAllTypes.OneofStringFieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message)); message.OneofUint32 = 10; Assert.AreSame(descriptor.Fields[TestAllTypes.OneofUint32FieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message)); oneof.Accessor.Clear(message); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); }
public void RoundTrip_SingleValues() { var message = new TestAllTypes { SingleBool = true, SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), SingleDouble = 23.5, SingleFixed32 = 23, SingleFixed64 = 1234567890123, SingleFloat = 12.25f, SingleForeignEnum = ForeignEnum.FOREIGN_BAR, SingleForeignMessage = new ForeignMessage { C = 10 }, SingleImportEnum = ImportEnum.IMPORT_BAZ, SingleImportMessage = new ImportMessage { D = 20 }, SingleInt32 = 100, SingleInt64 = 3210987654321, SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, SinglePublicImportMessage = new PublicImportMessage { E = 54 }, SingleSfixed32 = -123, SingleSfixed64 = -12345678901234, SingleSint32 = -456, SingleSint64 = -12345678901235, SingleString = "test", SingleUint32 = uint.MaxValue, SingleUint64 = ulong.MaxValue }; byte[] bytes = message.ToByteArray(); TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); Assert.AreEqual(message, parsed); }
public void TestFieldPresenceReflection() { MessageDescriptor descriptor = TestAllTypes.Descriptor; FieldDescriptor optionalInt32Field = descriptor.FindFieldByName("optional_int32"); FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string"); FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes"); FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum"); TestAllTypes message = TestAllTypes.CreateBuilder().Build(); Assert.IsFalse(message.HasField(optionalInt32Field)); Assert.IsFalse(message.HasField(optionalStringField)); Assert.IsFalse(message.HasField(optionalBytesField)); Assert.IsFalse(message.HasField(optionalNestedEnumField)); // Set to default value is seen as not present message = TestAllTypes.CreateBuilder() .SetOptionalInt32(0) .SetOptionalString("") .SetOptionalBytes(ByteString.Empty) .SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO) .Build(); Assert.IsFalse(message.HasField(optionalInt32Field)); Assert.IsFalse(message.HasField(optionalStringField)); Assert.IsFalse(message.HasField(optionalBytesField)); Assert.IsFalse(message.HasField(optionalNestedEnumField)); Assert.AreEqual(0, message.AllFields.Count); // Set t0 non-defalut value is seen as present message = TestAllTypes.CreateBuilder() .SetOptionalInt32(1) .SetOptionalString("x") .SetOptionalBytes(ByteString.CopyFromUtf8("y")) .SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR) .Build(); Assert.IsTrue(message.HasField(optionalInt32Field)); Assert.IsTrue(message.HasField(optionalStringField)); Assert.IsTrue(message.HasField(optionalBytesField)); Assert.IsTrue(message.HasField(optionalNestedEnumField)); Assert.AreEqual(4, message.AllFields.Count); }
public void ParseKnownAndUnknown() { // Test mixing known and unknown fields when parsing. UnknownFieldSet fields = UnknownFieldSet.CreateBuilder(unknownFields) .AddField(123456, UnknownField.CreateBuilder().AddVarint(654321).Build()) .Build(); ByteString data = fields.ToByteString(); TestAllTypes destination = TestAllTypes.ParseFrom(data); TestUtil.AssertAllFieldsSet(destination); Assert.AreEqual(1, destination.UnknownFields.FieldDictionary.Count); UnknownField field = destination.UnknownFields[123456]; Assert.AreEqual(1, field.VarintList.Count); Assert.AreEqual(654321, (long)field.VarintList[0]); }
public void WriteWholeMessage_VaryingBlockSizes() { TestAllTypes message = SampleMessages.CreateFullTestAllTypes(); byte[] rawBytes = message.ToByteArray(); // Try different block sizes. for (int blockSize = 1; blockSize < 256; blockSize *= 2) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, blockSize); message.WriteTo(output); output.Flush(); Assert.AreEqual(rawBytes, rawOutput.ToArray()); var bufferWriter = new TestArrayBufferWriter <byte>(); bufferWriter.MaxGrowBy = blockSize; message.WriteTo(bufferWriter); Assert.AreEqual(rawBytes, bufferWriter.WrittenSpan.ToArray()); } }
public void ReadHugeBlob() { // Allocate and initialize a 1MB blob. byte[] blob = new byte[1 << 20]; for (int i = 0; i < blob.Length; i++) { blob[i] = (byte)i; } // Make a message containing it. var message = new TestAllTypes { SingleBytes = ByteString.CopyFrom(blob) }; // Serialize and parse it. Make sure to parse from an InputStream, not // directly from a ByteString, so that CodedInputStream uses buffered // reading. TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(message.ToByteString()); Assert.AreEqual(message, message2); }
public void RoundTrip_SingleValues() { var message = new TestAllTypes { SingleBool = true, SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), SingleDouble = 23.5, SingleFixed32 = 23, SingleFixed64 = 1234567890123, SingleFloat = 12.25f, SingleForeignEnum = ForeignEnum.ForeignBar, SingleForeignMessage = new ForeignMessage { C = 10 }, SingleImportEnum = ImportEnum.ImportBaz, SingleImportMessage = new ImportMessage { D = 20 }, SingleInt32 = 100, SingleInt64 = 3210987654321, SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, SinglePublicImportMessage = new PublicImportMessage { E = 54 }, SingleSfixed32 = -123, SingleSfixed64 = -12345678901234, SingleSint32 = -456, SingleSint64 = -12345678901235, SingleString = "test", SingleUint32 = uint.MaxValue, SingleUint64 = ulong.MaxValue }; MessageParsingHelpers.AssertWritingMessage(message); MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message); }
public void FieldPresence() { var message = new TestAllTypes(); Assert.False(message.HasOptionalBool); Assert.False(message.OptionalBool); message.OptionalBool = true; Assert.True(message.HasOptionalBool); Assert.True(message.OptionalBool); message.OptionalBool = false; Assert.True(message.HasOptionalBool); Assert.False(message.OptionalBool); message.ClearOptionalBool(); Assert.False(message.HasOptionalBool); Assert.False(message.OptionalBool); Assert.False(message.HasDefaultBool); Assert.True(message.DefaultBool); message.DefaultBool = false; Assert.True(message.HasDefaultBool); Assert.False(message.DefaultBool); message.DefaultBool = true; Assert.True(message.HasDefaultBool); Assert.True(message.DefaultBool); message.ClearDefaultBool(); Assert.False(message.HasDefaultBool); Assert.True(message.DefaultBool); }
public void TestHasMethod() { // Optional non-message fields don't have HasFoo method generated Type proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes); Type proto3Type = typeof(TestAllTypes); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum"); proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes.Builder); proto3Type = typeof(TestAllTypes.Builder); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum"); // message fields still have the HasFoo method generated Assert.IsFalse(TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage); Assert.IsFalse(TestAllTypes.CreateBuilder().HasOptionalNestedMessage); }
public void ShouldGenerateValidEventsForRepeatedFields() { var allTypes = new TestAllTypes(); allTypes.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage { Bb = 10 }); allTypes.RepeatedNestedMessage[0].Bb = 11; allTypes.RepeatedNestedMessage[0].Bb = 12; allTypes.RepeatedNestedMessage.RemoveAt(0); allTypes.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage { Bb = 13 }); allTypes.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage { Bb = 14 }); allTypes.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage { Bb = 15 }); allTypes.RepeatedNestedMessage[0].Bb = 16; allTypes.RepeatedNestedMessage[0].Bb = 17; allTypes.RepeatedNestedMessage.RemoveAt(0); allTypes.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage { Bb = 18 }); allTypes.RepeatedNestedMessage[0].Bb = 19; allTypes.RepeatedNestedMessage[0].Bb = 20; var events = allTypes.GenerateEvents(); var target = new TestAllTypes(); target.ApplyEvents(events); Assert.Equal(allTypes, target); }
public static void AddMap(TestAllTypes allTypes, int keyIndex, int offsetIndex = 0) { allTypes.MapInt32Int32.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapInt64Int64.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapUint32Uint32.Add(UintValue(keyIndex), UintValue(offsetIndex)); allTypes.MapUint64Uint64.Add(UintValue(keyIndex), UintValue(offsetIndex)); allTypes.MapSint32Sint32.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapSint64Sint64.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapFixed32Fixed32.Add(UintValue(keyIndex), UintValue(offsetIndex)); allTypes.MapFixed64Fixed64.Add(UintValue(keyIndex), UintValue(offsetIndex)); allTypes.MapSfixed32Sfixed32.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapSfixed64Sfixed64.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapInt32Float.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapInt32Double.Add(IntValue(keyIndex), IntValue(offsetIndex)); allTypes.MapBoolBool.Add(BoolValue(keyIndex), BoolValue(offsetIndex)); allTypes.MapStringString.Add(StringValue(keyIndex), StringValue(offsetIndex)); allTypes.MapInt32Bytes.Add(IntValue(keyIndex), ByteValue(offsetIndex)); allTypes.MapInt32Enum.Add(IntValue(keyIndex), MapEnumValue(offsetIndex)); allTypes.MapInt32ForeignMessage.Add(IntValue(keyIndex), ForeignMessageValue(offsetIndex)); allTypes.MapInt32ForeignNoEventsMessage.Add(IntValue(keyIndex), ForeignMessageNoEventsValue(offsetIndex)); allTypes.MapInt32TestAllTypesMessage.Add(IntValue(keyIndex), TestAllTypesValue(offsetIndex)); allTypes.MapInt32TestAllTypesNoEventsMessage.Add(IntValue(keyIndex), TestAllTypesNoEventsValue(offsetIndex)); }
public static void RemoveMap(TestAllTypes allTypes, int keyIndex) { allTypes.MapInt32Int32.Remove(IntValue(keyIndex)); allTypes.MapInt64Int64.Remove(IntValue(keyIndex)); allTypes.MapUint32Uint32.Remove(UintValue(keyIndex)); allTypes.MapUint64Uint64.Remove(UintValue(keyIndex)); allTypes.MapSint32Sint32.Remove(IntValue(keyIndex)); allTypes.MapSint64Sint64.Remove(IntValue(keyIndex)); allTypes.MapFixed32Fixed32.Remove(UintValue(keyIndex)); allTypes.MapFixed64Fixed64.Remove(UintValue(keyIndex)); allTypes.MapSfixed32Sfixed32.Remove(IntValue(keyIndex)); allTypes.MapSfixed64Sfixed64.Remove(IntValue(keyIndex)); allTypes.MapInt32Float.Remove(IntValue(keyIndex)); allTypes.MapInt32Double.Remove(IntValue(keyIndex)); allTypes.MapBoolBool.Remove(BoolValue(keyIndex)); allTypes.MapStringString.Remove(StringValue(keyIndex)); allTypes.MapInt32Bytes.Remove(IntValue(keyIndex)); allTypes.MapInt32Enum.Remove(IntValue(keyIndex)); allTypes.MapInt32ForeignMessage.Remove(IntValue(keyIndex)); allTypes.MapInt32ForeignNoEventsMessage.Remove(IntValue(keyIndex)); allTypes.MapInt32TestAllTypesMessage.Remove(IntValue(keyIndex)); allTypes.MapInt32TestAllTypesNoEventsMessage.Remove(IntValue(keyIndex)); }
public static void ReplaceMap(TestAllTypes allTypes, int keyIndex, int offsetIndex = 0) { allTypes.MapInt32Int32[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapInt64Int64[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapUint32Uint32[UintValue(keyIndex)] = UintValue(offsetIndex); allTypes.MapUint64Uint64[UintValue(keyIndex)] = UintValue(offsetIndex); allTypes.MapSint32Sint32[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapSint64Sint64[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapFixed32Fixed32[UintValue(keyIndex)] = UintValue(offsetIndex); allTypes.MapFixed64Fixed64[UintValue(keyIndex)] = UintValue(offsetIndex); allTypes.MapSfixed32Sfixed32[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapSfixed64Sfixed64[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapInt32Float[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapInt32Double[IntValue(keyIndex)] = IntValue(offsetIndex); allTypes.MapBoolBool[BoolValue(keyIndex)] = BoolValue(offsetIndex); allTypes.MapStringString[StringValue(keyIndex)] = StringValue(offsetIndex); allTypes.MapInt32Bytes[IntValue(keyIndex)] = ByteValue(offsetIndex); allTypes.MapInt32Enum[IntValue(keyIndex)] = MapEnumValue(offsetIndex); allTypes.MapInt32ForeignMessage[IntValue(keyIndex)] = ForeignMessageValue(offsetIndex); allTypes.MapInt32ForeignNoEventsMessage[IntValue(keyIndex)] = ForeignMessageNoEventsValue(offsetIndex); allTypes.MapInt32TestAllTypesMessage[IntValue(keyIndex)] = TestAllTypesValue(offsetIndex); allTypes.MapInt32TestAllTypesNoEventsMessage[IntValue(keyIndex)] = TestAllTypesNoEventsValue(offsetIndex); }
public static void ClearMap(TestAllTypes allTypes) { allTypes.MapInt32Int32.Clear(); allTypes.MapInt64Int64.Clear(); allTypes.MapUint32Uint32.Clear(); allTypes.MapUint64Uint64.Clear(); allTypes.MapSint32Sint32.Clear(); allTypes.MapSint64Sint64.Clear(); allTypes.MapFixed32Fixed32.Clear(); allTypes.MapFixed64Fixed64.Clear(); allTypes.MapSfixed32Sfixed32.Clear(); allTypes.MapSfixed64Sfixed64.Clear(); allTypes.MapInt32Float.Clear(); allTypes.MapInt32Double.Clear(); allTypes.MapBoolBool.Clear(); allTypes.MapStringString.Clear(); allTypes.MapInt32Bytes.Clear(); allTypes.MapInt32Enum.Clear(); allTypes.MapInt32ForeignMessage.Clear(); allTypes.MapInt32ForeignNoEventsMessage.Clear(); allTypes.MapInt32TestAllTypesMessage.Clear(); allTypes.MapInt32TestAllTypesNoEventsMessage.Clear(); }
public void EqualsAndHashCode() { TestAllTypes a = TestUtil.GetAllSet(); TestAllTypes b = TestAllTypes.CreateBuilder().Build(); TestAllTypes c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build(); TestAllTypes d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build(); TestAllExtensions e = TestUtil.GetAllExtensionsSet(); TestAllExtensions f = TestAllExtensions.CreateBuilder(e) .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build(); CheckEqualsIsConsistent(a); CheckEqualsIsConsistent(b); CheckEqualsIsConsistent(c); CheckEqualsIsConsistent(d); CheckEqualsIsConsistent(e); CheckEqualsIsConsistent(f); CheckNotEqual(a, b); CheckNotEqual(a, c); CheckNotEqual(a, d); CheckNotEqual(a, e); CheckNotEqual(a, f); CheckNotEqual(b, c); CheckNotEqual(b, d); CheckNotEqual(b, e); CheckNotEqual(b, f); CheckNotEqual(c, d); CheckNotEqual(c, e); CheckNotEqual(c, f); CheckNotEqual(d, e); CheckNotEqual(d, f); CheckNotEqual(e, f); }
public void ParseLongString() { string longText = "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567890"; TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); TextFormat.Merge("optional_string: \"" + longText + "\"", builder); Assert.AreEqual(longText, builder.OptionalString); }
public void Clear() { var message = SampleMessages.CreateFullTestAllTypes(); var fields = TestAllTypes.Descriptor.Fields; fields[TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message); fields[TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message); fields[TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message); fields[TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message); fields[TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message); fields[TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message); fields[TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message); var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes()) { SingleBool = false, SingleInt32 = 0, SingleString = "", SingleBytes = ByteString.Empty, SingleForeignEnum = 0, SingleForeignMessage = null, }; expected.RepeatedDouble.Clear(); Assert.AreEqual(expected, message); // Separately, maps. var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; fields = TestMap.Descriptor.Fields; fields[TestMap.MapStringStringFieldNumber].Accessor.Clear(mapMessage); Assert.AreEqual(0, mapMessage.MapStringString.Count); }
public void RoundTrip_RepeatedValues() { var message = new TestAllTypes { RepeatedBool = { true, false }, RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) }, RepeatedDouble = { -12.25, 23.5 }, RepeatedFixed32 = { uint.MaxValue, 23 }, RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, RepeatedFloat = { 100f, 12.25f }, RepeatedForeignEnum = { ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_BAR }, RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } }, RepeatedImportEnum = { ImportEnum.IMPORT_BAZ, ImportEnum.IMPORT_ENUM_UNSPECIFIED }, RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } }, RepeatedInt32 = { 100, 200 }, RepeatedInt64 = { 3210987654321, long.MaxValue }, RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.NEG }, RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } }, RepeatedSfixed32 = { -123, 123 }, RepeatedSfixed64 = { -12345678901234, 12345678901234 }, RepeatedSint32 = { -456, 100 }, RepeatedSint64 = { -12345678901235, 123 }, RepeatedString = { "foo", "bar" }, RepeatedUint32 = { uint.MaxValue, uint.MinValue }, RepeatedUint64 = { ulong.MaxValue, uint.MinValue } }; byte[] bytes = message.ToByteArray(); TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); Assert.AreEqual(message, parsed); }
public void RoundTrip_Empty() { var message = new TestAllTypes(); // Without setting any values, there's nothing to write. byte[] bytes = message.ToByteArray(); Assert.AreEqual(0, bytes.Length); TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); Assert.AreEqual(message, parsed); }
public void CustomDiagnosticMessage_Nested() { var message = new TestAllTypes { SingleForeignMessage = new ForeignMessage { C = 16 } }; Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString()); Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonFormatter.Default.Format(message)); }
public void NullStringAndBytesRejected() { var message = new TestAllTypes(); Assert.Throws<ArgumentNullException>(() => message.SingleString = null); Assert.Throws<ArgumentNullException>(() => message.OneofString = null); Assert.Throws<ArgumentNullException>(() => message.SingleBytes = null); Assert.Throws<ArgumentNullException>(() => message.OneofBytes = null); }
public void UnknownEnumValueOmitted_RepeatedField() { var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } }; AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message)); }
public void AnyNested() { var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); // Nest an Any as the value of an Any. var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 }; var nestedMessage = Any.Pack(doubleNestedMessage); var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) }; AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }", formatter.Format(message)); }
public void AnyUnknownType() { // The default type registry doesn't have any types in it. var message = new TestAllTypes(); var any = Any.Pack(message); Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(any)); }
public void InvalidSurrogatePairsFail() { // Note: don't use TestCase for these, as the strings can't be reliably represented // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/ // Lone low surrogate var message = new TestAllTypes { SingleString = "a\uDC01b" }; Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message)); // Lone high surrogate message = new TestAllTypes { SingleString = "a\uD801b" }; Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message)); }
public void AnyMessageType() { var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var any = Any.Pack(message); AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any)); }
public void SurrogatePairEscaping() { var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" }; AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message)); }
[TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...) public void SimpleNonAscii(string text, string encoded) { var message = new TestAllTypes { SingleString = text }; AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message)); }
public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown() { // *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write... // but this is what happens at the moment, and it doesn't seem too awful. var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } }; AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.Format(message)); }
public void CloneSingleNonMessageValues() { var original = new TestAllTypes { SingleBool = true, SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), SingleDouble = 23.5, SingleFixed32 = 23, SingleFixed64 = 1234567890123, SingleFloat = 12.25f, SingleInt32 = 100, SingleInt64 = 3210987654321, SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, SingleSfixed32 = -123, SingleSfixed64 = -12345678901234, SingleSint32 = -456, SingleSint64 = -12345678901235, SingleString = "test", SingleUint32 = uint.MaxValue, SingleUint64 = ulong.MaxValue }; var clone = original.Clone(); Assert.AreNotSame(original, clone); Assert.AreEqual(original, clone); // Just as a single example clone.SingleInt32 = 150; Assert.AreNotEqual(original, clone); }
public void OneofProperties() { // Switch the oneof case between each of the different options, and check everything behaves // as expected in each case. var message = new TestAllTypes(); Assert.AreEqual("", message.OneofString); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); message.OneofString = "sample"; Assert.AreEqual("sample", message.OneofString); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message.OneofFieldCase); var bytes = ByteString.CopyFrom(1, 2, 3); message.OneofBytes = bytes; Assert.AreEqual("", message.OneofString); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual(bytes, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofBytes, message.OneofFieldCase); message.OneofUint32 = 20; Assert.AreEqual("", message.OneofString); Assert.AreEqual(20, message.OneofUint32); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase); var nestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 25 }; message.OneofNestedMessage = nestedMessage; Assert.AreEqual("", message.OneofString); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.AreEqual(nestedMessage, message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofNestedMessage, message.OneofFieldCase); message.ClearOneofField(); Assert.AreEqual("", message.OneofString); Assert.AreEqual(0, message.OneofUint32); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); }
public void CloneRepeatedNonMessageValues() { var original = new TestAllTypes { RepeatedBool = { true, false }, RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) }, RepeatedDouble = { -12.25, 23.5 }, RepeatedFixed32 = { uint.MaxValue, 23 }, RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, RepeatedFloat = { 100f, 12.25f }, RepeatedInt32 = { 100, 200 }, RepeatedInt64 = { 3210987654321, long.MaxValue }, RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.NEG }, RepeatedSfixed32 = { -123, 123 }, RepeatedSfixed64 = { -12345678901234, 12345678901234 }, RepeatedSint32 = { -456, 100 }, RepeatedSint64 = { -12345678901235, 123 }, RepeatedString = { "foo", "bar" }, RepeatedUint32 = { uint.MaxValue, uint.MinValue }, RepeatedUint64 = { ulong.MaxValue, uint.MinValue } }; var clone = original.Clone(); Assert.AreNotSame(original, clone); Assert.AreEqual(original, clone); // Just as a single example clone.RepeatedDouble.Add(25.5); Assert.AreNotEqual(original, clone); }
public void DoubleRepresentations(double value, string expectedValueText) { var message = new TestAllTypes { SingleDouble = value }; string actualText = JsonFormatter.Default.Format(message); string expectedText = "{ 'singleDouble': " + expectedValueText + " }"; AssertJson(expectedText, actualText); }
public void AnyMessageType_CustomPrefix() { var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10 }; var any = Any.Pack(message, "foo.bar/baz"); AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any)); }
public void UnknownEnumValueOmitted_SingleField() { var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 }; AssertJson("{ }", JsonFormatter.Default.Format(message)); }
public void OneofSerialization_NonDefaultValue() { var message = new TestAllTypes(); message.OneofString = "this would take a bit of space"; message.OneofUint32 = 10; var bytes = message.ToByteArray(); Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string! var message2 = TestAllTypes.Parser.ParseFrom(bytes); Assert.AreEqual(message, message2); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase); }
public void WriteValue_Message() { var value = new TestAllTypes { SingleInt32 = 100, SingleInt64 = 3210987654321L }; AssertWriteValue(value, "{ 'singleInt32': 100, 'singleInt64': '3210987654321' }"); }
public void OneofSerialization_DefaultValue() { var message = new TestAllTypes(); message.OneofString = "this would take a bit of space"; message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized var bytes = message.ToByteArray(); Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized var message2 = TestAllTypes.Parser.ParseFrom(bytes); Assert.AreEqual(message, message2); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase); }
public void UnknownEnumValue_RepeatedField_AllEntriesUnknown() { var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } }; AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter.Default.Format(message)); }