blob: 08cedad8feb2d380d76d0c329f33774a4e220a65 [file] [log] [blame]
Jon Skeetf8c151f2015-07-03 11:56:29 +01001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#endregion
32
33using System;
Jon Skeetf8c151f2015-07-03 11:56:29 +010034using Google.Protobuf.TestProtos;
35using NUnit.Framework;
Jon Skeet4fed0b52015-07-31 10:33:31 +010036using UnitTest.Issues.TestProtos;
Jon Skeet16e272e2015-07-31 13:22:15 +010037using Google.Protobuf.WellKnownTypes;
Jon Skeetf8c151f2015-07-03 11:56:29 +010038
39namespace Google.Protobuf
40{
Jon Skeet6cf5f662015-07-31 10:44:59 +010041 /// <summary>
42 /// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
43 /// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
44 /// </summary>
Jon Skeetf8c151f2015-07-03 11:56:29 +010045 public class JsonFormatterTest
46 {
47 [Test]
48 public void DefaultValues_WhenOmitted()
49 {
50 var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));
51
Jon Skeet6cf5f662015-07-31 10:44:59 +010052 AssertJson("{ }", formatter.Format(new ForeignMessage()));
53 AssertJson("{ }", formatter.Format(new TestAllTypes()));
54 AssertJson("{ }", formatter.Format(new TestMap()));
Jon Skeetf8c151f2015-07-03 11:56:29 +010055 }
56
57 [Test]
58 public void DefaultValues_WhenIncluded()
59 {
60 var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
Jon Skeet6cf5f662015-07-31 10:44:59 +010061 AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
Jon Skeetf8c151f2015-07-03 11:56:29 +010062 }
63
64 [Test]
65 public void AllSingleFields()
66 {
67 var message = new TestAllTypes
68 {
69 SingleBool = true,
70 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
71 SingleDouble = 23.5,
72 SingleFixed32 = 23,
73 SingleFixed64 = 1234567890123,
74 SingleFloat = 12.25f,
75 SingleForeignEnum = ForeignEnum.FOREIGN_BAR,
76 SingleForeignMessage = new ForeignMessage { C = 10 },
77 SingleImportEnum = ImportEnum.IMPORT_BAZ,
78 SingleImportMessage = new ImportMessage { D = 20 },
79 SingleInt32 = 100,
80 SingleInt64 = 3210987654321,
81 SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO,
82 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
83 SinglePublicImportMessage = new PublicImportMessage { E = 54 },
84 SingleSfixed32 = -123,
85 SingleSfixed64 = -12345678901234,
86 SingleSint32 = -456,
87 SingleSint64 = -12345678901235,
88 SingleString = "test\twith\ttabs",
89 SingleUint32 = uint.MaxValue,
90 SingleUint64 = ulong.MaxValue,
91 };
92 var actualText = JsonFormatter.Default.Format(message);
93
Jon Skeet6cf5f662015-07-31 10:44:59 +010094 // Fields in numeric order
Jon Skeetf8c151f2015-07-03 11:56:29 +010095 var expectedText = "{ " +
Jon Skeet6cf5f662015-07-31 10:44:59 +010096 "'singleInt32': 100, " +
97 "'singleInt64': '3210987654321', " +
98 "'singleUint32': 4294967295, " +
99 "'singleUint64': '18446744073709551615', " +
100 "'singleSint32': -456, " +
101 "'singleSint64': '-12345678901235', " +
102 "'singleFixed32': 23, " +
103 "'singleFixed64': '1234567890123', " +
104 "'singleSfixed32': -123, " +
105 "'singleSfixed64': '-12345678901234', " +
106 "'singleFloat': 12.25, " +
107 "'singleDouble': 23.5, " +
108 "'singleBool': true, " +
109 "'singleString': 'test\\twith\\ttabs', " +
110 "'singleBytes': 'AQIDBA==', " +
111 "'singleNestedMessage': { 'bb': 35 }, " +
112 "'singleForeignMessage': { 'c': 10 }, " +
113 "'singleImportMessage': { 'd': 20 }, " +
114 "'singleNestedEnum': 'FOO', " +
115 "'singleForeignEnum': 'FOREIGN_BAR', " +
116 "'singleImportEnum': 'IMPORT_BAZ', " +
117 "'singlePublicImportMessage': { 'e': 54 }" +
Jon Skeetf8c151f2015-07-03 11:56:29 +0100118 " }";
Jon Skeet6cf5f662015-07-31 10:44:59 +0100119 AssertJson(expectedText, actualText);
Jon Skeetf8c151f2015-07-03 11:56:29 +0100120 }
121
122 [Test]
123 public void RepeatedField()
124 {
Jon Skeet6cf5f662015-07-31 10:44:59 +0100125 AssertJson("{ 'repeatedInt32': [ 1, 2, 3, 4, 5 ] }",
Jon Skeetf8c151f2015-07-03 11:56:29 +0100126 JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
127 }
128
129 [Test]
130 public void MapField_StringString()
131 {
Jon Skeet6cf5f662015-07-31 10:44:59 +0100132 AssertJson("{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b' } }",
Jon Skeetf8c151f2015-07-03 11:56:29 +0100133 JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
134 }
135
136 [Test]
137 public void MapField_Int32Int32()
138 {
139 // The keys are quoted, but the values aren't.
Jon Skeet6cf5f662015-07-31 10:44:59 +0100140 AssertJson("{ 'mapInt32Int32': { '0': 1, '2': 3 } }",
Jon Skeetf8c151f2015-07-03 11:56:29 +0100141 JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
142 }
143
144 [Test]
145 public void MapField_BoolBool()
146 {
147 // The keys are quoted, but the values aren't.
Jon Skeet6cf5f662015-07-31 10:44:59 +0100148 AssertJson("{ 'mapBoolBool': { 'false': true, 'true': false } }",
Jon Skeetf8c151f2015-07-03 11:56:29 +0100149 JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
150 }
151
152 [TestCase(1.0, "1")]
Jon Skeet6cf5f662015-07-31 10:44:59 +0100153 [TestCase(double.NaN, "'NaN'")]
154 [TestCase(double.PositiveInfinity, "'Infinity'")]
155 [TestCase(double.NegativeInfinity, "'-Infinity'")]
Jon Skeetf8c151f2015-07-03 11:56:29 +0100156 public void DoubleRepresentations(double value, string expectedValueText)
157 {
158 var message = new TestAllTypes { SingleDouble = value };
159 string actualText = JsonFormatter.Default.Format(message);
Jon Skeet6cf5f662015-07-31 10:44:59 +0100160 string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
161 AssertJson(expectedText, actualText);
Jon Skeetf8c151f2015-07-03 11:56:29 +0100162 }
163
164 [Test]
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100165 public void UnknownEnumValueOmitted_SingleField()
Jon Skeetf8c151f2015-07-03 11:56:29 +0100166 {
167 var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100168 AssertJson("{ }", JsonFormatter.Default.Format(message));
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100169 }
170
171 [Test]
172 public void UnknownEnumValueOmitted_RepeatedField()
173 {
174 var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100175 AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100176 }
177
178 [Test]
179 public void UnknownEnumValueOmitted_MapField()
180 {
181 // This matches the C++ behaviour.
182 var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100183 AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100184 }
185
186 [Test]
187 public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown()
188 {
189 // *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
190 // but this is what happens at the moment, and it doesn't seem too awful.
191 var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100192 AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.Format(message));
Jon Skeetf8c151f2015-07-03 11:56:29 +0100193 }
194
195 [Test]
196 public void NullValueForMessage()
197 {
198 var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100199 AssertJson("{ 'mapInt32ForeignMessage': { '10': null } }", JsonFormatter.Default.Format(message));
Jon Skeetf8c151f2015-07-03 11:56:29 +0100200 }
201
202 [Test]
203 [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
204 [TestCase("a\u0601b", "a\\u0601b")] // Ranged
205 [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...)
206 public void SimpleNonAscii(string text, string encoded)
207 {
208 var message = new TestAllTypes { SingleString = text };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100209 AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
Jon Skeetf8c151f2015-07-03 11:56:29 +0100210 }
211
212 [Test]
213 public void SurrogatePairEscaping()
214 {
215 var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100216 AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message));
Jon Skeetf8c151f2015-07-03 11:56:29 +0100217 }
218
219 [Test]
220 public void InvalidSurrogatePairsFail()
221 {
222 // Note: don't use TestCase for these, as the strings can't be reliably represented
223 // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/
224
225 // Lone low surrogate
226 var message = new TestAllTypes { SingleString = "a\uDC01b" };
227 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
228
229 // Lone high surrogate
230 message = new TestAllTypes { SingleString = "a\uD801b" };
231 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
232 }
233
234 [Test]
235 [TestCase("foo_bar", "fooBar")]
236 [TestCase("bananaBanana", "bananaBanana")]
237 [TestCase("BANANABanana", "bananaBanana")]
238 public void ToCamelCase(string original, string expected)
239 {
240 Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original));
241 }
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100242
243 [Test]
244 [TestCase(null, "{ }")]
Jon Skeet6cf5f662015-07-31 10:44:59 +0100245 [TestCase("x", "{ 'fooString': 'x' }")]
246 [TestCase("", "{ 'fooString': '' }")]
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100247 public void Oneof(string fooStringValue, string expectedJson)
248 {
249 var message = new TestOneof();
250 if (fooStringValue != null)
251 {
252 message.FooString = fooStringValue;
253 }
254
255 // We should get the same result both with and without "format default values".
256 var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
Jon Skeet6cf5f662015-07-31 10:44:59 +0100257 AssertJson(expectedJson, formatter.Format(message));
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100258 formatter = new JsonFormatter(new JsonFormatter.Settings(true));
Jon Skeet6cf5f662015-07-31 10:44:59 +0100259 AssertJson(expectedJson, formatter.Format(message));
Jon Skeet6ea9bc72015-07-10 14:05:52 +0100260 }
Jon Skeetc9fd53a2015-07-20 11:48:24 +0100261
262 [Test]
263 public void WrapperFormatting_Single()
264 {
265 // Just a few examples, handling both classes and value types, and
266 // default vs non-default values
267 var message = new TestWellKnownTypes
268 {
269 Int64Field = 10,
270 Int32Field = 0,
271 BytesField = ByteString.FromBase64("ABCD"),
272 StringField = ""
273 };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100274 var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
275 AssertJson(expectedJson, JsonFormatter.Default.Format(message));
Jon Skeetc9fd53a2015-07-20 11:48:24 +0100276 }
277
278 [Test]
Jon Skeetfb248822015-09-04 12:41:14 +0100279 public void WrapperFormatting_Message()
280 {
281 Assert.AreEqual("\"\"", JsonFormatter.Default.Format(new StringValue()));
282 Assert.AreEqual("0", JsonFormatter.Default.Format(new Int32Value()));
283 }
284
285 [Test]
Jon Skeetc9fd53a2015-07-20 11:48:24 +0100286 public void WrapperFormatting_IncludeNull()
287 {
288 // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
289 var message = new TestWellKnownTypes { Int32Field = 10 };
290 var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
291 var actualJson = formatter.Format(message);
292 Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
293 Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
294 }
Jon Skeet4fed0b52015-07-31 10:33:31 +0100295
296 [Test]
297 public void OutputIsInNumericFieldOrder_NoDefaults()
298 {
299 var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
300 var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100301 AssertJson("{ 'plainString': 'p1', 'plainInt32': 2 }", formatter.Format(message));
Jon Skeet4fed0b52015-07-31 10:33:31 +0100302 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100303 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
Jon Skeet4fed0b52015-07-31 10:33:31 +0100304 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100305 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
Jon Skeet4fed0b52015-07-31 10:33:31 +0100306 }
307
308 [Test]
309 public void OutputIsInNumericFieldOrder_WithDefaults()
310 {
311 var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
312 var message = new TestJsonFieldOrdering();
Jon Skeet6cf5f662015-07-31 10:44:59 +0100313 AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
Jon Skeet4fed0b52015-07-31 10:33:31 +0100314 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100315 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
Jon Skeet4fed0b52015-07-31 10:33:31 +0100316 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
Jon Skeet6cf5f662015-07-31 10:44:59 +0100317 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
318 }
319
Jon Skeet16e272e2015-07-31 13:22:15 +0100320 [Test]
321 public void TimestampStandalone()
322 {
323 Assert.AreEqual("1970-01-01T00:00:00Z", new Timestamp().ToString());
324 Assert.AreEqual("1970-01-01T00:00:00.100Z", new Timestamp { Nanos = 100000000 }.ToString());
325 Assert.AreEqual("1970-01-01T00:00:00.120Z", new Timestamp { Nanos = 120000000 }.ToString());
326 Assert.AreEqual("1970-01-01T00:00:00.123Z", new Timestamp { Nanos = 123000000 }.ToString());
327 Assert.AreEqual("1970-01-01T00:00:00.123400Z", new Timestamp { Nanos = 123400000 }.ToString());
328 Assert.AreEqual("1970-01-01T00:00:00.123450Z", new Timestamp { Nanos = 123450000 }.ToString());
329 Assert.AreEqual("1970-01-01T00:00:00.123456Z", new Timestamp { Nanos = 123456000 }.ToString());
330 Assert.AreEqual("1970-01-01T00:00:00.123456700Z", new Timestamp { Nanos = 123456700 }.ToString());
331 Assert.AreEqual("1970-01-01T00:00:00.123456780Z", new Timestamp { Nanos = 123456780 }.ToString());
332 Assert.AreEqual("1970-01-01T00:00:00.123456789Z", new Timestamp { Nanos = 123456789 }.ToString());
333
334 // One before and one after the Unix epoch
335 Assert.AreEqual("1673-06-19T12:34:56Z",
336 new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp().ToString());
337 Assert.AreEqual("2015-07-31T10:29:34Z",
338 new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimestamp().ToString());
339 }
340
341 [Test]
342 public void TimestampField()
343 {
344 var message = new TestWellKnownTypes { TimestampField = new Timestamp() };
345 AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
346 }
347
348 [Test]
349 [TestCase(0, 0, "0s")]
350 [TestCase(1, 0, "1s")]
351 [TestCase(-1, 0, "-1s")]
352 [TestCase(0, 100000000, "0.100s")]
353 [TestCase(0, 120000000, "0.120s")]
354 [TestCase(0, 123000000, "0.123s")]
355 [TestCase(0, 123400000, "0.123400s")]
356 [TestCase(0, 123450000, "0.123450s")]
357 [TestCase(0, 123456000, "0.123456s")]
358 [TestCase(0, 123456700, "0.123456700s")]
359 [TestCase(0, 123456780, "0.123456780s")]
360 [TestCase(0, 123456789, "0.123456789s")]
361 [TestCase(0, -100000000, "-0.100s")]
362 [TestCase(1, 100000000, "1.100s")]
363 [TestCase(-1, -100000000, "-1.100s")]
364 // Non-normalized examples
365 [TestCase(1, 2123456789, "3.123456789s")]
366 [TestCase(1, -100000000, "0.900s")]
367 public void DurationStandalone(long seconds, int nanoseconds, string expected)
368 {
369 Assert.AreEqual(expected, new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString());
370 }
371
372 [Test]
373 public void DurationField()
374 {
375 var message = new TestWellKnownTypes { DurationField = new Duration() };
376 AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
Jon Skeete7caf152015-07-31 15:07:50 +0100377 }
Jon Skeet16e272e2015-07-31 13:22:15 +0100378
Jon Skeete7caf152015-07-31 15:07:50 +0100379 [Test]
380 public void StructSample()
381 {
382 var message = new Struct
383 {
384 Fields =
385 {
Jon Skeetfb248822015-09-04 12:41:14 +0100386 { "a", Value.ForNull() },
387 { "b", Value.ForBool(false) },
388 { "c", Value.ForNumber(10.5) },
389 { "d", Value.ForString("text") },
390 { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber(5)) },
391 { "f", Value.ForStruct(new Struct { Fields = { { "nested", Value.ForString("value") } } }) }
Jon Skeete7caf152015-07-31 15:07:50 +0100392 }
393 };
394 AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
Jon Skeet16e272e2015-07-31 13:22:15 +0100395 }
396
Jon Skeet0e30de32015-08-03 11:43:07 +0100397 [Test]
398 public void FieldMaskStandalone()
399 {
400 var fieldMask = new FieldMask { Paths = { "", "single", "with_underscore", "nested.field.name", "nested..double_dot" } };
401 Assert.AreEqual(",single,withUnderscore,nested.field.name,nested..doubleDot", fieldMask.ToString());
402
403 // Invalid, but we shouldn't create broken JSON...
404 fieldMask = new FieldMask { Paths = { "x\\y" } };
405 Assert.AreEqual(@"x\\y", fieldMask.ToString());
406 }
407
408 [Test]
409 public void FieldMaskField()
410 {
411 var message = new TestWellKnownTypes { FieldMaskField = new FieldMask { Paths = { "user.display_name", "photo" } } };
412 AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
413 }
414
Jon Skeetfb248822015-09-04 12:41:14 +0100415 // SourceContext is an example of a well-known type with no special JSON handling
416 [Test]
417 public void SourceContextStandalone()
418 {
419 var message = new SourceContext { FileName = "foo.proto" };
420 AssertJson("{ 'fileName': 'foo.proto' }", JsonFormatter.Default.Format(message));
421 }
422
Jon Skeet6cf5f662015-07-31 10:44:59 +0100423 /// <summary>
424 /// Checks that the actual JSON is the same as the expected JSON - but after replacing
425 /// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
426 /// to read.
427 /// </summary>
428 private static void AssertJson(string expectedJsonWithApostrophes, string actualJson)
429 {
430 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
431 Assert.AreEqual(expectedJson, actualJson);
Jon Skeet4fed0b52015-07-31 10:33:31 +0100432 }
Jon Skeetf8c151f2015-07-03 11:56:29 +0100433 }
434}