New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Multi dimension arrays assertions #2596
Comments
You can already use |
But what kind of assertion would you suggest? |
for asserting the maybe something like this var array = new string[2,3] { { "a", "b", "c" }, { "d", "e", "f" } };
array.Should().BeEmpty();
array.Should().NotBeEmpty();
array.Should().HaveElementAt(0, 0, "a");
array.Should().HaveElementAt(1, 2, "f");
array.Should().HaveTotalLength(6);
array.Should().HaveDimensionLength(0, 2);
array.Should().HaveDimensionLength(1, 3); |
I like that. What do you think @jnyrup ? |
I'm wondering if for each different array dimension there should be a dedicated assertions class, this will allow to have strongly-typed assertions and not just use the this would mean that instead of something like this: public static class Extensions
{
public static TwoDimensionalArrayAssertions<T> Should<T>(this T[,] actual) => new(actual);
public static ThreeDimensionalArrayAssertions<T> Should<T>(this T[,,] actual) => new(actual);
public static FourDimensionalArrayAssertions<T> Should<T>(this T[,,,] actual) => new(actual);
}
public class TwoDimensionalArrayAssertions<T>
{
public TwoDimensionalArrayAssertions(T[,] array) { }
}
public class ThreeDimensionalArrayAssertions<T>
{
public ThreeDimensionalArrayAssertions(T[,,] array) { }
}
public class FourDimensionalArrayAssertions<T>
{
public FourDimensionalArrayAssertions(T[,,,] array) { }
} |
That's possible, but this is the first time in 13 years that somebody suggests this feature, so your original proposal should be good enough. |
I think that a generic assertion extension will not work here public static ArrayAssertions Should(this Array array) => new ArrayAssertions(array); I think that the assertion extensions need to be for each dimension separately, otherwise when using a single-dimension array the extension method used will be the new one instead of the so we would need something like this - public static class Ex
{
public static MultiDimensionArrayAssertions Should<T>(this T[,] array) => new MultiDimensionArrayAssertions(array);
public static MultiDimensionArrayAssertions Should<T>(this T[,,] array) => new MultiDimensionArrayAssertions(array);
public static MultiDimensionArrayAssertions Should<T>(this T[,,,] array) => new MultiDimensionArrayAssertions(array);
// more dimensions ...
}
public class MultiDimensionArrayAssertions // what should this extend from?
{
public MultiDimensionArrayAssertions(Array array) { }
} |
Yeah, you're right. You can define an extension method on |
I agree about not going down the Since Since we haven't had requests for multi-dimensional arrays, I don't think we should duplicate all functionality from I suggest starting out soft with these:
|
That settles it then |
Background and motivation
Allowing to fluently assert on multi-dimension arrays
background - fluentassertions/fluentassertions.analyzers#309
Are you willing help with a pull-request?
Yes
The text was updated successfully, but these errors were encountered: