Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
csharptest authored and rogerk committed Sep 21, 2013
2 parents 0a0dd03 dc8149f commit 785ac69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 82,10 @@ protected override void WriteToOutput(char ch)
{
if (_bufferPos >= _buffer.Length)
{
if (_output == null)
{
_output = new StringWriter(new StringBuilder(_buffer.Length * 2));
}
Flush();
}
_buffer[_bufferPos ] = ch;
Expand Down
17 changes: 17 additions & 0 deletions src/ProtocolBuffers.Test/ByteStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 34,7 @@

#endregion

using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -127,5 128,21 @@ public void ToStringWithExplicitEncoding()
ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
}

[TestMethod]
public void FromBase64_WithText()
{
byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
string base64 = Convert.ToBase64String(data);
ByteString bs = ByteString.FromBase64(base64);
TestUtil.AssertBytesEqual(data, bs.ToByteArray());
}

[TestMethod]
public void FromBase64_Empty()
{
// Optimization which also fixes issue 61.
Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
}
}
}
37 changes: 37 additions & 0 deletions src/ProtocolBuffers.Test/TestWriterFormatJson.cs
Original file line number Diff line number Diff line change
@@ -1,9 1,11 @@
using System;
using System.IO;
using System.Text;
using Google.ProtocolBuffers.DescriptorProtos;
using Google.ProtocolBuffers.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Google.ProtocolBuffers.TestProtos;
using EnumOptions = Google.ProtocolBuffers.TestProtos.EnumOptions;

namespace Google.ProtocolBuffers
{
Expand Down Expand Up @@ -152,6 154,7 @@ public void TestEmptyMessage()
@"{}"
);
}

[TestMethod]
public void TestRepeatedField()
{
Expand All @@ -163,6 166,7 @@ public void TestRepeatedField()
@"{""options"":[""ONE"",""TWO""]}"
);
}

[TestMethod]
public void TestNestedEmptyMessage()
{
Expand All @@ -173,6 177,7 @@ public void TestNestedEmptyMessage()
@"{""child"":{}}"
);
}

[TestMethod]
public void TestNestedMessage()
{
Expand All @@ -183,6 188,7 @@ public void TestNestedMessage()
@"{""child"":{""options"":[""TWO""]}}"
);
}

[TestMethod]
public void TestBooleanTypes()
{
Expand All @@ -193,6 199,7 @@ public void TestBooleanTypes()
@"{""valid"":true}"
);
}

[TestMethod]
public void TestFullMessage()
{
Expand Down Expand Up @@ -222,6 229,7 @@ public void TestFullMessage()
0x1010101010L.ToString()
);
}

[TestMethod]
public void TestMessageWithXmlText()
{
Expand All @@ -232,6 240,7 @@ public void TestMessageWithXmlText()
@"{""text"":""<text><\/text>""}"
);
}

[TestMethod]
public void TestWithEscapeChars()
{
Expand All @@ -242,6 251,7 @@ public void TestWithEscapeChars()
"{\"text\":\" \\t <- \\\"leading space and trailing\\\" -> \\\\ \\uef54 \\u0000 \\u00ff \\uffff \\b \\f \\r \\n \\t \"}"
);
}

[TestMethod]
public void TestWithExtensionText()
{
Expand All @@ -253,6 263,7 @@ public void TestWithExtensionText()
@"{""valid"":false,""extension_text"":"" extension text value ! ""}"
);
}

[TestMethod]
public void TestWithExtensionNumber()
{
Expand All @@ -264,6 275,7 @@ public void TestWithExtensionNumber()
@"{""number"":42}"
);
}

[TestMethod]
public void TestWithExtensionArray()
{
Expand All @@ -276,6 288,7 @@ public void TestWithExtensionArray()
@"{""extension_number"":[100,101,102]}"
);
}

[TestMethod]
public void TestWithExtensionEnum()
{
Expand All @@ -286,6 299,7 @@ public void TestWithExtensionEnum()
@"{""extension_enum"":""ONE""}"
);
}

[TestMethod]
public void TestMessageWithExtensions()
{
Expand All @@ -308,6 322,7 @@ public void TestMessageWithExtensions()
@"""extension_message"":{""number"":42}"
);
}

[TestMethod]
public void TestMessageMissingExtensions()
{
Expand Down Expand Up @@ -340,6 355,7 @@ public void TestMessageMissingExtensions()
Assert.AreNotEqual(original, copy);
Assert.AreEqual(message, copy);
}

[TestMethod]
public void TestMergeFields()
{
Expand All @@ -350,6 366,7 @@ public void TestMergeFields()
Assert.AreEqual("text", builder.Text);
Assert.AreEqual(411, builder.Number);
}

[TestMethod]
public void TestMessageArray()
{
Expand All @@ -374,6 391,7 @@ public void TestMessageArray()
Assert.AreEqual(3, ordinal);
Assert.AreEqual(3, builder.TextlinesCount);
}

[TestMethod]
public void TestNestedMessageArray()
{
Expand Down Expand Up @@ -403,6 421,7 @@ public void TestNestedMessageArray()
Assert.AreEqual(3, ordinal);
Assert.AreEqual(3, builder.TextlinesCount);
}

[TestMethod]
public void TestReadWriteJsonWithoutRoot()
{
Expand All @@ -425,6 444,7 @@ public void TestReadWriteJsonWithoutRoot()

Assert.AreEqual(message, copy);
}

[TestMethod,ExpectedException(typeof(RecursionLimitExceededException))]
public void TestRecursiveLimit()
{
Expand All @@ -433,29 453,46 @@ public void TestRecursiveLimit()
sb.Append("{\"child\":");
TestXmlRescursive msg = Extensions.MergeFromJson(new TestXmlRescursive.Builder(), sb.ToString()).Build();
}

[TestMethod, ExpectedException(typeof(FormatException))]
public void FailWithEmptyText()
{
JsonFormatReader.CreateInstance("")
.Merge(TestXmlMessage.CreateBuilder());
}

[TestMethod, ExpectedException(typeof(FormatException))]
public void FailWithUnexpectedValue()
{
JsonFormatReader.CreateInstance("{{}}")
.Merge(TestXmlMessage.CreateBuilder());
}

[TestMethod, ExpectedException(typeof(FormatException))]
public void FailWithUnQuotedName()
{
JsonFormatReader.CreateInstance("{name:{}}")
.Merge(TestXmlMessage.CreateBuilder());
}

[TestMethod, ExpectedException(typeof(FormatException))]
public void FailWithUnexpectedType()
{
JsonFormatReader.CreateInstance("{\"valid\":{}}")
.Merge(TestXmlMessage.CreateBuilder());
}

// See issue 64 for background.
[TestMethod]
public void ToJsonRequiringBufferExpansion()
{
string s = new string('.', 4086);
var opts = FileDescriptorProto.CreateBuilder()
.SetName(s)
.SetPackage("package")
.BuildPartial();

Assert.NotNull(opts.ToJson());
}
}
}
4 changes: 3 additions & 1 deletion src/ProtocolBuffers/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 129,9 @@ public string ToBase64()
/// </summary>
public static ByteString FromBase64(string bytes)
{
return new ByteString(Convert.FromBase64String(bytes));
// By handling the empty string explicitly, we not only optimize but we fix a
// problem on CF 2.0. See issue 61 for details.
return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes));
}

/// <summary>
Expand Down

0 comments on commit 785ac69

Please sign in to comment.