From 5677da0b4253323f6af2309e869f570274bc2fe5 Mon Sep 17 00:00:00 2001 From: jw Date: Mon, 1 Oct 2018 14:08:20 -0500 Subject: [PATCH 1/4] DefaultEmpty added --- ShittyLINQ/DefaultIfEmpty.cs | 31 +++++++++++++++++++ ShittyLinqTests/DefaultIfEmptyTests.cs | 43 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 ShittyLINQ/DefaultIfEmpty.cs create mode 100644 ShittyLinqTests/DefaultIfEmptyTests.cs diff --git a/ShittyLINQ/DefaultIfEmpty.cs b/ShittyLINQ/DefaultIfEmpty.cs new file mode 100644 index 0000000..efe409d --- /dev/null +++ b/ShittyLINQ/DefaultIfEmpty.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static IEnumerable DefaultIfEmpty(this IEnumerable source) + { + return DefaultIfEmpty(source, default(T)); + } + + public static IEnumerable DefaultIfEmpty(this IEnumerable self, T defaultValue) + { + if (defaultValue == null) throw new ArgumentNullException(); + if (self == null || self.Count() <= 0) + { + yield return defaultValue; + } + else + { + var iterator = self.GetEnumerator(); + while (iterator.MoveNext()) + { + yield return iterator.Current; + } + } + } + } +} diff --git a/ShittyLinqTests/DefaultIfEmptyTests.cs b/ShittyLinqTests/DefaultIfEmptyTests.cs new file mode 100644 index 0000000..92cdbbd --- /dev/null +++ b/ShittyLinqTests/DefaultIfEmptyTests.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using Enumerable = System.Linq.Enumerable; + +namespace ShittyTests +{ + [TestClass] + public class DefaultIfEmptyTests + { + [TestMethod] + public void Default_NotEmpty() + { + int[] source = { 1, 2, 3 }; + int[] expectedResult = { 1, 2, 3 }; + + var result = source.DefaultIfEmpty(0); + Assert.IsTrue(Enumerable.SequenceEqual(expectedResult, result)); + } + + [TestMethod] + public void Default_Empty() + { + int[] source = { }; + int[] expectedResult = { 0 }; + + var result = source.DefaultIfEmpty(0); + Assert.IsTrue(Enumerable.SequenceEqual(expectedResult, result)); + } + + [TestMethod] + public void Default_EnumerableNull() + { + int[] source = null; + int[] expectedResult = { 0 }; + + var result = source.DefaultIfEmpty(0); + Assert.IsTrue(Enumerable.SequenceEqual(expectedResult, result)); + } + } +} From f8dbd13bdf3604927c73a77b976cc19d19b65b44 Mon Sep 17 00:00:00 2001 From: IcecreamBurglar Date: Mon, 1 Oct 2018 14:15:07 -0500 Subject: [PATCH 2/4] Added in undocumented exceptions The way this method works in Linq appears to be like this after playing around with the Linq implementation. Exceptions for this extension aren't documented. --- ShittyLINQ/DefaultIfEmpty.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ShittyLINQ/DefaultIfEmpty.cs b/ShittyLINQ/DefaultIfEmpty.cs index efe409d..930af39 100644 --- a/ShittyLINQ/DefaultIfEmpty.cs +++ b/ShittyLINQ/DefaultIfEmpty.cs @@ -13,8 +13,8 @@ public static IEnumerable DefaultIfEmpty(this IEnumerable source) public static IEnumerable DefaultIfEmpty(this IEnumerable self, T defaultValue) { - if (defaultValue == null) throw new ArgumentNullException(); - if (self == null || self.Count() <= 0) + if (self == null) throw new ArgumentNullException(); + if (self.Count() <= 0) { yield return defaultValue; } From e633ad2d58e02834198919b3c5fd0edb8ad553ec Mon Sep 17 00:00:00 2001 From: IcecreamBurglar Date: Thu, 4 Oct 2018 00:58:36 -0500 Subject: [PATCH 3/4] Removed unused using --- ShittyLINQ/DefaultIfEmpty.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ShittyLINQ/DefaultIfEmpty.cs b/ShittyLINQ/DefaultIfEmpty.cs index 930af39..f7fac4d 100644 --- a/ShittyLINQ/DefaultIfEmpty.cs +++ b/ShittyLINQ/DefaultIfEmpty.cs @@ -1,6 +1,5 @@ -using System; +using System; using System.Collections.Generic; -using System.Text; namespace ShittyLINQ { From 51983892cc7860ff53d4ddf6350d2fc8e1ecbc7c Mon Sep 17 00:00:00 2001 From: IcecreamBurglar Date: Thu, 4 Oct 2018 01:05:46 -0500 Subject: [PATCH 4/4] Update DefaultIfEmptyTests.cs --- ShittyLinqTests/DefaultIfEmptyTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ShittyLinqTests/DefaultIfEmptyTests.cs b/ShittyLinqTests/DefaultIfEmptyTests.cs index 92cdbbd..039356c 100644 --- a/ShittyLinqTests/DefaultIfEmptyTests.cs +++ b/ShittyLinqTests/DefaultIfEmptyTests.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using ShittyLINQ; using Enumerable = System.Linq.Enumerable;