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