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