Selaa lähdekoodia

Add test for clearing fields with reflection.

Jon Skeet 11 vuotta sitten
vanhempi
commit
f34759e05c

+ 10 - 0
src/ProtocolBuffers.Test/GeneratedMessageTest.cs

@@ -264,6 +264,16 @@ namespace Google.ProtocolBuffers
             TestUtil.AssertAllFieldsSet(message);
         }
 
+        [TestMethod]
+        public void ReflectionClear()
+        {
+            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
+            reflectionTester.SetAllFieldsViaReflection(builder);
+            reflectionTester.ClearAllFieldsViaReflection(builder);
+            TestAllTypes message = builder.Build();
+            TestUtil.AssertClear(message);
+        }
+
         [TestMethod]
         public void ReflectionSettersRejectNull()
         {

+ 12 - 0
src/ProtocolBuffers.Test/ReflectionTester.cs

@@ -384,6 +384,18 @@ namespace Google.ProtocolBuffers
             message[f("default_cord")] = "425";
         }
 
+        /// <summary>
+        /// Clears every field of the message, using the reflection interface.
+        /// </summary>
+        /// <param name="message"></param>
+        internal void ClearAllFieldsViaReflection(IBuilder message)
+        {
+            foreach (FieldDescriptor field in message.AllFields.Keys)
+            {
+                message.WeakClearField(field);
+            }
+        }
+
         // -------------------------------------------------------------------
 
         /// <summary>