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 Google.ProtocolBuffers;
|
| 40 | using Google.ProtocolBuffers.TestProtos;
|
| 41 | using NUnit.Framework;
|
| 42 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 43 | namespace 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);
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 56 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 57 | TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 58 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 59 | Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
|
| 60 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 61 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 62 | [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 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 185 | }
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 186 | } |