Skip to content

Commit

Permalink
Merge pull request #671 from suzicurran/tests_for_matching_ref_and_va…
Browse files Browse the repository at this point in the history
…lue_types

Add argument matching tests to demonstrate matching of value and reference types
  • Loading branch information
dtchepak authored Nov 24, 2021
2 parents b119cc3 + afaa969 commit e15f0f4
Showing 1 changed file with 61 additions and 3 deletions.
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

0 comments on commit e15f0f4

Please sign in to comment.