Reverting ThatCallsBase option until we clarify requirement and syntax

This commit is contained in:
David Tchepak 2013-07-21 18:01:08 +10:00
Родитель ff82035229
Коммит 4a57d0bdd0
10 изменённых файлов: 7 добавлений и 229 удалений

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

@ -71,7 +71,6 @@
<Compile Include="ArgumentInvocationFromMatchers.cs" /> <Compile Include="ArgumentInvocationFromMatchers.cs" />
<Compile Include="ArgDoFromMatcher.cs" /> <Compile Include="ArgDoFromMatcher.cs" />
<Compile Include="AutoValuesForSubs.cs" /> <Compile Include="AutoValuesForSubs.cs" />
<Compile Include="SubstituteTests.cs" />
<Compile Include="SubstituteExtensionsTests.cs" /> <Compile Include="SubstituteExtensionsTests.cs" />
<Compile Include="FieldReports\CallingIntoNewSubWithinReturns.cs" /> <Compile Include="FieldReports\CallingIntoNewSubWithinReturns.cs" />
<Compile Include="FieldReports\Issue110_CustomExceptions.cs" /> <Compile Include="FieldReports\Issue110_CustomExceptions.cs" />

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

@ -1,168 +0,0 @@
using System;
using NSubstitute.Exceptions;
using NUnit.Framework;
namespace NSubstitute.Acceptance.Specs
{
[TestFixture]
public class SubstituteTests
{
[TestFixture]
public class ForTests
{
[TestFixture]
public class ThatCallsBaseByDefaultTests
{
[Test]
public void Given_SubstituteForInterface_When_MethodIsCalled_Then_ShouldThrowException()
{
var testAbstractClass = Substitute.For<ITestInterface>(ThatCallsBase.ByDefault);
Assert.Throws<CouldNotCallBaseException>(testAbstractClass.TestMethod);
}
[Test]
public void Given_SubstituteForAbstractClass_When_AbstractMethodIsCalled_Then_ShouldThrowException()
{
var testAbstractClass = Substitute.For<TestAbstractClass>(ThatCallsBase.ByDefault);
Assert.Throws<CouldNotCallBaseException>(testAbstractClass.AbstractMethod);
}
[Test]
public void Given_SubstituteForAction_When_ActionIsCalled_Then_ShouldThrowException()
{
var action = Substitute.For<Action>(ThatCallsBase.ByDefault);
Assert.Throws<CouldNotCallBaseException>(() => action());
}
[Test]
public void Given_SubstituteForFunc_When_FuncIsCalled_Then_ShouldThrowException()
{
var func = Substitute.For<Func<int>>(ThatCallsBase.ByDefault);
Assert.Throws<CouldNotCallBaseException>(() => func());
}
[Test]
public void Given_SubstituteForEventHandler_When_EventHandlerIsCalled_Then_ShouldThrowException()
{
var eventHandler = Substitute.For<EventHandler>(ThatCallsBase.ByDefault);
Assert.Throws<CouldNotCallBaseException>(() => eventHandler.Invoke(null, null));
}
[Test]
public void Given_SubstituteForAbstractClass_When_MethodWithImplementationIsCalled_Then_ShouldCallBaseImplementation()
{
var testAbstractClass = Substitute.For<TestAbstractClass>(ThatCallsBase.ByDefault);
Assert.That(testAbstractClass.MethodWithImplementation(1), Is.EqualTo(1));
}
[Test]
public void Given_SubstituteForClass_When_VirtualMethodIsCalled_Then_ShouldCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethod();
Assert.That(testClass.CalledTimes, Is.EqualTo(1));
}
[Test]
public void Given_SubstituteForClass_When_AbstractMethodIsCalled_Then_ShouldCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.AbstractMethod();
Assert.That(testClass.CalledTimes, Is.EqualTo(1));
}
[Test]
public void Given_SubstituteForClass_When_NotVirtualMethodIsCalled_Then_ShouldCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.NotVirtualMethodReturnsSameInt(1);
Assert.That(testClass.CalledTimes, Is.EqualTo(1));
}
[Test]
public void Given_SubstituteForClass_When_VirtualMethodIsCalledTwice_Then_ShouldCallBaseImplementationTwice()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethod();
testClass.VirtualMethod();
Assert.That(testClass.CalledTimes, Is.EqualTo(2));
}
[Test]
public void Given_SubstituteForClass_When_VirtualMethodsReturnValueIsOverwritten_Then_ShouldNotCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethodReturnsSameInt(Arg.Any<int>()).Returns(1);
Assert.That(testClass.CalledTimes, Is.EqualTo(0));
}
[Test]
public void Given_SubstituteForClass_And_ReturnValueIsOverwritten_When_VirtualMethodIsCalled_Then_ShouldReturnOverwrittenValue()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethodReturnsSameInt(Arg.Any<int>()).Returns(2);
Assert.That(testClass.VirtualMethodReturnsSameInt(1), Is.EqualTo(2));
}
[Test]
public void Given_SubstituteForClass_And_VirtualMethodReturnValueForSpecifiedArg_When_VirtualMethodIsCalledWithSpecifiedArg_Then_ShouldNotCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethodReturnsSameInt(Arg.Is(1)).Returns(2);
Assert.That(testClass.VirtualMethodReturnsSameInt(1), Is.EqualTo(2));
Assert.That(testClass.CalledTimes, Is.EqualTo(0));
}
[Test]
public void Given_SubstituteForClass_And_VirtualMethodReturnValueForSpecifiedArg_When_VirtualMethodIsCalledWithoutSpecifiedArg_Then_ShouldCallBaseImplementation()
{
var testClass = Substitute.For<TestClass>(ThatCallsBase.ByDefault);
testClass.VirtualMethodReturnsSameInt(Arg.Is(1)).Returns(2);
Assert.That(testClass.VirtualMethodReturnsSameInt(3), Is.EqualTo(3));
Assert.That(testClass.CalledTimes, Is.EqualTo(1));
}
public interface ITestInterface
{
void TestMethod();
}
public abstract class TestAbstractClass
{
public abstract void AbstractMethod();
public int MethodWithImplementation(int i)
{
return i;
}
}
public class TestClass : TestAbstractClass
{
public int CalledTimes { get; set; }
public virtual int VirtualMethodReturnsSameInt(int i)
{
CalledTimes++;
return i;
}
public virtual void VirtualMethod()
{
CalledTimes++;
}
public int NotVirtualMethodReturnsSameInt(int i)
{
CalledTimes++;
return i;
}
public override void AbstractMethod()
{
CalledTimes++;
}
}
}
}
}
}

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

