Преглед на файлове

Adding tests and docs for IYielder.

Bart De Smet преди 10 години
родител
ревизия
80a1e2d857

+ 6 - 0
Ix.NET/Source/System.Interactive/EnumerableEx.Creation.cs

@@ -21,6 +21,12 @@ namespace System.Linq
         }
 
 #if HAS_AWAIT
+        /// <summary>
+        /// Creates an enumerable sequence based on an asynchronous method that provides a yielder.
+        /// </summary>
+        /// <typeparam name="T">Result sequence element type.</typeparam>
+        /// <param name="create">Delegate implementing an asynchronous method that can use the specified yielder to yield return values.</param>
+        /// <returns>Sequence that will use the asynchronous method to obtain its elements.</returns>
         public static IEnumerable<T> Create<T>(Action<IYielder<T>> create)
         {
             if (create == null)

+ 0 - 5
Ix.NET/Source/System.Interactive/IAwaitable.cs

@@ -1,11 +1,6 @@
 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
 #if HAS_AWAIT
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
 using System.Runtime.CompilerServices;
-using System.Threading;
-using System.Threading.Tasks;
 
 namespace System.Linq
 {

+ 15 - 4
Ix.NET/Source/System.Interactive/IYielder.cs

@@ -1,15 +1,26 @@
 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
-using System;
-using System.Collections.Generic;
-using System.Runtime.CompilerServices;
+#if HAS_AWAIT
 using System.Security;
 
-#if HAS_AWAIT
 namespace System.Linq
 {
+    /// <summary>
+    /// Interface for yielding elements to enumerator.
+    /// </summary>
+    /// <typeparam name="T">Type of the elements yielded to an enumerator.</typeparam>
     public interface IYielder<in T>
     {
+        /// <summary>
+        /// Yields a value to the enumerator.
+        /// </summary>
+        /// <param name="value">Value to yield return.</param>
+        /// <returns>Awaitable object for use in an asynchronous method.</returns>
         IAwaitable Return(T value);
+
+        /// <summary>
+        /// Stops the enumeration.
+        /// </summary>
+        /// <returns>Awaitable object for use in an asynchronous method.</returns>
         IAwaitable Break();
     }
 

+ 42 - 3
Ix.NET/Source/Tests/Tests.Creation.cs

@@ -46,20 +46,59 @@ namespace Tests
         {
             var xs = EnumerableEx.Create<int>(async yield => {
                 var i = 0;
-                while (i < 10) {
+                while (i < 10)
+                {
                     await yield.Return(i++);
                 }
             });
 
             int j = 0;
-            foreach (int elem in xs) {
+            foreach (int elem in xs)
+            {
+                Assert.AreEqual(elem, j);
+                j++;
+            }
+
+            Assert.AreEqual(j, 10);
+        }
+
+        [TestMethod]
+        public void CreateYieldBreak()
+        {
+            var xs = EnumerableEx.Create<int>(async yield => {
+                var i = 0;
+                while (true)
+                {
+                    if (i == 10)
+                    {
+                        await yield.Break();
+                        return;
+                    }
+
+                    await yield.Return(i++);
+                }
+            });
+
+            int j = 0;
+            foreach (int elem in xs)
+            {
                 Assert.AreEqual(elem, j);
                 j++;
             }
 
             Assert.AreEqual(j, 10);
         }
-#endif 
+
+        [TestMethod]
+        public void YielderNoReset()
+        {
+            var xs = EnumerableEx.Create<int>(async yield => {
+               await yield.Break();
+            });
+
+            AssertThrows<NotSupportedException>(() => xs.GetEnumerator().Reset());
+        }
+#endif
 
         private static IEnumerator<int> MyEnumerator()
         {