blob: ca5c4bff21e90be9c0001c4c9944d0f06a2b9b5a [file] [log] [blame]
csharptestd965c662011-05-20 13:57:07 -05001#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;
36using System.Collections.Generic;
37using System.IO;
38using Google.ProtocolBuffers.Descriptors;
39using Google.ProtocolBuffers.TestProtos;
40using NUnit.Framework;
41
42namespace Google.ProtocolBuffers {
43 /// <summary>
44 /// Miscellaneous tests for message operations that apply to both
45 /// generated and dynamic messages.
46 /// </summary>
47 [TestFixture]
48 public class LiteTest {
49 [Test]
50 public void TestLite() {
51 // Since lite messages are a subset of regular messages, we can mostly
52 // assume that the functionality of lite messages is already thoroughly
53 // tested by the regular tests. All this test really verifies is that
54 // a proto with optimize_for = LITE_RUNTIME compiles correctly when
55 // linked only against the lite library. That is all tested at compile
56 // time, leaving not much to do in this method. Let's just do some random
57 // stuff to make sure the lite message is actually here and usable.
58
59 TestAllTypesLite message =
60 TestAllTypesLite.CreateBuilder()
61 .SetOptionalInt32(123)
62 .AddRepeatedString("hello")
63 .SetOptionalNestedMessage(
64 TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7))
65 .Build();
66
67 ByteString data = message.ToByteString();
68
69 TestAllTypesLite message2 = TestAllTypesLite.ParseFrom(data);
70
71 Assert.AreEqual(123, message2.OptionalInt32);
72 Assert.AreEqual(1, message2.RepeatedStringCount);
73 Assert.AreEqual("hello", message2.RepeatedStringList[0]);
74 Assert.AreEqual(7, message2.OptionalNestedMessage.Bb);
75 }
76
77 [Test]
78 public void TestLiteExtensions() {
79 // TODO(kenton): Unlike other features of the lite library, extensions are
80 // implemented completely differently from the regular library. We
81 // should probably test them more thoroughly.
82
83 TestAllExtensionsLite message =
84 TestAllExtensionsLite.CreateBuilder()
85 .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123)
86 .AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
87 .SetExtension(UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite,
88 TestAllTypesLite.Types.NestedEnum.BAZ)
89 .SetExtension(UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite,
90 TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7).Build())
91 .Build();
92
93 // Test copying a message, since coping extensions actually does use a
94 // different code path between lite and regular libraries, and as of this
95 // writing, parsing hasn't been implemented yet.
96 TestAllExtensionsLite message2 = message.ToBuilder().Build();
97
98 Assert.AreEqual(123, (int)message2.GetExtension(
99 UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
100 Assert.AreEqual(1, message2.GetExtensionCount(
101 UnitTestLiteProtoFile.RepeatedStringExtensionLite));
102 Assert.AreEqual(1, message2.GetExtension(
103 UnitTestLiteProtoFile.RepeatedStringExtensionLite).Count);
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 }
csharptest80824a52010-11-07 17:25:47 -0600112}