Exemplo n.º 1
0
        public void NonDefaultSingleValues()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "x",
                BytesField = ByteString.CopyFrom(1, 2, 3),
                BoolField = true,
                FloatField = 12.5f,
                DoubleField = 12.25d,
                Int32Field = 1,
                Int64Field = 2,
                Uint32Field = 3,
                Uint64Field = 4
            };

            var bytes = message.ToByteArray();
            var parsed = TestWellKnownTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual("x", parsed.StringField);
            Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), parsed.BytesField);
            Assert.AreEqual(true, parsed.BoolField);
            Assert.AreEqual(12.5f, parsed.FloatField);
            Assert.AreEqual(12.25d, parsed.DoubleField);
            Assert.AreEqual(1, parsed.Int32Field);
            Assert.AreEqual(2L, parsed.Int64Field);
            Assert.AreEqual(3U, parsed.Uint32Field);
            Assert.AreEqual(4UL, parsed.Uint64Field);
        }
Exemplo n.º 2
0
        public void ToString_MessageContainingAny()
        {
            var message = new TestWellKnownTypes {
                AnyField = new Any()
            };

            Assert.AreEqual("{ \"anyField\": { \"@type\": \"\", \"@value\": \"\" } }", message.ToString());
        }
Exemplo n.º 3
0
        public void DurationField()
        {
            var message = new TestWellKnownTypes {
                DurationField = new Duration()
            };

            AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
        }
Exemplo n.º 4
0
        public void TimestampField()
        {
            var message = new TestWellKnownTypes {
                TimestampField = new Timestamp()
            };

            AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
        }
Exemplo n.º 5
0
        public void SingularWrappers_ExplicitNulls()
        {
            var message = new TestWellKnownTypes();
            var json    = new JsonFormatter(new JsonFormatter.Settings(true)).Format(message);
            var parsed  = JsonParser.Default.Parse <TestWellKnownTypes>(json);

            Assert.AreEqual(message, parsed);
        }
Exemplo n.º 6
0
        public void FieldMaskField()
        {
            var message = new TestWellKnownTypes {
                FieldMaskField = new FieldMask {
                    Paths = { "user.display_name", "photo" }
                }
            };

            AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
        }
Exemplo n.º 7
0
        public void ClearWithReflection()
        {
            // String and Bytes are the tricky ones here, as the CLR type of the property
            // is the same between the wrapper and non-wrapper types.
            var message = new TestWellKnownTypes {
                StringField = "foo"
            };

            TestWellKnownTypes.Descriptor.Fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.Clear(message);
            Assert.IsNull(message.StringField);
        }
Exemplo n.º 8
0
        public void SingularWrappers_ExplicitNulls()
        {
            // When we parse the "valueField": null part, we remember it... basically, it's one case
            // where explicit default values don't fully roundtrip.
            var message = new TestWellKnownTypes {
                ValueField = Value.ForNull()
            };
            var json   = new JsonFormatter(new JsonFormatter.Settings(true)).Format(message);
            var parsed = JsonParser.Default.Parse <TestWellKnownTypes>(json);

            Assert.AreEqual(message, parsed);
        }
Exemplo n.º 9
0
        public void WrapperFormatting_IncludeNull()
        {
            // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
            var message = new TestWellKnownTypes {
                Int32Field = 10
            };
            var formatter  = new JsonFormatter(new JsonFormatter.Settings(true));
            var actualJson = formatter.Format(message);

            Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
            Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
        }
Exemplo n.º 10
0
 public void NullIsDefault()
 {
     var message = new TestWellKnownTypes();
     Assert.IsNull(message.StringField);
     Assert.IsNull(message.BytesField);
     Assert.IsNull(message.BoolField);
     Assert.IsNull(message.FloatField);
     Assert.IsNull(message.DoubleField);
     Assert.IsNull(message.Int32Field);
     Assert.IsNull(message.Int64Field);
     Assert.IsNull(message.Uint32Field);
     Assert.IsNull(message.Uint64Field);
 }
