Skip to content

Commit ac60b22

Browse files
committed
feat: add MustNotContain for ImmutableArray<T>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
1 parent 9c578e8 commit ac60b22

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

Code/Light.GuardClauses.Tests/CollectionAssertions/MustNotContainTests.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Collections.ObjectModel;
45
using FluentAssertions;
56
using Light.GuardClauses.Exceptions;
@@ -75,4 +76,68 @@ public static void CallerArgumentExpression()
7576
act.Should().Throw<ExistingItemException>()
7677
.WithParameterName(nameof(array));
7778
}
79+
80+
[Theory]
81+
[InlineData(new[] { "Foo", "Bar" }, "Foo")]
82+
[InlineData(new[] { "Baz", "Qux", "Quux" }, "Qux")]
83+
[InlineData(new[] { "Corge", "Grault", null }, null)]
84+
public static void ImmutableArrayItemExists(string[] items, string item)
85+
{
86+
var array = ImmutableArray.Create(items);
87+
88+
Action act = () => array.MustNotContain(item, nameof(array));
89+
90+
var assertions = act.Should().Throw<ExistingItemException>().Which;
91+
assertions.Message.Should()
92+
.Contain($"{nameof(array)} must not contain {item.ToStringOrNull()}, but it actually does.");
93+
assertions.ParamName.Should().BeSameAs(nameof(array));
94+
}
95+
96+
[Theory]
97+
[InlineData(new[] { 100, 101, 102 }, 42)]
98+
[InlineData(new[] { 11 }, -5000)]
99+
public static void ImmutableArrayItemExistsNot(int[] items, int item)
100+
{
101+
var array = ImmutableArray.Create(items);
102+
array.MustNotContain(item).Should().Equal(array);
103+
}
104+
105+
[Fact]
106+
public static void ImmutableArrayEmptyDoesNotContainItem()
107+
{
108+
var emptyArray = ImmutableArray<int>.Empty;
109+
emptyArray.MustNotContain(42).Should().Equal(emptyArray);
110+
}
111+
112+
[Fact]
113+
public static void ImmutableArrayCustomException() =>
114+
Test.CustomException(
115+
ImmutableArray.Create("Foo"),
116+
"Foo",
117+
(array, value, exceptionFactory) => array.MustNotContain(value, exceptionFactory)
118+
);
119+
120+
[Fact]
121+
public static void ImmutableArrayNoCustomExceptionThrown()
122+
{
123+
var array = ImmutableArray.Create(1, 2);
124+
array.MustNotContain(3, (_, _) => new ()).Should().Equal(array);
125+
}
126+
127+
[Fact]
128+
public static void ImmutableArrayCustomMessage() =>
129+
Test.CustomMessage<ExistingItemException>(
130+
message => ImmutableArray.Create(42).MustNotContain(42, message: message)
131+
);
132+
133+
[Fact]
134+
public static void ImmutableArrayCallerArgumentExpression()
135+
{
136+
var array = ImmutableArray.Create(1, 2, 3);
137+
138+
Action act = () => array.MustNotContain(3);
139+
140+
act.Should().Throw<ExistingItemException>()
141+
.WithParameterName(nameof(array));
142+
}
78143
}

Code/Light.GuardClauses/Check.MustNotContain.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Linq;
45
using System.Runtime.CompilerServices;
56
using JetBrains.Annotations;
@@ -197,4 +198,51 @@ public static string MustNotContain(
197198

198199
return parameter;
199200
}
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>
209+
/// <exception cref="ExistingItemException">Thrown when <paramref name="parameter" /> contains <paramref name="item" />.</exception>
210+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
211+
public static ImmutableArray<T> MustNotContain<T>(
212+
this ImmutableArray<T> parameter,
213+
T item,
214+
[CallerArgumentExpression("parameter")] string? parameterName = null,
215+
string? message = null
216+
)
217+
{
218+
if (parameter.Contains(item))
219+
{
220+
Throw.ExistingItem(parameter, item, parameterName, message);
221+
}
222+
223+
return parameter;
224+
}
225+
226+
/// <summary>
227+
/// 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>
232+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> contains <paramref name="item" />.</exception>
233+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
234+
[ContractAnnotation("exceptionFactory:null => halt")]
235+
public static ImmutableArray<T> MustNotContain<T>(
236+
this ImmutableArray<T> parameter,
237+
T item,
238+
Func<ImmutableArray<T>, T, Exception> exceptionFactory
239+
)
240+
{
241+
if (parameter.Contains(item))
242+
{
243+
Throw.CustomException(exceptionFactory, parameter, item);
244+
}
245+
246+
return parameter;
247+
}
200248
}

0 commit comments

Comments
 (0)