Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="CustomizeAttribute.cs" />
<Compile Include="ExcludeAllAttribute.cs" />
<Compile Include="ExcludeAttribute.cs" />
<Compile Include="ExcludePrivateAttribute.cs" />
<Compile Include="ExclusionType.cs" />
<Compile Include="IncludeAttribute.cs" />
<Compile Include="MethodDataExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace AutoTest.ArgNullEx.Xunit
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

/// <summary>
/// An attribute that can be applied to methods in an <see cref="RequiresArgumentNullExceptionAttribute"/>-driven
/// Theory to exclude private members from the test.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ExcludePrivateAttribute : CustomizeAttribute, IArgNullExCustomization
{
/// <summary>
/// Gets a customization for a test method.
/// </summary>
/// <param name="method">The method to be customized.</param>
/// <returns>A customization for a test method.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="method"/> parameter is
/// <see langword="null"/>.</exception>
public override IArgNullExCustomization GetCustomization(MethodInfo method)
{
if (method == null)
throw new ArgumentNullException("method");

return this;
}

/// <summary>
/// Customizes the specified <paramref name="fixture"/> by excluding private members.
/// </summary>
/// <param name="fixture">The fixture to customize.</param>
/// <exception cref="ArgumentNullException">The <paramref name="fixture"/> parameter is
/// <see langword="null"/>.</exception>
public virtual void Customize(IArgumentNullExceptionFixture fixture)
{
if (fixture == null)
throw new ArgumentNullException("fixture");

fixture.ExcludePrivate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ public static IArgumentNullExceptionFixture ExcludeAll(this IArgumentNullExcepti
return fixture;
}

/// <summary>
/// Excludes all private members.
/// </summary>
/// <param name="fixture">The fixture.</param>
/// <returns>The <paramref name="fixture"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="fixture"/> parameter is <see langword="null"/>.</exception>
public static IArgumentNullExceptionFixture ExcludePrivate(this IArgumentNullExceptionFixture fixture)
{
if (fixture == null)
throw new ArgumentNullException("fixture");

fixture.BindingFlags &= ~BindingFlags.NonPublic;

return fixture;
}

/// <summary>
/// Applies the <paramref name="customization"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,31 @@ void ExcludeAll(

#endregion ExcludeAll

#region ExcludePrivate

[Theory]
[InlineData(ArgumentNullExceptionFixture.DefaultBindingFlags,
ArgumentNullExceptionFixture.DefaultBindingFlags & ~(BindingFlags.NonPublic))]
[InlineData(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic,
BindingFlags.Instance | BindingFlags.DeclaredOnly)]
[InlineData(BindingFlags.Static | BindingFlags.DeclaredOnly, BindingFlags.Static | BindingFlags.DeclaredOnly)]
void ExcludePrivate(BindingFlags initial, BindingFlags expected)
{
// Arrange
var fixtureMock = new Mock<IArgumentNullExceptionFixture>();
fixtureMock.SetupProperty(f => f.BindingFlags);
fixtureMock.Object.BindingFlags = initial;

// Act
IArgumentNullExceptionFixture fixture = fixtureMock.Object.ExcludePrivate();

// Assert
Assert.Equal(expected, fixtureMock.Object.BindingFlags);
Assert.Same(fixtureMock.Object, fixture);
}

#endregion ExcludePrivate

#region Customizations

[Theory, AutoMock]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Issues\Issue009\Issue009.cs" />
<Compile Include="Issues\Issue012\Issue012.cs" />
<Compile Include="Issues\Issue015\Issue015.cs" />
<Compile Include="Issues\Issue020\Issue020.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequiresArgNullEx.cs" />
<Compile Include="RequiresArgNullExAutoMoqAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace AutoTest.ExampleLibrary.Issues.Issue020
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoTest.ArgNullEx;
using AutoTest.ArgNullEx.Xunit;
using Xunit;
using Xunit.Extensions;

public class ExcludePrivateShould
{
[Theory, RequiresArgNullExAutoMoq(typeof(Mixture)), ExcludePrivate]
[Include(Type = typeof(Mixture))]
public async Task OnlyTestPublic(MethodData method)
{
await method.Execute();
Assert.True(Mixture.Tested);
}
}
}
2 changes: 2 additions & 0 deletions src/Tests/AutoTest.ExampleLibrary.Tests/RequiresArgNullEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
using AutoTest.ArgNullEx;
using AutoTest.ArgNullEx.Xunit;
using AutoTest.ExampleLibrary.Issues.Issue009;
using AutoTest.ExampleLibrary.Issues.Issue020;
using Xunit.Extensions;

public class RequiresArgNullEx
{
[Theory, RequiresArgNullExAutoMoq(typeof(Class1))]
[Exclude(Type = typeof(InternalInnerInterface))]
[Exclude(Type = typeof(Mixture), Method = "Private")]
public Task TestAllNullArguments(MethodData method)
{
return method.Execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Issues\Issue015\ExplicitEquals.cs" />
<Compile Include="Issues\Issue015\ImplicitEquals.cs" />
<Compile Include="Issues\Issue015\OtherEquals.cs" />
<Compile Include="Issues\Issue020\Mixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
31 changes: 31 additions & 0 deletions src/Tests/AutoTest.ExampleLibrary/Issues/Issue020/Mixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace AutoTest.ExampleLibrary.Issues.Issue020
{
using System;
using System.Collections.Generic;
using System.Linq;

public static class Mixture
{
/// <summary>
/// Gets a value indicating if the <see cref="Mixture"/> has been tested.
/// </summary>
public static bool Tested { get; private set; }

public static string Public(string stringValue)
{
Tested = false;
string returnVal = Private(stringValue);

if (stringValue != null)
return returnVal;

Tested = true;
throw new ArgumentNullException("stringValue");
}

private static string Private(string stringValue)
{
return stringValue ?? string.Empty;
}
}
}