Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Single method and tests. #82

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions ShittyLINQ/Single.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ShittyLINQ
{
public static partial class Extensions
{
public static T Single<T>(this IEnumerable<T> self)
{
if (self == null) throw new ArgumentNullException();
using (IEnumerator<T> 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<T>(this IEnumerable<T> self, Func<T, bool> predicate)
{
if (self == null || predicate == null) throw new ArgumentNullException();

return self.Where(predicate).Single();
}
}
}
101 changes: 101 additions & 0 deletions ShittyLinqTests/SingleTests.cs
Original file line number Diff line number Diff line change
@@ -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<int, bool> 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);
}
}
}