Skip to content

Commit

Permalink
Fixes machine#290 - inheriting from generic class's nested class at ref
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleSouffle committed Mar 29, 2016
1 parent cdd8d63 commit 1c15b9c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Source/Machine.Specifications.Tests/ExampleSpecifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,32 @@ public void Reset()
{
}
}

public class OuterNonGenericContext
{
public class Nested
{ }
}

public class OuterGenericContext<T1, T2, T3>
{
public class NestedBase<TN1, TN2> : OuterGenericContext<TN1, string, TN2>
{}

public class Nested : NestedBase<char, bool>
{}
}

public class ContextInheritingFromNestedGeneric : OuterGenericContext<int, string, float>.Nested, IFakeContext
{
public static bool ItInvoked;

It should_be_invoked = () =>
ItInvoked = true;

public void Reset()
{
ItInvoked = false;
}
}
}
18 changes: 18 additions & 0 deletions Source/Machine.Specifications.Tests/Model/ContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,22 @@ public void ShouldCleanup()
ContextWithSingleSpecification.CleanupInvoked.Should().BeTrue();
}
}

[TestFixture]
public class InheritingFromNestedGenericTests : With<ContextInheritingFromNestedGeneric>
{
IEnumerable<Result> results;

public override void BeforeEachTest()
{
base.BeforeEachTest();
results = Run(context);
}

[Test]
public void ShouldInvokeIt()
{
ContextInheritingFromNestedGeneric.ItInvoked.Should().BeTrue();
}
}
}
27 changes: 26 additions & 1 deletion Source/Machine.Specifications/Factories/ContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,32 @@ static void CollectDetailsOf<T>(Type target, Func<object> instanceResolver, ICol
CollectDetailsOf(target.BaseType, () => instance, items, ensureMaximumOfOne, attributeFullName);
}

CollectDetailsOf(target.DeclaringType, () => Activator.CreateInstance(target.DeclaringType), items, ensureMaximumOfOne, attributeFullName);
CollectDetailsOf(target.DeclaringType, () => CreateDeclaringTypeInstance(target),
items, ensureMaximumOfOne, attributeFullName);
}

static object CreateDeclaringTypeInstance(Type target)
{
var instantiatingType = target.DeclaringType;
object declaringTypeInstance = null;
if (instantiatingType != typeof(object))
{
if (instantiatingType.IsGenericType)
{
var genericTypeDefinition = target.GetGenericTypeDefinition();
if (genericTypeDefinition == null)
{
throw new InvalidOperationException(string.Format("Unable to get generic type definition for {0}", target));
}
var genericArgs = target.GetGenericArguments().ToArray();
if (genericArgs.Length != 0)
{
instantiatingType = genericTypeDefinition.MakeGenericType(genericArgs);
}
}
declaringTypeInstance = Activator.CreateInstance(instantiatingType);
}
return declaringTypeInstance;
}

static bool IsStatic(Type target)
Expand Down

0 comments on commit 1c15b9c

Please sign in to comment.