1 using Google.Protobuf.TestProtos.Proto2; 2 using NUnit.Framework; 3 4 using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions; 5 6 namespace Google.Protobuf 7 { 8 public class ExtensionSetTest 9 { 10 [Test] EmptyExtensionSet()11 public void EmptyExtensionSet() 12 { 13 ExtensionSet<TestAllExtensions> extensions = new ExtensionSet<TestAllExtensions>(); 14 Assert.AreEqual(0, extensions.CalculateSize()); 15 } 16 17 [Test] MergeExtensionSet()18 public void MergeExtensionSet() 19 { 20 ExtensionSet<TestAllExtensions> extensions = null; 21 ExtensionSet.Set(ref extensions, OptionalBoolExtension, true); 22 23 ExtensionSet<TestAllExtensions> other = null; 24 25 Assert.IsFalse(ExtensionSet.Has(ref other, OptionalBoolExtension)); 26 ExtensionSet.MergeFrom(ref other, extensions); 27 Assert.IsTrue(ExtensionSet.Has(ref other, OptionalBoolExtension)); 28 } 29 30 [Test] TestMergeCodedInput()31 public void TestMergeCodedInput() 32 { 33 var message = new TestAllExtensions(); 34 message.SetExtension(OptionalBoolExtension, true); 35 var serialized = message.ToByteArray(); 36 37 MessageParsingHelpers.AssertWritingMessage(message); 38 39 MessageParsingHelpers.AssertReadingMessage( 40 TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { OptionalBoolExtension }), 41 serialized, 42 other => 43 { 44 Assert.AreEqual(message, other); 45 Assert.AreEqual(message.CalculateSize(), other.CalculateSize()); 46 }); 47 } 48 49 [Test] TestMergeMessage()50 public void TestMergeMessage() 51 { 52 var message = new TestAllExtensions(); 53 message.SetExtension(OptionalBoolExtension, true); 54 55 var other = new TestAllExtensions(); 56 57 Assert.AreNotEqual(message, other); 58 Assert.AreNotEqual(message.CalculateSize(), other.CalculateSize()); 59 60 other.MergeFrom(message); 61 62 Assert.AreEqual(message, other); 63 Assert.AreEqual(message.CalculateSize(), other.CalculateSize()); 64 } 65 66 [Test] TryMergeFieldFrom_CodedInputStream()67 public void TryMergeFieldFrom_CodedInputStream() 68 { 69 var message = new TestAllExtensions(); 70 message.SetExtension(OptionalStringExtension, "abcd"); 71 72 var input = new CodedInputStream(message.ToByteArray()); 73 input.ExtensionRegistry = new ExtensionRegistry() { OptionalStringExtension }; 74 input.ReadTag(); // TryMergeFieldFrom expects that a tag was just read and will inspect the LastTag value 75 76 ExtensionSet<TestAllExtensions> extensionSet = null; 77 // test the legacy overload of TryMergeFieldFrom that takes a CodedInputStream 78 Assert.IsTrue(ExtensionSet.TryMergeFieldFrom(ref extensionSet, input)); 79 Assert.AreEqual("abcd", ExtensionSet.Get(ref extensionSet, OptionalStringExtension)); 80 } 81 82 [Test] TestEquals()83 public void TestEquals() 84 { 85 var message = new TestAllExtensions(); 86 message.SetExtension(OptionalBoolExtension, true); 87 88 var other = new TestAllExtensions(); 89 90 Assert.AreNotEqual(message, other); 91 Assert.AreNotEqual(message.CalculateSize(), other.CalculateSize()); 92 93 other.SetExtension(OptionalBoolExtension, true); 94 95 Assert.AreEqual(message, other); 96 Assert.AreEqual(message.CalculateSize(), other.CalculateSize()); 97 } 98 99 [Test] TestHashCode()100 public void TestHashCode() 101 { 102 var message = new TestAllExtensions(); 103 var hashCode = message.GetHashCode(); 104 105 message.SetExtension(OptionalBoolExtension, true); 106 107 Assert.AreNotEqual(hashCode, message.GetHashCode()); 108 } 109 110 [Test] TestClone()111 public void TestClone() 112 { 113 var message = new TestAllExtensions(); 114 message.SetExtension(OptionalBoolExtension, true); 115 116 var other = message.Clone(); 117 118 Assert.AreEqual(message, other); 119 Assert.AreEqual(message.CalculateSize(), message.CalculateSize()); 120 } 121 } 122 } 123