diff --git a/ShittyLINQ/Single.cs b/ShittyLINQ/Single.cs new file mode 100644 index 0000000..6466863 --- /dev/null +++ b/ShittyLINQ/Single.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static T Single(this IEnumerable self) + { + if (self == null) throw new ArgumentNullException(); + using (IEnumerator e = self.GetEnumerator()) + { + if (!e.MoveNext()) + { + throw new InvalidOperationException(); + } + + T result = e.Current; + if (!e.MoveNext()) + { + return result; + } + throw new InvalidOperationException(); + } + } + + public static T Single(this IEnumerable self, Func predicate) + { + if (self == null || predicate == null) throw new ArgumentNullException(); + + return self.Where(predicate).Single(); + } + } +} diff --git a/ShittyLinqTests/SingleTests.cs b/ShittyLinqTests/SingleTests.cs new file mode 100644 index 0000000..7efbd85 --- /dev/null +++ b/ShittyLinqTests/SingleTests.cs @@ -0,0 +1,101 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyTests +{ + [TestClass] + public class SingleTests + { + [TestMethod] + public void Single_GetSingleElement() + { + int[] numbers = new int[] { 1 }; + int expectedResult = 1; + + int result = numbers.Single(); + + Assert.AreEqual(expectedResult, result); + } + + [TestMethod] + public void Single_GetMatchingSingleElement() + { + int[] numbers = new int[] { 1, 2 }; + bool predicate(int i) => i == 1; + int expectedResult = 1; + + int result = numbers.Single(predicate); + + Assert.AreEqual(expectedResult, result); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Single_SourceIsNull() + { + int[] numbers = null; + + int result = numbers.Single(); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Single_PredicateIsNull() + { + int[] numbers = new int[] { 1 }; + Func predicate = null; + + int result = numbers.Single(predicate); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void Single_SourceIsEmpty() + { + int[] numbers = new int[] { }; + + int result = numbers.Single(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void Single_SourceIsEmptyWithPredicate() + { + int[] numbers = new int[] { }; + bool predicate(int i) => i == 1; + + int result = numbers.Single(predicate); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void Single_SourceHasMoreThanOneElement() + { + int[] numbers = new int[] { 1, 2 }; + int result = numbers.Single(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void Single_MoreThanOneMatchingElement() + { + int[] numbers = new int[] { 1, 2, 1 }; + bool predicate(int i) => i == 1; + + int result = numbers.Single(predicate); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void Single_NoMatchingElement() + { + int[] numbers = new int[] { 1, 2, 3 }; + bool predicate(int i) => i == 4; + + int result = numbers.Single(predicate); + } + } +}