Exemplo n.º 11
0
        public void WrapperFormatting_FormatDefaultValuesDoesNotFormatNull()
        {
            // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
            var message = new TestWellKnownTypes {
                Int32Field = 10
            };
            var formatter  = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var actualJson = formatter.Format(message);

            // This *used* to include "int64Field": null, but that was a bug.
            // WithDefaultValues should not affect message fields, including wrapper types.
            Assert.IsFalse(actualJson.Contains("\"int64Field\": null"));
            Assert.IsTrue(actualJson.Contains("\"int32Field\": 10"));
        }
Exemplo n.º 12
0
        public void MergingMessageWithZero(int?originValue, int?mergingValue, int?expectedResult)
        {
            // This differs from the MergingStreamCornerCase because when we merge message *objects*,
            // we ignore default values from the "source".
            var message1 = new TestWellKnownTypes {
                Int32Field = originValue
            };
            var message2 = new TestWellKnownTypes {
                Int32Field = mergingValue
            };

            message1.MergeFrom(message2);
            Assert.AreEqual(expectedResult, message1.Int32Field);
        }
Exemplo n.º 13
0
        public void NullIsDefault()
        {
            var message = new TestWellKnownTypes();

            Assert.IsNull(message.StringField);
            Assert.IsNull(message.BytesField);
            Assert.IsNull(message.BoolField);
            Assert.IsNull(message.FloatField);
            Assert.IsNull(message.DoubleField);
            Assert.IsNull(message.Int32Field);
            Assert.IsNull(message.Int64Field);
            Assert.IsNull(message.Uint32Field);
            Assert.IsNull(message.Uint64Field);
        }
Exemplo n.º 14
0
        public void WrapperFormatting_Single()
        {
            // Just a few examples, handling both classes and value types, and
            // default vs non-default values
            var message = new TestWellKnownTypes
            {
                Int64Field  = 10,
                Int32Field  = 0,
                BytesField  = ByteString.FromBase64("ABCD"),
                StringField = ""
            };
            var expectedJson = "{ \"int64Field\": \"10\", \"int32Field\": 0, \"stringField\": \"\", \"bytesField\": \"ABCD\" }";

            Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(message));
        }
Exemplo n.º 15
0
        public void NaNComparisons()
        {
            var message1 = new TestWellKnownTypes {
                DoubleField = SampleNaNs.Regular
            };
            var message2 = new TestWellKnownTypes {
                DoubleField = SampleNaNs.PayloadFlipped
            };
            var message3 = new TestWellKnownTypes {
                DoubleField = SampleNaNs.Regular
            };

            EqualityTester.AssertInequality(message1, message2);
            EqualityTester.AssertEquality(message1, message3);
        }
Exemplo n.º 16
0
        public void WrapperFormatting_Single()
        {
            // Just a few examples, handling both classes and value types, and
            // default vs non-default values
            var message = new TestWellKnownTypes
            {
                Int64Field  = 10,
                Int32Field  = 0,
                BytesField  = ByteString.FromBase64("ABCD"),
                StringField = ""
            };
            var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";

            AssertJson(expectedJson, JsonFormatter.Default.Format(message));
        }
Exemplo n.º 17
0
        public void AnyNested()
        {
            var registry  = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));

            // Nest an Any as the value of an Any.
            var doubleNestedMessage = new TestAllTypes {
                SingleInt32 = 20
            };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message       = new TestWellKnownTypes {
                AnyField = Any.Pack(nestedMessage)
            };

            AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
                       formatter.Format(message));
        }
Exemplo n.º 18
0
        public void SingularWrappers_DefaultNonNullValues()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "",
                BytesField  = ByteString.Empty,
                BoolField   = false,
                FloatField  = 0f,
                DoubleField = 0d,
                Int32Field  = 0,
                Int64Field  = 0,
                Uint32Field = 0,
                Uint64Field = 0
            };

            AssertRoundtrip(message);
        }
