aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf.Test/JsonParserTest.cs
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2016-01-06 12:05:31 +0000
committerGravatar Jon Skeet <jonskeet@google.com>2016-01-06 12:05:31 +0000
commitb4a58173f24a6689d96ad4a294713f3f6b91ee17 (patch)
tree31e0fda0ea7a38d5b953eaa4ae4294b1068ee46b /csharp/src/Google.Protobuf.Test/JsonParserTest.cs
parentd19c26f2c878883f103d9dd4d6c53bc20482756f (diff)
Ensure all formatted well-known-type values are valid JSON
This involves quoting timestamp/duration/field-mask values, even when they're not in fields. It's better for consistency. Fixes issue #1097.
Diffstat (limited to 'csharp/src/Google.Protobuf.Test/JsonParserTest.cs')
-rw-r--r--csharp/src/Google.Protobuf.Test/JsonParserTest.cs25
1 files changed, 17 insertions, 8 deletions
diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs
index 874489e4..3babb391 100644
--- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs
+++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs
@@ -149,7 +149,7 @@ namespace Google.Protobuf
{
ByteString data = ByteString.CopyFrom(1, 2, 3);
// Can't do this with attributes...
- var parsed = JsonParser.Default.Parse<BytesValue>("\"" + data.ToBase64() + "\"");
+ var parsed = JsonParser.Default.Parse<BytesValue>(WrapInQuotes(data.ToBase64()));
var expected = new BytesValue { Value = data };
Assert.AreEqual(expected, parsed);
}
@@ -565,9 +565,9 @@ namespace Google.Protobuf
public void Timestamp_Valid(string jsonValue, string expectedFormatted)
{
expectedFormatted = expectedFormatted ?? jsonValue;
- string json = "\"" + jsonValue + "\"";
+ string json = WrapInQuotes(jsonValue);
var parsed = Timestamp.Parser.ParseJson(json);
- Assert.AreEqual(expectedFormatted, parsed.ToString());
+ Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString());
}
[Test]
@@ -592,7 +592,7 @@ namespace Google.Protobuf
[TestCase("2100-02-29T14:46:23.123456789Z", Description = "Feb 29th on a non-leap-year")]
public void Timestamp_Invalid(string jsonValue)
{
- string json = "\"" + jsonValue + "\"";
+ string json = WrapInQuotes(jsonValue);
Assert.Throws<InvalidProtocolBufferException>(() => Timestamp.Parser.ParseJson(json));
}
@@ -666,9 +666,9 @@ namespace Google.Protobuf
public void Duration_Valid(string jsonValue, string expectedFormatted)
{
expectedFormatted = expectedFormatted ?? jsonValue;
- string json = "\"" + jsonValue + "\"";
+ string json = WrapInQuotes(jsonValue);
var parsed = Duration.Parser.ParseJson(json);
- Assert.AreEqual(expectedFormatted, parsed.ToString());
+ Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString());
}
// The simplest way of testing that the value has parsed correctly is to reformat it,
@@ -697,7 +697,7 @@ namespace Google.Protobuf
[TestCase("-3155760000000s", Description = "Integer part too long (negative)")]
public void Duration_Invalid(string jsonValue)
{
- string json = "\"" + jsonValue + "\"";
+ string json = WrapInQuotes(jsonValue);
Assert.Throws<InvalidProtocolBufferException>(() => Duration.Parser.ParseJson(json));
}
@@ -713,7 +713,7 @@ namespace Google.Protobuf
[TestCase("fooBar.bazQux", "foo_bar.baz_qux")]
public void FieldMask_Valid(string jsonValue, params string[] expectedPaths)
{
- string json = "\"" + jsonValue + "\"";
+ string json = WrapInQuotes(jsonValue);
var parsed = FieldMask.Parser.ParseJson(json);
CollectionAssert.AreEqual(expectedPaths, parsed.Paths);
}
@@ -789,7 +789,16 @@ namespace Google.Protobuf
var parser63 = new JsonParser(new JsonParser.Settings(63));
Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<TestRecursiveMessage>(data64));
+ }
+ /// <summary>
+ /// Various tests use strings which have quotes round them for parsing or as the result
+ /// of formatting, but without those quotes being specified in the tests (for the sake of readability).
+ /// This method simply returns the input, wrapped in double quotes.
+ /// </summary>
+ internal static string WrapInQuotes(string text)
+ {
+ return '"' + text + '"';
}
}
}