diff options
Diffstat (limited to 'src/ProtocolBuffers.Test')
5 files changed, 164 insertions, 54 deletions
diff --git a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs index 0306c41e..b54900bb 100644 --- a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs +++ b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs @@ -71,5 +71,117 @@ namespace Google.ProtocolBuffers.CompatTests Assert.AreEqual(TestResources.google_message2, copy.ToByteArray());
}
+ #region Test message builders
+
+ private static TestAllTypes.Builder AddAllTypes(TestAllTypes.Builder builder)
+ {
+ return builder.SetOptionalInt32(1001)
+ .SetOptionalInt64(1001)
+ .SetOptionalUint32(1001)
+ .SetOptionalUint64(1001)
+ .SetOptionalSint32(-1001)
+ .SetOptionalSint64(-1001)
+ .SetOptionalFixed32(1001)
+ .SetOptionalFixed64(1001)
+ .SetOptionalSfixed32(-1001)
+ .SetOptionalSfixed64(-1001)
+ .SetOptionalFloat(1001.1001f)
+ .SetOptionalDouble(1001.1001)
+ .SetOptionalBool(true)
+ .SetOptionalString("this is a string value")
+ .SetOptionalBytes(ByteString.CopyFromUtf8("this is an array of bytes"))
+ .SetOptionalGroup(new TestAllTypes.Types.OptionalGroup.Builder().SetA(1001))
+ .SetOptionalNestedMessage(new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))
+ .SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO)
+ ;
+ }
+
+ private static TestAllTypes.Builder AddRepeatedTypes(TestAllTypes.Builder builder, int size)
+ {
+ //repeated values
+ for (int i = 0; i < size; i++)
+ builder.AddRepeatedInt32(1001 + i)
+ .AddRepeatedInt64(1001)
+ .AddRepeatedUint32(1001)
+ .AddRepeatedUint64(1001)
+ .AddRepeatedSint32(-1001)
+ .AddRepeatedSint64(-1001)
+ .AddRepeatedFixed32(1001)
+ .AddRepeatedFixed64(1001)
+ .AddRepeatedSfixed32(-1001)
+ .AddRepeatedSfixed64(-1001)
+ .AddRepeatedFloat(1001.1001f)
+ .AddRepeatedDouble(1001.1001)
+ .AddRepeatedBool(true)
+ .AddRepeatedString("this is a string value")
+ .AddRepeatedBytes(ByteString.CopyFromUtf8("this is an array of bytes"))
+ .AddRepeatedGroup(new TestAllTypes.Types.RepeatedGroup.Builder().SetA(1001))
+ .AddRepeatedNestedMessage(new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))
+ .AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.FOO)
+ ;
+ return builder;
+ }
+
+ private static TestPackedTypes.Builder AddPackedTypes(TestPackedTypes.Builder builder, int size)
+ {
+ for(int i=0; i < size; i++ )
+ builder.AddPackedInt32(1001)
+ .AddPackedInt64(1001)
+ .AddPackedUint32(1001)
+ .AddPackedUint64(1001)
+ .AddPackedSint32(-1001)
+ .AddPackedSint64(-1001)
+ .AddPackedFixed32(1001)
+ .AddPackedFixed64(1001)
+ .AddPackedSfixed32(-1001)
+ .AddPackedSfixed64(-1001)
+ .AddPackedFloat(1001.1001f)
+ .AddPackedDouble(1001.1001)
+ .AddPackedBool(true)
+ .AddPackedEnum(ForeignEnum.FOREIGN_FOO)
+ ;
+ return builder;
+ }
+
+ #endregion
+
+ [Test]
+ public void TestRoundTripAllTypes()
+ {
+ TestAllTypes msg = AddAllTypes(new TestAllTypes.Builder()).Build();
+ object content = SerializeMessage<TestAllTypes, TestAllTypes.Builder>(msg);
+
+ TestAllTypes copy = DeerializeMessage<TestAllTypes, TestAllTypes.Builder>(content, TestAllTypes.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ AssertOutputEquals(content, SerializeMessage<TestAllTypes, TestAllTypes.Builder>(copy));
+ Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
+ }
+
+ [Test]
+ public void TestRoundTripRepeatedTypes()
+ {
+ TestAllTypes msg = AddRepeatedTypes(new TestAllTypes.Builder(), 5).Build();
+ object content = SerializeMessage<TestAllTypes, TestAllTypes.Builder>(msg);
+
+ TestAllTypes copy = DeerializeMessage<TestAllTypes, TestAllTypes.Builder>(content, TestAllTypes.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ AssertOutputEquals(content, SerializeMessage<TestAllTypes, TestAllTypes.Builder>(copy));
+ Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
+ }
+
+ [Test]
+ public void TestRoundTripPackedTypes()
+ {
+ TestPackedTypes msg = AddPackedTypes(new TestPackedTypes.Builder(), 5).Build();
+ object content = SerializeMessage<TestPackedTypes, TestPackedTypes.Builder>(msg);
+
+ TestPackedTypes copy = DeerializeMessage<TestPackedTypes, TestPackedTypes.Builder>(content, TestPackedTypes.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ AssertOutputEquals(content, SerializeMessage<TestPackedTypes, TestPackedTypes.Builder>(copy));
+ Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
+ }
}
}
diff --git a/src/ProtocolBuffers.Test/CompatTests/JsonCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/JsonCompatibilityTests.cs index 30a08398..23fa2638 100644 --- a/src/ProtocolBuffers.Test/CompatTests/JsonCompatibilityTests.cs +++ b/src/ProtocolBuffers.Test/CompatTests/JsonCompatibilityTests.cs @@ -11,7 +11,7 @@ namespace Google.ProtocolBuffers.CompatTests protected override object SerializeMessage<TMessage, TBuilder>(TMessage message)
{
StringWriter sw = new StringWriter();
- new JsonFormatWriter(sw)
+ JsonFormatWriter.CreateInstance(sw)
.Formatted()
.WriteMessage(message);
return sw.ToString();
@@ -19,7 +19,7 @@ namespace Google.ProtocolBuffers.CompatTests protected override TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
{
- new JsonFormatReader((string)message).Merge(builder);
+ JsonFormatReader.CreateInstance((string)message).Merge(builder);
return builder;
}
}
diff --git a/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs index e204200d..14616b74 100644 --- a/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs +++ b/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs @@ -10,14 +10,14 @@ namespace Google.ProtocolBuffers.CompatTests protected override object SerializeMessage<TMessage, TBuilder>(TMessage message)
{
StringWriter text = new StringWriter();
- XmlFormatWriter writer = new XmlFormatWriter(text);
+ XmlFormatWriter writer = XmlFormatWriter.CreateInstance(text);
writer.WriteMessage("root", message);
return text.ToString();
}
protected override TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
{
- XmlFormatReader reader = new XmlFormatReader((string)message);
+ XmlFormatReader reader = XmlFormatReader.CreateInstance((string)message);
return reader.Merge("root", builder, registry);
}
}
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs index 1c95d841..88c059d2 100644 --- a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs +++ b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs @@ -14,7 +14,7 @@ namespace Google.ProtocolBuffers protected void FormatterAssert<TMessage>(TMessage message, params string[] expecting) where TMessage : IMessageLite
{
StringWriter sw = new StringWriter();
- new JsonFormatWriter(sw).WriteMessage(message);
+ JsonFormatWriter.CreateInstance(sw).WriteMessage(message);
Content = sw.ToString();
@@ -22,7 +22,7 @@ namespace Google.ProtocolBuffers UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
IMessageLite copy =
- new JsonFormatReader(Content)
+ JsonFormatReader.CreateInstance(Content)
.Merge(message.WeakCreateBuilderForType(), registry).WeakBuild();
Assert.AreEqual(typeof(TMessage), copy.GetType());
@@ -44,12 +44,12 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new JsonFormatWriter(sw).Formatted()
+ JsonFormatWriter.CreateInstance(sw).Formatted()
.WriteMessage(message);
string json = sw.ToString();
- TestXmlMessage copy = new JsonFormatReader(json)
+ TestXmlMessage copy = JsonFormatReader.CreateInstance(json)
.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -240,11 +240,11 @@ namespace Google.ProtocolBuffers .ClearExtension(UnitTestXmlSerializerTestProtoFile.ExtensionEnum)
.Build();
- JsonFormatWriter writer = new JsonFormatWriter();
+ JsonFormatWriter writer = JsonFormatWriter.CreateInstance();
writer.WriteMessage(original);
Content = writer.ToString();
- IMessageLite copy = new JsonFormatReader(Content)
+ IMessageLite copy = JsonFormatReader.CreateInstance(Content)
.Merge(message.CreateBuilderForType()).Build();
Assert.AreNotEqual(original, message);
@@ -255,8 +255,8 @@ namespace Google.ProtocolBuffers public void TestMergeFields()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
- builder.MergeFrom(new JsonFormatReader("\"valid\": true"));
- builder.MergeFrom(new JsonFormatReader("\"text\": \"text\", \"number\": \"411\""));
+ builder.MergeFrom(JsonFormatReader.CreateInstance("\"valid\": true"));
+ builder.MergeFrom(JsonFormatReader.CreateInstance("\"text\": \"text\", \"number\": \"411\""));
Assert.AreEqual(true, builder.Valid);
Assert.AreEqual("text", builder.Text);
Assert.AreEqual(411, builder.Number);
@@ -264,7 +264,7 @@ namespace Google.ProtocolBuffers [Test]
public void TestMessageArray()
{
- JsonFormatWriter writer = new JsonFormatWriter().Formatted();
+ JsonFormatWriter writer = JsonFormatWriter.CreateInstance().Formatted();
using (writer.StartArray())
{
writer.WriteMessage(TestXmlMessage.CreateBuilder().SetNumber(1).AddTextlines("a").Build());
@@ -272,7 +272,7 @@ namespace Google.ProtocolBuffers writer.WriteMessage(TestXmlMessage.CreateBuilder().SetNumber(3).AddTextlines("c").Build());
}
string json = writer.ToString();
- JsonFormatReader reader = new JsonFormatReader(json);
+ JsonFormatReader reader = JsonFormatReader.CreateInstance(json);
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
int ordinal = 0;
@@ -288,7 +288,7 @@ namespace Google.ProtocolBuffers [Test]
public void TestNestedMessageArray()
{
- JsonFormatWriter writer = new JsonFormatWriter();
+ JsonFormatWriter writer = JsonFormatWriter.CreateInstance();
using (writer.StartArray())
{
using (writer.StartArray())
@@ -300,7 +300,7 @@ namespace Google.ProtocolBuffers writer.WriteMessage(TestXmlMessage.CreateBuilder().SetNumber(3).AddTextlines("c").Build());
}
string json = writer.ToString();
- JsonFormatReader reader = new JsonFormatReader(json);
+ JsonFormatReader reader = JsonFormatReader.CreateInstance(json);
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
int ordinal = 0;
@@ -317,25 +317,25 @@ namespace Google.ProtocolBuffers [Test, ExpectedException(typeof(FormatException))]
public void FailWithEmptyText()
{
- new JsonFormatReader("")
+ JsonFormatReader.CreateInstance("")
.Merge(TestXmlMessage.CreateBuilder());
}
[Test, ExpectedException(typeof(FormatException))]
public void FailWithUnexpectedValue()
{
- new JsonFormatReader("{{}}")
+ JsonFormatReader.CreateInstance("{{}}")
.Merge(TestXmlMessage.CreateBuilder());
}
[Test, ExpectedException(typeof(FormatException))]
public void FailWithUnQuotedName()
{
- new JsonFormatReader("{name:{}}")
+ JsonFormatReader.CreateInstance("{name:{}}")
.Merge(TestXmlMessage.CreateBuilder());
}
[Test, ExpectedException(typeof(FormatException))]
public void FailWithUnexpectedType()
{
- new JsonFormatReader("{\"valid\":{}}")
+ JsonFormatReader.CreateInstance("{\"valid\":{}}")
.Merge(TestXmlMessage.CreateBuilder());
}
}
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs index a2def5e0..2ddf39e5 100644 --- a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs +++ b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs @@ -23,12 +23,12 @@ namespace Google.ProtocolBuffers //When we call message.WriteTo, we are responsible for the root element
xw.WriteStartElement("root");
- message.WriteTo(new XmlFormatWriter(xw));
+ message.WriteTo(XmlFormatWriter.CreateInstance(xw));
xw.WriteEndElement();
xw.Flush();
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlChild copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -42,10 +42,10 @@ namespace Google.ProtocolBuffers //Allow the writer to write the root element
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlChild copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -57,10 +57,10 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -72,10 +72,10 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -87,10 +87,10 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -113,11 +113,11 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -140,14 +140,13 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw)
- {
- Options = XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues
- }.WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw)
+ .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues)
+ .WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
rdr.Options = XmlReaderOptions.ReadNestedArrays;
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
@@ -174,10 +173,9 @@ namespace Google.ProtocolBuffers Assert.AreEqual(0, message.AllFields.Count);
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw)
- {
- Options = XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues
- }.WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw)
+ .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues)
+ .WriteMessage("root", message);
string xml = sw.ToString();
@@ -191,7 +189,7 @@ namespace Google.ProtocolBuffers );
}
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
rdr.Options = XmlReaderOptions.ReadNestedArrays;
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(TestXmlMessage.DefaultInstance, copy);
@@ -203,11 +201,11 @@ namespace Google.ProtocolBuffers .SetText("<text>").Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -218,11 +216,11 @@ namespace Google.ProtocolBuffers .SetText(" \t <- leading space and trailing -> \r\n\t").Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();
Assert.AreEqual(message, copy);
}
@@ -234,14 +232,14 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();
Assert.AreEqual(message, copy);
}
@@ -253,14 +251,14 @@ namespace Google.ProtocolBuffers new TestXmlExtension.Builder().SetNumber(42).Build()).Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();
Assert.AreEqual(message, copy);
}
@@ -274,14 +272,14 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();
Assert.AreEqual(message, copy);
}
@@ -293,14 +291,14 @@ namespace Google.ProtocolBuffers .Build();
StringWriter sw = new StringWriter();
- new XmlFormatWriter(sw).WriteMessage("root", message);
+ XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);
string xml = sw.ToString();
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
- XmlFormatReader rdr = new XmlFormatReader(xml);
+ XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);
TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();
Assert.AreEqual(message, copy);
}
|