Skip to content

Commit 1e22f5b

Browse files
authored
Merge pull request #31 from JSkimming/ignore-nullable-value-types
Ignore nullable value types
2 parents 420b2ab + 6357e7a commit 1e22f5b

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

src/AutoTest.ArgumentNullException/AutoTest.ArgumentNullException.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<Compile Include="Filter\MethodExtensions.cs" />
5656
<Compile Include="Filter\NotAbstractMethod.cs" />
5757
<Compile Include="Filter\NotEqualsMethod.cs" />
58+
<Compile Include="Filter\NotNullableValueType.cs" />
5859
<Compile Include="Filter\NotOutParameter.cs" />
5960
<Compile Include="Filter\NotPropertySetter.cs" />
6061
<Compile Include="Filter\TypeFiltering.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace AutoTest.ArgNullEx.Filter
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
8+
/// <summary>
9+
/// Filters out parameters that are <see cref="Nullable{T}"/> value types.
10+
/// </summary>
11+
public sealed class NotNullableValueType : FilterBase, IParameterFilter
12+
{
13+
/// <summary>
14+
/// Filters out parameters that are <see cref="Nullable{T}"/> value types.
15+
/// </summary>
16+
/// <param name="type">The type.</param>
17+
/// <param name="method">The method.</param>
18+
/// <param name="parameter">The parameter.</param>
19+
/// <returns><see langword="true"/> if the <paramref name="parameter"/> should be excluded;
20+
/// otherwise <see langword="false"/>.</returns>
21+
/// <exception cref="ArgumentNullException">The <paramref name="type"/>, <paramref name="method"/> or
22+
/// <paramref name="parameter"/> parameters are <see langword="null"/>.</exception>
23+
bool IParameterFilter.ExcludeParameter(Type type, MethodBase method, ParameterInfo parameter)
24+
{
25+
if (type == null)
26+
throw new ArgumentNullException(nameof(type));
27+
if (method == null)
28+
throw new ArgumentNullException(nameof(method));
29+
if (parameter == null)
30+
throw new ArgumentNullException(nameof(parameter));
31+
32+
Type parameterType = parameter.ParameterType;
33+
return parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Nullable<>);
34+
}
35+
}
36+
}

src/Tests/AutoTest.ArgumentNullException.Tests/AutoTest.ArgumentNullException.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Execution\DefaultExecutionSetupShould.cs" />
7979
<Compile Include="Execution\ErroredExecutionSetupShould.cs" />
8080
<Compile Include="Filter\NotAbstractMethodShould.cs" />
81+
<Compile Include="Filter\NotNullableValueTypeShould.cs" />
8182
<Compile Include="Filter\NotOutParameterShould.cs" />
8283
<Compile Include="Filter\NotPropertySetterShould.cs" />
8384
<Compile Include="Filter\ParameterFilteringShould.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace AutoTest.ArgNullEx.Filter
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using global::Xunit;
8+
9+
public class NotNullableValueTypeShould
10+
{
11+
private static void SomeNullableParametersMethod(
12+
int intInput,
13+
string stringInput,
14+
Guid guidInput,
15+
int? intNullable,
16+
Guid? guidNullable)
17+
{
18+
}
19+
20+
public static IEnumerable<object[]> SomeOutParameters => GetSomeOutParameters();
21+
22+
internal static IEnumerable<object[]> GetSomeOutParameters()
23+
{
24+
Type type = typeof(NotNullableValueTypeShould);
25+
26+
MethodInfo method = type.GetMethod(
27+
nameof(SomeNullableParametersMethod),
28+
BindingFlags.NonPublic | BindingFlags.Static);
29+
ParameterInfo[] someOutParameters = method.GetParameters();
30+
31+
return someOutParameters.Select(
32+
parameter => new object[] { type, method, parameter, parameter.Name.Contains("Nullable") });
33+
}
34+
35+
[Theory, AutoMock]
36+
public void ReturnName(NotNullableValueType sut)
37+
{
38+
Assert.Equal(nameof(NotNullableValueType), sut.Name);
39+
}
40+
41+
[Theory, MemberData(nameof(SomeOutParameters))]
42+
public void ExcludeNullableValueTypeParameter(
43+
Type type,
44+
MethodInfo method,
45+
ParameterInfo parameter,
46+
bool expected)
47+
{
48+
// Arrange
49+
IParameterFilter sut = new NotNullableValueType();
50+
51+
// Act
52+
bool actual = sut.ExcludeParameter(type, method, parameter);
53+
54+
// Assert
55+
Assert.Equal(expected, actual);
56+
}
57+
}
58+
}

src/Tests/AutoTest.ExampleLibrary.Tests/AutoTest.ExampleLibrary.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Issues\Issue015\Issue015.cs" />
7878
<Compile Include="Issues\Issue020\Issue020.cs" />
7979
<Compile Include="Issues\Issue022\Issue022.cs" />
80+
<Compile Include="Issues\Issue031\Issue031.cs" />
8081
<Compile Include="Properties\AssemblyInfo.cs" />
8182
<Compile Include="RequiresArgNullEx.cs" />
8283
<Compile Include="RequiresArgNullExAutoMoqAttribute.cs" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace AutoTest.ExampleLibrary.Issues.Issue031
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using AutoTest.ArgNullEx;
8+
using AutoTest.ArgNullEx.Xunit;
9+
using Xunit;
10+
11+
public class Issue031
12+
{
13+
[Theory, RequiresArgNullExAutoMoq(typeof(SomeNullableValueTypeParameters))]
14+
[Include(
15+
ExclusionType = ExclusionType.All,
16+
Type = typeof(SomeNullableValueTypeParameters),
17+
Method = "SomeNullableValueTypeParametersMethod",
18+
Parameter = "stringInput")]
19+
public async Task TestStringInput(MethodData method)
20+
{
21+
await method.Execute();
22+
23+
Assert.True(SomeNullableValueTypeParameters.StringInputTested);
24+
}
25+
}
26+
}

src/Tests/AutoTest.ExampleLibrary/AutoTest.ExampleLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Issues\Issue015\OtherEquals.cs" />
5353
<Compile Include="Issues\Issue020\Mixture.cs" />
5454
<Compile Include="Issues\Issue022\GenericClass.cs" />
55+
<Compile Include="Issues\Issue031\SomeNullableValueTypeParameters.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
5657
</ItemGroup>
5758
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace AutoTest.ExampleLibrary.Issues.Issue031
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
/// <summary>
8+
/// Test class used to demonstrate pull request 31
9+
/// https://github.com/AutoTestNET/AutoTest.ArgumentNullException/pull/31
10+
/// </summary>
11+
public class SomeNullableValueTypeParameters
12+
{
13+
/// <summary>
14+
/// Gets a value indicating if the <see cref="SomeNullableValueTypeParametersMethod"/> stringInput parameter
15+
/// has been tested.
16+
/// </summary>
17+
public static bool StringInputTested { get; private set; }
18+
19+
private static void SomeNullableValueTypeParametersMethod(
20+
int intInput,
21+
string stringInput,
22+
Guid guidInput,
23+
int? intNullable,
24+
Guid? guidNullable)
25+
{
26+
StringInputTested = false;
27+
28+
if (stringInput == null)
29+
{
30+
StringInputTested = true;
31+
throw new ArgumentNullException(nameof(stringInput));
32+
}
33+
34+
throw new Exception("Shouldn't ever get here.");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)