Exemplo n.º 19
0
        public void SingularWrappers_NonDefaultValues()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "x",
                BytesField  = ByteString.CopyFrom(1, 2, 3),
                BoolField   = true,
                FloatField  = 12.5f,
                DoubleField = 12.25d,
                Int32Field  = 1,
                Int64Field  = 2,
                Uint32Field = 3,
                Uint64Field = 4
            };

            AssertRoundtrip(message);
        }
Exemplo n.º 20
0
        public void Any_Nested()
        {
            var registry            = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter           = new JsonFormatter(new JsonFormatter.Settings(false, registry));
            var parser              = new JsonParser(new JsonParser.Settings(10, registry));
            var doubleNestedMessage = new TestAllTypes {
                SingleInt32 = 20
            };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message       = new TestWellKnownTypes {
                AnyField = Any.Pack(nestedMessage)
            };
            var json = formatter.Format(message);

            // Use the descriptor-based parser just for a change.
            Assert.AreEqual(message, parser.Parse(json, TestWellKnownTypes.Descriptor));
        }
Exemplo n.º 21
0
        public void MergingStreamNoValue()
        {
            var message = new TestWellKnownTypes {
                Int32Field = 5
            };

            // Create a byte array which an Int32 field, but with no value.
            var bytes = new TestWellKnownTypes {
                Int32Field = 0
            }.ToByteArray();

            Assert.AreEqual(2, bytes.Length); // The tag for Int32Field is a single byte, then a byte indicating a 0-length message.
            message.MergeFrom(bytes);

            // The "implicit" 0 did *not* overwrite the value.
            // (This is the correct behaviour.)
            Assert.AreEqual(5, message.Int32Field);
        }
Exemplo n.º 22
0
        public void MergeWrapperFieldsWithNullFieldsInSource(
            bool replaceMessageFields,
            string expectedStringValue,
            long?expectedInt64Value)
        {
            // Instantiate a destination with wrapper-based field types.
            var destination = new TestWellKnownTypes()
            {
                StringField = "Hello",
                Int32Field  = 12,
                Int64Field  = 24,
                BoolField   = true,
            };

            // Set up a targeted update with null valued fields.
            var source = new TestWellKnownTypes()
            {
                StringField = null,
                Int64Field  = null
            };

            Merge(new FieldMaskTree().AddFieldPath("string_field").AddFieldPath("int64_field"),
                  source,
                  destination,
                  new FieldMask.MergeOptions()
            {
                ReplaceMessageFields = replaceMessageFields
            },
                  false);

            // Make sure the targeted fields changed according to our expectations, depending on the value of ReplaceMessageFields.
            // When ReplaceMessageFields is false, the null values are not applied to the destination, because, although wrapped types
            // are semantically primitives, FieldMaskTree.Merge still treats them as message types in order to maintain consistency with other Protobuf
            // libraries such as Java and C++.
            Assert.AreEqual(expectedStringValue, destination.StringField);
            Assert.AreEqual(expectedInt64Value, destination.Int64Field);

            // Prove that non-targeted fields stay intact...
            Assert.AreEqual(12, destination.Int32Field);
            Assert.IsTrue(destination.BoolField);

            // ...including default values which were not explicitly set in the destination object.
            Assert.IsNull(destination.FloatField);
        }
Exemplo n.º 23
0
        public void Merging(string original, string merged, string expected)
        {
            var originalMessage = new TestWellKnownTypes {
                StringField = original
            };
            var mergingMessage = new TestWellKnownTypes {
                StringField = merged
            };

            originalMessage.MergeFrom(mergingMessage);
            Assert.AreEqual(expected, originalMessage.StringField);

            // Try it using MergeFrom(CodedInputStream) too...
            originalMessage = new TestWellKnownTypes {
                StringField = original
            };
            originalMessage.MergeFrom(mergingMessage.ToByteArray());
            Assert.AreEqual(expected, originalMessage.StringField);
        }
