Skip to content

Commit f4c6da1

Browse files
committed
fix: add IsDefault check into MustNotContain for ImmutableArray<T>
Signed-off-by: Kenny Pflug <[email protected]>
1 parent ef2b78e commit f4c6da1

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,51 @@ public static void ImmutableArrayCallerArgumentExpression()
140140
act.Should().Throw<ExistingItemException>()
141141
.WithParameterName(nameof(array));
142142
}
143+
144+
[Fact]
145+
public static void ImmutableArrayDefaultInstanceDoesNotContainItem()
146+
{
147+
var defaultArray = default(ImmutableArray<int>);
148+
149+
// Default instance should not throw for any item since it cannot contain anything
150+
var result = defaultArray.MustNotContain(42);
151+
152+
result.IsDefault.Should().BeTrue();
153+
}
154+
155+
[Fact]
156+
public static void ImmutableArrayDefaultInstanceCustomException()
157+
{
158+
var defaultArray = default(ImmutableArray<string>);
159+
160+
// Default instance should not throw even with custom exception factory
161+
var result = defaultArray.MustNotContain(
162+
"test",
163+
(_, _) => new InvalidOperationException("Should not be called")
164+
);
165+
166+
result.IsDefault.Should().BeTrue();
167+
}
168+
169+
[Fact]
170+
public static void ImmutableArrayDefaultInstanceCustomMessage()
171+
{
172+
var defaultArray = default(ImmutableArray<object>);
173+
174+
// Default instance should not throw even with custom message
175+
var result = defaultArray.MustNotContain(new object(), message: "Custom message");
176+
177+
result.IsDefault.Should().BeTrue();
178+
}
179+
180+
[Fact]
181+
public static void ImmutableArrayDefaultInstanceCallerArgumentExpression()
182+
{
183+
var defaultArray = default(ImmutableArray<char>);
184+
185+
// Default instance should not throw, so no exception to check parameter name
186+
var result = defaultArray.MustNotContain('x');
187+
188+
result.IsDefault.Should().BeTrue();
189+
}
143190
}

Code/Light.GuardClauses/Check.MustNotContain.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ public static string MustNotContain(
207207
/// <param name="parameterName">The name of the parameter (optional).</param>
208208
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
209209
/// <exception cref="ExistingItemException">Thrown when <paramref name="parameter" /> contains <paramref name="item" />.</exception>
210+
/// <remarks>
211+
/// The default instance of <see cref="ImmutableArray{T}" /> cannot contain any items, so this method will not throw for default instances.
212+
/// </remarks>
210213
[MethodImpl(MethodImplOptions.AggressiveInlining)]
211214
public static ImmutableArray<T> MustNotContain<T>(
212215
this ImmutableArray<T> parameter,
@@ -215,7 +218,7 @@ public static ImmutableArray<T> MustNotContain<T>(
215218
string? message = null
216219
)
217220
{
218-
if (parameter.Contains(item))
221+
if (!parameter.IsDefault && parameter.Contains(item))
219222
{
220223
Throw.ExistingItem(parameter, item, parameterName, message);
221224
}
@@ -230,6 +233,9 @@ public static ImmutableArray<T> MustNotContain<T>(
230233
/// <param name="item">The item that must not be part of the <see cref="ImmutableArray{T}" />.</param>
231234
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" /> and <paramref name="item" /> are passed to this delegate.</param>
232235
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> contains <paramref name="item" />.</exception>
236+
/// <remarks>
237+
/// The default instance of <see cref="ImmutableArray{T}" /> cannot contain any items, so this method will not throw for default instances.
238+
/// </remarks>
233239
[MethodImpl(MethodImplOptions.AggressiveInlining)]
234240
[ContractAnnotation("exceptionFactory:null => halt")]
235241
public static ImmutableArray<T> MustNotContain<T>(
@@ -238,7 +244,7 @@ public static ImmutableArray<T> MustNotContain<T>(
238244
Func<ImmutableArray<T>, T, Exception> exceptionFactory
239245
)
240246
{
241-
if (parameter.Contains(item))
247+
if (!parameter.IsDefault && parameter.Contains(item))
242248
{
243249
Throw.CustomException(exceptionFactory, parameter, item);
244250
}

0 commit comments

Comments
 (0)