diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2016-01-26 08:43:45 -0800 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2016-01-26 08:43:45 -0800 |
commit | b13874d59e976371a1a87e6dc2bf347ed0a0ce5d (patch) | |
tree | d42e74c8539d2f52afdc92bc25145c50f67937ff /csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs | |
parent | 60f7fc51fb01ac98fd64d9372e7547f5ab267ce5 (diff) | |
parent | dd43dcca8c3a0af761ae981edcadd7e78e875fe8 (diff) | |
download | protobuf-b13874d59e976371a1a87e6dc2bf347ed0a0ce5d.tar.gz protobuf-b13874d59e976371a1a87e6dc2bf347ed0a0ce5d.tar.bz2 protobuf-b13874d59e976371a1a87e6dc2bf347ed0a0ce5d.zip |
Merge pull request #1158 from jskeet/nonnormalized-tostring
Ensure that FieldMask, Timestamp and Duration ToString() calls don't throw
Diffstat (limited to 'csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs index 9e994a6a..42455043 100644 --- a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs @@ -346,6 +346,17 @@ namespace Google.Protobuf } [Test] + [TestCase(-1, -1)] // Would be valid as duration + [TestCase(1, Timestamp.MaxNanos + 1)] + [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)] + [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, 0)] + public void TimestampStandalone_NonNormalized(long seconds, int nanoseconds) + { + var timestamp = new Timestamp { Seconds = seconds, Nanos = nanoseconds }; + Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(timestamp)); + } + + [Test] public void TimestampField() { var message = new TestWellKnownTypes { TimestampField = new Timestamp() }; @@ -378,7 +389,8 @@ namespace Google.Protobuf [TestCase(-1, -100000000, "-1.100s")] public void DurationStandalone(long seconds, int nanoseconds, string expected) { - Assert.AreEqual(WrapInQuotes(expected), new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString()); + var json = JsonFormatter.Default.Format(new Duration { Seconds = seconds, Nanos = nanoseconds }); + Assert.AreEqual(WrapInQuotes(expected), json); } [Test] @@ -386,7 +398,8 @@ namespace Google.Protobuf [TestCase(1, -100000000)] public void DurationStandalone_NonNormalized(long seconds, int nanoseconds) { - Assert.Throws<InvalidOperationException>(() => new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString()); + var duration = new Duration { Seconds = seconds, Nanos = nanoseconds }; + Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(duration)); } [Test] |