From afaa96902ca2ebbe57270cb240f95282662faffc Mon Sep 17 00:00:00 2001 From: Suzi Curran Date: Fri, 22 Oct 2021 15:08:03 -0400 Subject: [PATCH] add arg matching tests to demo behavior of value vs ref types --- .../ArgumentMatching.cs | 64 ++++++++++++++++++- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs index 347ba8b35..56966e7af 100644 --- a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs +++ b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs @@ -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; @@ -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? 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{1, 2}; + _something.Anything(Arg.Is(listToMatch)).Returns(123); + + Assert.That(_something.Anything(listToMatch), Is.EqualTo(123)); + Assert.That(_something.Anything(new List{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() { @@ -233,10 +291,10 @@ public void Should_allow_to_check_received_using_properties_from_other_substitut // Arrange var otherSubs = Substitute.For(); otherSubs.SomeProperty.Returns(42); - + // Act _something.Echo(42); - + // Assert _something.Received().Echo(otherSubs.SomeProperty); }