diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2015-04-16 09:55:22 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@google.com> | 2015-04-16 09:55:22 -0700 |
commit | 84fa3e2bbc876428efd0c99700f146115a9d5162 (patch) | |
tree | 47baa5dafe3c3ab1c49fe2ca44e9f3ffd0e0b127 /csharp/src/ProtocolBuffers.Test/GeneratedBuilderTest.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 836a273544f55c88d8d0988ef0117cb0636707c6 (diff) | |
download | protobuf-84fa3e2bbc876428efd0c99700f146115a9d5162.tar.gz protobuf-84fa3e2bbc876428efd0c99700f146115a9d5162.tar.bz2 protobuf-84fa3e2bbc876428efd0c99700f146115a9d5162.zip |
Imported protobuf-csharp-port into csharp/ tree
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)
+ );
+ }
+ }
+}
|