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

Fix "Pad*" signatures with more honest nullable annotations #804

Merged
merged 5 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
89 changes: 63 additions & 26 deletions MoreLinq.Test/PadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
// limitations under the License.
#endregion

#nullable enable

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

[TestFixture]
Expand All @@ -41,39 +44,73 @@ public void PadWithFillerIsLazy()
new BreakingSequence<object>().Pad(0, new object());
}

[Test]
public void PadWideSourceSequence()
public class ValueTypeElements
{
var result = new[] { 123, 456, 789 }.Pad(2);
result.AssertSequenceEqual(123, 456, 789);
}
[Test]
public void PadWideSourceSequence()
{
var result = new[] { 123, 456, 789 }.Pad(2);
result.AssertSequenceEqual(123, 456, 789);
}

[Test]
public void PadEqualSourceSequence()
{
var result = new[] { 123, 456, 789 }.Pad(3);
result.AssertSequenceEqual(123, 456, 789);
}
[Test]
public void PadEqualSourceSequence()
{
var result = new[] { 123, 456, 789 }.Pad(3);
result.AssertSequenceEqual(123, 456, 789);
}

[Test]
public void PadNarrowSourceSequenceWithDefaultPadding()
{
var result = new[] { 123, 456, 789 }.Pad(5);
result.AssertSequenceEqual(123, 456, 789, 0, 0);
}
[Test]
public void PadNarrowSourceSequenceWithDefaultPadding()
{
var result = new[] { 123, 456, 789 }.Pad(5);
result.AssertSequenceEqual(123, 456, 789, 0, 0);
}

[Test]
public void PadNarrowSourceSequenceWithNonDefaultPadding()
{
var result = new[] { 123, 456, 789 }.Pad(5, -1);
result.AssertSequenceEqual(123, 456, 789, -1, -1);
[Test]
public void PadNarrowSourceSequenceWithNonDefaultPadding()
{
var result = new[] { 123, 456, 789 }.Pad(5, -1);
result.AssertSequenceEqual(123, 456, 789, -1, -1);
}

[Test]
public void PadNarrowSourceSequenceWithDynamicPadding()
{
var result = "hello".ToCharArray().Pad(15, i => i % 2 == 0 ? '+' : '-');
result.AssertSequenceEqual("hello-+-+-+-+-+".ToCharArray());
}
}

[Test]
public void PadNarrowSourceSequenceWithDynamicPadding()
public class ReferenceTypeElements
{
var result = "hello".ToCharArray().Pad(15, i => i % 2 == 0 ? '+' : '-');
result.AssertSequenceEqual("hello-+-+-+-+-+".ToCharArray());
[Test]
public void PadWideSourceSequence()
{
var result = new[] { "123", "456", "789" }.Pad(2);
result.AssertSequenceEqual("123", "456", "789");
}

[Test]
public void PadEqualSourceSequence()
{
var result = new[] { "123", "456", "789" }.Pad(3);
result.AssertSequenceEqual("123", "456", "789");
}

[Test]
public void PadNarrowSourceSequenceWithDefaultPadding()
{
var result = new[] { "123", "456", "789" }.Pad(5);
result.AssertSequenceEqual("123", "456", "789", null, null);
}

[Test]
public void PadNarrowSourceSequenceWithNonDefaultPadding()
{
var result = new[] { "123", "456", "789" }.Pad(5, string.Empty);
result.AssertSequenceEqual("123", "456", "789", string.Empty, string.Empty);
}
}
}
}
4 changes: 2 additions & 2 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3747,7 +3747,7 @@ public static partial class PadExtension
/// 123, 456, 789 and two zeroes, in turn.
/// </example>

public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width)
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width)
=> MoreEnumerable.Pad(source, width);

/// <summary>
Expand Down Expand Up @@ -3801,7 +3801,7 @@ public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source
/// 0, 1, 2, -3 and -4, in turn.
/// </example>

public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource> paddingSelector)
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource?> paddingSelector)
=> MoreEnumerable.Pad(source, width, paddingSelector);

}
Expand Down
8 changes: 4 additions & 4 deletions MoreLinq/Pad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ static partial class MoreEnumerable
/// 123, 456, 789 and two zeroes, in turn.
/// </example>

public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width)
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width)
{
return Pad(source, width, default(TSource)!);
return Pad(source, width, default(TSource));
}

/// <summary>
Expand Down Expand Up @@ -106,12 +106,12 @@ public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source
/// 0, 1, 2, -3 and -4, in turn.
/// </example>

public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource> paddingSelector)
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource?> paddingSelector)
{
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));
return PadImpl(source, width, default!, paddingSelector);
return PadImpl(source, width, default, paddingSelector);
}

static IEnumerable<T> PadImpl<T>(IEnumerable<T> source,
Expand Down