From dd43dcca8c3a0af761ae981edcadd7e78e875fe8 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 20 Jan 2016 18:43:00 +0000 Subject: Ensure that FieldMask, Timestamp and Duration ToString() calls don't throw The usage of ICustomDiagnosticMessage here is non-essential - ToDiagnosticString doesn't actually get called by ToString() in this case, due to JsonFormatter code. It was intended to make it clearer that it *did* have a custom format... but then arguably I should do the same for Value, Struct, Any etc. Moving some of the code out of JsonFormatter and into Duration/Timestamp/FieldMask likewise feels somewhat nice, somewhat nasty... basically there are JSON-specific bits of formatting, but also domain-specific bits of computation. Thoughts welcome. --- csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs') diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs index 84717d66..9ecd24c6 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs @@ -103,5 +103,13 @@ namespace Google.Protobuf.WellKnownTypes Assert.AreEqual(t1, t2 + difference); Assert.AreEqual(t2, t1 - difference); } + + [Test] + public void ToString_NonNormalized() + { + // Just a single example should be sufficient... + var duration = new Timestamp { Seconds = 1, Nanos = -1 }; + Assert.AreEqual("{ \"@warning\": \"Invalid Timestamp\", \"seconds\": \"1\", \"nanos\": -1 }", duration.ToString()); + } } } -- cgit v1.2.3