@ -26,7 +26,7 @@ namespace NSubstitute.Specs
public override void Because() public override void Because()
{ {
_result = sut.Create(_method, _args, _target); _result = sut.Create(_method, _args, _target, () => null);
} }
[Test] [Test]

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

@ -11,7 +11,6 @@ namespace NSubstitute.Specs
public abstract class Concern : ConcernFor<ConfigureCall> public abstract class Concern : ConcernFor<ConfigureCall>
{ {
protected ICallActions _callActions; protected ICallActions _callActions;
protected ICallBaseSpecifications _callBaseSpecifications;
protected ICallResults _configuredResults; protected ICallResults _configuredResults;
protected IGetCallSpec _getCallSpec; protected IGetCallSpec _getCallSpec;
protected IReturn _compatibleReturnValue; protected IReturn _compatibleReturnValue;
@ -21,7 +20,6 @@ namespace NSubstitute.Specs
_configuredResults = mock<ICallResults>(); _configuredResults = mock<ICallResults>();
_callActions = mock<ICallActions>(); _callActions = mock<ICallActions>();
_getCallSpec = mock<IGetCallSpec>(); _getCallSpec = mock<IGetCallSpec>();
_callBaseSpecifications = mock<ICallBaseSpecifications>();
_compatibleReturnValue = mock<IReturn>(); _compatibleReturnValue = mock<IReturn>();
_compatibleReturnValue.stub(x => x.CanBeAssignedTo(It.IsAny<Type>())).Return(true); _compatibleReturnValue.stub(x => x.CanBeAssignedTo(It.IsAny<Type>())).Return(true);
@ -29,7 +27,7 @@ namespace NSubstitute.Specs
public override ConfigureCall CreateSubjectUnderTest() public override ConfigureCall CreateSubjectUnderTest()
{ {
return new ConfigureCall(_configuredResults, _callActions, _getCallSpec, _callBaseSpecifications); return new ConfigureCall(_configuredResults, _callActions, _getCallSpec);
} }
} }

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

@ -16,7 +16,7 @@ namespace NSubstitute.Core
_context = context; _context = context;
} }
public ICall Create(MethodInfo methodInfo, object[] arguments, object target, Func<object> baseMethod = null) public ICall Create(MethodInfo methodInfo, object[] arguments, object target, Func<object> baseMethod)
{ {
var argSpecs = (methodInfo.GetParameters().Length == 0) ? EmptyList() : _context.DequeueAllArgumentSpecifications(); var argSpecs = (methodInfo.GetParameters().Length == 0) ? EmptyList() : _context.DequeueAllArgumentSpecifications();
return new Call(methodInfo, arguments, target, argSpecs, baseMethod); return new Call(methodInfo, arguments, target, argSpecs, baseMethod);

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

@ -7,14 +7,12 @@ namespace NSubstitute.Core
private readonly ICallResults _configuredResults; private readonly ICallResults _configuredResults;
private readonly ICallActions _callActions; private readonly ICallActions _callActions;
private readonly IGetCallSpec _getCallSpec; private readonly IGetCallSpec _getCallSpec;
private readonly ICallBaseSpecifications _callBaseSpecifications;
public ConfigureCall(ICallResults configuredResults, ICallActions callActions, IGetCallSpec getCallSpec, ICallBaseSpecifications callBaseSpecifications) public ConfigureCall(ICallResults configuredResults, ICallActions callActions, IGetCallSpec getCallSpec)
{ {
_configuredResults = configuredResults; _configuredResults = configuredResults;
_callActions = callActions; _callActions = callActions;
_getCallSpec = getCallSpec; _getCallSpec = getCallSpec;
_callBaseSpecifications = callBaseSpecifications;
} }
public ConfiguredCall SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs) public ConfiguredCall SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs)

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

