Skip to content

Commit

Permalink
Fix exception thrown for invalid "Pad"/"PadStart" width
Browse files Browse the repository at this point in the history
This is a squashed merge of PR #1030 that:

- fixes #1027
- fixes #1028
  • Loading branch information
atifaziz authored Oct 28, 2023
1 parent 0209897 commit 092a40d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
35 changes: 16 additions & 19 deletions MoreLinq.Test/PadStartTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@

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

[TestFixture]
public class PadStartTest
{
// PadStart(source, width)
static readonly IEnumerable<ITestCaseData> PadStartWithNegativeWidthCases =
from e in new (string Name, TestDelegate Delegate)[]
{
("DefaultPadding" , static () => new object[0].PadStart(-1)),
("Padding" , static () => new object[0].PadStart(-1, -2)),
("PaddingSelector", static () => new object[0].PadStart(-1, BreakingFunc.Of<int, int>())),
}
select new TestCaseData(e.Delegate).SetName(e.Name);

[Test]
public void PadStartWithNegativeWidth()
[TestCaseSource(nameof(PadStartWithNegativeWidthCases))]
public void PadStartWithNegativeWidth(TestDelegate @delegate)
{
Assert.That(() => new int[0].PadStart(-1), Throws.ArgumentException("width"));
Assert.That(@delegate, Throws.ArgumentOutOfRangeException("width")
.And.Property(nameof(ArgumentOutOfRangeException.ActualValue)).EqualTo(-1));
}

// PadStart(source, width)

[Test]
public void PadStartIsLazy()
{
Expand Down Expand Up @@ -61,12 +72,6 @@ public void ReferenceTypeElements(ICollection<string?> source, int width, IEnume

// PadStart(source, width, padding)

[Test]
public void PadStartWithPaddingWithNegativeWidth()
{
Assert.That(() => new int[0].PadStart(-1, 1), Throws.ArgumentException("width"));
}

[Test]
public void PadStartWithPaddingIsLazy()
{
Expand Down Expand Up @@ -94,14 +99,6 @@ public void ReferenceTypeElements(ICollection<string> source, int width, IEnumer
}
}

// PadStart(source, width, paddingSelector)

[Test]
public void PadStartWithSelectorWithNegativeWidth()
{
Assert.That(() => new int[0].PadStart(-1, x => x), Throws.ArgumentException("width"));
}

[Test]
public void PadStartWithSelectorIsLazy()
{
Expand Down
19 changes: 16 additions & 3 deletions MoreLinq.Test/PadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@

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

[TestFixture]
public class PadTest
{
[Test]
public void PadNegativeWidth()
static readonly IEnumerable<ITestCaseData> PadNegativeWidthCases =
from e in new (string Name, TestDelegate Delegate)[]
{
("DefaultPadding" , static () => new object[0].Pad(-1)),
("Padding" , static () => new object[0].Pad(-1, -2)),
("PaddingSelector", static () => new object[0].Pad(-1, BreakingFunc.Of<int, int>())),
}
select new TestCaseData(e.Delegate).SetName(e.Name);

[TestCaseSource(nameof(PadNegativeWidthCases))]
public void PadNegativeWidth(TestDelegate @delegate)
{
Assert.That(() => new object[0].Pad(-1), Throws.ArgumentException("width"));
Assert.That(@delegate, Throws.ArgumentOutOfRangeException("width")
.And.Property(nameof(ArgumentOutOfRangeException.ActualValue)).EqualTo(-1));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/Pad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static partial class MoreEnumerable
public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, TSource padding)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (width < 0) throw new ArgumentException(null, nameof(width));
if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), width, null);
return PadImpl(source, width, padding, null);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (paddingSelector == null) throw new ArgumentNullException(nameof(paddingSelector));
if (width < 0) throw new ArgumentException(null, nameof(width));
if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), width, null);
return PadImpl(source, width, default, paddingSelector);
}

Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/PadStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static partial class MoreEnumerable
public static IEnumerable<TSource> PadStart<TSource>(this IEnumerable<TSource> source, int width, TSource padding)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (width < 0) throw new ArgumentException(null, nameof(width));
if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), width, null);
return PadStartImpl(source, width, padding, null);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public static IEnumerable<TSource> PadStart<TSource>(this IEnumerable<TSource> s
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (paddingSelector == null) throw new ArgumentNullException(nameof(paddingSelector));
if (width < 0) throw new ArgumentException(null, nameof(width));
if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), width, null);
return PadStartImpl(source, width, default, paddingSelector);
}

Expand Down

0 comments on commit 092a40d

Please sign in to comment.