Skip to content

Commit ea304cb

Browse files
authored
refactor!: rename Match to It (#280)
This PR refactors the mocking library's parameter matching API by renaming the `Match` class to `It`. This is a breaking change that aligns the library's API naming with industry-standard mocking libraries. The changes involve systematically replacing method calls like `Any<T>()`, `With<T>()`, etc. from the `Match` class to their equivalents in the new `It` class (e.g., `It.IsAny<T>()`, `It.Is<T>()`, etc.). ### Key Changes - Renamed parameter matching methods from `Match.Any<T>()` to `It.IsAny<T>()` - Renamed parameter matching methods from `Match.With<T>()` to `It.Is<T>()` - Updated special parameter matching methods like `Null<T>()` to `It.IsNull<T>()` - Updated span-related matchers like `WithSpan<T>()` to `It.IsSpan<T>()` - Updated out/ref parameter matchers like `Out<T>()` to `It.IsOut<T>()` - Removed global using for `Match` class - Retained `Match.AnyParameters()` and `Match.WithDefaultParameters()` (not moved to `It`)
1 parent 13ead0b commit ea304cb

File tree

113 files changed

+2035
-1926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+2035
-1926
lines changed

Benchmarks/Mockolate.Benchmarks/HappyCaseBenchmarks.Simple.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using BenchmarkDotNet.Attributes;
22
using FakeItEasy;
33
using Mockolate.Verify;
4-
using Moq;
54
using NSubstitute;
6-
using static Mockolate.Match;
75
using Times = Moq.Times;
86

97
namespace Mockolate.Benchmarks;
@@ -21,11 +19,11 @@ public partial class HappyCaseBenchmarks
2119
public void Simple_Mockolate()
2220
{
2321
IMyInterface mock = Mock.Create<IMyInterface>();
24-
mock.SetupMock.Method.MyFunc(Any<int>()).Returns(true);
22+
mock.SetupMock.Method.MyFunc(It.IsAny<int>()).Returns(true);
2523

2624
mock.MyFunc(42);
2725

28-
mock.VerifyMock.Invoked.MyFunc(Any<int>()).Once();
26+
mock.VerifyMock.Invoked.MyFunc(It.IsAny<int>()).Once();
2927
}
3028

3129
/// <summary>
@@ -35,11 +33,11 @@ public void Simple_Mockolate()
3533
public void Simple_Moq()
3634
{
3735
Moq.Mock<IMyInterface> mock = new();
38-
mock.Setup(x => x.MyFunc(It.IsAny<int>())).Returns(true);
36+
mock.Setup(x => x.MyFunc(Moq.It.IsAny<int>())).Returns(true);
3937

4038
mock.Object.MyFunc(42);
4139

42-
mock.Verify(x => x.MyFunc(It.IsAny<int>()), Times.Once());
40+
mock.Verify(x => x.MyFunc(Moq.It.IsAny<int>()), Times.Once());
4341
}
4442

4543
/// <summary>

Docs/pages/00-index.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ Framework 4.8.
2222
2. Create and use the mock
2323
```csharp
2424
using Mockolate;
25-
using static Mockolate.Match;
2625

2726
// Create a mock for IChocolateDispenser
2827
var sut = Mock.Create<IChocolateDispenser>();
2928

3029
// Setup: Initial stock of 10 for Dark chocolate
31-
sut.SetupMock.Indexer(With("Dark")).InitializeWith(10);
30+
sut.SetupMock.Indexer(It.Is("Dark")).InitializeWith(10);
3231
// Setup: Dispense decreases Dark chocolate if enough, returns true/false
33-
sut.SetupMock.Method.Dispense(With("Dark"), Any<int>())
32+
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
3433
.Returns((type, amount) =>
3534
{
3635
var current = sut[type];
@@ -53,7 +52,7 @@ Framework 4.8.
5352
bool gotChoc3 = sut.Dispense("Dark", 6); // false
5453
5554
// Verify: Check interactions
56-
sut.VerifyMock.Invoked.Dispense(With("Dark"), Any<int>()).Exactly(3);
55+
sut.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.IsAny<int>()).Exactly(3);
5756

5857
// Output: "Dispensed amount: 9. Got chocolate? True, True, False"
5958
Console.WriteLine($"Dispensed amount: {dispensedAmount}. Got chocolate? {gotChoc1}, {gotChoc2}, {gotChoc3}");

Docs/pages/02-setup.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Use `mock.SetupMock.Method.MethodName(…)` to set up methods. You can specify a
99

1010
```csharp
1111
// Setup Dispense to decrease stock and raise event
12-
sut.SetupMock.Method.Dispense(With("Dark"), Any<int>())
12+
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
1313
.Returns((type, amount) =>
1414
{
1515
var current = sut[type];
@@ -23,11 +23,11 @@ sut.SetupMock.Method.Dispense(With("Dark"), Any<int>())
2323
});
2424

2525
// Setup method with callback
26-
sut.SetupMock.Method.Dispense(With("White"), Any<int>())
26+
sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>())
2727
.Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate."));
2828

