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

Adds Last() method and tests. #81

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
49 changes: 49 additions & 0 deletions ShittyLINQ/Last.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ShittyLINQ
{
public static partial class Extensions
{
public static T Last<T>(this IEnumerable<T> self)
{
if (self == null) throw new ArgumentNullException(nameof(self));
var iterator = self.GetEnumerator();
if (iterator.MoveNext())
{
T result;
do
{
result = iterator.Current;
}
while (iterator.MoveNext());
return result;
}
throw new InvalidOperationException("The source sequence is empty.");
}

public static T Last<T>(this IEnumerable<T> self, Func<T, bool> predicate)
{
if (self == null || predicate == null) throw new ArgumentNullException(nameof(self));

if (self.Count() == 0) throw new InvalidOperationException("The source sequence is empty.");

self = self.Where(predicate);

var iterator = self.GetEnumerator();
if (iterator.MoveNext())
{
T result;
do
{
result = iterator.Current;
}
while (iterator.MoveNext());
return result;
}

throw new InvalidOperationException("No matching element in source sequence.");
}
}
}
91 changes: 91 additions & 0 deletions ShittyLinqTests/LastTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShittyLINQ;
using System;

namespace ShittyTests
{
[TestClass]
public class LastTests
{
[TestMethod]
public void Last_GetLastNumber()
{
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
int expectedResult = 5;

int result = numbers.Last();

Assert.AreEqual(expectedResult, result);
}

[TestMethod]
public void Last_GetLastNumberMatchingPredicate()
{
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
int expectedResult = 3;
bool predicate(int i) => i == 3;

int result = numbers.Last(predicate);

Assert.AreEqual(expectedResult, result);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Last_SourceIsNull()
{
int[] numbers = null;

int result = numbers.Last();
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Last_SourceIsNullWithPredicate()
{
int[] numbers = null;
bool predicate(int i) => i == 3;

int result = numbers.Last(predicate);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Last_PredicateIsNull()
{
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Func<int, bool> predicate = null;

int result = numbers.Last(predicate);
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Last_SourceIsEmpty()
{
int[] numbers = new int[] { };

int result = numbers.Last();
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Last_SourceIsEmptyWithPredicate()
{
int[] numbers = new int[] { };
bool predicate(int i) => i == 3;

int result = numbers.Last(predicate);
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Last_NoElementMatchesPredicate()
{
int[] numbers = new int[] { 1, 2, 4, 5 };
bool predicate(int i) => i == 3;

int result = numbers.Last(predicate);
}
}
}