Skip to content

Commit e15f0f4

Browse files
authored
Merge pull request #671 from suzicurran/tests_for_matching_ref_and_value_types
Add argument matching tests to demonstrate matching of value and reference types
2 parents b119cc3 + afaa969 commit e15f0f4

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using NSubstitute.Acceptance.Specs.Infrastructure;
34
using NSubstitute.Core;
45
using NSubstitute.Core.Arguments;
@@ -52,6 +53,63 @@ public void Should_not_match_when_arg_matcher_throws()
5253
Assert.That(_something.Say(null), Is.EqualTo(string.Empty));
5354
}
5455

56+
[Test]
57+
public void Should_match_value_types_by_content()
58+
{
59+
const int intToMatch = 123;
60+
const int identicalInt = 123;
61+
_something.Echo(Arg.Is(intToMatch)).Returns("matching int");
62+
63+
Assert.That(_something.Echo(intToMatch), Is.EqualTo("matching int"));
64+
Assert.That(_something.Echo(identicalInt), Is.EqualTo("matching int"));
65+
66+
var dateToMatch = new DateTime(2021, 10, 22);
67+
var identicalDate = new DateTime(2021, 10, 22);
68+
_something.Anything(dateToMatch).Returns(20211022);
69+
70+
Assert.That(_something.Anything(dateToMatch), Is.EqualTo(20211022));
71+
Assert.That(_something.Anything(identicalDate), Is.EqualTo(20211022));
72+
}
73+
74+
[Test]
75+
public void Should_match_strings_by_content()
76+
{
77+
const string stringToMatch = "hello";
78+
_something.Say(Arg.Is(stringToMatch)).Returns("hi");
79+
80+
Assert.That(_something.Say(stringToMatch), Is.EqualTo("hi"));
81+
Assert.That(_something.Say("hello"), Is.EqualTo("hi"));
82+
}
83+
84+
[Test]
85+
public void Should_match_nullable_ref_types_by_content()
86+
{
87+
#nullable enable
88+
SomeClass? nullClassToMatch = null;
89+
List<int>? nullList = null;
90+
_something.Anything(Arg.Is(nullClassToMatch)).Returns(456);
91+
92+
Assert.That(_something.Anything(nullClassToMatch), Is.EqualTo(456));
93+
Assert.That(_something.Anything(nullList), Is.EqualTo(456));
94+
#nullable disable
95+
}
96+
97+
[Test]
98+
public void Should_match_non_string_non_record_ref_types_by_reference()
99+
{
100+
var listToMatch = new List<int>{1, 2};
101+
_something.Anything(Arg.Is(listToMatch)).Returns(123);
102+
103+
Assert.That(_something.Anything(listToMatch), Is.EqualTo(123));
104+
Assert.That(_something.Anything(new List<int>{1, 2}), Is.EqualTo(0));
105+
106+
var classToMatch = new SomeClass();
107+
_something.Anything(Arg.Is(classToMatch)).Returns(456);
108+
109+
Assert.That(_something.Anything(classToMatch), Is.EqualTo(456));
110+
Assert.That(_something.Anything(new SomeClass()), Is.EqualTo(0));
111+
}
112+
55113
[Test]
56114
public void Return_result_with_only_one_matcher_for_that_type()
57115
{
@@ -233,10 +291,10 @@ public void Should_allow_to_check_received_using_properties_from_other_substitut
233291
// Arrange
234292
var otherSubs = Substitute.For<ISomething>();
235293
otherSubs.SomeProperty.Returns(42);
236-
294+
237295
// Act
238296
_something.Echo(42);
239-
297+
240298
// Assert
241299
_something.Received().Echo(otherSubs.SomeProperty);
242300
}

0 commit comments

Comments
 (0)