Exemplo n.º 24
0
        public void NegativeSingleValues()
        {
            var message = new TestWellKnownTypes
            {
                FloatField  = -12.5f,
                DoubleField = -12.25d,
                Int32Field  = -1,
                Int64Field  = -2
            };

            MessageParsingHelpers.AssertWritingMessage(message);

            MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
            {
                Assert.AreEqual(-12.5f, parsed.FloatField);
                Assert.AreEqual(-12.25d, parsed.DoubleField);
                Assert.AreEqual(-1, parsed.Int32Field);
                Assert.AreEqual(-2L, parsed.Int64Field);
            });
        }
Exemplo n.º 25
0
        public void MergingStreamNoValue()
        {
            var message = new TestWellKnownTypes { Int32Field = 5 };

            // Create a byte array which an Int32 field, but with no value.
            var bytes = new TestWellKnownTypes { Int32Field = 0 }.ToByteArray();
            Assert.AreEqual(2, bytes.Length); // The tag for Int32Field is a single byte, then a byte indicating a 0-length message.
            message.MergeFrom(bytes);

            // The "implicit" 0 did *not* overwrite the value.
            // (This is the correct behaviour.)
            Assert.AreEqual(5, message.Int32Field);
        }
Exemplo n.º 26
0
        public void MergingCornerCase()
        {
            var message = new TestWellKnownTypes { Int32Field = 5 };

            // Create a byte array which has the data of an Int32Value explicitly containing a value of 0.
            // This wouldn't normally happen.
            byte[] bytes;
            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
            using (var stream = new MemoryStream())
            {
                var coded = new CodedOutputStream(stream);
                coded.WriteTag(wrapperTag);
                coded.WriteLength(2); // valueTag + a value 0, each one byte
                coded.WriteTag(valueTag);
                coded.WriteInt32(0);
                coded.Flush();
                bytes = stream.ToArray();
            }

            message.MergeFrom(bytes);
            // A normal implementation would have 0 now, as the explicit default would have been overwritten the 5.
            Assert.AreEqual(5, message.Int32Field);
        }
Exemplo n.º 27
0
        public void Merging(string original, string merged, string expected)
        {
            var originalMessage = new TestWellKnownTypes { StringField = original };
            var mergingMessage = new TestWellKnownTypes { StringField = merged };
            originalMessage.MergeFrom(mergingMessage);
            Assert.AreEqual(expected, originalMessage.StringField);

            // Try it using MergeFrom(CodedInputStream) too...
            originalMessage = new TestWellKnownTypes { StringField = original };
            originalMessage.MergeFrom(mergingMessage.ToByteArray());
            Assert.AreEqual(expected, originalMessage.StringField);
        }
