Commit 80f89b4e authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #669 from jskeet/simpify-json-tests

Simpify json tests
parents 82aa6bbd 6cf5f660
...@@ -31,16 +31,16 @@ ...@@ -31,16 +31,16 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf.TestProtos; using Google.Protobuf.TestProtos;
using NUnit.Framework; using NUnit.Framework;
using UnitTest.Issues.TestProtos; using UnitTest.Issues.TestProtos;
namespace Google.Protobuf 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 public class JsonFormatterTest
{ {
[Test] [Test]
...@@ -48,16 +48,16 @@ namespace Google.Protobuf ...@@ -48,16 +48,16 @@ namespace Google.Protobuf
{ {
var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false)); var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));
Assert.AreEqual("{ }", formatter.Format(new ForeignMessage())); AssertJson("{ }", formatter.Format(new ForeignMessage()));
Assert.AreEqual("{ }", formatter.Format(new TestAllTypes())); AssertJson("{ }", formatter.Format(new TestAllTypes()));
Assert.AreEqual("{ }", formatter.Format(new TestMap())); AssertJson("{ }", formatter.Format(new TestMap()));
} }
[Test] [Test]
public void DefaultValues_WhenIncluded() public void DefaultValues_WhenIncluded()
{ {
var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true)); 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] [Test]
...@@ -90,45 +90,45 @@ namespace Google.Protobuf ...@@ -90,45 +90,45 @@ namespace Google.Protobuf
}; };
var actualText = JsonFormatter.Default.Format(message); var actualText = JsonFormatter.Default.Format(message);
// Fields in declaration order, which matches numeric order. // Fields in numeric order
var expectedText = "{ " + var expectedText = "{ " +
"\"singleInt32\": 100, " + "'singleInt32': 100, " +
"\"singleInt64\": \"3210987654321\", " + "'singleInt64': '3210987654321', " +
"\"singleUint32\": 4294967295, " + "'singleUint32': 4294967295, " +
"\"singleUint64\": \"18446744073709551615\", " + "'singleUint64': '18446744073709551615', " +
"\"singleSint32\": -456, " + "'singleSint32': -456, " +
"\"singleSint64\": \"-12345678901235\", " + "'singleSint64': '-12345678901235', " +
"\"singleFixed32\": 23, " + "'singleFixed32': 23, " +
"\"singleFixed64\": \"1234567890123\", " + "'singleFixed64': '1234567890123', " +
"\"singleSfixed32\": -123, " + "'singleSfixed32': -123, " +
"\"singleSfixed64\": \"-12345678901234\", " + "'singleSfixed64': '-12345678901234', " +
"\"singleFloat\": 12.25, " + "'singleFloat': 12.25, " +
"\"singleDouble\": 23.5, " + "'singleDouble': 23.5, " +
"\"singleBool\": true, " + "'singleBool': true, " +
"\"singleString\": \"test\\twith\\ttabs\", " + "'singleString': 'test\\twith\\ttabs', " +
"\"singleBytes\": \"AQIDBA==\", " + "'singleBytes': 'AQIDBA==', " +
"\"singleNestedMessage\": { \"bb\": 35 }, " + "'singleNestedMessage': { 'bb': 35 }, " +
"\"singleForeignMessage\": { \"c\": 10 }, " + "'singleForeignMessage': { 'c': 10 }, " +
"\"singleImportMessage\": { \"d\": 20 }, " + "'singleImportMessage': { 'd': 20 }, " +
"\"singleNestedEnum\": \"FOO\", " + "'singleNestedEnum': 'FOO', " +
"\"singleForeignEnum\": \"FOREIGN_BAR\", " + "'singleForeignEnum': 'FOREIGN_BAR', " +
"\"singleImportEnum\": \"IMPORT_BAZ\", " + "'singleImportEnum': 'IMPORT_BAZ', " +
"\"singlePublicImportMessage\": { \"e\": 54 }" + "'singlePublicImportMessage': { 'e': 54 }" +
" }"; " }";
Assert.AreEqual(expectedText, actualText); AssertJson(expectedText, actualText);
} }
[Test] [Test]
public void RepeatedField() 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 } })); JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
} }
[Test] [Test]
public void MapField_StringString() 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" } } })); JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
} }
...@@ -136,7 +136,7 @@ namespace Google.Protobuf ...@@ -136,7 +136,7 @@ namespace Google.Protobuf
public void MapField_Int32Int32() public void MapField_Int32Int32()
{ {
// The keys are quoted, but the values aren't. // 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 } } })); JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
} }
...@@ -144,34 +144,34 @@ namespace Google.Protobuf ...@@ -144,34 +144,34 @@ namespace Google.Protobuf
public void MapField_BoolBool() public void MapField_BoolBool()
{ {
// The keys are quoted, but the values aren't. // 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 } } })); JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
} }
[TestCase(1.0, "1")] [TestCase(1.0, "1")]
[TestCase(double.NaN, "\"NaN\"")] [TestCase(double.NaN, "'NaN'")]
[TestCase(double.PositiveInfinity, "\"Infinity\"")] [TestCase(double.PositiveInfinity, "'Infinity'")]
[TestCase(double.NegativeInfinity, "\"-Infinity\"")] [TestCase(double.NegativeInfinity, "'-Infinity'")]
public void DoubleRepresentations(double value, string expectedValueText) public void DoubleRepresentations(double value, string expectedValueText)
{ {
var message = new TestAllTypes { SingleDouble = value }; var message = new TestAllTypes { SingleDouble = value };
string actualText = JsonFormatter.Default.Format(message); string actualText = JsonFormatter.Default.Format(message);
string expectedText = "{ \"singleDouble\": " + expectedValueText + " }"; string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
Assert.AreEqual(expectedText, actualText); AssertJson(expectedText, actualText);
} }
[Test] [Test]
public void UnknownEnumValueOmitted_SingleField() public void UnknownEnumValueOmitted_SingleField()
{ {
var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 }; var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
Assert.AreEqual("{ }", JsonFormatter.Default.Format(message)); AssertJson("{ }", JsonFormatter.Default.Format(message));
} }
[Test] [Test]
public void UnknownEnumValueOmitted_RepeatedField() public void UnknownEnumValueOmitted_RepeatedField()
{ {
var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } }; 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] [Test]
...@@ -179,7 +179,7 @@ namespace Google.Protobuf ...@@ -179,7 +179,7 @@ namespace Google.Protobuf
{ {
// This matches the C++ behaviour. // This matches the C++ behaviour.
var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } }; 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] [Test]
...@@ -188,14 +188,14 @@ namespace Google.Protobuf ...@@ -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... // *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. // but this is what happens at the moment, and it doesn't seem too awful.
var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } }; var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
Assert.AreEqual("{ \"repeatedForeignEnum\": [ ] }", JsonFormatter.Default.Format(message)); AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.Format(message));
} }
[Test] [Test]
public void NullValueForMessage() public void NullValueForMessage()
{ {
var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } }; 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] [Test]
...@@ -205,14 +205,14 @@ namespace Google.Protobuf ...@@ -205,14 +205,14 @@ namespace Google.Protobuf
public void SimpleNonAscii(string text, string encoded) public void SimpleNonAscii(string text, string encoded)
{ {
var message = new TestAllTypes { SingleString = text }; var message = new TestAllTypes { SingleString = text };
Assert.AreEqual("{ \"singleString\": \"" + encoded + "\" }", JsonFormatter.Default.Format(message)); AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
} }
[Test] [Test]
public void SurrogatePairEscaping() public void SurrogatePairEscaping()
{ {
var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" }; 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] [Test]
...@@ -241,8 +241,8 @@ namespace Google.Protobuf ...@@ -241,8 +241,8 @@ namespace Google.Protobuf
[Test] [Test]
[TestCase(null, "{ }")] [TestCase(null, "{ }")]
[TestCase("x", "{ \"fooString\": \"x\" }")] [TestCase("x", "{ 'fooString': 'x' }")]
[TestCase("", "{ \"fooString\": \"\" }")] [TestCase("", "{ 'fooString': '' }")]
[TestCase(null, "{ }")] [TestCase(null, "{ }")]
public void Oneof(string fooStringValue, string expectedJson) public void Oneof(string fooStringValue, string expectedJson)
{ {
...@@ -254,9 +254,9 @@ namespace Google.Protobuf ...@@ -254,9 +254,9 @@ namespace Google.Protobuf
// We should get the same result both with and without "format default values". // We should get the same result both with and without "format default values".
var formatter = new JsonFormatter(new JsonFormatter.Settings(false)); 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)); formatter = new JsonFormatter(new JsonFormatter.Settings(true));
Assert.AreEqual(expectedJson, formatter.Format(message)); AssertJson(expectedJson, formatter.Format(message));
} }
[Test] [Test]
...@@ -271,8 +271,8 @@ namespace Google.Protobuf ...@@ -271,8 +271,8 @@ namespace Google.Protobuf
BytesField = ByteString.FromBase64("ABCD"), BytesField = ByteString.FromBase64("ABCD"),
StringField = "" StringField = ""
}; };
var expectedJson = "{ \"int64Field\": \"10\", \"int32Field\": 0, \"stringField\": \"\", \"bytesField\": \"ABCD\" }"; var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(message)); AssertJson(expectedJson, JsonFormatter.Default.Format(message));
} }
[Test] [Test]
...@@ -291,11 +291,11 @@ namespace Google.Protobuf ...@@ -291,11 +291,11 @@ namespace Google.Protobuf
{ {
var formatter = new JsonFormatter(new JsonFormatter.Settings(false)); var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 }; 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" }; 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" }; 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] [Test]
...@@ -303,11 +303,22 @@ namespace Google.Protobuf ...@@ -303,11 +303,22 @@ namespace Google.Protobuf
{ {
var formatter = new JsonFormatter(new JsonFormatter.Settings(true)); var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
var message = new TestJsonFieldOrdering(); 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" }; 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" }; 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);
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment