blob: c49e0350f1ee20e5bf1a78ca02b6b3a28da6fb03 [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 Google.ProtocolBuffers;
40using Google.ProtocolBuffers.TestProtos;
41using NUnit.Framework;
42
csharptest71f662c2011-05-20 15:15:34 -050043namespace Google.ProtocolBuffers
44{
45 [TestFixture]
46 public class InteropLiteTest
47 {
48 [Test]
49 public void TestConvertFromFullMinimal()
50 {
51 TestInteropPerson person = TestInteropPerson.CreateBuilder()
52 .SetId(123)
53 .SetName("abc")
54 .Build();
55 Assert.IsTrue(person.IsInitialized);
csharptestd965c662011-05-20 13:57:07 -050056
csharptest71f662c2011-05-20 15:15:34 -050057 TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray());
csharptestd965c662011-05-20 13:57:07 -050058
csharptest71f662c2011-05-20 15:15:34 -050059 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
60 }
csharptestd965c662011-05-20 13:57:07 -050061
csharptest71f662c2011-05-20 15:15:34 -050062 [Test]
63 public void TestConvertFromFullComplete()
64 {
65 TestInteropPerson person = TestInteropPerson.CreateBuilder()
66 .SetId(123)
67 .SetName("abc")
68 .SetEmail("abc@123.com")
69 .AddRangeCodes(new[] {1, 2, 3})
70 .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
71 .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
72 .AddAddresses(
73 TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
74 SetState("NA").SetZip(12345).Build())
75 .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId,
76 TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
77 .Build();
78 Assert.IsTrue(person.IsInitialized);
79
80 ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
81 UnitTestExtrasLiteProtoFile.RegisterAllExtensions(registry);
82
83 TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray(), registry);
84
85 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
86 }
87
88 [Test]
89 public void TestConvertFromLiteMinimal()
90 {
91 TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
92 .SetId(123)
93 .SetName("abc")
94 .Build();
95 Assert.IsTrue(person.IsInitialized);
96
97 TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray());
98
99 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
100 }
101
102 [Test]
103 public void TestConvertFromLiteComplete()
104 {
105 TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
106 .SetId(123)
107 .SetName("abc")
108 .SetEmail("abc@123.com")
109 .AddRangeCodes(new[] {1, 2, 3})
110 .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
111 .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
112 .AddAddresses(
113 TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
114 .SetState("NA").SetZip(12345).Build())
115 .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
116 TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
117 .Build();
118 Assert.IsTrue(person.IsInitialized);
119
120 ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
121 UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
122
123 TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);
124
125 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
126 }
127
128 public ByteString AllBytes
129 {
130 get
131 {
132 byte[] bytes = new byte[256];
133 for (int i = 0; i < bytes.Length; i++)
134 bytes[i] = (byte) i;
135 return ByteString.CopyFrom(bytes);
136 }
137 }
138
139 [Test]
140 public void TestCompareStringValues()
141 {
142 TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
143 .SetId(123)
144 .SetName("abc")
145 .SetEmail("abc@123.com")
146 .AddRangeCodes(new[] {1, 2, 3})
147 .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
148 .AddPhone(
149 TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber(
150 System.Text.Encoding.ASCII.GetString(AllBytes.ToByteArray())).Build())
151 .AddAddresses(
152 TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
153 .SetState("NA").SetZip(12345).Build())
154 .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
155 TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
156 .Build();
157 Assert.IsTrue(person.IsInitialized);
158
159 ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
160 UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
161
162 TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);
163
164 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
165
166 TestInteropPerson.Builder copyBuilder = TestInteropPerson.CreateBuilder();
167 TextFormat.Merge(
168 person.ToString().Replace("[protobuf_unittest_extra.employee_id_lite]",
169 "[protobuf_unittest_extra.employee_id]"), registry, copyBuilder);
170
171 copy = copyBuilder.Build();
172 Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
173
174 string liteText = person.ToString().TrimEnd().Replace("\r", "");
175 string fullText = copy.ToString().TrimEnd().Replace("\r", "");
176 //map the extension type
177 liteText = liteText.Replace("[protobuf_unittest_extra.employee_id_lite]",
178 "[protobuf_unittest_extra.employee_id]");
179 //lite version does not indent
180 while (fullText.IndexOf("\n ") >= 0)
181 fullText = fullText.Replace("\n ", "\n");
182
183 Assert.AreEqual(fullText, liteText);
184 }
csharptestd965c662011-05-20 13:57:07 -0500185 }
csharptest71f662c2011-05-20 15:15:34 -0500186}