Skip to content

Commit

Permalink
Merge pull request #1251 from zvirja/fix-nsub-value-rewrite
Browse files Browse the repository at this point in the history
To not override ref/out if was modified to same value
  • Loading branch information
zvirja committed Apr 1, 2021
2 parents 359a3b0 + 6489cec commit 7e26b35
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Src/AutoFoq/FoqMethodQuery.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ type FoqMethodQuery(builder : ISpecimenBuilder) =
/// <summary>
/// Selects methods for the supplied type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="targetType">The type.</param>
/// <returns>
/// Constructors for <paramref name="type"/>.
/// Constructors for <paramref name="targetType"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method returns a sequence of <see cref="IMethod"/> according to
/// the public and protected constructors available on
/// <paramref name="type"/>.
/// <paramref name="targetType"/>.
/// </para>
/// </remarks>
member this.SelectMethods targetType =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using NSubstitute.Core;

namespace AutoFixture.AutoNSubstitute.CustomCallHandler
Expand Down Expand Up @@ -77,7 +76,7 @@ public RouteAction Handle(ICall call)

private static bool ArgValueWasModified(object current, object original)
{
return !EqualityComparer<object>.Default.Equals(current, original);
return !ReferenceEquals(current, original);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,34 @@ public void ShouldNotUpdateModifiedRefArgument()
Assert.Equal(42, callArgs[0]);
}

[Fact]
public void ShouldNotUpdateModifiedRefArgumentIfUpdatedToSame()
{
// Arrange
AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

var target = Substitute.For<IInterfaceWithRefVoidMethod>();

int origValue = 10;
var call = CallHelper.CreateCallMock(() => target.Method(ref origValue));
call.GetArguments()[0] = 10;

var callArgs = call.GetArguments();

sut.ResultResolver
.ResolveResult(call)
.Returns(
new CallResultData(
Maybe.Nothing<object>(),
new[] { new CallResultData.ArgumentValue(0, 84) }));

// Act
sut.Handle(call);

// Assert
Assert.Equal(10, callArgs[0]);
}

[Fact]
public void ShouldNotUpdateModifiedOutArgument()
{
Expand All @@ -230,6 +258,32 @@ public void ShouldNotUpdateModifiedOutArgument()
Assert.Equal(42, call.GetArguments()[0]);
}

[Fact]
public void ShouldNotUpdateModifiedOutArgumentIfUpdatedToDefault()
{
// Arrange
AutoFixtureValuesHandler sut = CreateSutWithMockedDependencies();

var target = Substitute.For<IInterfaceWithOutVoidMethod>();

int origValue;
var call = CallHelper.CreateCallMock(() => target.Method(out origValue));
call.GetArguments()[0] = 0;

sut.ResultResolver
.ResolveResult(call)
.Returns(
new CallResultData(
Maybe.Nothing<object>(),
new[] { new CallResultData.ArgumentValue(0, 84) }));

// Act
sut.Handle(call);

// Assert
Assert.Equal(0, call.GetArguments()[0]);
}

[Fact]
public void ShouldNotSetPropertyIfResultNotResolved()
{
Expand Down
1 change: 0 additions & 1 deletion Src/Idioms.FsCheck/ReturnValueMustNotBeNullAssertion.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type ReturnValueMustNotBeNullAssertion (builder) =
/// <summary>
/// Verifies that the return value for a method is not null.
/// </summary>
/// <remarks>
/// <param name="methodInfo">The method to verify.</param>
override this.Verify (methodInfo : MethodInfo) =
if methodInfo = null then
Expand Down

0 comments on commit 7e26b35

Please sign in to comment.