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

Cleanup NUnit1030 #1086

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions MoreLinq.Test/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,5 @@ dotnet_diagnostic.CA1861.severity = none
# IDE0022: Use expression/block body for methods
dotnet_diagnostic.IDE0022.severity = none

# NUnit1030: The type of parameter provided by the TestCaseSource does not match the type of the parameter in the Test method
dotnet_diagnostic.NUnit1030.severity = suggestion

# Nunit1029: The number of parameters provided by the TestCaseSource does not match the number of parameters in the Test method
dotnet_diagnostic.NUnit1029.severity = suggestion
22 changes: 11 additions & 11 deletions MoreLinq.Test/NullArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace MoreLinq.Test
using System.Reflection;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using StackTrace = System.Diagnostics.StackTrace;

[TestFixture]
Expand All @@ -38,7 +37,7 @@ public void NotNull(Action testCase) =>
public void CanBeNull(Action testCase) =>
testCase();

static IEnumerable<ITestCaseData> GetNotNullTestCases() =>
static IEnumerable<Action> GetNotNullTestCases() =>
GetTestCases(canBeNull: false, testCaseFactory: (method, args, paramName) => () =>
{
Exception? e = null;
Expand All @@ -61,28 +60,29 @@ static IEnumerable<ITestCaseData> GetNotNullTestCases() =>
Assert.That(actualType, Is.SameAs(typeof(MoreEnumerable)));
});

static IEnumerable<ITestCaseData> GetCanBeNullTestCases() =>
static IEnumerable<Action> GetCanBeNullTestCases() =>
GetTestCases(canBeNull: true, testCaseFactory: (method, args, _) => () => method.Invoke(null, args));

static IEnumerable<ITestCaseData> GetTestCases(bool canBeNull, Func<MethodInfo, object?[], string, Action> testCaseFactory) =>
static IEnumerable<Action> GetTestCases(bool canBeNull, Func<MethodInfo, object?[], string, Action> testCaseFactory) =>
from m in typeof(MoreEnumerable).GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
from t in CreateTestCases(m, canBeNull, testCaseFactory)
select t;

static IEnumerable<ITestCaseData> CreateTestCases(MethodInfo methodDefinition, bool canBeNull, Func<MethodInfo, object?[], string, Action> testCaseFactory)
static IEnumerable<Action> CreateTestCases(MethodInfo methodDefinition, bool canBeNull, Func<MethodInfo, object?[], string, Action> testCaseFactory)
{
var method = InstantiateMethod(methodDefinition);
var parameters = method.GetParameters().ToList();

return from param in parameters
where IsReferenceType(param) && CanBeNull(param) == canBeNull
let arguments = parameters.Select(p => p == param ? null : CreateInstance(p.ParameterType)).ToArray()
let testCase = testCaseFactory(method, arguments,
where IsReferenceType(param) && CanBeNull(param) == canBeNull
let arguments = parameters
.Select(p => p == param ? null : CreateInstance(p.ParameterType)).ToArray()
let testCase = testCaseFactory(method, arguments,
#pragma warning disable CA2201 // Do not raise reserved exception types
param.Name ?? throw new NullReferenceException())
param.Name ?? throw new NullReferenceException())
#pragma warning restore CA2201 // Do not raise reserved exception types
let testName = GetTestName(methodDefinition, param)
select (ITestCaseData)new TestCaseData(testCase).SetName(testName);
let testName = GetTestName(methodDefinition, param)
select testCase;
}

static string GetTestName(MethodInfo definition, ParameterInfo parameter) =>
Expand Down
18 changes: 9 additions & 9 deletions MoreLinq.Test/PadStartTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ namespace MoreLinq.Test
using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

[TestFixture]
public class PadStartTest
{
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);
static IEnumerable<TestDelegate> PadStartWithNegativeWidthCases()
{
return
[
static () => Array.Empty<object>().PadStart(-1),
static () => Array.Empty<object>().PadStart(-1, -2),
static () => Array.Empty<object>().PadStart(-1, BreakingFunc.Of<int, int>())
];
}

[TestCaseSource(nameof(PadStartWithNegativeWidthCases))]
public void PadStartWithNegativeWidth(TestDelegate @delegate)
Expand Down
18 changes: 9 additions & 9 deletions MoreLinq.Test/PadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ namespace MoreLinq.Test
using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

[TestFixture]
public class PadTest
{
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);
static IEnumerable<TestDelegate> PadNegativeWidthCases()
{
return
[
static () => Array.Empty<object>().Pad(-1),
static () => Array.Empty<object>().Pad(-1, -2),
static () => Array.Empty<object>().Pad(-1, BreakingFunc.Of<int, int>())
];
}

[TestCaseSource(nameof(PadNegativeWidthCases))]
public void PadNegativeWidth(TestDelegate @delegate)
Expand Down
30 changes: 14 additions & 16 deletions MoreLinq.Test/ReturnTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace MoreLinq.Test
using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

public class ReturnTest
{
Expand Down Expand Up @@ -136,21 +135,20 @@ public void TestIndexOfAnItemNotContainedIsNegativeOne()
Assert.That(SomeSingleton.List.IndexOf(new object()), Is.EqualTo(-1));
}

static IEnumerable<ITestCaseData> UnsupportedActions(string testName) =>
from ma in new (string MethodName, Action Action)[]
{
("Add" , () => SomeSingleton.List.Add(new object())),
("Clear" , SomeSingleton.Collection.Clear),
("Remove" , () => SomeSingleton.Collection.Remove(SomeSingleton.Item)),
("RemoveAt", () => SomeSingleton.List.RemoveAt(0)),
("Insert" , () => SomeSingleton.List.Insert(0, new object())),
("Index" , () => SomeSingleton.List[0] = new object()),
}
select new TestCaseData(ma.Action).SetName($"{testName}({ma.MethodName})");

#pragma warning disable NUnit1018 // Parameter count does not match (false negative)
[TestCaseSource(nameof(UnsupportedActions), [nameof(TestUnsupportedMethodShouldThrow)])]
#pragma warning restore NUnit1018 // Parameter count does not match
static IEnumerable<Action> UnsupportedActions()
{
return
[
() => SomeSingleton.List.Add(new object()),
SomeSingleton.Collection.Clear,
() => SomeSingleton.Collection.Remove(SomeSingleton.Item),
() => SomeSingleton.List.RemoveAt(0),
() => SomeSingleton.List.Insert(0, new object()),
() => SomeSingleton.List[0] = new object(),
];
}

[TestCaseSource(nameof(UnsupportedActions))]
public void TestUnsupportedMethodShouldThrow(Action unsupportedAction)
{
Assert.That(() => unsupportedAction(), Throws.InstanceOf<NotSupportedException>());
Expand Down
Loading