Excluded static methods when determining if a type is pure virtual (Fixes #118)

Also added a (already passing) test that checks that a substitute for a concrete with a static method can setup a return value to improve coverage of this type of class
This commit is contained in:
Robert Moore 2013-10-24 21:20:42 +08:00
Родитель 9ebc8b1981
Коммит 969cce38de
3 изменённых файлов: 36 добавлений и 1 удалений

Просмотреть файл

@ -47,6 +47,18 @@ namespace NSubstitute.Acceptance.Specs.FieldReports
Assert.That(returnedClass, Is.SameAs(obj)); Assert.That(returnedClass, Is.SameAs(obj));
} }
[Test]
public void Substitute_of_concrete_with_static_member_should_allow_setup_of_return_value()
{
const string value = "test";
var sub = Substitute.For<ConcreteWithPublicStaticMethod>();
sub.AProperty.Returns(value);
var returnedValue = sub.AProperty;
Assert.That(returnedValue, Is.EqualTo(value));
}
} }
public class ConcreteWithPublicStaticMethod public class ConcreteWithPublicStaticMethod

Просмотреть файл

@ -29,6 +29,9 @@ namespace NSubstitute.Specs.Routing.AutoValues
[TestCase(typeof(TestClasses.PureVirtualClassWithVirtualInterfaceImpl), true)] [TestCase(typeof(TestClasses.PureVirtualClassWithVirtualInterfaceImpl), true)]
[TestCase(typeof(TestClasses.PureDescendentOfPureVirtualClass), true)] [TestCase(typeof(TestClasses.PureDescendentOfPureVirtualClass), true)]
[TestCase(typeof(TestClasses.PureVirtualClassWithAPublicField), true)] [TestCase(typeof(TestClasses.PureVirtualClassWithAPublicField), true)]
[TestCase(typeof(TestClasses.PureVirtualClassWithAPublicStaticMethod), true)]
[TestCase(typeof(TestClasses.PureVirtualClassWithAPublicStaticField), true)]
[TestCase(typeof(TestClasses.PureVirtualClassWithAPublicStaticProperty), true)]
[TestCase(typeof(TestClasses.PureVirtualClassWithoutParameterlessConstructor), false)] [TestCase(typeof(TestClasses.PureVirtualClassWithoutParameterlessConstructor), false)]
[TestCase(typeof(TestClasses.ClassWithANonVirtualPublicMember), false)] [TestCase(typeof(TestClasses.ClassWithANonVirtualPublicMember), false)]
[TestCase(typeof(TestClasses.ClassWithNonVirtualInterfaceImpl), false)] [TestCase(typeof(TestClasses.ClassWithNonVirtualInterfaceImpl), false)]
@ -132,6 +135,21 @@ namespace NSubstitute.Specs.Routing.AutoValues
} }
public class VirtualClassWithInternalConstructor { internal VirtualClassWithInternalConstructor() { } } public class VirtualClassWithInternalConstructor { internal VirtualClassWithInternalConstructor() { } }
public class PureVirtualClassWithAPublicStaticMethod
{
public static void StaticMethod() {}
}
public class PureVirtualClassWithAPublicStaticProperty
{
public static string StaticProperty { get; set; }
}
public class PureVirtualClassWithAPublicStaticField
{
public string StaticField;
}
} }
} }
} }

Просмотреть файл

@ -45,7 +45,7 @@ namespace NSubstitute.Routing.AutoValues
private bool IsPureVirtualType(Type type) private bool IsPureVirtualType(Type type)
{ {
var methods = type.GetMethods().Where(NotMethodFromObject); var methods = type.GetMethods().Where(NotMethodFromObject).Where(NotStaticMethod);
return methods.All(IsOverridable); return methods.All(IsOverridable);
} }
@ -63,5 +63,10 @@ namespace NSubstitute.Routing.AutoValues
{ {
return methodInfo.DeclaringType != typeof(object); return methodInfo.DeclaringType != typeof(object);
} }
private bool NotStaticMethod(MethodInfo methodInfo)
{
return !methodInfo.IsStatic;
}
} }
} }