aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp
diff options
context:
space:
mode:
Diffstat (limited to 'csharp')
-rw-r--r--csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs131
1 files changed, 71 insertions, 60 deletions
diff --git a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
index b3f6041e..ac33a750 100644
--- a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+++ b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
@@ -31,16 +31,16 @@
#endregion
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
using UnitTest.Issues.TestProtos;
namespace Google.Protobuf
{
+ /// <summary>
+ /// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
+ /// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
+ /// </summary>
public class JsonFormatterTest
{
[Test]
@@ -48,16 +48,16 @@ namespace Google.Protobuf
{
var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));
- Assert.AreEqual("{ }", formatter.Format(new ForeignMessage()));
- Assert.AreEqual("{ }", formatter.Format(new TestAllTypes()));
- Assert.AreEqual("{ }", formatter.Format(new TestMap()));
+ AssertJson("{ }", formatter.Format(new ForeignMessage()));
+ AssertJson("{ }", formatter.Format(new TestAllTypes()));
+ AssertJson("{ }", formatter.Format(new TestMap()));
}
[Test]
public void DefaultValues_WhenIncluded()
{
var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
- Assert.AreEqual("{ \"c\": 0 }", formatter.Format(new ForeignMessage()));
+ AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
}
[Test]
@@ -90,45 +90,45 @@ namespace Google.Protobuf
};
var actualText = JsonFormatter.Default.Format(message);
- // Fields in declaration order, which matches numeric order.
+ // 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 }" +
+ "'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 }" +
" }";
- Assert.AreEqual(expectedText, actualText);
+ AssertJson(expectedText, actualText);
}
[Test]
public void RepeatedField()
{
- Assert.AreEqual("{ \"repeatedInt32\": [ 1, 2, 3, 4, 5 ] }",
+ AssertJson("{ 'repeatedInt32': [ 1, 2, 3, 4, 5 ] }",
JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
}
[Test]
public void MapField_StringString()
{
- Assert.AreEqual("{ \"mapStringString\": { \"with spaces\": \"bar\", \"a\": \"b\" } }",
+ AssertJson("{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b' } }",
JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
}
@@ -136,7 +136,7 @@ namespace Google.Protobuf
public void MapField_Int32Int32()
{
// The keys are quoted, but the values aren't.
- Assert.AreEqual("{ \"mapInt32Int32\": { \"0\": 1, \"2\": 3 } }",
+ AssertJson("{ 'mapInt32Int32': { '0': 1, '2': 3 } }",
JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
}
@@ -144,34 +144,34 @@ namespace Google.Protobuf
public void MapField_BoolBool()
{
// The keys are quoted, but the values aren't.
- Assert.AreEqual("{ \"mapBoolBool\": { \"false\": true, \"true\": false } }",
+ AssertJson("{ 'mapBoolBool': { 'false': true, 'true': false } }",
JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
}
[TestCase(1.0, "1")]
- [TestCase(double.NaN, "\"NaN\"")]
- [TestCase(double.PositiveInfinity, "\"Infinity\"")]
- [TestCase(double.NegativeInfinity, "\"-Infinity\"")]
+ [TestCase(double.NaN, "'NaN'")]
+ [TestCase(double.PositiveInfinity, "'Infinity'")]
+ [TestCase(double.NegativeInfinity, "'-Infinity'")]
public void DoubleRepresentations(double value, string expectedValueText)
{
var message = new TestAllTypes { SingleDouble = value };
string actualText = JsonFormatter.Default.Format(message);
- string expectedText = "{ \"singleDouble\": " + expectedValueText + " }";
- Assert.AreEqual(expectedText, actualText);
+ string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
+ AssertJson(expectedText, actualText);
}
[Test]
public void UnknownEnumValueOmitted_SingleField()
{
var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
- Assert.AreEqual("{ }", JsonFormatter.Default.Format(message));
+ AssertJson("{ }", JsonFormatter.Default.Format(message));
}
[Test]
public void UnknownEnumValueOmitted_RepeatedField()
{
var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } };
- Assert.AreEqual("{ \"repeatedForeignEnum\": [ \"FOREIGN_BAZ\", \"FOREIGN_FOO\" ] }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
}
[Test]
@@ -179,7 +179,7 @@ namespace Google.Protobuf
{
// This matches the C++ behaviour.
var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } };
- Assert.AreEqual("{ \"mapInt32Enum\": { \"1\": \"MAP_ENUM_FOO\", \"3\": \"MAP_ENUM_BAR\" } }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
}
[Test]
@@ -188,14 +188,14 @@ namespace Google.Protobuf
// *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 } };
- Assert.AreEqual("{ \"repeatedForeignEnum\": [ ] }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.Format(message));
}
[Test]
public void NullValueForMessage()
{
var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } };
- Assert.AreEqual("{ \"mapInt32ForeignMessage\": { \"10\": null } }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'mapInt32ForeignMessage': { '10': null } }", JsonFormatter.Default.Format(message));
}
[Test]
@@ -205,14 +205,14 @@ namespace Google.Protobuf
public void SimpleNonAscii(string text, string encoded)
{
var message = new TestAllTypes { SingleString = text };
- Assert.AreEqual("{ \"singleString\": \"" + encoded + "\" }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
}
[Test]
public void SurrogatePairEscaping()
{
var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
- Assert.AreEqual("{ \"singleString\": \"a\\ud801\\udc01b\" }", JsonFormatter.Default.Format(message));
+ AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message));
}
[Test]
@@ -241,8 +241,8 @@ namespace Google.Protobuf
[Test]
[TestCase(null, "{ }")]
- [TestCase("x", "{ \"fooString\": \"x\" }")]
- [TestCase("", "{ \"fooString\": \"\" }")]
+ [TestCase("x", "{ 'fooString': 'x' }")]
+ [TestCase("", "{ 'fooString': '' }")]
[TestCase(null, "{ }")]
public void Oneof(string fooStringValue, string expectedJson)
{
@@ -254,9 +254,9 @@ namespace Google.Protobuf
// We should get the same result both with and without "format default values".
var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
- Assert.AreEqual(expectedJson, formatter.Format(message));
+ AssertJson(expectedJson, formatter.Format(message));
formatter = new JsonFormatter(new JsonFormatter.Settings(true));
- Assert.AreEqual(expectedJson, formatter.Format(message));
+ AssertJson(expectedJson, formatter.Format(message));
}
[Test]
@@ -271,8 +271,8 @@ namespace Google.Protobuf
BytesField = ByteString.FromBase64("ABCD"),
StringField = ""
};
- var expectedJson = "{ \"int64Field\": \"10\", \"int32Field\": 0, \"stringField\": \"\", \"bytesField\": \"ABCD\" }";
- Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(message));
+ var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
+ AssertJson(expectedJson, JsonFormatter.Default.Format(message));
}
[Test]
@@ -291,11 +291,11 @@ namespace Google.Protobuf
{
var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 };
- Assert.AreEqual("{ \"plainString\": \"p1\", \"plainInt32\": 2 }", formatter.Format(message));
+ AssertJson("{ 'plainString': 'p1', 'plainInt32': 2 }", formatter.Format(message));
message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
- Assert.AreEqual("{ \"plainString\": \"plain\", \"o2String\": \"o2\", \"plainInt32\": 10, \"o1Int32\": 5 }", formatter.Format(message));
+ AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
- Assert.AreEqual("{ \"plainString\": \"plain\", \"o1String\": \"\", \"plainInt32\": 10, \"o2Int32\": 0 }", formatter.Format(message));
+ AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
}
[Test]
@@ -303,11 +303,22 @@ namespace Google.Protobuf
{
var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
var message = new TestJsonFieldOrdering();
- Assert.AreEqual("{ \"plainString\": \"\", \"plainInt32\": 0 }", formatter.Format(message));
+ AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
- Assert.AreEqual("{ \"plainString\": \"plain\", \"o2String\": \"o2\", \"plainInt32\": 10, \"o1Int32\": 5 }", formatter.Format(message));
+ AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
- Assert.AreEqual("{ \"plainString\": \"plain\", \"o1String\": \"\", \"plainInt32\": 10, \"o2Int32\": 0 }", formatter.Format(message));
+ AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
+ }
+
+ /// <summary>
+ /// Checks that the actual JSON is the same as the expected JSON - but after replacing
+ /// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
+ /// to read.
+ /// </summary>
+ private static void AssertJson(string expectedJsonWithApostrophes, string actualJson)
+ {
+ var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
+ Assert.AreEqual(expectedJson, actualJson);
}
}
}