Exemplo n.º 28
0
        public void Reflection_SingleValues()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "x",
                BytesField = ByteString.CopyFrom(1, 2, 3),
                BoolField = true,
                FloatField = 12.5f,
                DoubleField = 12.25d,
                Int32Field = 1,
                Int64Field = 2,
                Uint32Field = 3,
                Uint64Field = 4
            };
            var fields = TestWellKnownTypes.Descriptor.Fields;

            Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(true, fields[TestWellKnownTypes.BoolFieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(12.5f, fields[TestWellKnownTypes.FloatFieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(12.25d, fields[TestWellKnownTypes.DoubleFieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(1, fields[TestWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(2L, fields[TestWellKnownTypes.Int64FieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(3U, fields[TestWellKnownTypes.Uint32FieldFieldNumber].Accessor.GetValue(message));
            Assert.AreEqual(4UL, fields[TestWellKnownTypes.Uint64FieldFieldNumber].Accessor.GetValue(message));

            // And a couple of null fields...
            message.StringField = null;
            message.FloatField = null;
            Assert.IsNull(fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.GetValue(message));
            Assert.IsNull(fields[TestWellKnownTypes.FloatFieldFieldNumber].Accessor.GetValue(message));
        }
Exemplo n.º 29
0
 public void MergingMessageWithZero(int? originValue, int? mergingValue, int? expectedResult)
 {
     // This differs from the MergingStreamCornerCase because when we merge message *objects*,
     // we ignore default values from the "source".
     var message1 = new TestWellKnownTypes { Int32Field = originValue };
     var message2 = new TestWellKnownTypes { Int32Field = mergingValue };
     message1.MergeFrom(message2);
     Assert.AreEqual(expectedResult, message1.Int32Field);
 }
Exemplo n.º 30
0
        public void AnyNested()
        {
            var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));

            // Nest an Any as the value of an Any.
            var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
            AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
                formatter.Format(message));
        }
Exemplo n.º 31
0
 public void WrapperFormatting_Single()
 {
     // Just a few examples, handling both classes and value types, and
     // default vs non-default values
     var message = new TestWellKnownTypes
     {
         Int64Field = 10,
         Int32Field = 0,
         BytesField = ByteString.FromBase64("ABCD"),
         StringField = ""
     };
     var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
     AssertJson(expectedJson, JsonFormatter.Default.Format(message));
 }
Exemplo n.º 32
0
 public void ToString_MessageContainingAny()
 {
     var message = new TestWellKnownTypes { AnyField = new Any() };
     Assert.AreEqual("{ \"anyField\": { \"@type\": \"\", \"@value\": \"\" } }", message.ToString());
 }
Exemplo n.º 33
0
 public void WrapperFormatting_Single()
 {
     // Just a few examples, handling both classes and value types, and
     // default vs non-default values
     var message = new TestWellKnownTypes
     {
         Int64Field = 10,
         Int32Field = 0,
         BytesField = ByteString.FromBase64("ABCD"),
         StringField = ""
     };
     var expectedJson = "{ \"int64Field\": \"10\", \"int32Field\": 0, \"stringField\": \"\", \"bytesField\": \"ABCD\" }";
     Assert.AreEqual(expectedJson, JsonFormatter.Default.Format(message));
 }
Exemplo n.º 34
0
 public void TimestampField()
 {
     var message = new TestWellKnownTypes { TimestampField = new Timestamp() };
     AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
 }
Exemplo n.º 35
0
 public void DurationField()
 {
     var message = new TestWellKnownTypes { DurationField = new Duration() };
     AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
 }
Exemplo n.º 36
0
        public void NonNullDefaultIsPreservedThroughSerialization()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "",
                BytesField = ByteString.Empty,
                BoolField = false,
                FloatField = 0f,
                DoubleField = 0d,
                Int32Field = 0,
                Int64Field = 0,
                Uint32Field = 0,
                Uint64Field = 0
            };

            var bytes = message.ToByteArray();
            var parsed = TestWellKnownTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual("", parsed.StringField);
            Assert.AreEqual(ByteString.Empty, parsed.BytesField);
            Assert.AreEqual(false, parsed.BoolField);
            Assert.AreEqual(0f, parsed.FloatField);
            Assert.AreEqual(0d, parsed.DoubleField);
            Assert.AreEqual(0, parsed.Int32Field);
            Assert.AreEqual(0L, parsed.Int64Field);
            Assert.AreEqual(0U, parsed.Uint32Field);
            Assert.AreEqual(0UL, parsed.Uint64Field);
        }
Exemplo n.º 37
0
 public void ClearWithReflection()
 {
     // String and Bytes are the tricky ones here, as the CLR type of the property
     // is the same between the wrapper and non-wrapper types.
     var message = new TestWellKnownTypes { StringField = "foo" };
     TestWellKnownTypes.Descriptor.Fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.Clear(message);
     Assert.IsNull(message.StringField);
 }
Exemplo n.º 38
0
 public void WrapperFormatting_IncludeNull()
 {
     // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
     var message = new TestWellKnownTypes { Int32Field = 10 };
     var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
     var actualJson = formatter.Format(message);
     Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
     Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
 }
Exemplo n.º 39
0
 public void FieldMaskField()
 {
     var message = new TestWellKnownTypes { FieldMaskField = new FieldMask { Paths = { "user.display_name", "photo" } } };
     AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
 }