Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using FluentAssertions;
using Light.GuardClauses.Exceptions;
Expand Down Expand Up @@ -69,4 +70,72 @@ public static void CallerArgumentExpression()
act.Should().Throw<MissingItemException>()
.WithParameterName(nameof(array));
}

[Theory]
[InlineData(new[] { 1, 2, 3 }, 5)]
[InlineData(new[] { -5491, 6199 }, 42)]
public static void ImmutableArrayItemNotPartOf(int[] source, int item)
{
var immutableArray = source.ToImmutableArray();
Action act = () => immutableArray.MustContain(item, nameof(immutableArray));

var assertion = act.Should().Throw<MissingItemException>().Which;
assertion.Message.Should().Contain($"{nameof(immutableArray)} must contain {item}, but it actually does not.");
}

[Theory]
[InlineData(new[] { "Foo", "Bar" }, "Foo")]
[InlineData(new[] { "Foo", "Bar", "Foo" }, "Foo")]
[InlineData(new[] { "Qux" }, "Qux")]
[InlineData(new[] { "Qux", null }, null)]
public static void ImmutableArrayItemPartOf(string[] source, string item)
{
var immutableArray = source.ToImmutableArray();
immutableArray.MustContain(item).Should().Equal(immutableArray);
}

[Fact]
public static void ImmutableArrayEmptyDoesNotContainItem()
{
var immutableArray = ImmutableArray<string>.Empty;
Action act = () => immutableArray.MustContain("Foo");

act.Should().Throw<MissingItemException>();
}

[Theory]
[InlineData(new[] { 42L, 100L }, 1337L)]
public static void ImmutableArrayCustomException(long[] source, long item)
{
var immutableArray = source.ToImmutableArray();
Test.CustomException(
immutableArray,
item,
(array, i, exceptionFactory) => array.MustContain(i, exceptionFactory)
);
}

[Fact]
public static void ImmutableArrayCustomExceptionNotThrown()
{
var immutableArray = ImmutableArray.Create(1, 2, 3);
immutableArray.MustContain(2, (_, _) => new Exception()).Should().Equal(immutableArray);
}

[Fact]
public static void ImmutableArrayCustomMessage() =>
Test.CustomMessage<MissingItemException>(
message => ImmutableArray<string>.Empty.MustContain("Foo", message: message)
);

[Fact]
public static void ImmutableArrayCallerArgumentExpression()
{
var immutableArray = ImmutableArray.Create("Foo", "Bar");

var act = () => immutableArray.MustContain("Baz");

act.Should().Throw<MissingItemException>()
.WithParameterName(nameof(immutableArray));
}
}
47 changes: 47 additions & 0 deletions Code/Light.GuardClauses/Check.MustContain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
Expand Down Expand Up @@ -197,4 +198,50 @@ public static string MustContain(

return parameter;
}

/// <summary>
/// Ensures that the immutable array contains the specified item, or otherwise throws a <see cref="MissingItemException" />.
/// </summary>
/// <param name="parameter">The immutable array to be checked.</param>
/// <param name="item">The item that must be part of the immutable array.</param>
/// <param name="parameterName">The name of the parameter (optional).</param>
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
/// <exception cref="MissingItemException">Thrown when <paramref name="parameter" /> does not contain <paramref name="item" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ImmutableArray<T> MustContain<T>(
this ImmutableArray<T> parameter,
T item,
[CallerArgumentExpression("parameter")] string? parameterName = null,
string? message = null
)
{
if (!parameter.Contains(item))
{
Throw.MissingItem(parameter, item, parameterName, message);
}

return parameter;
}

/// <summary>
/// Ensures that the immutable array contains the specified item, or otherwise throws your custom exception.
/// </summary>
/// <param name="parameter">The immutable array to be checked.</param>
/// <param name="item">The item that must be part of the immutable array.</param>
/// <param name="exceptionFactory">The delegate that creates your custom exception. <paramref name="parameter" /> and <paramref name="item" /> are passed to this delegate.</param>
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> does not contain <paramref name="item" />.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ImmutableArray<T> MustContain<T>(
this ImmutableArray<T> parameter,
T item,
Func<ImmutableArray<T>, T, Exception> exceptionFactory
)
{
if (!parameter.Contains(item))
{
Throw.CustomException(exceptionFactory, parameter, item);
}

return parameter;
}
}
20 changes: 20 additions & 0 deletions Code/Plans/issue-119-must-contain-for-immutable-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Issue 119 - MustContain for ImmutableArray

## Context

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 `MustContain` assertion for `ImmutableArray<T>`.

## Tasks for this issue

- [ ] The production code should be placed in the Light.GuardClauses project. Extend the existing `Check.MustContain.cs` in the root folder of the project.
- [ ] In this file, add several extension method overloads called `MustContain` for `ImmutableArray<T>`. They should be placed in the existing class `Check` which is marked as `partial`.
- [ ] Each assertion in Light.GuardClauses has two overloads - the first one takes the optional `parameterName` and `message` arguments and throws the default exception (in this case the existing `MissingItemException`). The actual exception is thrown in the `Throw` class, you need to call the existing method for it in this class.
- [ ] 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 missing item.
- [ ] Create unit tests for both overloads. The corresponding tests should be placed in Light.GuardClauses.Tests project, in the existing file 'CollectionAssertions/MustContainTests.cs'. Please follow conventions of the existing tests (e.g. use FluentAssertions' `Should()` for assertions).

## Notes

- 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.
- This assertion specifically targets `ImmutableArray<T>` to avoid boxing that would occur with generic `IEnumerable<T>` extensions.
- The assertion should verify that the `ImmutableArray<T>` contains the specified item.
- If you have any questions or suggestions, please ask me about them.