Skip to content

Commit

Permalink
Complete coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Aug 12, 2022
1 parent 2248bda commit 8d6f42b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
37 changes: 36 additions & 1 deletion RangeExtensions.Tests/RangeEnumerable.ICollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,47 @@ static int[] CopyCollection<TCollection>(int index, TCollection collection)
}
}

#if !NET48
[Theory, MemberData(nameof(ValidRangePairs))]
public void CopyToSpan_MatchesICollectionCopyTo(Range range, IEnumerable<int> enumerable)
{
var numbersArray = enumerable.ToArray();

static int[] CopyCollection<TCollection>(int index, TCollection collection)
where TCollection : ICollection<int>
{
var numbers = new int[collection.Count];

collection.CopyTo(numbers, index);

return numbers;
}

static int[] CopySpan(int index, RangeEnumerable rangeCollection)
{
var numbers = new int[rangeCollection.Count];

rangeCollection.CopyTo(numbers.AsSpan(), index);

return numbers;
}

foreach (var index in Data.Indexes(numbersArray))
{
AssertHelpers.EqualSequenceOrException(
() => CopyCollection(index, numbersArray),
() => CopySpan(index, range),
allowInherited: true);
}
}
#endif

[Fact]
public void Remove_ThrowsNotSupportedException()
{
static void Remove()
{
(..100).AsEnumerable().Remove(10);
_ = (..100).AsEnumerable().Remove(10);
}

Assert.Throws<NotSupportedException>(Remove);
Expand Down
14 changes: 14 additions & 0 deletions RangeExtensions.Tests/RangeEnumerable.IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ IEnumerable<int> EnumerateBoxed()

Assert.Equal(enumerable, EnumerateBoxed());
}

[Theory, MemberData(nameof(ValidRangePairs))]
public void RangeEnumerableBoxedGeneric_MatchesStandardEnumerableRange(Range range, IEnumerable<int> enumerable)
{
IEnumerable<int> EnumerateBoxed()
{
foreach (var i in (IEnumerable<int>)range.AsEnumerable())
{
yield return i;
}
}

Assert.Equal(enumerable, EnumerateBoxed());
}
}
38 changes: 32 additions & 6 deletions RangeExtensions.Tests/RangeEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ public partial class RangeEnumerableTests
public static IEnumerable<object[]> ValidRangePairs() => Data.ValidRangePairs();
public static IEnumerable<object[]> InvalidRanges() => Data.InvalidRanges();

[Theory]
[MemberData(nameof(ValidRangePairs))]
[Theory, MemberData(nameof(ValidRangePairs))]
public void RangeEnumerable_MatchesStandardEnumerableRange(Range range, IEnumerable<int> enumerable)
{
Assert.Equal(enumerable, range.AsEnumerable());
}

[Theory]
[MemberData(nameof(InvalidRanges))]
[Theory, MemberData(nameof(InvalidRanges))]
public void RangeEnumerable_ThrowsOnInvalidRange(Range range)
{
void AsEnumerable()
Expand All @@ -24,8 +22,7 @@ void AsEnumerable()
Assert.Throws<ArgumentOutOfRangeException>(AsEnumerable);
}

[Theory]
[MemberData(nameof(InvalidRanges))]
[Theory, MemberData(nameof(InvalidRanges))]
public void RangeEnumerator_ThrowsOnInvalidRange(Range range)
{
void Enumerate()
Expand All @@ -46,4 +43,33 @@ static void Reset()

Assert.Throws<NotSupportedException>(Reset);
}

[Theory, MemberData(nameof(ValidRangePairs))]
public void RangeEnumerable_ImplicitConversionToRange_KeepsRangeIntact(Range range, IEnumerable<int> enumerable)
{
_ = enumerable;
var rangeEnumerable = range.AsEnumerable();

Assert.Equal<Range>(range, rangeEnumerable);
}

[Theory, MemberData(nameof(ValidRangePairs))]
public void RangeEnumerable_ImplicitConversionFromRange_KeepsRangeIntact(Range range, IEnumerable<int> enumerable)
{
_ = enumerable;
var rangeEnumerable = range.AsEnumerable();

Assert.Equal<RangeEnumerable>(rangeEnumerable, range);
}

[Theory, MemberData(nameof(InvalidRanges))]
public void RangeEnumerable_ImplicitConverstionFromRange_ThrowsOnInvalidRange(Range range)
{
void ToRangeEnumerable()
{
RangeEnumerable _ = range;
}

Assert.Throws<ArgumentOutOfRangeException>(ToRangeEnumerable);
}
}
1 change: 0 additions & 1 deletion RangeExtensions.Tests/RangeEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ static void Sum()
Assert.Throws<OverflowException>(Sum);
}


[Theory]
[MemberData(nameof(ValidRangePairs))]
public void Take_MatchesIEnumerableTake(Range range, IEnumerable<int> enumerable)
Expand Down
2 changes: 1 addition & 1 deletion RangeExtensions.Tests/RangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void CopyToSpan_DoesNotRunPastRangeCount(Range range, IEnumerable<int> en
[Fact]
public void CopyToSpan_ThrowsOnRangeLongerThanSpan()
{
void CopyToSpan()
static void CopyToSpan()
{
var span = (stackalloc int[128]);

Expand Down

0 comments on commit 8d6f42b

Please sign in to comment.