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; | |
43 | 41 |
44 namespace Google.Protobuf | 42 namespace Google.Protobuf |
45 { | 43 { |
46 /// <summary> | 44 /// <summary> |
47 /// 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 |
48 /// 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. |
49 /// </summary> | 47 /// </summary> |
50 public class JsonFormatterTest | 48 public class JsonFormatterTest |
51 { | 49 { |
52 [Test] | 50 [Test] |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); | 220 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); |
223 | 221 |
224 // Lone high surrogate | 222 // Lone high surrogate |
225 message = new TestAllTypes { SingleString = "a\uD801b" }; | 223 message = new TestAllTypes { SingleString = "a\uD801b" }; |
226 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); | 224 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(
message)); |
227 } | 225 } |
228 | 226 |
229 [Test] | 227 [Test] |
230 [TestCase("foo_bar", "fooBar")] | 228 [TestCase("foo_bar", "fooBar")] |
231 [TestCase("bananaBanana", "bananaBanana")] | 229 [TestCase("bananaBanana", "bananaBanana")] |
232 [TestCase("BANANABanana", "BANANABanana")] | 230 [TestCase("BANANABanana", "bananaBanana")] |
233 [TestCase("simple", "simple")] | 231 public void ToCamelCase(string original, string expected) |
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) | |
240 { | 232 { |
241 Assert.AreEqual(expected, JsonFormatter.ToJsonName(original)); | 233 Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original)); |
242 } | 234 } |
243 | 235 |
244 [Test] | 236 [Test] |
245 [TestCase(null, "{ }")] | 237 [TestCase(null, "{ }")] |
246 [TestCase("x", "{ 'fooString': 'x' }")] | 238 [TestCase("x", "{ 'fooString': 'x' }")] |
247 [TestCase("", "{ 'fooString': '' }")] | 239 [TestCase("", "{ 'fooString': '' }")] |
248 public void Oneof(string fooStringValue, string expectedJson) | 240 public void Oneof(string fooStringValue, string expectedJson) |
249 { | 241 { |
250 var message = new TestOneof(); | 242 var message = new TestOneof(); |
251 if (fooStringValue != null) | 243 if (fooStringValue != null) |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 | 505 |
514 [Test] | 506 [Test] |
515 public void AnyUnknownType() | 507 public void AnyUnknownType() |
516 { | 508 { |
517 // The default type registry doesn't have any types in it. | 509 // The default type registry doesn't have any types in it. |
518 var message = new TestAllTypes(); | 510 var message = new TestAllTypes(); |
519 var any = Any.Pack(message); | 511 var any = Any.Pack(message); |
520 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default
.Format(any)); | 512 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default
.Format(any)); |
521 } | 513 } |
522 | 514 |
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 | |
584 /// <summary> | 515 /// <summary> |
585 /// Checks that the actual JSON is the same as the expected JSON - but a
fter replacing | 516 /// Checks that the actual JSON is the same as the expected JSON - but a
fter replacing |
586 /// 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 |
587 /// to read. | 518 /// to read. |
588 /// </summary> | 519 /// </summary> |
589 private static void AssertJson(string expectedJsonWithApostrophes, strin
g actualJson) | 520 private static void AssertJson(string expectedJsonWithApostrophes, strin
g actualJson) |
590 { | 521 { |
591 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\""); | 522 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\""); |
592 Assert.AreEqual(expectedJson, actualJson); | 523 Assert.AreEqual(expectedJson, actualJson); |
593 } | 524 } |
594 } | 525 } |
595 } | 526 } |
OLD | NEW |