@ -35,7 +35,7 @@ namespace NSubstitute.Core
var getCallSpec = new GetCallSpec(callStack, PendingSpecification, CallSpecificationFactory, CallActions); var getCallSpec = new GetCallSpec(callStack, PendingSpecification, CallSpecificationFactory, CallActions);
ConfigureCall = new ConfigureCall(CallResults, CallActions, getCallSpec, CallBaseSpecifications); ConfigureCall = new ConfigureCall(CallResults, CallActions, getCallSpec);
EventHandlerRegistry = new EventHandlerRegistry(); EventHandlerRegistry = new EventHandlerRegistry();
AutoValueProviders = new IAutoValueProvider[] { AutoValueProviders = new IAutoValueProvider[] {
new AutoSubstituteProvider(substituteFactory), new AutoSubstituteProvider(substituteFactory),

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

@ -88,7 +88,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Arg.cs" /> <Compile Include="Arg.cs" />
<Compile Include="ThatCallsBase.cs" />
<Compile Include="Core\Argument.cs" /> <Compile Include="Core\Argument.cs" />
<Compile Include="Core\Arguments\AnyArgumentMatcher.cs" /> <Compile Include="Core\Arguments\AnyArgumentMatcher.cs" />
<Compile Include="Core\Arguments\ArgumentAction.cs" /> <Compile Include="Core\Arguments\ArgumentAction.cs" />
@ -269,4 +268,4 @@
<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) /targetplatform:&quot;v4,$(FrameworkReferenceAssemblyPath).&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v4.0'" /> <exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) /targetplatform:&quot;v4,$(FrameworkReferenceAssemblyPath).&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v4.0'" />
<delete files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" /> <delete files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target> </Target>
</Project> </Project>

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

@ -22,21 +22,6 @@ namespace NSubstitute
return (T) For(new[] {typeof(T)}, constructorArguments); return (T) For(new[] {typeof(T)}, constructorArguments);
} }
/// <summary>
/// Substitute for an interface or class.
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <typeparam name="T">The type of interface or class to substitute.</typeparam>
/// <param name="callBaseBehavior">Represents default substitute behavior. If <see cref="ThatCallsBase.ByDefault"/> is passed then the substitute calls base implementation by default. </param>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <returns>A substitute for the interface or class.</returns>
public static T For<T>(ThatCallsBase callBaseBehavior, params object[] constructorArguments)
where T : class
{
return (T)For(new[] { typeof(T) }, constructorArguments, callBaseBehavior == ThatCallsBase.ByDefault);
}
/// <summary> /// <summary>
/// <para>Substitute for multiple interfaces or a class that implements an interface. At most one class can be specified.</para> /// <para>Substitute for multiple interfaces or a class that implements an interface. At most one class can be specified.</para>
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members /// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
@ -55,7 +40,7 @@ namespace NSubstitute
/// <summary> /// <summary>
/// <para>Substitute for multiple interfaces or a class that implements multiple interfaces. At most one class can be specified.</para> /// <para>Substitute for multiple interfaces or a class that implements multiple interfaces. At most one class can be specified.</para>
/// If additional interfaces are required use the <see cref="M:For(System.Type[], System.Object[])" /> overload. /// If additional interfaces are required use the <see cref="For(System.Type[], System.Object[])" /> overload.
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members /// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para> /// can be recorded or have return values specified.</para>
/// </summary> /// </summary>
@ -85,20 +70,5 @@ namespace NSubstitute
var substituteFactory = SubstitutionContext.Current.SubstituteFactory; var substituteFactory = SubstitutionContext.Current.SubstituteFactory;
return substituteFactory.Create(typesToProxy, constructorArguments); return substituteFactory.Create(typesToProxy, constructorArguments);
} }
/// <summary>
/// <para>Substitute for multiple interfaces or a class that implements multiple interfaces. At most one class can be specified.</para>
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <param name="typesToProxy">The types of interfaces or a type of class and multiple interfaces the substitute should implement.</param>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <param name="callBaseByDefault">If true the substitute calls base implementation by default if it's not overwritten</param>
/// <returns>A substitute implementing the specified types.</returns>
private static object For(Type[] typesToProxy, object[] constructorArguments, bool callBaseByDefault)
{
var substituteFactory = SubstitutionContext.Current.SubstituteFactory;
return substituteFactory.Create(typesToProxy, constructorArguments, callBaseByDefault);
}
} }
} }

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

@ -1,18 +0,0 @@
namespace NSubstitute
{
/// <summary>
/// Represents the substitute behavior regarding base implementation
/// </summary>
public enum ThatCallsBase
{
/// <summary>
/// The substitute calls base implementation only when it's specified explicitly.
/// </summary>
OnlyWhenSpecified = 0,
/// <summary>
/// The substitute calls base implementation by default. Make sure that you substitute for a class.
/// </summary>
ByDefault,
}
}