Exemplo n.º 1
0
        public void AddEntriesFrom_Message()
        {
            var message1 = new ForeignMessage { C = 2000 };
            var message2 = new ForeignMessage { C = -250 };

            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(tag);
            output.WriteMessage(message1);
            output.WriteTag(tag);
            output.WriteMessage(message2);
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField<ForeignMessage>();
            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            CollectionAssert.AreEqual(new[] { message1, message2}, field);
            Assert.IsTrue(input.IsAtEnd);
        }
Exemplo n.º 2
0
        public void MapWithOnlyValue()
        {
            // Hand-craft the stream to contain a single entry with just a value.
            var memoryStream = new MemoryStream();
            var output = new CodedOutputStream(memoryStream);
            output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
            var nestedMessage = new ForeignMessage { C = 20 };
            // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
            output.WriteLength(2 + nestedMessage.CalculateSize());
            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
            output.WriteMessage(nestedMessage);
            output.Flush();

            var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
            Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]);
        }
Exemplo n.º 3
0
 public void CustomDiagnosticMessage_DirectToTextWriterCall()
 {
     var message = new ForeignMessage { C = 31 };
     var writer = new StringWriter();
     JsonFormatter.Default.Format(message, writer);
     Assert.AreEqual("{ \"c\": 31 }", writer.ToString());
 }
Exemplo n.º 4
0
 public void CustomDiagnosticMessage_DirectToStringCall()
 {
     var message = new ForeignMessage { C = 31 };
     Assert.AreEqual("{ \"c\": 31, \"@cInHex\": \"1f\" }", message.ToString());
     Assert.AreEqual("{ \"c\": 31 }", JsonFormatter.Default.Format(message));
 }
Exemplo n.º 5
0
        public void WriteTo_Message()
        {
            var message1 = new ForeignMessage { C = 20 };
            var message2 = new ForeignMessage { C = 25 };
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<ForeignMessage> { message1, message2 };
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
            input.AssertNextTag(tag);
            Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser));
            Assert.IsTrue(input.IsAtEnd);
        }