public void ParseExtensions() { TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder(); TextFormat.Merge(AllExtensionsSetText, TestUtil.CreateExtensionRegistry(), builder); TestUtil.AssertAllExtensionsSet(builder.Build()); }
private static void AssertParseError(string error, string text) { TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); try { TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder); Assert.Fail("Expected parse exception."); } catch (FormatException e) { Assert.AreEqual(error, e.Message); } }
public void ParsePackedExtensions() { // Ensure that packed extensions can be properly parsed. TestPackedExtensions message = TestUtil.GetPackedExtensionsSet(); ByteString rawBytes = message.ToByteString(); ExtensionRegistry registry = TestUtil.CreateExtensionRegistry(); TestPackedExtensions message2 = TestPackedExtensions.ParseFrom(rawBytes, registry); TestUtil.AssertPackedExtensionsSet(message2); }
public void ParseUnknownEnumValue() { FieldDescriptor singularField = TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("optional_nested_enum"); FieldDescriptor repeatedField = TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("repeated_nested_enum"); Assert.IsNotNull(singularField); Assert.IsNotNull(repeatedField); ByteString data = UnknownFieldSet.CreateBuilder() .AddField(singularField.FieldNumber, UnknownField.CreateBuilder() .AddVarint((int)TestAllTypes.Types.NestedEnum.BAR) .AddVarint(5) // not valid .Build()) .AddField(repeatedField.FieldNumber, UnknownField.CreateBuilder() .AddVarint((int)TestAllTypes.Types.NestedEnum.FOO) .AddVarint(4) // not valid .AddVarint((int)TestAllTypes.Types.NestedEnum.BAZ) .AddVarint(6) // not valid .Build()) .Build() .ToByteString(); { TestAllTypes message = TestAllTypes.ParseFrom(data); Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.OptionalNestedEnum); TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ }, message.RepeatedNestedEnumList); TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList); TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList); } { TestAllExtensions message = TestAllExtensions.ParseFrom(data, TestUtil.CreateExtensionRegistry()); Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension)); TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ }, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension)); TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList); TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList); } }
private static void AssertParseError(string error, string text) { TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); Exception exception = Assert.Throws <FormatException>(() => TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder)); Assert.AreEqual(error, exception.Message); }