mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 22:55:42 +00:00
Add AsArray/AsEnumerable (#47)
This commit is contained in:
parent
1460e6b6c3
commit
3a58ed88c9
@ -5,6 +5,18 @@ namespace X10D.Tests.Core;
|
|||||||
[TestClass]
|
[TestClass]
|
||||||
public class ArrayTests
|
public class ArrayTests
|
||||||
{
|
{
|
||||||
|
[TestMethod]
|
||||||
|
[DataRow(1)]
|
||||||
|
[DataRow("f")]
|
||||||
|
[DataRow(true)]
|
||||||
|
public void AsArray(object o)
|
||||||
|
{
|
||||||
|
object[] array = o.AsArray();
|
||||||
|
Assert.IsNotNull(array);
|
||||||
|
Assert.IsTrue(array.Length == 1);
|
||||||
|
Assert.AreEqual(o, array[0]);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[DataRow]
|
[DataRow]
|
||||||
[DataRow(1)]
|
[DataRow(1)]
|
||||||
|
@ -5,6 +5,18 @@ namespace X10D.Tests.Core;
|
|||||||
[TestClass]
|
[TestClass]
|
||||||
public partial class EnumerableTests
|
public partial class EnumerableTests
|
||||||
{
|
{
|
||||||
|
[TestMethod]
|
||||||
|
[DataRow(1)]
|
||||||
|
[DataRow("f")]
|
||||||
|
[DataRow(true)]
|
||||||
|
public void AsEnumerable(object o)
|
||||||
|
{
|
||||||
|
IEnumerable<object> array = o.AsEnumerable().ToArray(); // prevent multiple enumeration of IEnumerable
|
||||||
|
Assert.IsNotNull(array);
|
||||||
|
Assert.IsTrue(array.Count() == 1);
|
||||||
|
Assert.AreEqual(o, array.ElementAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
|
[DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
|
||||||
public void Shuffled(params int[] source)
|
public void Shuffled(params int[] source)
|
||||||
|
33
X10D/src/Extensions/Extensions.cs
Normal file
33
X10D/src/Extensions/Extensions.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
namespace X10D;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods which apply to all types.
|
||||||
|
/// </summary>
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Returns an array containing the specified value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to encapsulate.</param>
|
||||||
|
/// <typeparam name="T">The value type.</typeparam>
|
||||||
|
/// <returns>
|
||||||
|
/// An array of type <typeparamref name="T" /> with length 1, whose only element is <paramref name="value" />.
|
||||||
|
/// </returns>
|
||||||
|
public static T[] AsArray<T>(this T value)
|
||||||
|
{
|
||||||
|
return new[] {value};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns an enumerable collection containing the specified value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to encapsulate.</param>
|
||||||
|
/// <typeparam name="T">The value type.</typeparam>
|
||||||
|
/// <returns>
|
||||||
|
/// An enumerable collection of type <typeparamref name="T" />, whose only element is <paramref name="value" />.
|
||||||
|
/// </returns>
|
||||||
|
public static IEnumerable<T> AsEnumerable<T>(this T value)
|
||||||
|
{
|
||||||
|
yield return value;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user