csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
| 2 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 34 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
| 38 | using System.Collections.Generic;
|
| 39 | using System.IO;
|
| 40 | using Google.ProtocolBuffers.Descriptors;
|
| 41 | using Google.ProtocolBuffers.TestProtos;
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame] | 42 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 43 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 44 | namespace Google.ProtocolBuffers
|
| 45 | {
|
| 46 | /// <summary>
|
| 47 | /// Miscellaneous tests for message operations that apply to both
|
| 48 | /// generated and dynamic messages.
|
| 49 | /// </summary>
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame] | 50 | [TestClass]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 51 | public class LiteTest
|
| 52 | {
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame] | 53 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 54 | public void TestLite()
|
| 55 | {
|
| 56 | // Since lite messages are a subset of regular messages, we can mostly
|
| 57 | // assume that the functionality of lite messages is already thoroughly
|
| 58 | // tested by the regular tests. All this test really verifies is that
|
| 59 | // a proto with optimize_for = LITE_RUNTIME compiles correctly when
|
| 60 | // linked only against the lite library. That is all tested at compile
|
| 61 | // time, leaving not much to do in this method. Let's just do some random
|
| 62 | // stuff to make sure the lite message is actually here and usable.
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 63 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 64 | TestAllTypesLite message =
|
| 65 | TestAllTypesLite.CreateBuilder()
|
| 66 | .SetOptionalInt32(123)
|
| 67 | .AddRepeatedString("hello")
|
| 68 | .SetOptionalNestedMessage(
|
| 69 | TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7))
|
| 70 | .Build();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 71 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 72 | ByteString data = message.ToByteString();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 73 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 74 | TestAllTypesLite message2 = TestAllTypesLite.ParseFrom(data);
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 75 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 76 | Assert.AreEqual(123, message2.OptionalInt32);
|
| 77 | Assert.AreEqual(1, message2.RepeatedStringCount);
|
| 78 | Assert.AreEqual("hello", message2.RepeatedStringList[0]);
|
| 79 | Assert.AreEqual(7, message2.OptionalNestedMessage.Bb);
|
| 80 | }
|
| 81 |
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame] | 82 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 83 | public void TestLiteExtensions()
|
| 84 | {
|
| 85 | // TODO(kenton): Unlike other features of the lite library, extensions are
|
| 86 | // implemented completely differently from the regular library. We
|
| 87 | // should probably test them more thoroughly.
|
| 88 |
|
| 89 | TestAllExtensionsLite message =
|
| 90 | TestAllExtensionsLite.CreateBuilder()
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 91 | .SetExtension(UnittestLite.OptionalInt32ExtensionLite, 123)
|
| 92 | .AddExtension(UnittestLite.RepeatedStringExtensionLite, "hello")
|
| 93 | .SetExtension(UnittestLite.OptionalNestedEnumExtensionLite,
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 94 | TestAllTypesLite.Types.NestedEnum.BAZ)
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 95 | .SetExtension(UnittestLite.OptionalNestedMessageExtensionLite,
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 96 | TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7).Build())
|
| 97 | .Build();
|
| 98 |
|
| 99 | // Test copying a message, since coping extensions actually does use a
|
| 100 | // different code path between lite and regular libraries, and as of this
|
| 101 | // writing, parsing hasn't been implemented yet.
|
| 102 | TestAllExtensionsLite message2 = message.ToBuilder().Build();
|
| 103 |
|
| 104 | Assert.AreEqual(123, (int) message2.GetExtension(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 105 | UnittestLite.OptionalInt32ExtensionLite));
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 106 | Assert.AreEqual(1, message2.GetExtensionCount(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 107 | UnittestLite.RepeatedStringExtensionLite));
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 108 | Assert.AreEqual(1, message2.GetExtension(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 109 | UnittestLite.RepeatedStringExtensionLite).Count);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 110 | Assert.AreEqual("hello", message2.GetExtension(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 111 | UnittestLite.RepeatedStringExtensionLite, 0));
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 112 | Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 113 | UnittestLite.OptionalNestedEnumExtensionLite));
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 114 | Assert.AreEqual(7, message2.GetExtension(
|
Jon Skeet | ce66c5f | 2015-04-28 15:06:59 +0100 | [diff] [blame] | 115 | UnittestLite.OptionalNestedMessageExtensionLite).Bb);
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 116 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 117 | }
|
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 118 | } |