blob: 9ef5cc84945caa3231f8031c76158d4040645e5f [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
csharptestd965c662011-05-20 13:57:07 -05003// 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.
csharptest71f662c2011-05-20 15:15:34 -050034
csharptestd965c662011-05-20 13:57:07 -050035#endregion
36
37using System;
38using System.Collections.Generic;
39using System.IO;
40using Google.ProtocolBuffers.Descriptors;
41using Google.ProtocolBuffers.TestProtos;
csharptesteac64a52011-10-04 13:43:26 -050042using Microsoft.VisualStudio.TestTools.UnitTesting;
csharptestd965c662011-05-20 13:57:07 -050043
csharptest71f662c2011-05-20 15:15:34 -050044namespace Google.ProtocolBuffers
45{
46 /// <summary>
47 /// Miscellaneous tests for message operations that apply to both
48 /// generated and dynamic messages.
49 /// </summary>
csharptesteac64a52011-10-04 13:43:26 -050050 [TestClass]
csharptest71f662c2011-05-20 15:15:34 -050051 public class LiteTest
52 {
csharptesteac64a52011-10-04 13:43:26 -050053 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050054 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.
csharptestd965c662011-05-20 13:57:07 -050063
csharptest71f662c2011-05-20 15:15:34 -050064 TestAllTypesLite message =
65 TestAllTypesLite.CreateBuilder()
66 .SetOptionalInt32(123)
67 .AddRepeatedString("hello")
68 .SetOptionalNestedMessage(
69 TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7))
70 .Build();
csharptestd965c662011-05-20 13:57:07 -050071
csharptest71f662c2011-05-20 15:15:34 -050072 ByteString data = message.ToByteString();
csharptestd965c662011-05-20 13:57:07 -050073
csharptest71f662c2011-05-20 15:15:34 -050074 TestAllTypesLite message2 = TestAllTypesLite.ParseFrom(data);
csharptestd965c662011-05-20 13:57:07 -050075
csharptest71f662c2011-05-20 15:15:34 -050076 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
csharptesteac64a52011-10-04 13:43:26 -050082 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050083 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 Skeetce66c5f2015-04-28 15:06:59 +010091 .SetExtension(UnittestLite.OptionalInt32ExtensionLite, 123)
92 .AddExtension(UnittestLite.RepeatedStringExtensionLite, "hello")
93 .SetExtension(UnittestLite.OptionalNestedEnumExtensionLite,
csharptest71f662c2011-05-20 15:15:34 -050094 TestAllTypesLite.Types.NestedEnum.BAZ)
Jon Skeetce66c5f2015-04-28 15:06:59 +010095 .SetExtension(UnittestLite.OptionalNestedMessageExtensionLite,
csharptest71f662c2011-05-20 15:15:34 -050096 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 Skeetce66c5f2015-04-28 15:06:59 +0100105 UnittestLite.OptionalInt32ExtensionLite));
csharptest71f662c2011-05-20 15:15:34 -0500106 Assert.AreEqual(1, message2.GetExtensionCount(
Jon Skeetce66c5f2015-04-28 15:06:59 +0100107 UnittestLite.RepeatedStringExtensionLite));
csharptest71f662c2011-05-20 15:15:34 -0500108 Assert.AreEqual(1, message2.GetExtension(
Jon Skeetce66c5f2015-04-28 15:06:59 +0100109 UnittestLite.RepeatedStringExtensionLite).Count);
csharptest71f662c2011-05-20 15:15:34 -0500110 Assert.AreEqual("hello", message2.GetExtension(
Jon Skeetce66c5f2015-04-28 15:06:59 +0100111 UnittestLite.RepeatedStringExtensionLite, 0));
csharptest71f662c2011-05-20 15:15:34 -0500112 Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension(
Jon Skeetce66c5f2015-04-28 15:06:59 +0100113 UnittestLite.OptionalNestedEnumExtensionLite));
csharptest71f662c2011-05-20 15:15:34 -0500114 Assert.AreEqual(7, message2.GetExtension(
Jon Skeetce66c5f2015-04-28 15:06:59 +0100115 UnittestLite.OptionalNestedMessageExtensionLite).Bb);
csharptest71f662c2011-05-20 15:15:34 -0500116 }
csharptestd965c662011-05-20 13:57:07 -0500117 }
csharptest80824a52010-11-07 17:25:47 -0600118}