OLD | NEW |
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 20 matching lines...) Expand all Loading... |
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 | 40 using static Google.Protobuf.JsonParserTest; // For WrapInQuotes |
| 41 using System.IO; |
| 42 using Google.Protobuf.Collections; |
41 | 43 |
42 namespace Google.Protobuf | 44 namespace Google.Protobuf |
43 { | 45 { |
44 /// <summary> | 46 /// <summary> |
45 /// Tests for the JSON formatter. Note that in these tests, double quotes ar
e replaced with apostrophes | 47 /// Tests for the JSON formatter. Note that in these tests, double quotes ar
e replaced with apostrophes |
46 /// for the sake of readability (embedding \" everywhere is painful). See th
e AssertJson method for details. | 48 /// for the sake of readability (embedding \" everywhere is painful). See th
e AssertJson method for details. |
47 /// </summary> | 49 /// </summary> |
48 public class JsonFormatterTest | 50 public class JsonFormatterTest |
49 { | 51 { |
50 [Test] | 52 [Test] |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); | 222 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); |
221 | 223 |
222 // Lone high surrogate | 224 // Lone high surrogate |
223 message = new TestAllTypes { SingleString = "a\uD801b" }; | 225 message = new TestAllTypes { SingleString = "a\uD801b" }; |
224 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); | 226 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); |
225 } | 227 } |
226 | 228 |
227 [Test] | 229 [Test] |
228 [TestCase("foo_bar", "fooBar")] | 230 [TestCase("foo_bar", "fooBar")] |
229 [TestCase("bananaBanana", "bananaBanana")] | 231 [TestCase("bananaBanana", "bananaBanana")] |
230 [TestCase("BANANABanana", "bananaBanana")] | 232 [TestCase("BANANABanana", "BANANABanana")] |
231 public void ToCamelCase(string original, string expected) | 233 [TestCase("simple", "simple")] |
| 234 [TestCase("ACTION_AND_ADVENTURE", "ACTIONANDADVENTURE")] |
| 235 [TestCase("action_and_adventure", "actionAndAdventure")] |
| 236 [TestCase("kFoo", "kFoo")] |
| 237 [TestCase("HTTPServer", "HTTPServer")] |
| 238 [TestCase("CLIENT", "CLIENT")] |
| 239 public void ToJsonName(string original, string expected) |
232 { | 240 { |
233 Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original)); | 241 Assert.AreEqual(expected, JsonFormatter.ToJsonName(original)); |
234 } | 242 } |
235 | 243 |
236 [Test] | 244 [Test] |
237 [TestCase(null, "{ }")] | 245 [TestCase(null, "{ }")] |
238 [TestCase("x", "{ 'fooString': 'x' }")] | 246 [TestCase("x", "{ 'fooString': 'x' }")] |
239 [TestCase("", "{ 'fooString': '' }")] | 247 [TestCase("", "{ 'fooString': '' }")] |
240 public void Oneof(string fooStringValue, string expectedJson) | 248 public void Oneof(string fooStringValue, string expectedJson) |
241 { | 249 { |
242 var message = new TestOneof(); | 250 var message = new TestOneof(); |
243 if (fooStringValue != null) | 251 if (fooStringValue != null) |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 | 513 |
506 [Test] | 514 [Test] |
507 public void AnyUnknownType() | 515 public void AnyUnknownType() |
508 { | 516 { |
509 // The default type registry doesn't have any types in it. | 517 // The default type registry doesn't have any types in it. |
510 var message = new TestAllTypes(); | 518 var message = new TestAllTypes(); |
511 var any = Any.Pack(message); | 519 var any = Any.Pack(message); |
512 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default
.Format(any)); | 520 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default
.Format(any)); |
513 } | 521 } |
514 | 522 |
| 523 [Test] |
| 524 [TestCase(typeof(BoolValue), true, "true")] |
| 525 [TestCase(typeof(Int32Value), 32, "32")] |
| 526 [TestCase(typeof(Int64Value), 32L, "\"32\"")] |
| 527 [TestCase(typeof(UInt32Value), 32U, "32")] |
| 528 [TestCase(typeof(UInt64Value), 32UL, "\"32\"")] |
| 529 [TestCase(typeof(StringValue), "foo", "\"foo\"")] |
| 530 [TestCase(typeof(FloatValue), 1.5f, "1.5")] |
| 531 [TestCase(typeof(DoubleValue), 1.5d, "1.5")] |
| 532 public void Wrappers_Standalone(System.Type wrapperType, object value, s
tring expectedJson) |
| 533 { |
| 534 IMessage populated = (IMessage)Activator.CreateInstance(wrapperType)
; |
| 535 populated.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumb
er].Accessor.SetValue(populated, value); |
| 536 Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(populated
)); |
| 537 } |
| 538 |
| 539 // Sanity tests for WriteValue. Not particularly comprehensive, as it's
all covered above already, |
| 540 // as FormatMessage uses WriteValue. |
| 541 |
| 542 [TestCase(null, "null")] |
| 543 [TestCase(1, "1")] |
| 544 [TestCase(1L, "'1'")] |
| 545 [TestCase(0.5f, "0.5")] |
| 546 [TestCase(0.5d, "0.5")] |
| 547 [TestCase("text", "'text'")] |
| 548 [TestCase("x\ny", @"'x\ny'")] |
| 549 [TestCase(ForeignEnum.ForeignBar, "'FOREIGN_BAR'")] |
| 550 public void WriteValue_Constant(object value, string expectedJson) |
| 551 { |
| 552 AssertWriteValue(value, expectedJson); |
| 553 } |
| 554 |
| 555 [Test] |
| 556 public void WriteValue_Timestamp() |
| 557 { |
| 558 var value = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).
ToTimestamp(); |
| 559 AssertWriteValue(value, "'1673-06-19T12:34:56Z'"); |
| 560 } |
| 561 |
| 562 [Test] |
| 563 public void WriteValue_Message() |
| 564 { |
| 565 var value = new TestAllTypes { SingleInt32 = 100, SingleInt64 = 3210
987654321L }; |
| 566 AssertWriteValue(value, "{ 'singleInt32': 100, 'singleInt64': '32109
87654321' }"); |
| 567 } |
| 568 |
| 569 [Test] |
| 570 public void WriteValue_List() |
| 571 { |
| 572 var value = new RepeatedField<int> { 1, 2, 3 }; |
| 573 AssertWriteValue(value, "[ 1, 2, 3 ]"); |
| 574 } |
| 575 |
| 576 private static void AssertWriteValue(object value, string expectedJson) |
| 577 { |
| 578 var writer = new StringWriter(); |
| 579 JsonFormatter.Default.WriteValue(writer, value); |
| 580 string actual = writer.ToString(); |
| 581 AssertJson(expectedJson, actual); |
| 582 } |
| 583 |
515 /// <summary> | 584 /// <summary> |
516 /// Checks that the actual JSON is the same as the expected JSON - but a
fter replacing | 585 /// Checks that the actual JSON is the same as the expected JSON - but a
fter replacing |
517 /// all apostrophes in the expected JSON with double quotes. This basica
lly makes the tests easier | 586 /// all apostrophes in the expected JSON with double quotes. This basica
lly makes the tests easier |
518 /// to read. | 587 /// to read. |
519 /// </summary> | 588 /// </summary> |
520 private static void AssertJson(string expectedJsonWithApostrophes, strin
g actualJson) | 589 private static void AssertJson(string expectedJsonWithApostrophes, strin
g actualJson) |
521 { | 590 { |
522 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\""); | 591 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\""); |
523 Assert.AreEqual(expectedJson, actualJson); | 592 Assert.AreEqual(expectedJson, actualJson); |
524 } | 593 } |
525 } | 594 } |
526 } | 595 } |
OLD | NEW |