Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: third_party/protobuf/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs

Issue 1983203003: Update third_party/protobuf to protobuf-v3.0.0-beta-3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: owners Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #region Copyright notice and license 1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format 2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc. All rights reserved. 3 // Copyright 2008 Google Inc. All rights reserved.
4 // https://developers.google.com/protocol-buffers/ 4 // https://developers.google.com/protocol-buffers/
5 // 5 //
6 // Redistribution and use in source and binary forms, with or without 6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are 7 // modification, are permitted provided that the following conditions are
8 // met: 8 // met:
9 // 9 //
10 // * Redistributions of source code must retain the above copyright 10 // * Redistributions of source code must retain the above copyright
(...skipping 19 matching lines...) Expand all
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion 31 #endregion
32 32
33 using System; 33 using System;
34 using Google.Protobuf.TestProtos; 34 using Google.Protobuf.TestProtos;
35 using NUnit.Framework; 35 using NUnit.Framework;
36 using UnitTest.Issues.TestProtos; 36 using UnitTest.Issues.TestProtos;
37 using Google.Protobuf.WellKnownTypes; 37 using Google.Protobuf.WellKnownTypes;
38 using Google.Protobuf.Reflection; 38 using Google.Protobuf.Reflection;
39 39
40 using static Google.Protobuf.JsonParserTest; // For WrapInQuotes
41
40 namespace Google.Protobuf 42 namespace Google.Protobuf
41 { 43 {
42 /// <summary> 44 /// <summary>
43 /// Tests for the JSON formatter. Note that in these tests, double quotes ar e replaced with apostrophes 45 /// Tests for the JSON formatter. Note that in these tests, double quotes ar e replaced with apostrophes
44 /// for the sake of readability (embedding \" everywhere is painful). See th e AssertJson method for details. 46 /// for the sake of readability (embedding \" everywhere is painful). See th e AssertJson method for details.
45 /// </summary> 47 /// </summary>
46 public class JsonFormatterTest 48 public class JsonFormatterTest
47 { 49 {
48 [Test] 50 [Test]
49 public void DefaultValues_WhenOmitted() 51 public void DefaultValues_WhenOmitted()
(...skipping 16 matching lines...) Expand all
66 public void AllSingleFields() 68 public void AllSingleFields()
67 { 69 {
68 var message = new TestAllTypes 70 var message = new TestAllTypes
69 { 71 {
70 SingleBool = true, 72 SingleBool = true,
71 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), 73 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
72 SingleDouble = 23.5, 74 SingleDouble = 23.5,
73 SingleFixed32 = 23, 75 SingleFixed32 = 23,
74 SingleFixed64 = 1234567890123, 76 SingleFixed64 = 1234567890123,
75 SingleFloat = 12.25f, 77 SingleFloat = 12.25f,
76 SingleForeignEnum = ForeignEnum.FOREIGN_BAR, 78 SingleForeignEnum = ForeignEnum.ForeignBar,
77 SingleForeignMessage = new ForeignMessage { C = 10 }, 79 SingleForeignMessage = new ForeignMessage { C = 10 },
78 SingleImportEnum = ImportEnum.IMPORT_BAZ, 80 SingleImportEnum = ImportEnum.ImportBaz,
79 SingleImportMessage = new ImportMessage { D = 20 }, 81 SingleImportMessage = new ImportMessage { D = 20 },
80 SingleInt32 = 100, 82 SingleInt32 = 100,
81 SingleInt64 = 3210987654321, 83 SingleInt64 = 3210987654321,
82 SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, 84 SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
83 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, 85 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
84 SinglePublicImportMessage = new PublicImportMessage { E = 54 }, 86 SinglePublicImportMessage = new PublicImportMessage { E = 54 },
85 SingleSfixed32 = -123, 87 SingleSfixed32 = -123,
86 SingleSfixed64 = -12345678901234, 88 SingleSfixed64 = -12345678901234,
87 SingleSint32 = -456, 89 SingleSint32 = -456,
88 SingleSint64 = -12345678901235, 90 SingleSint64 = -12345678901235,
89 SingleString = "test\twith\ttabs", 91 SingleString = "test\twith\ttabs",
90 SingleUint32 = uint.MaxValue, 92 SingleUint32 = uint.MaxValue,
91 SingleUint64 = ulong.MaxValue, 93 SingleUint64 = ulong.MaxValue,
92 }; 94 };
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 [TestCase(double.NegativeInfinity, "'-Infinity'")] 158 [TestCase(double.NegativeInfinity, "'-Infinity'")]
157 public void DoubleRepresentations(double value, string expectedValueText ) 159 public void DoubleRepresentations(double value, string expectedValueText )
158 { 160 {
159 var message = new TestAllTypes { SingleDouble = value }; 161 var message = new TestAllTypes { SingleDouble = value };
160 string actualText = JsonFormatter.Default.Format(message); 162 string actualText = JsonFormatter.Default.Format(message);
161 string expectedText = "{ 'singleDouble': " + expectedValueText + " } "; 163 string expectedText = "{ 'singleDouble': " + expectedValueText + " } ";
162 AssertJson(expectedText, actualText); 164 AssertJson(expectedText, actualText);
163 } 165 }
164 166
165 [Test] 167 [Test]
166 public void UnknownEnumValueOmitted_SingleField() 168 public void UnknownEnumValueNumeric_SingleField()
167 { 169 {
168 var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 1 00 }; 170 var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 1 00 };
169 AssertJson("{ }", JsonFormatter.Default.Format(message)); 171 AssertJson("{ 'singleForeignEnum': 100 }", JsonFormatter.Default.For mat(message));
170 } 172 }
171 173
172 [Test] 174 [Test]
173 public void UnknownEnumValueOmitted_RepeatedField() 175 public void UnknownEnumValueNumeric_RepeatedField()
174 { 176 {
175 var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum .FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } }; 177 var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum .ForeignBaz, (ForeignEnum) 100, ForeignEnum.ForeignFoo } };
176 AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message)); 178 AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 100, 'FOREIGN_ FOO' ] }", JsonFormatter.Default.Format(message));
177 } 179 }
178 180
179 [Test] 181 [Test]
180 public void UnknownEnumValueOmitted_MapField() 182 public void UnknownEnumValueNumeric_MapField()
181 { 183 {
182 // This matches the C++ behaviour. 184 var message = new TestMap { MapInt32Enum = { { 1, MapEnum.Foo }, { 2 , (MapEnum) 100 }, { 3, MapEnum.Bar } } };
183 var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_F OO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } }; 185 AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '2': 100, '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
184 AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_ BAR' } }", JsonFormatter.Default.Format(message));
185 } 186 }
186 187
187 [Test] 188 [Test]
188 public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown() 189 public void UnknownEnumValue_RepeatedField_AllEntriesUnknown()
189 { 190 {
190 // *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
191 // but this is what happens at the moment, and it doesn't seem too a wful.
192 var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnu m) 200, (ForeignEnum) 100 } }; 191 var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnu m) 200, (ForeignEnum) 100 } };
193 AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.F ormat(message)); 192 AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter. Default.Format(message));
194 } 193 }
195 194
196 [Test] 195 [Test]
197 public void NullValueForMessage()
198 {
199 var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } };
200 AssertJson("{ 'mapInt32ForeignMessage': { '10': null } }", JsonForma tter.Default.Format(message));
201 }
202
203 [Test]
204 [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit 196 [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
205 [TestCase("a\u0601b", "a\\u0601b")] // Ranged 197 [TestCase("a\u0601b", "a\\u0601b")] // Ranged
206 [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double b ackslash...) 198 [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double b ackslash...)
207 public void SimpleNonAscii(string text, string encoded) 199 public void SimpleNonAscii(string text, string encoded)
208 { 200 {
209 var message = new TestAllTypes { SingleString = text }; 201 var message = new TestAllTypes { SingleString = text };
210 AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.De fault.Format(message)); 202 AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.De fault.Format(message));
211 } 203 }
212 204
213 [Test] 205 [Test]
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 var formatter = new JsonFormatter(new JsonFormatter.Settings(true)); 304 var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
313 var message = new TestJsonFieldOrdering(); 305 var message = new TestJsonFieldOrdering();
314 AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Forma t(message)); 306 AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Forma t(message));
315 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" }; 307 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
316 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32' : 10, 'o1Int32': 5 }", formatter.Format(message)); 308 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32' : 10, 'o1Int32': 5 }", formatter.Format(message));
317 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, Pl ainInt32 = 10, PlainString = "plain" }; 309 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, Pl ainInt32 = 10, PlainString = "plain" };
318 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message)); 310 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
319 } 311 }
320 312
321 [Test] 313 [Test]
322 public void TimestampStandalone() 314 [TestCase("1970-01-01T00:00:00Z", 0)]
315 [TestCase("1970-01-01T00:00:00.000000001Z", 1)]
316 [TestCase("1970-01-01T00:00:00.000000010Z", 10)]
317 [TestCase("1970-01-01T00:00:00.000000100Z", 100)]
318 [TestCase("1970-01-01T00:00:00.000001Z", 1000)]
319 [TestCase("1970-01-01T00:00:00.000010Z", 10000)]
320 [TestCase("1970-01-01T00:00:00.000100Z", 100000)]
321 [TestCase("1970-01-01T00:00:00.001Z", 1000000)]
322 [TestCase("1970-01-01T00:00:00.010Z", 10000000)]
323 [TestCase("1970-01-01T00:00:00.100Z", 100000000)]
324 [TestCase("1970-01-01T00:00:00.120Z", 120000000)]
325 [TestCase("1970-01-01T00:00:00.123Z", 123000000)]
326 [TestCase("1970-01-01T00:00:00.123400Z", 123400000)]
327 [TestCase("1970-01-01T00:00:00.123450Z", 123450000)]
328 [TestCase("1970-01-01T00:00:00.123456Z", 123456000)]
329 [TestCase("1970-01-01T00:00:00.123456700Z", 123456700)]
330 [TestCase("1970-01-01T00:00:00.123456780Z", 123456780)]
331 [TestCase("1970-01-01T00:00:00.123456789Z", 123456789)]
332 public void TimestampStandalone(string expected, int nanos)
323 { 333 {
324 Assert.AreEqual("1970-01-01T00:00:00Z", new Timestamp().ToString()); 334 Assert.AreEqual(WrapInQuotes(expected), new Timestamp { Nanos = nano s }.ToString());
325 Assert.AreEqual("1970-01-01T00:00:00.100Z", new Timestamp { Nanos = 100000000 }.ToString()); 335 }
326 Assert.AreEqual("1970-01-01T00:00:00.120Z", new Timestamp { Nanos = 120000000 }.ToString());
327 Assert.AreEqual("1970-01-01T00:00:00.123Z", new Timestamp { Nanos = 123000000 }.ToString());
328 Assert.AreEqual("1970-01-01T00:00:00.123400Z", new Timestamp { Nanos = 123400000 }.ToString());
329 Assert.AreEqual("1970-01-01T00:00:00.123450Z", new Timestamp { Nanos = 123450000 }.ToString());
330 Assert.AreEqual("1970-01-01T00:00:00.123456Z", new Timestamp { Nanos = 123456000 }.ToString());
331 Assert.AreEqual("1970-01-01T00:00:00.123456700Z", new Timestamp { Na nos = 123456700 }.ToString());
332 Assert.AreEqual("1970-01-01T00:00:00.123456780Z", new Timestamp { Na nos = 123456780 }.ToString());
333 Assert.AreEqual("1970-01-01T00:00:00.123456789Z", new Timestamp { Na nos = 123456789 }.ToString());
334 336
335 // One before and one after the Unix epoch 337 [Test]
336 Assert.AreEqual("1673-06-19T12:34:56Z", 338 public void TimestampStandalone_FromDateTime()
339 {
340 // One before and one after the Unix epoch, more easily represented via DateTime.
341 Assert.AreEqual("\"1673-06-19T12:34:56Z\"",
337 new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimest amp().ToString()); 342 new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimest amp().ToString());
338 Assert.AreEqual("2015-07-31T10:29:34Z", 343 Assert.AreEqual("\"2015-07-31T10:29:34Z\"",
339 new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimest amp().ToString()); 344 new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimest amp().ToString());
340 } 345 }
341 346
342 [Test] 347 [Test]
348 [TestCase(-1, -1)] // Would be valid as duration
349 [TestCase(1, Timestamp.MaxNanos + 1)]
350 [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
351 [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, 0)]
352 public void TimestampStandalone_NonNormalized(long seconds, int nanoseco nds)
353 {
354 var timestamp = new Timestamp { Seconds = seconds, Nanos = nanosecon ds };
355 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default .Format(timestamp));
356 }
357
358 [Test]
343 public void TimestampField() 359 public void TimestampField()
344 { 360 {
345 var message = new TestWellKnownTypes { TimestampField = new Timestam p() }; 361 var message = new TestWellKnownTypes { TimestampField = new Timestam p() };
346 AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonForma tter.Default.Format(message)); 362 AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonForma tter.Default.Format(message));
347 } 363 }
348 364
349 [Test] 365 [Test]
350 [TestCase(0, 0, "0s")] 366 [TestCase(0, 0, "0s")]
351 [TestCase(1, 0, "1s")] 367 [TestCase(1, 0, "1s")]
352 [TestCase(-1, 0, "-1s")] 368 [TestCase(-1, 0, "-1s")]
369 [TestCase(0, 1, "0.000000001s")]
370 [TestCase(0, 10, "0.000000010s")]
371 [TestCase(0, 100, "0.000000100s")]
372 [TestCase(0, 1000, "0.000001s")]
373 [TestCase(0, 10000, "0.000010s")]
374 [TestCase(0, 100000, "0.000100s")]
375 [TestCase(0, 1000000, "0.001s")]
376 [TestCase(0, 10000000, "0.010s")]
353 [TestCase(0, 100000000, "0.100s")] 377 [TestCase(0, 100000000, "0.100s")]
354 [TestCase(0, 120000000, "0.120s")] 378 [TestCase(0, 120000000, "0.120s")]
355 [TestCase(0, 123000000, "0.123s")] 379 [TestCase(0, 123000000, "0.123s")]
356 [TestCase(0, 123400000, "0.123400s")] 380 [TestCase(0, 123400000, "0.123400s")]
357 [TestCase(0, 123450000, "0.123450s")] 381 [TestCase(0, 123450000, "0.123450s")]
358 [TestCase(0, 123456000, "0.123456s")] 382 [TestCase(0, 123456000, "0.123456s")]
359 [TestCase(0, 123456700, "0.123456700s")] 383 [TestCase(0, 123456700, "0.123456700s")]
360 [TestCase(0, 123456780, "0.123456780s")] 384 [TestCase(0, 123456780, "0.123456780s")]
361 [TestCase(0, 123456789, "0.123456789s")] 385 [TestCase(0, 123456789, "0.123456789s")]
362 [TestCase(0, -100000000, "-0.100s")] 386 [TestCase(0, -100000000, "-0.100s")]
363 [TestCase(1, 100000000, "1.100s")] 387 [TestCase(1, 100000000, "1.100s")]
364 [TestCase(-1, -100000000, "-1.100s")] 388 [TestCase(-1, -100000000, "-1.100s")]
365 // Non-normalized examples
366 [TestCase(1, 2123456789, "3.123456789s")]
367 [TestCase(1, -100000000, "0.900s")]
368 public void DurationStandalone(long seconds, int nanoseconds, string exp ected) 389 public void DurationStandalone(long seconds, int nanoseconds, string exp ected)
369 { 390 {
370 Assert.AreEqual(expected, new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString()); 391 var json = JsonFormatter.Default.Format(new Duration { Seconds = sec onds, Nanos = nanoseconds });
392 Assert.AreEqual(WrapInQuotes(expected), json);
371 } 393 }
372 394
373 [Test] 395 [Test]
396 [TestCase(1, 2123456789)]
397 [TestCase(1, -100000000)]
398 public void DurationStandalone_NonNormalized(long seconds, int nanosecon ds)
399 {
400 var duration = new Duration { Seconds = seconds, Nanos = nanoseconds };
401 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default .Format(duration));
402 }
403
404 [Test]
374 public void DurationField() 405 public void DurationField()
375 { 406 {
376 var message = new TestWellKnownTypes { DurationField = new Duration( ) }; 407 var message = new TestWellKnownTypes { DurationField = new Duration( ) };
377 AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format (message)); 408 AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format (message));
378 } 409 }
379 410
380 [Test] 411 [Test]
381 public void StructSample() 412 public void StructSample()
382 { 413 {
383 var message = new Struct 414 var message = new Struct
384 { 415 {
385 Fields = 416 Fields =
386 { 417 {
387 { "a", Value.ForNull() }, 418 { "a", Value.ForNull() },
388 { "b", Value.ForBool(false) }, 419 { "b", Value.ForBool(false) },
389 { "c", Value.ForNumber(10.5) }, 420 { "c", Value.ForNumber(10.5) },
390 { "d", Value.ForString("text") }, 421 { "d", Value.ForString("text") },
391 { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber( 5)) }, 422 { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber( 5)) },
392 { "f", Value.ForStruct(new Struct { Fields = { { "nested", V alue.ForString("value") } } }) } 423 { "f", Value.ForStruct(new Struct { Fields = { { "nested", V alue.ForString("value") } } }) }
393 } 424 }
394 }; 425 };
395 AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString()); 426 AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
396 } 427 }
397 428
398 [Test] 429 [Test]
430 [TestCase("foo__bar")]
431 [TestCase("foo_3_ar")]
432 [TestCase("fooBar")]
433 public void FieldMaskInvalid(string input)
434 {
435 var mask = new FieldMask { Paths = { input } };
436 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default .Format(mask));
437 }
438
439 [Test]
399 public void FieldMaskStandalone() 440 public void FieldMaskStandalone()
400 { 441 {
401 var fieldMask = new FieldMask { Paths = { "", "single", "with_unders core", "nested.field.name", "nested..double_dot" } }; 442 var fieldMask = new FieldMask { Paths = { "", "single", "with_unders core", "nested.field.name", "nested..double_dot" } };
402 Assert.AreEqual(",single,withUnderscore,nested.field.name,nested..do ubleDot", fieldMask.ToString()); 443 Assert.AreEqual("\",single,withUnderscore,nested.field.name,nested.. doubleDot\"", fieldMask.ToString());
403 444
404 // Invalid, but we shouldn't create broken JSON... 445 // Invalid, but we shouldn't create broken JSON...
405 fieldMask = new FieldMask { Paths = { "x\\y" } }; 446 fieldMask = new FieldMask { Paths = { "x\\y" } };
406 Assert.AreEqual(@"x\\y", fieldMask.ToString()); 447 Assert.AreEqual(@"""x\\y""", fieldMask.ToString());
407 } 448 }
408 449
409 [Test] 450 [Test]
410 public void FieldMaskField() 451 public void FieldMaskField()
411 { 452 {
412 var message = new TestWellKnownTypes { FieldMaskField = new FieldMas k { Paths = { "user.display_name", "photo" } } }; 453 var message = new TestWellKnownTypes { FieldMaskField = new FieldMas k { Paths = { "user.display_name", "photo" } } };
413 AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFor matter.Default.Format(message)); 454 AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFor matter.Default.Format(message));
414 } 455 }
415 456
416 // SourceContext is an example of a well-known type with no special JSON handling 457 // SourceContext is an example of a well-known type with no special JSON handling
(...skipping 16 matching lines...) Expand all
433 [Test] 474 [Test]
434 public void AnyMessageType() 475 public void AnyMessageType()
435 { 476 {
436 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); 477 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
437 var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessa ge = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; 478 var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessa ge = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
438 var any = Any.Pack(message); 479 var any = Any.Pack(message);
439 AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAl lTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Fo rmat(any)); 480 AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAl lTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Fo rmat(any));
440 } 481 }
441 482
442 [Test] 483 [Test]
484 public void AnyMessageType_CustomPrefix()
485 {
486 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
487 var message = new TestAllTypes { SingleInt32 = 10 };
488 var any = Any.Pack(message, "foo.bar/baz");
489 AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
490 }
491
492 [Test]
443 public void AnyNested() 493 public void AnyNested()
444 { 494 {
445 var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descript or, TestAllTypes.Descriptor); 495 var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descript or, TestAllTypes.Descriptor);
446 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); 496 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));
447 497
448 // Nest an Any as the value of an Any. 498 // Nest an Any as the value of an Any.
449 var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 }; 499 var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
450 var nestedMessage = Any.Pack(doubleNestedMessage); 500 var nestedMessage = Any.Pack(doubleNestedMessage);
451 var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMes sage) }; 501 var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMes sage) };
452 AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.pro tobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTy pes', 'singleInt32': 20 } } }", 502 AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.pro tobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTy pes', 'singleInt32': 20 } } }",
(...skipping 14 matching lines...) Expand all
467 /// all apostrophes in the expected JSON with double quotes. This basica lly makes the tests easier 517 /// all apostrophes in the expected JSON with double quotes. This basica lly makes the tests easier
468 /// to read. 518 /// to read.
469 /// </summary> 519 /// </summary>
470 private static void AssertJson(string expectedJsonWithApostrophes, strin g actualJson) 520 private static void AssertJson(string expectedJsonWithApostrophes, strin g actualJson)
471 { 521 {
472 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\""); 522 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
473 Assert.AreEqual(expectedJson, actualJson); 523 Assert.AreEqual(expectedJson, actualJson);
474 } 524 }
475 } 525 }
476 } 526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698