Skip to content

Add new SourceKind: Collection and List and use it for EndsWith tests. #960

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
50 changes: 50 additions & 0 deletions MoreLinq.Test/Collection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2023 Pierre Lando. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq.Test
{
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// This class is a wrapper around a <see cref="IList{T}"/> that only implement <see cref="ICollection{T}"/>.
/// </summary>
sealed class Collection<T> : ICollection<T>
{
readonly IList<T> _list;

public Collection(IList<T> list) => _list = list;

public int Count => _list.Count;

public bool IsReadOnly => false;

public void Add(T item) => _list.Add(item);

public void Clear() => _list.Clear();

public bool Contains(T item) => _list.Contains(item);

public void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex);

public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_list).GetEnumerator();

public bool Remove(T item) => _list.Remove(item);
}
}
84 changes: 42 additions & 42 deletions MoreLinq.Test/EndsWithTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,50 @@

namespace MoreLinq.Test
{
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;

[TestFixture]
[TestFixture(SourceKind.Sequence, SourceKind.Sequence)]
[TestFixture(SourceKind.Sequence, SourceKind.Collection)]
[TestFixture(SourceKind.Sequence, SourceKind.List)]
[TestFixture(SourceKind.Collection, SourceKind.Sequence)]
[TestFixture(SourceKind.Collection, SourceKind.Collection)]
[TestFixture(SourceKind.Collection, SourceKind.List)]
[TestFixture(SourceKind.List, SourceKind.Sequence)]
[TestFixture(SourceKind.List, SourceKind.Collection)]
[TestFixture(SourceKind.List, SourceKind.List)]
public class EndsWithTest
{
readonly SourceKind _firstSourceKind;
readonly SourceKind _secondSourceKind;

public EndsWithTest(SourceKind firstSourceKind, SourceKind secondSourceKind)
{
_firstSourceKind = firstSourceKind;
_secondSourceKind = secondSourceKind;
}

[TestCase(new int[0], new int[0], ExpectedResult = true)]
[TestCase(new int[0], new[] { 1, 2, 3 }, ExpectedResult = false)]
[TestCase(new[] { 1, 2, 3 }, new int[0], ExpectedResult = true)]
[TestCase(new[] { 1, 2, 3 }, new[] { 2, 3 }, ExpectedResult = true)]
[TestCase(new[] { 1, 2, 3 }, new[] { 1, 2, 3 }, ExpectedResult = true)]
[TestCase(new[] { 1, 2, 3 }, new[] { 0, 1, 2, 3 }, ExpectedResult = false)]
public bool EndsWithWithIntegers(IEnumerable<int> first, IEnumerable<int> second)
{
return first.EndsWith(second);
}

[TestCase(new[] { '1', '2', '3' }, new[] { '2', '3' }, ExpectedResult = true)]
[TestCase(new[] { '1', '2', '3' }, new[] { '1', '2', '3' }, ExpectedResult = true)]
[TestCase(new[] { '1', '2', '3' }, new[] { '0', '1', '2', '3' }, ExpectedResult = false)]
public bool EndsWithWithChars(IEnumerable<char> first, IEnumerable<char> second)
{
return first.EndsWith(second);
return first.ToSourceKind(_firstSourceKind).EndsWith(second.ToSourceKind(_secondSourceKind));
}

[TestCase("", "", ExpectedResult = true)]
[TestCase("", "1", ExpectedResult = false)]
[TestCase("1", "", ExpectedResult = true)]
[TestCase("123", "23", ExpectedResult = true)]
[TestCase("123", "123", ExpectedResult = true)]
[TestCase("123", "0123", ExpectedResult = false)]
public bool EndsWithWithStrings(string first, string second)
{
// Conflict with String.EndsWith(), which has precedence in this case
return MoreEnumerable.EndsWith(first, second);
}

[Test]
public void EndsWithReturnsTrueIfBothEmpty()
{
Assert.That(new int[0].EndsWith(new int[0]), Is.True);
}

[Test]
public void EndsWithReturnsFalseIfOnlyFirstIsEmpty()
{
Assert.That(new int[0].EndsWith(new[] { 1, 2, 3 }), Is.False);
}

[TestCase("", "", ExpectedResult = true)]
[TestCase("1", "", ExpectedResult = true)]
public bool EndsWithReturnsTrueIfSecondIsEmpty(string first, string second)
{
// Conflict with String.EndsWith(), which has precedence in this case
return MoreEnumerable.EndsWith(first, second);
return first.ToSourceKind(_firstSourceKind).EndsWith(second.ToSourceKind(_secondSourceKind));
}

[Test]
Expand All @@ -78,17 +72,23 @@ public void EndsWithDisposesBothSequenceEnumerators()
_ = first.EndsWith(second);
}

[Test]
[SuppressMessage("ReSharper", "RedundantArgumentDefaultValue")]
public void EndsWithUsesSpecifiedEqualityComparerOrDefault()
[TestCaseSource(nameof(EndsWithUsesSpecifiedEqualityComparerOrDefaultTestCases))]
public bool EndsWithUsesSpecifiedEqualityComparerOrDefault(IEqualityComparer<int>? equalityComparer)
{
var first = new[] { 1, 2, 3 };
var second = new[] { 4, 5, 6 };
var first = new[] { 1, 2, 3 }.ToSourceKind(_firstSourceKind);
var second = new[] { 4, 5, 6 }.ToSourceKind(_secondSourceKind);

Assert.That(first.EndsWith(second), Is.False);
Assert.That(first.EndsWith(second, null), Is.False);
Assert.That(first.EndsWith(second, EqualityComparer.Create<int>(delegate { return false; })), Is.False);
Assert.That(first.EndsWith(second, EqualityComparer.Create<int>(delegate { return true; })), Is.True);
return first.EndsWith(second, equalityComparer);
}

public static IEnumerable EndsWithUsesSpecifiedEqualityComparerOrDefaultTestCases
{
get
{
yield return new TestCaseData(null).Returns(false);
yield return new TestCaseData(EqualityComparer.Create<int>(delegate { return false; })).Returns(false);
yield return new TestCaseData(EqualityComparer.Create<int>(delegate { return true; })).Returns(true);
}
}

[TestCase(SourceKind.BreakingCollection)]
Expand Down
4 changes: 4 additions & 0 deletions MoreLinq.Test/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace MoreLinq.Test
public enum SourceKind
{
Sequence,
Collection,
List,
BreakingList,
BreakingReadOnlyList,
BreakingCollection,
Expand Down Expand Up @@ -91,6 +93,8 @@ internal static IEnumerable<T> ToSourceKind<T>(this IEnumerable<T> input, Source
sourceKind switch
{
SourceKind.Sequence => input.Select(x => x),
SourceKind.Collection => new Collection<T>(input.ToList()),
SourceKind.List => input.ToList(),
SourceKind.BreakingList => new BreakingList<T>(input.ToList()),
SourceKind.BreakingReadOnlyList => new BreakingReadOnlyList<T>(input.ToList()),
SourceKind.BreakingCollection => new BreakingCollection<T>(input.ToList()),
Expand Down