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

Improve Error Checking Code #947

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ dotnet_diagnostic.IDE0055.severity = suggestion

# IDE0046: Convert to conditional expression
dotnet_diagnostic.IDE0046.severity = suggestion

# Inform the CA1062 analyzer that Guard.IsNotNull will do null-check for us
# CA1062: Validate arguments of public methods
dotnet_code_quality.CA1062.null_check_validation_methods = IsNotNull
3 changes: 2 additions & 1 deletion MoreLinq.Test/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using CommunityToolkit.Diagnostics;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put system namespaces first (in all files) since the project follows the default for formatting rule dotnet_sort_system_directives_first of true.

Unfortunately, IDE0055 is not enforceable since there are too many exceptions to the rules; it's therefore only configured to be a suggestion:

MoreLINQ/.editorconfig

Lines 84 to 85 in c01e646

# IDE0055: Fix formatting
dotnet_diagnostic.IDE0055.severity = suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto-add code-fix. usually it respects that, but alas...

using System;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -39,7 +40,7 @@ public static string ToInvariantString<T>(this T formattable) where T : IFormatt
[DebuggerStepThrough]
public static string ToInvariantString<T>(this T formattable, string? format) where T : IFormattable
{
if (formattable is null) throw new ArgumentNullException(nameof(formattable));
Guard.IsNotNull(formattable);
return formattable.ToString(format, CultureInfo.InvariantCulture);
}
}
Expand Down
20 changes: 16 additions & 4 deletions MoreLinq.Test/NullArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using CommunityToolkit.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -56,9 +57,20 @@ static IEnumerable<ITestCaseData> GetNotNullTestCases() =>
Assert.That(e, Is.InstanceOf<ArgumentNullException>().With.Property(nameof(ArgumentNullException.ParamName)).EqualTo(paramName));
Debug.Assert(e is not null);
var stackTrace = new StackTrace(e, false);
var stackFrame = stackTrace.GetFrames().First();
var actualType = stackFrame.GetMethod()?.DeclaringType;
Assert.That(actualType, Is.SameAs(typeof(MoreEnumerable)));
var stackFrames = stackTrace.GetFrames();

// `Guard.IsNotNull()` adds additional stack frames
// so find first non-CommunityToolkit.Diagnostics
for (var i = 0; i < stackFrames.Length; i++)
{
var actualType = stackFrames[i]?.GetMethod()?.DeclaringType;
if (actualType?.Namespace != "CommunityToolkit.Diagnostics")
{
// top _non_-`Guard` should be `MoreEnumerable`
Assert.That(actualType, Is.SameAs(typeof(MoreEnumerable)));
break;
}
}
});

static IEnumerable<ITestCaseData> GetCanBeNullTestCases() =>
Expand Down Expand Up @@ -226,7 +238,7 @@ public sealed class OrderedEnumerable<T> : Enumerable<T>, System.Linq.IOrderedEn
{
public System.Linq.IOrderedEnumerable<T?> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey>? comparer, bool descending)
{
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Guard.IsNotNull(keySelector);
return this;
}
}
Expand Down
6 changes: 3 additions & 3 deletions MoreLinq.Test/PadStartTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PadStartTest
[Test]
public void PadStartWithNegativeWidth()
{
Assert.That(() => new int[0].PadStart(-1), Throws.ArgumentException("width"));
Assert.That(() => new int[0].PadStart(-1), Throws.ArgumentOutOfRangeException("width"));
}

[Test]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void ReferenceTypeElements(ICollection<string?> source, int width, IEnume
[Test]
public void PadStartWithPaddingWithNegativeWidth()
{
Assert.That(() => new int[0].PadStart(-1, 1), Throws.ArgumentException("width"));
Assert.That(() => new int[0].PadStart(-1, 1), Throws.ArgumentOutOfRangeException("width"));
}

[Test]
Expand Down Expand Up @@ -99,7 +99,7 @@ public void ReferenceTypeElements(ICollection<string> source, int width, IEnumer
[Test]
public void PadStartWithSelectorWithNegativeWidth()
{
Assert.That(() => new int[0].PadStart(-1, x => x), Throws.ArgumentException("width"));
Assert.That(() => new int[0].PadStart(-1, x => x), Throws.ArgumentOutOfRangeException("width"));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/PadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class PadTest
[Test]
public void PadNegativeWidth()
{
Assert.That(() => new object[0].Pad(-1), Throws.ArgumentException("width"));
Assert.That(() => new object[0].Pad(-1), Throws.ArgumentOutOfRangeException("width"));
}

[Test]
Expand Down
5 changes: 3 additions & 2 deletions MoreLinq.Test/SequenceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

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

static class SequenceReader
{
public static SequenceReader<T> Read<T>(this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
Guard.IsNotNull(source);
return new SequenceReader<T>(source);
}
}
Expand Down Expand Up @@ -59,7 +60,7 @@ public SequenceReader(IEnumerator<T> enumerator) =>

static IEnumerator<T> GetEnumerator(IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
Guard.IsNotNull(source);
return source.GetEnumerator();
}

Expand Down
3 changes: 2 additions & 1 deletion MoreLinq/Acquire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq
{
using CommunityToolkit.Diagnostics;
using System;
using System.Collections.Generic;

Expand All @@ -41,7 +42,7 @@ static partial class MoreEnumerable
public static TSource[] Acquire<TSource>(this IEnumerable<TSource> source)
where TSource : IDisposable
{
if (source == null) throw new ArgumentNullException(nameof(source));
Guard.IsNotNull(source);

var disposables = new List<TSource>();
try
Expand Down
Loading