You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Code/Light.GuardClauses/Check.MustNotContain.cs
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
usingSystem;
2
2
usingSystem.Collections.Generic;
3
+
usingSystem.Collections.Immutable;
3
4
usingSystem.Linq;
4
5
usingSystem.Runtime.CompilerServices;
5
6
usingJetBrains.Annotations;
@@ -197,4 +198,51 @@ public static string MustNotContain(
197
198
198
199
returnparameter;
199
200
}
201
+
202
+
/// <summary>
203
+
/// Ensures that the <see cref="ImmutableArray{T}" /> does not contain the specified item, or otherwise throws an <see cref="ExistingItemException" />.
204
+
/// </summary>
205
+
/// <param name="parameter">The <see cref="ImmutableArray{T}" /> to be checked.</param>
206
+
/// <param name="item">The item that must not be part of the <see cref="ImmutableArray{T}" />.</param>
207
+
/// <param name="parameterName">The name of the parameter (optional).</param>
208
+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
/// Ensures that the <see cref="ImmutableArray{T}" /> does not contain the specified item, or otherwise throws your custom exception.
228
+
/// </summary>
229
+
/// <param name="parameter">The <see cref="ImmutableArray{T}" /> to be checked.</param>
230
+
/// <param name="item">The item that must not be part of the <see cref="ImmutableArray{T}" />.</param>
231
+
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" /> and <paramref name="item" /> are passed to this delegate.</param>
The .NET library Light.GuardClauses already has several assertions for collections. They often rely on `IEnumerable<T>` or `IEnumerable`. However, these assertions would result in `ImmutableArray<T>` being boxed - we want to avoid that by providing dedicated assertions for this type which avoids boxing. For this issue, we implement the `MustNotContain` assertion for `ImmutableArray<T>`.
6
+
7
+
## Tasks for this issue
8
+
9
+
-[ ] The production code should be placed in the Light.GuardClauses project. The file `Check.MustNotContain.cs` already exists in the root folder of the project - extend this existing file.
10
+
-[ ] In this file, create two extension method overloads called `MustNotContain` for `ImmutableArray<T>`. It should be placed in the class `Check` which is marked as `partial`.
11
+
-[ ] Each assertion in Light.GuardClauses has two overloads - the first one takes the optional `parameterName` and `message` arguments and throw the default exception. The actual exception is thrown in the `Throw` class - use the existing `Throw.ExistingItem` method which is located in `ExceptionFactory/Throw.ExistingItem.cs`.
12
+
-[ ] The other overload takes a delegate which allows the caller to provide their own custom exceptions. Use the existing `Throw.CustomException` method and pass the delegate, the erroneous `ImmutableArray<T>` instance and the unwanted item.
13
+
-[ ] Create unit tests for both overloads. The corresponding tests should be placed in Light.GuardClauses.Tests project, in the existing file 'CollectionAssertions/MustNotContainTests.cs'. Please follow conventions of the existing tests (e.g. use FluentAssertions' `Should()` for assertions).
14
+
15
+
## Notes
16
+
17
+
- There are already plenty of other assertions and tests in this library. All overloads are placed in the same file in the production code project. The test projects has top-level folders for different groups of assertions, like `CollectionAssertions`, `StringAssertions`, `DateTimeAssertions` and so on. Please take a look at them to follow a similar structure and code style.
18
+
- This assertion specifically targets `ImmutableArray<T>` to avoid boxing that would occur with generic `IEnumerable<T>` extensions.
19
+
- The assertion should verify that the `ImmutableArray<T>` does not contain the specified item.
20
+
- If you have any questions or suggestions, please ask me about them.
0 commit comments