Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ShittyLINQ/Cast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace ShittyLINQ
{
using System;
using System.Collections;
using System.Collections.Generic;

public static partial class Extensions
{
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));

if (source is IEnumerable<TResult> results)
{
foreach (var result in results)
yield return result;
}

foreach (TResult result in source)
yield return result;
}
}
}
52 changes: 52 additions & 0 deletions ShittyLinqTests/CastTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace ShittyTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShittyLINQ;
using ShittyTests.TestHelpers;
using System;
using System.Collections;
using System.Collections.Generic;

[TestClass]
public class CastTests
{
[TestMethod]
public void Cast_ReturnsExpected()
{
var source = new ArrayList { "1", "2", "3" };
var expectedResult = new List<string> { "1", "2", "3" };


var result = source.Cast<string>();

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}

[TestMethod]
public void Cast_ReturnsExpectedWithEmptyCollection()
{
var source = new ArrayList();
var expectedResult = new List<string>();

var result = source.Cast<string>();

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Cast_CollectionIsNull()
{
((ArrayList)null).Cast<string>().ToList();
}

[TestMethod]
[ExpectedException(typeof(InvalidCastException))]
public void Cast_Invalid()
{
var source = new ArrayList { "1", "2", "3" };

source.Cast<char>().ToList();
}
}
}