Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/NSubstitute/Arg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public static ref T Is<T>(IArgumentMatcher matcher) =>
public static ref T Is<T>(IArgumentMatcher<T> matcher) =>
ref ArgumentMatcher.Enqueue(matcher);

/// <summary>
/// Match argument that has the same reference as <paramref name="value"/>.
/// </summary>
public static ref T Same<T>(T value)
where T : class
{
return ref ArgumentMatcher.Enqueue(new ReferenceArgumentMatcher<T>(value));
}

/// <summary>
/// Invoke any <see cref="Action"/> argument whenever a matching call is made to the substitute.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/NSubstitute/Compatibility/Arg.Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public static class Compat
/// </summary>
public static T Is<T>(IArgumentMatcher<T> matcher) => ArgumentMatcher.Enqueue(matcher);

/// <summary>
/// Match argument that has the same reference as <paramref name="value"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Same{T}(T)" /> instead.
/// </summary>
public static T Same<T>(T value) where T : class => Arg.Same(value);

/// <summary>
/// Invoke any <see cref="Action"/> argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
Expand Down
7 changes: 7 additions & 0 deletions src/NSubstitute/Compatibility/CompatArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ private CompatArg() { }
/// </summary>
public static T Is<T>(IArgumentMatcher<T> matcher) => ArgumentMatcher.Enqueue(matcher);

/// <summary>
/// Match argument that has the same reference as <paramref name="value"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Same{T}(T)" /> instead.
/// </summary>
public T Same<T>(T value) where T : class => Arg.Same(value);

/// <summary>
/// Invoke any <see cref="Action"/> argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
Expand Down
17 changes: 17 additions & 0 deletions src/NSubstitute/Core/Arguments/ReferenceArgumentMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NSubstitute.Core.Arguments;

public class ReferenceArgumentMatcher<T>(T value) : IArgumentMatcher<T>, IDescribeNonMatches
where T : class
{
public override string ToString() => ArgumentFormatter.Default.Format(value, false);

public string DescribeFor(object? argument)
{
var expected = ArgumentFormatter.Default.Format(value, false);
var actual = ArgumentFormatter.Default.Format(argument, false);

return $"Reference of {expected} does not match reference of {actual}.";
}

public bool IsSatisfiedBy(T? argument) => ReferenceEquals(argument, value);
}
9 changes: 9 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ public void Should_add_list_of_all_pending_specifications_to_ambiguous_exception
Assert.That(exception.Message, Contains.Substring("42"));
}

[Test]
public void Should_match_identical_objects()
{
var obj = new object();
_something.Anything(Arg.Same(obj)).Returns(1);
Assert.That(_something.Anything(obj), Is.EqualTo(1));
Assert.That(_something.Anything(new object()), Is.EqualTo(0));
}

[Test]
public void Returns_should_work_with_params()
{
Expand Down
9 changes: 9 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ArgumentMatchingCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ public void Should_add_list_of_all_pending_specifications_to_ambiguous_exception
Assert.That(exception.Message, Contains.Substring("42"));
}

[Test]
public void Should_match_identical_objects()
{
var obj = new object();
_something.Anything(Arg.Compat.Same(obj)).Returns(1);
Assert.That(_something.Anything(obj), Is.EqualTo(1));
Assert.That(_something.Anything(new object()), Is.EqualTo(0));
}

[Test]
public void Returns_should_work_with_params()
{
Expand Down