Skip to content

Commit

Permalink
More testing of generic types #1
Browse files Browse the repository at this point in the history
  • Loading branch information
JSkimming committed Jul 29, 2013
1 parent f6ed747 commit de1aa90
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="App_Packages\TaskHelpers.Sources.0.2\TaskHelpersExtensions.cs" />
<Compile Include="CustomizeAttribute.cs" />
<Compile Include="ExcludeAllAttribute.cs" />
<Compile Include="ExcludeAttribute.cs" />
<Compile Include="ExclusionType.cs" />
<Compile Include="IncludeAttribute.cs" />
<Compile Include="MethodDataExtensions.cs" />
Expand Down
69 changes: 69 additions & 0 deletions src/AutoTest.ArgumentNullException.Xunit/ExcludeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace AutoTest.ArgNullEx.Xunit
{
using System;
using System.Reflection;

/// <summary>
/// An attribute that can be applied to methods in an <see cref="RequiresArgumentNullExceptionAttribute"/>-driven
/// Theory to indicate that the specified <see cref="ExcludeAttribute.Type"/>, <see cref="ExcludeAttribute.Method"/>
/// and/or <see cref="ExcludeAttribute.Parameter"/> must be excluded from the test.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ExcludeAttribute : CustomizeAttribute, IArgNullExCustomization
{
/// <summary>
/// Gets or sets the <see cref="System.Type"/> to exclude from checks for <see cref="ArgumentNullException"/>.
/// </summary>
public Type Type { get; set; }

/// <summary>
/// Gets or sets the <see cref="Method"/> to exclude from checks for <see cref="ArgumentNullException"/>.
/// </summary>
public string Method { get; set; }

/// <summary>
/// Gets or sets the <see cref="Parameter"/> to exclude from checks for <see cref="ArgumentNullException"/>.
/// </summary>
public string Parameter { get; set; }

/// <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>
public override IArgNullExCustomization GetCustomization(MethodInfo method)
{
if (method == null)
throw new ArgumentNullException("method");

return this;
}

/// <summary>
/// Customizes the specified <paramref name="fixture"/> by excluding all types.
/// </summary>
/// <param name="fixture">The fixture to customize.</param>
public virtual void Customize(IArgumentNullExceptionFixture fixture)
{
if (fixture == null)
throw new ArgumentNullException("fixture");

if (!string.IsNullOrWhiteSpace(Parameter))
{
fixture.ExcludeParameter(Parameter, Type, Method);
return;
}

if (!string.IsNullOrWhiteSpace(Method))
{
fixture.ExcludeMethod(Method, Type);
return;
}

if (Type == null)
throw new InvalidOperationException("Either the Type, Method or Parameter must be specified.");

fixture.ExcludeType(Type);
}
}
}
12 changes: 6 additions & 6 deletions src/AutoTest.ArgumentNullException.Xunit/IncludeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
public class IncludeAttribute : ExcludeAllAttribute
{
/// <summary>
/// Gets or sets the <see name="System.Type"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any type rules that may exclude the <see name="System.Type"/>.
/// Gets or sets the <see cref="System.Type"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any type rules that may exclude the <see cref="System.Type"/>.
/// </summary>
public Type Type { get; set; }

/// <summary>
/// Gets or sets the <see name="Method"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any method rules that may exclude the <see name="Method"/>.
/// Gets or sets the <see cref="Method"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any method rules that may exclude the <see cref="Method"/>.
/// </summary>
public string Method { get; set; }

/// <summary>
/// Gets or sets the <see name="Parameter"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any parameter rules that may exclude the <see name="Parameter"/>.
/// Gets or sets the <see cref="Parameter"/> for checks for <see cref="ArgumentNullException"/>.
/// Overrides any parameter rules that may exclude the <see cref="Parameter"/>.
/// </summary>
public string Parameter { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static IArgumentNullExceptionFixture RemoveFilters(this IArgumentNullExce
}

/// <summary>
/// Removes all the supplied <paramref name="filtersToRemove"/> from the <paramref name="fixture"/> collection of <see name="IArgumentNullExceptionFixture.Filters"/>.
/// Removes all the supplied <paramref name="filtersToRemove"/> from the <paramref name="fixture"/> collection of <see cref="IArgumentNullExceptionFixture.Filters"/>.
/// </summary>
/// <param name="fixture">The fixture.</param>
/// <param name="filtersToRemove">The filters to remove.</param>
Expand All @@ -83,7 +83,7 @@ public static IArgumentNullExceptionFixture RemoveFilters(this IArgumentNullExce
}

/// <summary>
/// Adds the <paramref name="filters"/> to the <paramref name="fixture"/> collection of <see name="IArgumentNullExceptionFixture.Filters"/> if they are not already in he collection.
/// Adds the <paramref name="filters"/> to the <paramref name="fixture"/> collection of <see cref="IArgumentNullExceptionFixture.Filters"/> if they are not already in he collection.
/// </summary>
/// <param name="fixture">The fixture.</param>
/// <param name="filters">The filters to add.</param>
Expand Down
3 changes: 2 additions & 1 deletion src/AutoTest.ArgumentNullException/GenericTypeConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ private static bool TrySimple(Type genericType, out Type nonGenericType)
return true;
}

if (!constraint.IsInterface)
// If there is a single constraint with no attributes, just use the constraint as the non generic type.
if (attributes == GenericParameterAttributes.None)
{
nonGenericType = constraint;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class Issue001
{
[Theory, RequiresArgNullExAutoMoq(typeof(SimpleGenericMethods))]
[Exclude(Type = typeof(SimpleGenericMethods), Method = "GenericExceptionMethod")]
[Include(Type = typeof(SimpleGenericMethods))]
public async Task Simple(MethodData method)
{
Expand All @@ -17,6 +18,18 @@ public async Task Simple(MethodData method)
Assert.True(SimpleGenericMethods.GenericMethod2Tested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(SimpleGenericMethods))]
[Include(
ExclusionType = ExclusionType.Types | ExclusionType.Methods,
Type = typeof(SimpleGenericMethods),
Method = "GenericExceptionMethod")]
public async Task SimpleException(MethodData method)
{
await method.Execute();

Assert.True(SimpleGenericMethods.GenericExceptionMethodTested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(MixedGenericMethods))]
[Include(
ExclusionType = ExclusionType.All,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
using System;

/// <summary>
/// Test class used to demonstrate issue 1 on generic methods.
/// https://github.com/AutoTestNET/AutoTest.ArgumentNullException/issues/1
/// </summary>
public static class ComplexGenericMethods
{
/// <summary>
Expand Down Expand Up @@ -38,7 +42,7 @@ public static void GenericClassMethod<TClass>(TClass classValue, string stringVa
}

public static void GenericExceptionMethod<TException>(TException classValue, string stringValue)
where TException : Exception, ITest1, ITest2
where TException : Exception, ITest1, ITest2, new()
{
throw new Exception("Shouldn't ever get here.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
using System;

/// <summary>
/// Test class used to demonstrate issue 1 on generic methods.
/// https://github.com/AutoTestNET/AutoTest.ArgumentNullException/issues/1
/// </summary>
public static class InterfaceGenericMethods
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
using System;

/// <summary>
/// Test class used to demonstrate issue 1 on generic methods.
/// https://github.com/AutoTestNET/AutoTest.ArgumentNullException/issues/1
/// </summary>
public static class MixedGenericMethods
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static class SimpleGenericMethods
/// </summary>
public static bool GenericMethod2Tested { get; private set; }

/// <summary>
/// Gets a value indicating if the <see cref="GenericExceptionMethod{T}"/> method has been tested.
/// </summary>
public static bool GenericExceptionMethodTested { get; private set; }

public static void GenericMethod1<TStruct>(TStruct value)
where TStruct : struct
{
Expand All @@ -24,11 +29,27 @@ public static void GenericMethod2<TClass>(TClass value)
{
GenericMethod2Tested = false;

if (value != null)
throw new Exception("Shouldn't ever get here.");
if (value == null)
{
GenericMethod2Tested = true;
throw new ArgumentNullException("value");
}

throw new Exception("Shouldn't ever get here.");
}

public static void GenericExceptionMethod<TClass>(TClass value)
where TClass : Exception
{
GenericExceptionMethodTested = false;

GenericMethod2Tested = true;
throw new ArgumentNullException("value");
if (value == null)
{
GenericExceptionMethodTested = true;
throw new ArgumentNullException("value");
}

throw new Exception("Shouldn't ever get here.");
}
}
}

0 comments on commit de1aa90

Please sign in to comment.