Skip to content

Commit

Permalink
Handle Generic Types with default constructor #1
Browse files Browse the repository at this point in the history
  • Loading branch information
JSkimming committed Aug 9, 2013
1 parent ac81498 commit 6349a48
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 32 deletions.
11 changes: 11 additions & 0 deletions src/AutoTest.ArgumentNullException/GenericTypeConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ private static bool TrySimple(Type genericType, out Type nonGenericType)
nonGenericType = constraint;
return true;
}

// If there is a single constraint, the generic type requires a default constructor and the constraint
// has a default constructor, just use the constraint as the non generic type.
if (attributes == GenericParameterAttributes.DefaultConstructorConstraint)
{
if (constraint.GetConstructor(Type.EmptyTypes) != null)
{
nonGenericType = constraint;
return true;
}
}
}

nonGenericType = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AutoTest.ArgNullEx.Lock
{
using System;
using System.Threading;
using global::Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AutoTest.ArgNullEx.Lock
{
using System;
using System.Threading;
using global::Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,56 @@ public async Task InterfaceStringValue(MethodData method)
Assert.True(InterfaceGenericMethods.StringValueTested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(ComplexGenericMethods))]
[Theory, RequiresArgNullExAutoMoq(typeof(InterfaceGenericMethods))]
[Include(
ExclusionType = ExclusionType.All,
Type = typeof(ComplexGenericMethods),
Method = "GenericClassMethod",
Parameter = "classValue")]
public async Task ComplexClassValue(MethodData method)
{
await method.Execute();

Assert.True(ComplexGenericMethods.ClassValueTested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(InterfaceGenericMethods))]
[Include(
ExclusionType = ExclusionType.All,
Type = typeof(ComplexGenericMethods),
Method = "GenericClassMethod",
Parameter = "genericClassMethodStringValue")]
public async Task ComplexGenericClassMethodStringValue(MethodData method)
{
await method.Execute();

Assert.True(ComplexGenericMethods.GenericClassMethodStringValueTested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(InterfaceGenericMethods))]
[Include(
ExclusionType = ExclusionType.Parameters,
ExclusionType = ExclusionType.All,
Type = typeof(ComplexGenericMethods),
Method = "GenericExceptionMethod",
Parameter = "exceptionValue")]
public async Task ComplexExceptionValue(MethodData method)
{
await method.Execute();

Assert.True(ComplexGenericMethods.ExceptionValueTested);
}

[Theory, RequiresArgNullExAutoMoq(typeof(InterfaceGenericMethods))]
[Include(
ExclusionType = ExclusionType.All,
Type = typeof(ComplexGenericMethods),
Method = "NonGenericMethod",
Parameter = "value")]
public async Task ComplexNonGeneric(MethodData method)
Method = "GenericExceptionMethod",
Parameter = "genericExceptionMethodStringValue")]
public async Task ComplexGenericExceptionMethodStringValue(MethodData method)
{
await method.Execute();

Assert.True(ComplexGenericMethods.NonGenericTested);
Assert.True(ComplexGenericMethods.GenericExceptionMethodStringValueTested);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using AutoTest.ArgNullEx;
using AutoTest.ArgNullEx.Xunit;
using AutoTest.ExampleLibrary.Issues.Issue001;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;

Expand All @@ -30,8 +29,7 @@ private static IArgumentNullExceptionFixture CreateFixture(Assembly assemblyUnde
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

return new ArgumentNullExceptionFixture(assemblyUnderTest, fixture)
.ExcludeType(typeof(ComplexGenericMethods)); // Exclude until it can be tested.
return new ArgumentNullExceptionFixture(assemblyUnderTest, fixture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
/// </summary>
public static class ComplexGenericMethods
{
/// <summary>
/// Gets a value indicating if the <see cref="NonGenericMethod"/> value parameter has been tested.
/// </summary>
public static bool NonGenericTested { get; private set; }

public interface ITest1
{
string String1 { get; set; }
Expand All @@ -23,33 +18,61 @@ public interface ITest2
string String2 { get; set; }
}

public static void NonGenericMethod(string value)
/// <summary>
/// Gets a value indicating if the <see cref="GenericClassMethod{T}"/> classValue parameter has been tested.
/// </summary>
public static bool ClassValueTested { get; private set; }

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

public static void GenericClassMethod<TClass>(TClass classValue, string genericClassMethodStringValue)
where TClass : class, ITest1, ITest2
{
NonGenericTested = false;
if (value == null)
ClassValueTested = GenericClassMethodStringValueTested = false;

if (classValue == null)
{
NonGenericTested = true;
throw new ArgumentNullException("value");
ClassValueTested = true;
throw new ArgumentNullException("classValue");
}
if (genericClassMethodStringValue == null)
{
GenericClassMethodStringValueTested = true;
throw new ArgumentNullException("genericClassMethodStringValue");
}

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

public static void GenericClassMethod<TClass>(TClass classValue, string stringValue)
where TClass : class, ITest1, ITest2
{
throw new Exception("Shouldn't ever get here.");
}
/// <summary>
/// Gets a value indicating if the <see cref="GenericExceptionMethod{T}"/> exceptionValue parameter has been tested.
/// </summary>
public static bool ExceptionValueTested { get; private set; }

public static void GenericExceptionMethod<TException>(TException classValue, string stringValue)
/// <summary>
/// Gets a value indicating if the <see cref="GenericExceptionMethod{T}"/> genericExceptionMethodStringValue parameter has been tested.
/// </summary>
public static bool GenericExceptionMethodStringValueTested { get; private set; }

public static void GenericExceptionMethod<TException>(TException exceptionValue, string genericExceptionMethodStringValue)
where TException : Exception, new()
{
throw new Exception("Shouldn't ever get here.");
}
ExceptionValueTested = GenericExceptionMethodStringValueTested = false;

if (exceptionValue == null)
{
ExceptionValueTested = true;
throw new ArgumentNullException("exceptionValue");
}
if (genericExceptionMethodStringValue == null)
{
GenericExceptionMethodStringValueTested = true;
throw new ArgumentNullException("genericExceptionMethodStringValue");
}

public static void GenericStructMethod<TStruct>(TStruct classValue, string stringValue)
where TStruct : struct, ITest1, ITest2
{
throw new Exception("Shouldn't ever get here.");
}
}
Expand Down

0 comments on commit 6349a48

Please sign in to comment.