Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument matching tests to demonstrate matching of value and reference types #671

Merged
Merged
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
64 changes: 61 additions & 3 deletions tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using NSubstitute.Acceptance.Specs.Infrastructure;
using NSubstitute.Core;
using NSubstitute.Core.Arguments;
Expand Down Expand Up @@ -52,6 +53,63 @@ public void Should_not_match_when_arg_matcher_throws()
Assert.That(_something.Say(null), Is.EqualTo(string.Empty));
}

[Test]
public void Should_match_value_types_by_content()
{
const int intToMatch = 123;
const int identicalInt = 123;
_something.Echo(Arg.Is(intToMatch)).Returns("matching int");

Assert.That(_something.Echo(intToMatch), Is.EqualTo("matching int"));
Assert.That(_something.Echo(identicalInt), Is.EqualTo("matching int"));

var dateToMatch = new DateTime(2021, 10, 22);
var identicalDate = new DateTime(2021, 10, 22);
_something.Anything(dateToMatch).Returns(20211022);

Assert.That(_something.Anything(dateToMatch), Is.EqualTo(20211022));
Assert.That(_something.Anything(identicalDate), Is.EqualTo(20211022));
}

[Test]
public void Should_match_strings_by_content()
{
const string stringToMatch = "hello";
_something.Say(Arg.Is(stringToMatch)).Returns("hi");

Assert.That(_something.Say(stringToMatch), Is.EqualTo("hi"));
Assert.That(_something.Say("hello"), Is.EqualTo("hi"));
}

[Test]
public void Should_match_nullable_ref_types_by_content()
{
#nullable enable
SomeClass? nullClassToMatch = null;
List<int>? nullList = null;
_something.Anything(Arg.Is(nullClassToMatch)).Returns(456);

Assert.That(_something.Anything(nullClassToMatch), Is.EqualTo(456));
Assert.That(_something.Anything(nullList), Is.EqualTo(456));
#nullable disable
}

[Test]
public void Should_match_non_string_non_record_ref_types_by_reference()
{
var listToMatch = new List<int>{1, 2};
_something.Anything(Arg.Is(listToMatch)).Returns(123);

Assert.That(_something.Anything(listToMatch), Is.EqualTo(123));
Assert.That(_something.Anything(new List<int>{1, 2}), Is.EqualTo(0));

var classToMatch = new SomeClass();
_something.Anything(Arg.Is(classToMatch)).Returns(456);

Assert.That(_something.Anything(classToMatch), Is.EqualTo(456));
Assert.That(_something.Anything(new SomeClass()), Is.EqualTo(0));
}

[Test]
public void Return_result_with_only_one_matcher_for_that_type()
{
Expand Down Expand Up @@ -233,10 +291,10 @@ public void Should_allow_to_check_received_using_properties_from_other_substitut
// Arrange
var otherSubs = Substitute.For<ISomething>();
otherSubs.SomeProperty.Returns(42);

// Act
_something.Echo(42);

// Assert
_something.Received().Echo(otherSubs.SomeProperty);
}
Expand Down