2929
// Setup method to throw
30-
sut.SetupMock.Method.Dispense(With("Green"), Any<int>())
30+
sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny<int>())
3131
.Throws<InvalidChocolateException>();
3232
```
3333

@@ -46,19 +46,21 @@ sut.SetupMock.Method.Dispense(With("Green"), Any<int>())
4646
For `Task<T>` or `ValueTask<T>` methods, use `.ReturnsAsync(…)`:
4747

4848
```csharp
49-
sut.SetupMock.Method.DispenseAsync(Any<string>(), Any<int>())
49+
sut.SetupMock.Method.DispenseAsync(It.IsAny<string>(), It.IsAny<int>())
5050
.ReturnsAsync(true);
5151
```
5252

5353
#### Parameter Matching
5454

5555
Mockolate provides flexible parameter matching for method setups and verifications:
5656

57-
- `Match.Any<T>()`: Matches any value of type `T`.
58-
- `Match.With<T>(predicate)`: Matches values based on a predicate.
59-
- `Match.With<T>(value)`: Matches a specific value.
60-
- `Match.Null<T>()`: Matches null.
61-
- `Match.Out<T>(…)`/`Match.Ref<T>(…)`: Matches and sets out/ref parameters, supports value setting and
57+
- `It.IsAny<T>()`: Matches any value of type `T`.
58+
- `It.Is<T>(predicate)`: Matches values based on a predicate.
59+
- `It.Is<T>(value)`: Matches a specific value.
60+
- `It.IsNull<T>()`: Matches null.
61+
- `It.IsTrue()`/`It.IsFalse()`: Matches boolean true/false.
62+
- `It.IsInRange(min, max)`: Matches a number within the given range. You can append `.Exclusive()` to exclude the minimum and maximum value.
63+
- `It.IsOut<T>(…)`/`It.IsRef<T>(…)`: Matches and sets out/ref parameters, supports value setting and
6264
predicates.
6365

6466
#### Parameter Interaction
@@ -71,7 +73,7 @@ values passed during test execution and analyze them afterwards.
7173

7274
```csharp
7375
int lastAmount = 0;
74-
sut.SetupMock.Method.Dispense(With("Dark"), Any<int>().Do(amount => lastAmount = amount));
76+
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>().Do(amount => lastAmount = amount));
7577
sut.Dispense("Dark", 42);
7678
// lastAmount == 42
7779
```
@@ -80,7 +82,7 @@ sut.Dispense("Dark", 42);
8082

8183
```csharp
8284
Mockolate.ParameterMonitor<int> monitor;
83-
sut.SetupMock.Method.Dispense(With("Dark"), Any<int>().Monitor(out monitor));
85+
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>().Monitor(out monitor));
8486
sut.Dispense("Dark", 5);
8587
sut.Dispense("Dark", 7);
8688
// monitor.Values == [5, 7]
@@ -125,11 +127,11 @@ sut.SetupMock.Property.TotalDispensed.OnSet((oldValue, newValue) => Console.Writ
125127
Set up indexers with argument matchers. Supports initialization, returns/throws sequences, and callbacks.
126128

127129
```csharp
128-
sut.SetupMock.Indexer(Any<string>())
130+
sut.SetupMock.Indexer(It.IsAny<string>())
129131
.InitializeWith(type => 20)
130132
.OnGet(type => Console.WriteLine($"Stock for {type} was read"));
131133

132-
sut.SetupMock.Indexer(With("Dark"))
134+
sut.SetupMock.Indexer(It.Is("Dark"))
133135
.InitializeWith(10)
134136
.OnSet((value, type) => Console.WriteLine($"Set [{type}] to {value}"));
135137
```

Docs/pages/04-verify-interactions.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ You can verify that methods were invoked with specific arguments and how many ti
2222

2323
```csharp
2424
// Verify that Dispense("Dark", 5) was invoked at least once
25-
sut.VerifyMock.Invoked.Dispense(With("Dark"), With(5)).AtLeastOnce();
25+
sut.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(5)).AtLeastOnce();
2626

2727
// Verify that Dispense was never invoked with "White" and any amount
28-
sut.VerifyMock.Invoked.Dispense(With("White"), Any<int>()).Never();
28+
sut.VerifyMock.Invoked.Dispense(It.Is("White"), It.IsAny<int>()).Never();
2929

3030
// Verify that Dispense was invoked exactly twice with any type and any amount
31-
sut.VerifyMock.Invoked.Dispense(AnyParameters()).Exactly(2);
31+
sut.VerifyMock.Invoked.Dispense(Match.AnyParameters()).Exactly(2);
3232
```
3333

3434
### Argument Matchers
3535

3636
You can use argument matchers from the `Match` class to verify calls with flexible conditions:
3737

38-
- `Match.Any<T>()`: matches any value of type `T`
39-
- `Match.Null<T>()`: matches `null`
40-
- `Match.With<T>(predicate)`: matches values satisfying a predicate
41-
- `Match.With(value)`: matches a specific value
42-
- `Match.Out<T>()`: matches any out parameter of type `T`
43-
- `Match.Ref<T>()`: matches any ref parameter of type `T`
38+
- `It.IsAny<T>()`: matches any value of type `T`
39+
- `It.IsNull<T>()`: matches `null`
40+
- `It.Is<T>(predicate)`: matches values satisfying a predicate
41+
- `It.Is(value)`: matches a specific value
42+
- `It.IsOut<T>()`: matches any out parameter of type `T`
43+
- `It.IsRef<T>()`: matches any ref parameter of type `T`
4444

4545
**Example:**
4646

4747
```csharp
48-
sut.VerifyMock.Invoked.Dispense(With<string>(t => t.StartsWith("D")), Any<int>()).Once();
49-
sut.VerifyMock.Invoked.Dispense(With("Milk"), Any<int>()).AtLeastOnce();
48+
sut.VerifyMock.Invoked.Dispense(It.Is<string>(t => t.StartsWith("D")), It.IsAny<int>()).Once();
49+
sut.VerifyMock.Invoked.Dispense(It.Is("Milk"), It.IsAny<int>()).AtLeastOnce();
5050
```
5151

5252
## Properties
@@ -58,7 +58,7 @@ You can verify access to property getter and setter:
5858
sut.VerifyMock.Got.TotalDispensed().AtLeastOnce();
5959

6060
// Verify that the property 'TotalDispensed' was set to 42 exactly once
61-
sut.VerifyMock.Set.TotalDispensed(With(42)).Once();
61+
sut.VerifyMock.Set.TotalDispensed(It.Is(42)).Once();
6262
```
6363

6464
**Note:**
@@ -70,10 +70,10 @@ You can verify access to indexer getter and setter:
7070

7171
```csharp
7272
// Verify that the indexer was read with key "Dark" exactly once
73-
sut.VerifyMock.GotIndexer(With("Dark")).Once();
73+
sut.VerifyMock.GotIndexer(It.Is("Dark")).Once();
7474

7575
// Verify that the indexer was set with key "Milk" to value 7 at least once
76-
sut.VerifyMock.SetIndexer(With("Milk"), 7).AtLeastOnce();
76+
sut.VerifyMock.SetIndexer(It.Is("Milk"), 7).AtLeastOnce();
7777
```
7878

7979
**Note:**
@@ -96,17 +96,17 @@ sut.VerifyMock.UnsubscribedFrom.ChocolateDispensed().Once();
9696
Use `Then` to verify that calls occurred in a specific order:
9797

9898
```csharp
99-
sut.VerifyMock.Invoked.Dispense(With("Dark"), With(2)).Then(
100-
m => m.Invoked.Dispense(With("Dark"), With(3))
99+
sut.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(2)).Then(
100+
m => m.Invoked.Dispense(It.Is("Dark"), It.Is(3))
101101
);
102102
```
103103

104104
You can chain multiple calls for strict order verification:
105105

106106
```csharp
107-
sut.VerifyMock.Invoked.Dispense(With("Dark"), With(1)).Then(
108-
m => m.Invoked.Dispense(With("Milk"), With(2)),
109-
m => m.Invoked.Dispense(With("White"), With(3)));
107+
sut.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(1)).Then(
108+
m => m.Invoked.Dispense(It.Is("Milk"), It.Is(2)),
109+
m => m.Invoked.Dispense(It.Is("White"), It.Is(3)));
110110
```
111111

112112
If the order is incorrect or a call is missing, a `MockVerificationException` will be thrown with a descriptive message.

Docs/pages/05-comparison.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public interface IChocolateDispenser
2323

2424
```csharp
2525
IChocolateDispenser sut = Mock.Create<IChocolateDispenser>();
26-
sut.SetupMock.Method.Dispense(With("Dark"), With(2)).Returns(true);
27-
sut.SetupMock.Indexer(With("Dark")).Returns(10);
26+
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.Is(2)).Returns(true);
27+
sut.SetupMock.Indexer(It.Is("Dark")).Returns(10);
2828
```
2929

3030
**Moq**
@@ -82,16 +82,16 @@ substitute.ChocolateDispensed += Raise.Event<ChocolateDispensedDelegate>("Dark",
8282
```csharp
8383
int available = fake["Dark"];
8484
bool success = fake.Dispense("Dark", 2);
85-
fake.ChocolateDispensed += Raise.FreeForm.With("Dark", 2);
85+
fake.ChocolateDispensed += Raise.FreeForm.It.Is("Dark", 2);
8686
```
8787

8888
## Verification
8989

9090
**Mockolate**
9191

9292
```csharp
93-
sut.VerifyMock.Invoked.Dispense(With("Dark"), With(2)).Once();
94-
sut.VerifyMock.GotIndexer(With("Dark")).Once();
93+
sut.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(2)).Once();
94+
sut.VerifyMock.GotIndexer(It.Is("Dark")).Once();
9595
```
9696

9797
**Moq**

0 commit comments

Comments
 (0)