public void TestFullMessageWithRichTypes() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetValid(true) .SetText("text") .AddTextlines("a") .AddTextlines("b") .AddTextlines("c") .SetNumber(0x1010101010) .AddNumbers(1) .AddNumbers(2) .AddNumbers(3) .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3]))) .Build(); StringWriter sw = new StringWriter(); XmlWriter xwtr = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, IndentChars = " " }); XmlFormatWriter.CreateInstance(xwtr) .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues) .WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); rdr.Options = XmlReaderOptions.ReadNestedArrays; TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestXmlWithWhitespace() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetText(" \t <- leading space and trailing -> \r\n\t").Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestMessageWithXmlText() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetText("<text>").Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestBooleanTypes() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetValid(true) .Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestNestedMessage() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.TWO).Build()) .Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestFullMessageWithUnknownFields() { TestXmlMessage origial = TestXmlMessage.CreateBuilder() .SetValid(true) .SetText("text") .AddTextlines("a") .AddTextlines("b") .AddTextlines("c") .SetNumber(0x1010101010) .AddNumbers(1) .AddNumbers(2) .AddNumbers(3) .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3]))) .Build(); TestXmlNoFields message = TestXmlNoFields.CreateBuilder().MergeFrom(origial.ToByteArray()).Build(); Assert.AreEqual(0, message.AllFields.Count); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw) .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues) .WriteMessage("root", message); string xml = sw.ToString(); using (XmlReader x = XmlReader.Create(new StringReader(xml))) { x.MoveToContent(); Assert.AreEqual(XmlNodeType.Element, x.NodeType); //should always be empty Assert.IsTrue(x.IsEmptyElement || (x.Read() && x.NodeType == XmlNodeType.EndElement) ); } XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); rdr.Options = XmlReaderOptions.ReadNestedArrays; TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(TestXmlMessage.DefaultInstance, copy); }
public void TestRepeatedField() { TestXmlChild message = TestXmlChild.CreateBuilder() .AddOptions(EnumOptions.ONE) .AddOptions(EnumOptions.TWO) .Build(); //Allow the writer to write the root element StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlChild copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestEmptyMessage() { TestXmlChild message = TestXmlChild.CreateBuilder() .Build(); StringWriter sw = new StringWriter(); XmlWriter xw = XmlWriter.Create(sw); //When we call message.WriteTo, we are responsible for the root element xw.WriteStartElement("root"); message.WriteTo(XmlFormatWriter.CreateInstance(xw)); xw.WriteEndElement(); xw.Flush(); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlChild copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void TestXmlWithExtensionEnum() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetExtension(UnittestExtrasXmltest.ExtensionEnum, EnumOptions.ONE) .Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); ExtensionRegistry registry = ExtensionRegistry.CreateInstance(); UnittestExtrasXmltest.RegisterAllExtensions(registry); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build(); Assert.AreEqual(message, copy); }
public void TestXmlWithExtensionMessage() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build()).Build(); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message); string xml = sw.ToString(); ExtensionRegistry registry = ExtensionRegistry.CreateInstance(); UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build(); Assert.AreEqual(message, copy); }
protected override TBuilder DeserializeMessage <TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry) { XmlFormatReader reader = XmlFormatReader.CreateInstance((string)message).SetOptions(XmlReaderOptions.ReadNestedArrays); return(reader.Merge("root", builder, registry)); }