diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
commit | 01c8c35ca3d3424a9473dd22ea2456933b3e8d36 (patch) | |
tree | de582cb9ef3e449f24015dec09f80e998806b26a /csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 1eab56aee8dddcb656db67d9fb87a8cc6a2ac112 (diff) | |
download | protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.gz protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.bz2 protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.zip |
Merge pull request #2 from jtattermusch/csharp_protobuf_imported
Pulling protobuf-csharp-port into csharp/ directory
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs new file mode 100644 index 00000000..1dcb1c21 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs @@ -0,0 +1,125 @@ +using System;
+using System.Collections.Generic;
+using System.Text;
+using Google.ProtocolBuffers.TestProtos;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Google.ProtocolBuffers
+{
+ [TestClass]
+ public class GeneratedBuilderTest
+ {
+ class OneTimeEnumerator<T> : IEnumerable<T>
+ {
+ readonly T _item;
+ bool _enumerated;
+ public OneTimeEnumerator(T item)
+ {
+ _item = item;
+ }
+ public IEnumerator<T> GetEnumerator()
+ {
+ Assert.IsFalse(_enumerated, "The collection {0} has already been enumerated", GetType());
+ _enumerated = true;
+ yield return _item;
+ }
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ { return GetEnumerator(); }
+ }
+
+ [TestMethod]
+ public void DoesNotEnumerateTwiceForMessageList()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRangeRepeatedForeignMessage(
+ new OneTimeEnumerator<ForeignMessage>(
+ ForeignMessage.DefaultInstance));
+ }
+ [TestMethod]
+ public void DoesNotEnumerateTwiceForPrimitiveList()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRangeRepeatedInt32(new OneTimeEnumerator<int>(1));
+ }
+ [TestMethod]
+ public void DoesNotEnumerateTwiceForStringList()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRangeRepeatedString(new OneTimeEnumerator<string>("test"));
+ }
+ [TestMethod]
+ public void DoesNotEnumerateTwiceForEnumList()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRangeRepeatedForeignEnum(new OneTimeEnumerator<ForeignEnum>(ForeignEnum.FOREIGN_BAR));
+ }
+
+ private delegate void TestMethod();
+
+ private static void AssertThrows<T>(TestMethod method) where T : Exception
+ {
+ try
+ {
+ method();
+ }
+ catch (Exception error)
+ {
+ if (error is T)
+ return;
+ throw;
+ }
+ Assert.Fail("Expected exception of type " + typeof(T));
+ }
+
+ [TestMethod]
+ public void DoesNotAddNullToMessageListByAddRange()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ AssertThrows<ArgumentNullException>(
+ () => b.AddRangeRepeatedForeignMessage(new ForeignMessage[] { null })
+ );
+ }
+ [TestMethod]
+ public void DoesNotAddNullToMessageListByAdd()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ AssertThrows<ArgumentNullException>(
+ () => b.AddRepeatedForeignMessage((ForeignMessage)null)
+ );
+ }
+ [TestMethod]
+ public void DoesNotAddNullToMessageListBySet()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRepeatedForeignMessage(ForeignMessage.DefaultInstance);
+ AssertThrows<ArgumentNullException>(
+ () => b.SetRepeatedForeignMessage(0, (ForeignMessage)null)
+ );
+ }
+ [TestMethod]
+ public void DoesNotAddNullToStringListByAddRange()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ AssertThrows<ArgumentNullException>(
+ () => b.AddRangeRepeatedString(new String[] { null })
+ );
+ }
+ [TestMethod]
+ public void DoesNotAddNullToStringListByAdd()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ AssertThrows<ArgumentNullException>(
+ () => b.AddRepeatedString(null)
+ );
+ }
+ [TestMethod]
+ public void DoesNotAddNullToStringListBySet()
+ {
+ TestAllTypes.Builder b = new TestAllTypes.Builder();
+ b.AddRepeatedString("one");
+ AssertThrows<ArgumentNullException>(
+ () => b.SetRepeatedString(0, null)
+ );
+ }
+ }
+}
|