1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:45:42 +00:00

Add AsArray/AsEnumerable (#47)

This commit is contained in:
Oliver Booth 2022-04-20 23:14:16 +01:00
parent 1460e6b6c3
commit 3a58ed88c9
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 57 additions and 0 deletions

View File

@ -5,6 +5,18 @@ namespace X10D.Tests.Core;
[TestClass]
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]
[DataRow]
[DataRow(1)]

View File

@ -5,6 +5,18 @@ namespace X10D.Tests.Core;
[TestClass]
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]
[DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
public void Shuffled(params int[] source)

View 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;
}
}