From bbbd2d153608179bf26161cf239b654962014db7 Mon Sep 17 00:00:00 2001 From: poune05 Date: Sat, 20 Oct 2018 21:33:24 +0200 Subject: [PATCH] Add Prepend method and tests #56 --- ShittyLINQ/Prepend.cs | 21 +++++++++++++++++++++ ShittyLinqTests/PrependTests.cs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 ShittyLINQ/Prepend.cs create mode 100644 ShittyLinqTests/PrependTests.cs diff --git a/ShittyLINQ/Prepend.cs b/ShittyLINQ/Prepend.cs new file mode 100644 index 0000000..9d14c4b --- /dev/null +++ b/ShittyLINQ/Prepend.cs @@ -0,0 +1,21 @@ +namespace ShittyLINQ +{ + using System; + using System.Collections.Generic; + + public static partial class Extensions + { + public static IEnumerable Prepend(this IEnumerable source, T element) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + yield return element; + + foreach (var item in source) + { + yield return item; + } + } + } +} \ No newline at end of file diff --git a/ShittyLinqTests/PrependTests.cs b/ShittyLinqTests/PrependTests.cs new file mode 100644 index 0000000..d52942d --- /dev/null +++ b/ShittyLinqTests/PrependTests.cs @@ -0,0 +1,32 @@ +namespace ShittyTests +{ + using Microsoft.VisualStudio.TestTools.UnitTesting; + using ShittyLINQ; + using ShittyTests.TestHelpers; + + [TestClass] + public class PrependTests + { + [TestMethod] + public void Prepend_ReturnsExpected() + { + var source = new[] { 1, 2, 3, 4, 5 }; + var expectedResult = new[] { 0, 1, 2, 3, 4, 5 }; + + var result = source.Prepend(0); + + TestHelper.AssertCollectionsAreSame(expectedResult, result); + } + + [TestMethod] + public void Prepend_ReturnsExpectedWithEmptyCollection() + { + var source = new int[] { }; + var expectedResult = new int[] { 0 }; + + var result = source.Prepend(0); + + TestHelper.AssertCollectionsAreSame(expectedResult, result); + } + } +} \ No newline at end of file