|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using NSubstitute.Acceptance.Specs.Infrastructure; |
3 | 4 | using NSubstitute.Core; |
4 | 5 | using NSubstitute.Core.Arguments; |
@@ -52,6 +53,63 @@ public void Should_not_match_when_arg_matcher_throws() |
52 | 53 | Assert.That(_something.Say(null), Is.EqualTo(string.Empty)); |
53 | 54 | } |
54 | 55 |
|
| 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 | + |
55 | 113 | [Test] |
56 | 114 | public void Return_result_with_only_one_matcher_for_that_type() |
57 | 115 | { |
@@ -233,10 +291,10 @@ public void Should_allow_to_check_received_using_properties_from_other_substitut |
233 | 291 | // Arrange |
234 | 292 | var otherSubs = Substitute.For<ISomething>(); |
235 | 293 | otherSubs.SomeProperty.Returns(42); |
236 | | - |
| 294 | + |
237 | 295 | // Act |
238 | 296 | _something.Echo(42); |
239 | | - |
| 297 | + |
240 | 298 | // Assert |
241 | 299 | _something.Received().Echo(otherSubs.SomeProperty); |
242 | 300 | } |
|
0 commit comments